Skip to content

Instantly share code, notes, and snippets.

@studentIvan
Created May 25, 2018 13:05
Show Gist options
  • Save studentIvan/e54fde89335fe0432d9665e04daa5b2b to your computer and use it in GitHub Desktop.
Save studentIvan/e54fde89335fe0432d9665e04daa5b2b to your computer and use it in GitHub Desktop.
/**
* 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