Created
May 25, 2018 13:05
-
-
Save studentIvan/e54fde89335fe0432d9665e04daa5b2b to your computer and use it in GitHub Desktop.
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
/** | |
* simple memoize registry | |
*/ | |
const ApplicationRegistry = { | |
instance: Object.create(null), | |
getInstance() { | |
return this.instance; | |
}, | |
}; | |
ApplicationRegistry.instance.dataSet = function dataSet(group, key, value) { | |
if (!this[group]) { | |
this[group] = {}; | |
} | |
this[group][key] = value; | |
}; | |
ApplicationRegistry.instance.dataGet = function dataGet(group, key) { | |
if (!this[group]) { | |
this[group] = {}; | |
return null; | |
} | |
return key ? this[group][key] : this[group]; | |
}; | |
ApplicationRegistry.instance.dataHas = function dataHas(group, key) { | |
if (!this[group]) { | |
this[group] = {}; | |
return false; | |
} | |
return Boolean(this[group][key]); | |
}; | |
/** | |
* @type {{ | |
dataSet: (group: string, key: string, value: string|number) => void, | |
dataGet: (group: string, key:? string) => string|number|Object<string, string|number>, | |
dataHas: (group: string, key: string) => boolean | |
}} | |
*/ | |
const appRegistry = (typeof window !== 'undefined') | |
? ((() => { | |
if (!window['___application-registry___']) { | |
window['___application-registry___'] = ApplicationRegistry.getInstance(); | |
} | |
return window['___application-registry___']; | |
})()) | |
: ApplicationRegistry.getInstance(); | |
module.exports = appRegistry; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment