View polling-function.jsx
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
/** | |
* 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. | |
*/ |
View helpers.tsx
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
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 => ( |
View es6-obj-class-conversion-final-result.js
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
// 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 ) { |
View es6-obj-class-conversion-change-static-method-call.js
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
// Old | |
if ( ! Utils.configSettingExists( config, 'foo' ) ) { | |
Utils.printConfigFormatInstructions(); | |
} | |
// New | |
if ( ! Config.configSettingExists( 'foo' ) ) { | |
Config.printConfigFormatInstructions(); |
View es6-obj-class-conversion-add-instantiation.js
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
// Old | |
import Utils from 'utils.js'; | |
// Load the config. | |
let config = Utils.initConfig(); | |
// New | |
import Config from 'config.js'; | |
Config.initConfig(); |
View es6-obj-class-conversion-make-methods-static.js
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
export default class { | |
// ... | |
static printConfigFormatInstructions() { | |
console.log('Your config file must be in json format, and contain the following keys: foo, bar, baz.'); | |
} | |
} |
View es6-obj-class-conversion-save-data-to-instance.js
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
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 |
View es6-obj-class-conversion-initial-references.js
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
import Utils from 'utils.js'; | |
// Load the config. | |
let config = Utils.initConfig(); | |
// Later, use it | |
if ( ! Utils.configSettingExists( config, 'foo' ) ) { | |
Utils.printConfigFormatInstructions(); | |
} |
View es6-obj-class-conversion-change-obj-def.js
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
// Old | |
export default { | |
// ... | |
} | |
// New | |
export default class { | |
// ... | |
} |
View es6-obj-class-conversion-initial-obj.js
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
export default { | |
configSettingExists( config, key ) { | |
// Search the config object for the key value | |
let found = Object.keys( config ).indexOf( key ) | |
return found; | |
}, | |
indexArrayBy(arr, attrName, group=false) { | |
// ... | |
}, |
NewerOlder