-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
159 additions
and
61 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const cache = new Map(); | ||
|
||
export const cachedFetch = async ( url ) => { | ||
if ( cache.has( url ) ) { | ||
return cache.get( url ); | ||
} | ||
|
||
const response = await fetch( url ); | ||
const data = await response.text(); | ||
|
||
cache.set( url, data ); | ||
|
||
return data; | ||
}; | ||
|
||
export const clearFetchCache = ( url ) => { | ||
if ( url ) { | ||
cache.delete( url ); | ||
} else { | ||
cache.clear(); | ||
} | ||
}; | ||
|
||
export const setFetchCache = ( url, data ) => { | ||
cache.set( url, data ); | ||
}; |
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,24 @@ | ||
export function requestIdleCallback( callback, options = {} ) { | ||
if ( typeof window.requestIdleCallback === 'function' ) { | ||
return window.requestIdleCallback( callback, options ); | ||
} | ||
|
||
const timeout = options.timeout || 50; | ||
const start = Date.now(); | ||
|
||
return setTimeout( () => { | ||
callback( { | ||
didTimeout: false, | ||
timeRemaining: () => | ||
Math.max( 0, timeout - ( Date.now() - start ) ), | ||
} ); | ||
}, 1 ); | ||
} | ||
|
||
export function cancelIdleCallback( id ) { | ||
if ( typeof window.cancelIdleCallback === 'function' ) { | ||
return window.cancelIdleCallback( id ); | ||
} | ||
|
||
clearTimeout( id ); | ||
} |
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
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
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