UNPKG

refun

Version:

A collection of React Hook-enabled functions that compose harmoniously with each other. Similar to `recompose`, but:

96 lines (74 loc) 1.61 kB
Object.defineProperty(exports, "__esModule", { value: true }); exports.unwindGenerator = exports.generatorIdFactory = exports.shallowEqualByKeys = exports.shallowEquals = void 0; const shallowEquals = (a, b) => { if (a === b) { return true; } for (const key in b) { if (a[key] !== b[key]) { return false; } } for (const key in a) { if (a[key] !== b[key]) { return false; } } return true; }; exports.shallowEquals = shallowEquals; const shallowEqualByKeys = (a, b, keys) => { if (a === b) { return true; } for (const key of keys) { if (a[key] !== b[key]) { return false; } } return true; }; exports.shallowEqualByKeys = shallowEqualByKeys; const generatorIdFactory = () => { let latestId = 0; const ids = new Set(); const switchToGenerator = id => () => { ids.clear(); ids.add(id); }; const newId = () => { const id = latestId++; ids.add(id); return id; }; const isGeneratorRunning = id => ids.has(id); return { switchToGenerator, newId, isGeneratorRunning }; }; exports.generatorIdFactory = generatorIdFactory; const unwindGenerator = (gen, shouldContinue) => { const handle = async ir => { if (ir.done) { return; } try { const value = await ir.value; if (shouldContinue()) { return handle(gen.next(value)); } gen.return(); } catch (e) { if (shouldContinue()) { return handle(gen.throw(e)); } gen.return(); } }; handle(gen.next()); }; exports.unwindGenerator = unwindGenerator;