UNPKG

effect

Version:

The missing standard library for TypeScript, for writing production-grade software.

105 lines (104 loc) 3.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.prepareResetJournal = exports.isValid = exports.isInvalid = exports.execTodos = exports.commitJournal = exports.collectTodos = exports.analyzeJournal = exports.addTodo = exports.JournalAnalysisReadWrite = exports.JournalAnalysisReadOnly = exports.JournalAnalysisInvalid = void 0; var Entry = _interopRequireWildcard(require("./entry.js")); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } /** @internal */ const JournalAnalysisInvalid = exports.JournalAnalysisInvalid = "Invalid"; /** @internal */ const JournalAnalysisReadWrite = exports.JournalAnalysisReadWrite = "ReadWrite"; /** @internal */ const JournalAnalysisReadOnly = exports.JournalAnalysisReadOnly = "ReadOnly"; /** @internal */ const commitJournal = journal => { for (const entry of journal) { Entry.commit(entry[1]); } }; /** * Analyzes the journal, determining whether it is valid and whether it is * read only in a single pass. Note that information on whether the * journal is read only will only be accurate if the journal is valid, due * to short-circuiting that occurs on an invalid journal. * * @internal */ exports.commitJournal = commitJournal; const analyzeJournal = journal => { let val = JournalAnalysisReadOnly; for (const [, entry] of journal) { val = Entry.isInvalid(entry) ? JournalAnalysisInvalid : Entry.isChanged(entry) ? JournalAnalysisReadWrite : val; if (val === JournalAnalysisInvalid) { return val; } } return val; }; /** @internal */ exports.analyzeJournal = analyzeJournal; const prepareResetJournal = journal => { const saved = new Map(); for (const entry of journal) { saved.set(entry[0], Entry.copy(entry[1])); } return () => { journal.clear(); for (const entry of saved) { journal.set(entry[0], entry[1]); } }; }; /** @internal */ exports.prepareResetJournal = prepareResetJournal; const collectTodos = journal => { const allTodos = new Map(); for (const [, entry] of journal) { for (const todo of entry.ref.todos) { allTodos.set(todo[0], todo[1]); } entry.ref.todos = new Map(); } return allTodos; }; /** @internal */ exports.collectTodos = collectTodos; const execTodos = todos => { const todosSorted = Array.from(todos.entries()).sort((x, y) => x[0] - y[0]); for (const [_, todo] of todosSorted) { todo(); } }; /** @internal */ exports.execTodos = execTodos; const addTodo = (txnId, journal, todoEffect) => { let added = false; for (const [, entry] of journal) { if (!entry.ref.todos.has(txnId)) { entry.ref.todos.set(txnId, todoEffect); added = true; } } return added; }; /** @internal */ exports.addTodo = addTodo; const isValid = journal => { let valid = true; for (const [, entry] of journal) { valid = Entry.isValid(entry); if (!valid) { return valid; } } return valid; }; /** @internal */ exports.isValid = isValid; const isInvalid = journal => { return !isValid(journal); }; exports.isInvalid = isInvalid; //# sourceMappingURL=journal.js.map