recoil-toolkit
Version:
40 lines • 1.3 kB
JavaScript
export const recoilStores = {};
export const DEFAULT_STORE = '$defaultStore$';
export function set(recoilVal, valOrUpdater) {
var _a, _b;
if ((_a = recoilStores[DEFAULT_STORE]) === null || _a === void 0 ? void 0 : _a.set)
return (_b = recoilStores[DEFAULT_STORE]) === null || _b === void 0 ? void 0 : _b.set(recoilVal, valOrUpdater);
throw new Error('Recoil Toolkit Tunnel not ready');
}
const pendigGetStorePromises = [];
export const flushGetStorePending = (name) => {
pendigGetStorePromises.forEach(p => {
if (p.pending && p.name === name) {
// @ts-ignore
if (recoilStores[name]) {
// @ts-ignore
p.resolve(recoilStores[name]);
}
else
p.reject(new Error('no store found'));
p.pending = false;
}
});
};
export function getRecoilStore(name = DEFAULT_STORE) {
return new Promise((resolve, reject) => {
if (recoilStores[name]) {
// @ts-ignore
resolve(recoilStores[name]);
}
else {
pendigGetStorePromises.push({
resolve,
reject,
name,
pending: true,
});
}
});
}
//# sourceMappingURL=getStore.js.map