Skip to content

Instantly share code, notes, and snippets.

@TimMikeladze
Created June 29, 2016 07:27
Show Gist options
  • Save TimMikeladze/8e1cdde518aed8dff611c4e08cd86bef to your computer and use it in GitHub Desktop.
Save TimMikeladze/8e1cdde518aed8dff611c4e08cd86bef to your computer and use it in GitHub Desktop.
const FORM_REGISTRY_KEY = Symbol.for('yaFormRegistry');
const globalSymbols = Object.getOwnPropertySymbols(global);
const hasSymbol = globalSymbols.indexOf(FORM_REGISTRY_KEY) > -1;
if (!hasSymbol) {
global[FORM_REGISTRY_KEY] = {
handlers: {},
add(name, handler) {
this.handlers[name] = handler;
},
remove(name) {
delete this.handlers[name];
},
get(name) {
if (!this.handlers.hasOwnProperty(name)) {
throw new Error(`${name} handler does not exist in the form registry`);
}
return this.handlers[name];
},
};
}
const formRegistry = {};
Object.defineProperty(formRegistry, 'instance', {
get: () => global[FORM_REGISTRY_KEY],
});
Object.freeze(formRegistry);
export default formRegistry;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment