This file contains hidden or 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
export interface FetchWithRetriesParams<T> { | |
fn: () => Promise<T>; | |
backoffType?: 'exponential' | 'linear'; | |
maxRetries?: number; | |
retryCount?: number; | |
delayMs?: number; | |
// Allows us to define custom logic to determine if the response is successful. | |
successCondition: (response: T) => boolean; | |
// Allows us to define custom logic to determine if the response is a failure. | |
errorCondition?: (response: T) => boolean; |
This file contains hidden or 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
/** | |
* Run an async function repeatedly until a condition is met, or we hit a max # of attempts. | |
* | |
* @param {Number} maxAttempts - Max # of times to run iterate() before we quit. | |
* @param {Number} frequency - # milliseconds to wait in between attempts. | |
* @param {Function} iterate - Async. Code to run on every iteration. | |
* @param {Function} check - Accepts iterate response, return true/false if success criteria are met. | |
* @param {Function} onSuccess - Success callback. | |
* @param {Function} onMaxAttemptsExceeded - Failure callback. | |
*/ |
This file contains hidden or 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
export const LoggedInContent: FC<{}> = props => ( | |
<AuthContext.Consumer> | |
{(auth): React.ReactNode => (auth && auth.token ? props.children : null)} | |
</AuthContext.Consumer> | |
); | |
/** | |
* Helper to show content only if the user is logged out. | |
*/ | |
export const LoggedOutContent: FC<{}> = props => ( |
This file contains hidden or 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
// Final config.js | |
export default class { | |
init() { | |
this.config = // ... load the object ... | |
// For posterity, but we don't need to return it at the moment. | |
return this.config; | |
}, | |
settingExists( key ) { |
This file contains hidden or 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
// Old | |
if ( ! Utils.configSettingExists( config, 'foo' ) ) { | |
Utils.printConfigFormatInstructions(); | |
} | |
// New | |
if ( ! Config.configSettingExists( 'foo' ) ) { | |
Config.printConfigFormatInstructions(); |
This file contains hidden or 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
// Old | |
import Utils from 'utils.js'; | |
// Load the config. | |
let config = Utils.initConfig(); | |
// New | |
import Config from 'config.js'; | |
Config.initConfig(); |
This file contains hidden or 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
export default class { | |
// ... | |
static printConfigFormatInstructions() { | |
console.log('Your config file must be in json format, and contain the following keys: foo, bar, baz.'); | |
} | |
} |
This file contains hidden or 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
export default class { | |
initConfig() { | |
this.config = // ... load a json file via AJAX, filesystem, etc. ... | |
return this.config; | |
}, | |
configSettingExists( key ) { | |
// Search the config object for the key value |
This file contains hidden or 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
import Utils from 'utils.js'; | |
// Load the config. | |
let config = Utils.initConfig(); | |
// Later, use it | |
if ( ! Utils.configSettingExists( config, 'foo' ) ) { | |
Utils.printConfigFormatInstructions(); | |
} |
This file contains hidden or 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
// Old | |
export default { | |
// ... | |
} | |
// New | |
export default class { | |
// ... | |
} |
NewerOlder