any-observable
Version:
Support any Observable library and polyfill
42 lines (34 loc) • 1.34 kB
JavaScript
const REGISTRATION_KEY = Symbol('@@any-observable/REGISTRATION');
let isRegistered;
export default function registerObservable(global, loadImplementation) {
return (implementation, options = {}) => {
// Global registration unless explicitly `{global: false}`` in options (default true)
const registerGlobal = options.global !== false;
// Load any previous global registration
if (registerGlobal && !isRegistered) {
isRegistered = global[REGISTRATION_KEY];
}
if (isRegistered && implementation && isRegistered.implementation !== implementation) {
const message = `any-observable already defined as \`${isRegistered.implementation}\`. `
+ 'You can only register an implementation before the first import of `any-observable` and an implementation cannot be changed';
throw new Error(message);
}
if (!isRegistered) {
// Use provided implementation
if (implementation && options.Observable) {
isRegistered = {
Observable: options.Observable,
implementation,
};
} else {
// Require implementation if implementation is specified but not provided
isRegistered = loadImplementation(implementation);
}
if (registerGlobal) {
// Register preference globally in case multiple installations
global[REGISTRATION_KEY] = isRegistered;
}
}
return isRegistered;
};
}