UNPKG

reactronic

Version:

Reactronic - Transactional Reactive State Management

83 lines (82 loc) 3.37 kB
import { Log } from "./util/Dbg.js"; import { Kind, Isolation } from "./Options.js"; import { Meta, ObjectHandle } from "./core/Data.js"; import { Changeset } from "./core/Changeset.js"; import { Mvcc } from "./core/Mvcc.js"; import { Transaction } from "./core/Transaction.js"; import { OperationImpl } from "./core/Operation.js"; export class ReactiveSystem { static why(brief = false) { return brief ? OperationImpl.briefWhy() : OperationImpl.why(); } static getOperation(method) { return OperationImpl.getControllerOf(method); } static pullLastResult(method, args) { return ReactiveSystem.getOperation(method).pullLastResult(args); } static configureCurrentOperation(options) { return OperationImpl.configureImpl(undefined, options); } static getRevisionOf(obj) { return obj[Meta.Revision]; } static takeSnapshot(obj) { return Changeset.takeSnapshot(obj); } static dispose(obj) { Changeset.dispose(obj); } static get reactivityAutoStartDisabled() { return Mvcc.reactivityAutoStartDisabled; } static set reactivityAutoStartDisabled(value) { Mvcc.reactivityAutoStartDisabled = value; } static get isLogging() { return Log.isOn; } static get loggingOptions() { return Log.opt; } static setLoggingMode(isOn, options) { Log.setMode(isOn, options); } static setLoggingHint(obj, name) { Mvcc.setHint(obj, name); } static getLoggingHint(obj, full = false) { return ObjectHandle.getHint(obj, full); } static setProfilingMode(isOn, options) { Mvcc.setProfilingMode(isOn, options); } } export function atomicRun(p1, p2, p3) { if (p1 instanceof Function) { if (p2 !== undefined) return Transaction.run(null, p1, ...p2); else return Transaction.run(null, p1); } else { if (p3 !== undefined) return Transaction.run(p1, p2, ...p3); else return Transaction.run(p1, p2); } } export function nonReactiveRun(func, ...args) { return OperationImpl.proceedWithinGivenLaunch(undefined, func, ...args); } export function sensitiveRun(sensitivity, func, ...args) { return Mvcc.sensitive(sensitivity, func, ...args); } export function contextualRun(p) { throw new Error("not implemented yet"); } export function trigger(protoOrEnabled, prop) { if (typeof (protoOrEnabled) === "boolean") { return (proto, prop) => { return Mvcc.decorateData(protoOrEnabled, proto, prop); }; } else return Mvcc.decorateData(true, protoOrEnabled, prop); } export function atomicBlock(proto, prop, pd) { const opts = { kind: Kind.atomic, isolation: Isolation.joinToCurrentTransaction, }; return Mvcc.decorateOperation(true, atomicBlock, opts, proto, prop, pd); } export function reaction(proto, prop, pd) { const opts = { kind: Kind.reactive, isolation: Isolation.joinAsNestedTransaction, throttling: -1, }; return Mvcc.decorateOperation(true, reaction, opts, proto, prop, pd); } export function cache(proto, prop, pd) { const opts = { kind: Kind.cached, isolation: Isolation.joinToCurrentTransaction, noSideEffects: true, }; return Mvcc.decorateOperation(true, cache, opts, proto, prop, pd); } export function options(value) { return Mvcc.decorateOperationParametrized(options, value); }