Created
June 29, 2016 07:27
-
-
Save TimMikeladze/8e1cdde518aed8dff611c4e08cd86bef to your computer and use it in GitHub Desktop.
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
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