-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50130 from wildan-m/wildan/fix/47148-fix-sound-of…
…fline Resolves offline sound playback by caching assets locally
- Loading branch information
Showing
8 changed files
with
169 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import Onyx from 'react-native-onyx'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
|
||
let isMuted = false; | ||
|
||
Onyx.connect({ | ||
key: ONYXKEYS.USER, | ||
callback: (val) => (isMuted = !!val?.isMutedAllSounds), | ||
}); | ||
|
||
const SOUNDS = { | ||
DONE: 'done', | ||
SUCCESS: 'success', | ||
ATTENTION: 'attention', | ||
RECEIVE: 'receive', | ||
} as const; | ||
|
||
const getIsMuted = () => isMuted; | ||
|
||
/** | ||
* Creates a version of the given function that, when called, queues the execution and ensures that | ||
* calls are spaced out by at least the specified `minExecutionTime`, even if called more frequently. This allows | ||
* for throttling frequent calls to a function, ensuring each is executed with a minimum `minExecutionTime` between calls. | ||
* Each call returns a promise that resolves when the function call is executed, allowing for asynchronous handling. | ||
*/ | ||
function withMinimalExecutionTime<F extends (...args: Parameters<F>) => ReturnType<F>>(func: F, minExecutionTime: number) { | ||
const queue: Array<[() => ReturnType<F>, (value?: unknown) => void]> = []; | ||
let timerId: NodeJS.Timeout | null = null; | ||
|
||
function processQueue() { | ||
if (queue.length > 0) { | ||
const next = queue.shift(); | ||
|
||
if (!next) { | ||
return; | ||
} | ||
|
||
const [nextFunc, resolve] = next; | ||
nextFunc(); | ||
resolve(); | ||
timerId = setTimeout(processQueue, minExecutionTime); | ||
} else { | ||
timerId = null; | ||
} | ||
} | ||
|
||
return function (...args: Parameters<F>) { | ||
return new Promise((resolve) => { | ||
queue.push([() => func(...args), resolve]); | ||
|
||
if (!timerId) { | ||
// If the timer isn't running, start processing the queue | ||
processQueue(); | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
export {SOUNDS, withMinimalExecutionTime, getIsMuted}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import Sound from 'react-native-sound'; | ||
import type {ValueOf} from 'type-fest'; | ||
import {getIsMuted, SOUNDS, withMinimalExecutionTime} from './BaseSound'; | ||
import config from './config'; | ||
|
||
const playSound = (soundFile: ValueOf<typeof SOUNDS>) => { | ||
const sound = new Sound(`${config.prefix}${soundFile}.mp3`, Sound.MAIN_BUNDLE, (error) => { | ||
if (error || getIsMuted()) { | ||
return; | ||
} | ||
|
||
sound.play(); | ||
}); | ||
}; | ||
|
||
function clearSoundAssetsCache() {} | ||
|
||
export {SOUNDS, clearSoundAssetsCache}; | ||
export default withMinimalExecutionTime(playSound, 300); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,94 @@ | ||
import Onyx from 'react-native-onyx'; | ||
import Sound from 'react-native-sound'; | ||
import {Howl} from 'howler'; | ||
import type {ValueOf} from 'type-fest'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
import Log from '@libs/Log'; | ||
import {getIsMuted, SOUNDS, withMinimalExecutionTime} from './BaseSound'; | ||
import config from './config'; | ||
|
||
let isMuted = false; | ||
function cacheSoundAssets() { | ||
// Exit early if the Cache API is not available in the current browser. | ||
if (!('caches' in window)) { | ||
return; | ||
} | ||
|
||
Onyx.connect({ | ||
key: ONYXKEYS.USER, | ||
callback: (val) => (isMuted = !!val?.isMutedAllSounds), | ||
}); | ||
caches.open('sound-assets').then((cache) => { | ||
const soundFiles = Object.values(SOUNDS).map((sound) => `${config.prefix}${sound}.mp3`); | ||
|
||
const SOUNDS = { | ||
DONE: 'done', | ||
SUCCESS: 'success', | ||
ATTENTION: 'attention', | ||
RECEIVE: 'receive', | ||
} as const; | ||
// Cache each sound file if it's not already cached. | ||
const cachePromises = soundFiles.map((soundFile) => { | ||
return cache.match(soundFile).then((response) => { | ||
if (response) { | ||
return; | ||
} | ||
return cache.add(soundFile); | ||
}); | ||
}); | ||
|
||
/** | ||
* Creates a version of the given function that, when called, queues the execution and ensures that | ||
* calls are spaced out by at least the specified `minExecutionTime`, even if called more frequently. This allows | ||
* for throttling frequent calls to a function, ensuring each is executed with a minimum `minExecutionTime` between calls. | ||
* Each call returns a promise that resolves when the function call is executed, allowing for asynchronous handling. | ||
*/ | ||
function withMinimalExecutionTime<F extends (...args: Parameters<F>) => ReturnType<F>>(func: F, minExecutionTime: number) { | ||
const queue: Array<[() => ReturnType<F>, (value?: unknown) => void]> = []; | ||
let timerId: NodeJS.Timeout | null = null; | ||
return Promise.all(cachePromises); | ||
}); | ||
} | ||
|
||
function processQueue() { | ||
if (queue.length > 0) { | ||
const next = queue.shift(); | ||
const initializeAndPlaySound = (src: string) => { | ||
const sound = new Howl({ | ||
src: [src], | ||
format: ['mp3'], | ||
onloaderror: (_id: number, error: unknown) => { | ||
Log.alert('[sound] Load error:', {message: (error as Error).message}); | ||
}, | ||
onplayerror: (_id: number, error: unknown) => { | ||
Log.alert('[sound] Play error:', {message: (error as Error).message}); | ||
}, | ||
}); | ||
sound.play(); | ||
}; | ||
|
||
if (!next) { | ||
const playSound = (soundFile: ValueOf<typeof SOUNDS>) => { | ||
if (getIsMuted()) { | ||
return; | ||
} | ||
|
||
const soundSrc = `${config.prefix}${soundFile}.mp3`; | ||
|
||
if (!('caches' in window)) { | ||
// Fallback to fetching from network if not in cache | ||
initializeAndPlaySound(soundSrc); | ||
return; | ||
} | ||
|
||
caches.open('sound-assets').then((cache) => { | ||
cache.match(soundSrc).then((response) => { | ||
if (response) { | ||
response.blob().then((soundBlob) => { | ||
const soundUrl = URL.createObjectURL(soundBlob); | ||
initializeAndPlaySound(soundUrl); | ||
}); | ||
return; | ||
} | ||
initializeAndPlaySound(soundSrc); | ||
}); | ||
}); | ||
}; | ||
|
||
const [nextFunc, resolve] = next; | ||
nextFunc(); | ||
resolve(); | ||
timerId = setTimeout(processQueue, minExecutionTime); | ||
} else { | ||
timerId = null; | ||
} | ||
function clearSoundAssetsCache() { | ||
// Exit early if the Cache API is not available in the current browser. | ||
if (!('caches' in window)) { | ||
return; | ||
} | ||
|
||
return function (...args: Parameters<F>) { | ||
return new Promise((resolve) => { | ||
queue.push([() => func(...args), resolve]); | ||
|
||
if (!timerId) { | ||
// If the timer isn't running, start processing the queue | ||
processQueue(); | ||
caches | ||
.delete('sound-assets') | ||
.then((success) => { | ||
if (success) { | ||
return; | ||
} | ||
Log.alert('[sound] Failed to clear sound assets cache.'); | ||
}) | ||
.catch((error) => { | ||
Log.alert('[sound] Error clearing sound assets cache:', {message: (error as Error).message}); | ||
}); | ||
}; | ||
} | ||
|
||
const playSound = (soundFile: ValueOf<typeof SOUNDS>) => { | ||
const sound = new Sound(`${config.prefix}${soundFile}.mp3`, Sound.MAIN_BUNDLE, (error) => { | ||
if (error || isMuted) { | ||
return; | ||
} | ||
|
||
sound.play(); | ||
}); | ||
}; | ||
// Cache sound assets on load | ||
cacheSoundAssets(); | ||
|
||
export {SOUNDS}; | ||
export {SOUNDS, clearSoundAssetsCache}; | ||
export default withMinimalExecutionTime(playSound, 300); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters