@dhmk/atom
Advanced tools
Comparing version 2.0.1-test.9 to 2.0.1-test.10
@@ -14,4 +14,6 @@ export declare class DerivedAtom { | ||
isEffect: any; | ||
isError: any; | ||
fn: any; | ||
constructor(fn: any, isEffect?: boolean, options?: any); | ||
run(): void; | ||
actualize(): void; | ||
@@ -18,0 +20,0 @@ track(a: any): void; |
@@ -13,7 +13,9 @@ var __assign = (this && this.__assign) || function () { | ||
import { runtime } from "../runtime"; | ||
import { trackAtom, removeAtom, invalidateSubs } from "../shared"; | ||
import { trackAtom, removeAtom, invalidateSubs, thrower } from "../shared"; | ||
import { EID, defaultAtomOptions, Id, AtomState } from "../types"; | ||
var call = function (x) { return x(); }; | ||
var DerivedAtom = /** @class */ (function () { | ||
function DerivedAtom(fn, isEffect, options) { | ||
if (isEffect === void 0) { isEffect = false; } | ||
var _a; | ||
this.value = undefined; | ||
@@ -31,4 +33,14 @@ this.options = __assign(__assign({}, defaultAtomOptions), options); | ||
this.isEffect = isEffect; | ||
this.isError = false; | ||
this.fn = fn; | ||
if (this.isEffect) { | ||
var actualize_1 = this.actualize.bind(this); | ||
var scheduler_1 = (_a = options === null || options === void 0 ? void 0 : options.scheduler) !== null && _a !== void 0 ? _a : call; | ||
this.run = function () { return scheduler_1(actualize_1); }; | ||
} | ||
} | ||
DerivedAtom.prototype.run = function () { | ||
// only for effects | ||
// effects should override this | ||
}; | ||
DerivedAtom.prototype.actualize = function () { | ||
@@ -60,3 +72,3 @@ var _this = this; | ||
if (onBO) | ||
runtime.addEffect({ actualize: onBO }); | ||
runtime.addEffect(onBO); | ||
} | ||
@@ -66,2 +78,3 @@ var prev = runtime.currentAtom; | ||
var nextValue = void 0; | ||
var isError = false; | ||
try { | ||
@@ -71,5 +84,12 @@ this.state = AtomState.Computing; | ||
} | ||
finally { | ||
runtime.currentAtom = prev; | ||
catch (e) { | ||
if (this.isEffect) { | ||
runtime.addEffect(thrower(e)); | ||
} | ||
else { | ||
nextValue = e; | ||
isError = true; | ||
} | ||
} | ||
runtime.currentAtom = prev; | ||
prevDeps.forEach(function (t) { | ||
@@ -91,2 +111,3 @@ var a = t.a; | ||
this.value = nextValue; | ||
this.isError = isError; | ||
this.vid = new Id(); | ||
@@ -121,3 +142,3 @@ invalidateSubs(this, false); | ||
if (onBUO) | ||
runtime.addEffect({ actualize: onBUO }); | ||
runtime.addEffect(onBUO); | ||
this.deps.forEach(function (t) { return removeAtom(t.a, _this); }); | ||
@@ -131,2 +152,4 @@ this.deps.length = 0; | ||
trackAtom(this); | ||
if (this.isError) | ||
throw this.value; | ||
return this.value; | ||
@@ -133,0 +156,0 @@ }; |
@@ -42,3 +42,3 @@ var __assign = (this && this.__assign) || function () { | ||
if (onBO) | ||
runtime.addEffect({ actualize: onBO }); | ||
runtime.addEffect(onBO); | ||
} | ||
@@ -55,3 +55,3 @@ return this.value; | ||
if (onBUO) | ||
runtime.addEffect({ actualize: onBUO }); | ||
runtime.addEffect(onBUO); | ||
} | ||
@@ -58,0 +58,0 @@ }; |
@@ -11,2 +11,3 @@ import { ValueAtom } from "./atoms/value"; | ||
isInitial: boolean; | ||
isDisposed: boolean; | ||
invalidate(): void; | ||
@@ -13,0 +14,0 @@ }; |
@@ -5,3 +5,2 @@ import { ValueAtom } from "./atoms/value"; | ||
import { AtomState } from "./types"; | ||
import { invalidateSubs } from "./shared"; | ||
import observable from "./observable"; | ||
@@ -12,24 +11,24 @@ import observableObject, { as } from "./observable/object"; | ||
var ectrl = function () { | ||
ea.dispose(); | ||
ectrl.isDisposed = true; | ||
runtime.addEffect(ea.dispose.bind(ea)); | ||
runtime.runEffects(); | ||
}; | ||
ectrl.isInitial = true; | ||
ectrl.isDisposed = false; | ||
ectrl.invalidate = function () { | ||
if (ectrl.isDisposed) | ||
return; | ||
ea.state = AtomState.Stale; | ||
invalidateSubs(ea, false); | ||
runtime.addEffect(ea); | ||
runtime.addEffect(ea.run); | ||
runtime.runEffects(); | ||
}; | ||
var efn = function () { | ||
fn(ectrl); | ||
!ectrl.isDisposed && fn(ectrl); | ||
ectrl.isInitial = false; | ||
}; | ||
var ea = new DerivedAtom(efn, true, opts); | ||
var origac = ea.actualize.bind(ea); | ||
if (opts === null || opts === void 0 ? void 0 : opts.scheduler) | ||
ea.actualize = function () { return opts.scheduler(origac); }; | ||
var onBO = opts === null || opts === void 0 ? void 0 : opts.onBecomeObserved; | ||
if (onBO) | ||
runtime.addEffect({ actualize: onBO }); | ||
runtime.addEffect(ea); | ||
runtime.addEffect(onBO); | ||
runtime.addEffect(ea.run); | ||
runtime.runEffects(); | ||
@@ -36,0 +35,0 @@ return ectrl; |
@@ -1,4 +0,2 @@ | ||
type Effect = { | ||
actualize(): void; | ||
}; | ||
type Effect = () => void; | ||
export declare const runtime: { | ||
@@ -5,0 +3,0 @@ currentAtom: any; |
@@ -0,1 +1,2 @@ | ||
import { thrower } from "./shared"; | ||
export var runtime = { | ||
@@ -13,5 +14,10 @@ currentAtom: undefined, | ||
runtime.counter++; | ||
runtime.effects.forEach(function (x) { | ||
runtime.effects.delete(x); | ||
x.actualize(); | ||
runtime.effects.forEach(function (fn) { | ||
try { | ||
runtime.effects.delete(fn); | ||
fn(); | ||
} | ||
catch (e) { | ||
setTimeout(thrower(e)); | ||
} | ||
}); | ||
@@ -18,0 +24,0 @@ runtime.counter--; |
import { AtomState } from "./types"; | ||
export declare const removeAtom: (a: any, self: unknown) => void; | ||
export declare function trackAtom(a: any): void; | ||
export declare function reportError(e: unknown): () => never; | ||
export declare function thrower(e: unknown): () => never; | ||
export declare function invalidateSubs(atom: any, isValueAtom: any, newState?: AtomState): void; |
@@ -13,3 +13,3 @@ import { runtime } from "./runtime"; | ||
} | ||
export function reportError(e) { | ||
export function thrower(e) { | ||
return function () { | ||
@@ -24,3 +24,3 @@ throw e; | ||
if (a.isEffect) | ||
runtime.addEffect(a); | ||
runtime.addEffect(a.run); | ||
a.state = AtomState.InvalidatedAndComputing; | ||
@@ -33,5 +33,5 @@ return; | ||
if (a.isEffect) | ||
runtime.addEffect(a); | ||
runtime.addEffect(a.run); | ||
a.subs.size && invalidateSubs(a, isValueAtom, AtomState.PossiblyStale); | ||
}); | ||
} |
@@ -5,9 +5,2 @@ export declare class Id { | ||
export declare const EID: Id; | ||
export declare class Track { | ||
a?: any; | ||
m?: any; | ||
v?: any; | ||
constructor(a?: any, m?: any, v?: any); | ||
} | ||
export declare const ET: Track; | ||
export interface Dependency { | ||
@@ -14,0 +7,0 @@ actualize(): void; |
@@ -8,12 +8,2 @@ var Id = /** @class */ (function () { | ||
export var EID = new Id(); | ||
var Track = /** @class */ (function () { | ||
function Track(a, m, v) { | ||
this.a = a; | ||
this.m = m; | ||
this.v = v; | ||
} | ||
return Track; | ||
}()); | ||
export { Track }; | ||
export var ET = new Track(); | ||
export var AtomState; | ||
@@ -20,0 +10,0 @@ (function (AtomState) { |
@@ -14,4 +14,6 @@ export declare class DerivedAtom { | ||
isEffect: any; | ||
isError: any; | ||
fn: any; | ||
constructor(fn: any, isEffect?: boolean, options?: any); | ||
run(): void; | ||
actualize(): void; | ||
@@ -18,0 +20,0 @@ track(a: any): void; |
@@ -18,5 +18,7 @@ "use strict"; | ||
var types_1 = require("../types"); | ||
var call = function (x) { return x(); }; | ||
var DerivedAtom = /** @class */ (function () { | ||
function DerivedAtom(fn, isEffect, options) { | ||
if (isEffect === void 0) { isEffect = false; } | ||
var _a; | ||
this.value = undefined; | ||
@@ -34,4 +36,14 @@ this.options = __assign(__assign({}, types_1.defaultAtomOptions), options); | ||
this.isEffect = isEffect; | ||
this.isError = false; | ||
this.fn = fn; | ||
if (this.isEffect) { | ||
var actualize_1 = this.actualize.bind(this); | ||
var scheduler_1 = (_a = options === null || options === void 0 ? void 0 : options.scheduler) !== null && _a !== void 0 ? _a : call; | ||
this.run = function () { return scheduler_1(actualize_1); }; | ||
} | ||
} | ||
DerivedAtom.prototype.run = function () { | ||
// only for effects | ||
// effects should override this | ||
}; | ||
DerivedAtom.prototype.actualize = function () { | ||
@@ -63,3 +75,3 @@ var _this = this; | ||
if (onBO) | ||
runtime_1.runtime.addEffect({ actualize: onBO }); | ||
runtime_1.runtime.addEffect(onBO); | ||
} | ||
@@ -69,2 +81,3 @@ var prev = runtime_1.runtime.currentAtom; | ||
var nextValue = void 0; | ||
var isError = false; | ||
try { | ||
@@ -74,5 +87,12 @@ this.state = types_1.AtomState.Computing; | ||
} | ||
finally { | ||
runtime_1.runtime.currentAtom = prev; | ||
catch (e) { | ||
if (this.isEffect) { | ||
runtime_1.runtime.addEffect((0, shared_1.thrower)(e)); | ||
} | ||
else { | ||
nextValue = e; | ||
isError = true; | ||
} | ||
} | ||
runtime_1.runtime.currentAtom = prev; | ||
prevDeps.forEach(function (t) { | ||
@@ -94,2 +114,3 @@ var a = t.a; | ||
this.value = nextValue; | ||
this.isError = isError; | ||
this.vid = new types_1.Id(); | ||
@@ -124,3 +145,3 @@ (0, shared_1.invalidateSubs)(this, false); | ||
if (onBUO) | ||
runtime_1.runtime.addEffect({ actualize: onBUO }); | ||
runtime_1.runtime.addEffect(onBUO); | ||
this.deps.forEach(function (t) { return (0, shared_1.removeAtom)(t.a, _this); }); | ||
@@ -134,2 +155,4 @@ this.deps.length = 0; | ||
(0, shared_1.trackAtom)(this); | ||
if (this.isError) | ||
throw this.value; | ||
return this.value; | ||
@@ -136,0 +159,0 @@ }; |
@@ -45,3 +45,3 @@ "use strict"; | ||
if (onBO) | ||
runtime_1.runtime.addEffect({ actualize: onBO }); | ||
runtime_1.runtime.addEffect(onBO); | ||
} | ||
@@ -58,3 +58,3 @@ return this.value; | ||
if (onBUO) | ||
runtime_1.runtime.addEffect({ actualize: onBUO }); | ||
runtime_1.runtime.addEffect(onBUO); | ||
} | ||
@@ -61,0 +61,0 @@ }; |
@@ -11,2 +11,3 @@ import { ValueAtom } from "./atoms/value"; | ||
isInitial: boolean; | ||
isDisposed: boolean; | ||
invalidate(): void; | ||
@@ -13,0 +14,0 @@ }; |
@@ -37,3 +37,2 @@ "use strict"; | ||
var types_1 = require("./types"); | ||
var shared_1 = require("./shared"); | ||
var observable_1 = __importDefault(require("./observable")); | ||
@@ -48,24 +47,24 @@ exports.observable = observable_1.default; | ||
var ectrl = function () { | ||
ea.dispose(); | ||
ectrl.isDisposed = true; | ||
runtime_1.runtime.addEffect(ea.dispose.bind(ea)); | ||
runtime_1.runtime.runEffects(); | ||
}; | ||
ectrl.isInitial = true; | ||
ectrl.isDisposed = false; | ||
ectrl.invalidate = function () { | ||
if (ectrl.isDisposed) | ||
return; | ||
ea.state = types_1.AtomState.Stale; | ||
(0, shared_1.invalidateSubs)(ea, false); | ||
runtime_1.runtime.addEffect(ea); | ||
runtime_1.runtime.addEffect(ea.run); | ||
runtime_1.runtime.runEffects(); | ||
}; | ||
var efn = function () { | ||
fn(ectrl); | ||
!ectrl.isDisposed && fn(ectrl); | ||
ectrl.isInitial = false; | ||
}; | ||
var ea = new derived_1.DerivedAtom(efn, true, opts); | ||
var origac = ea.actualize.bind(ea); | ||
if (opts === null || opts === void 0 ? void 0 : opts.scheduler) | ||
ea.actualize = function () { return opts.scheduler(origac); }; | ||
var onBO = opts === null || opts === void 0 ? void 0 : opts.onBecomeObserved; | ||
if (onBO) | ||
runtime_1.runtime.addEffect({ actualize: onBO }); | ||
runtime_1.runtime.addEffect(ea); | ||
runtime_1.runtime.addEffect(onBO); | ||
runtime_1.runtime.addEffect(ea.run); | ||
runtime_1.runtime.runEffects(); | ||
@@ -72,0 +71,0 @@ return ectrl; |
@@ -1,4 +0,2 @@ | ||
type Effect = { | ||
actualize(): void; | ||
}; | ||
type Effect = () => void; | ||
export declare const runtime: { | ||
@@ -5,0 +3,0 @@ currentAtom: any; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.runtime = void 0; | ||
var shared_1 = require("./shared"); | ||
exports.runtime = { | ||
@@ -16,5 +17,10 @@ currentAtom: undefined, | ||
exports.runtime.counter++; | ||
exports.runtime.effects.forEach(function (x) { | ||
exports.runtime.effects.delete(x); | ||
x.actualize(); | ||
exports.runtime.effects.forEach(function (fn) { | ||
try { | ||
exports.runtime.effects.delete(fn); | ||
fn(); | ||
} | ||
catch (e) { | ||
setTimeout((0, shared_1.thrower)(e)); | ||
} | ||
}); | ||
@@ -21,0 +27,0 @@ exports.runtime.counter--; |
import { AtomState } from "./types"; | ||
export declare const removeAtom: (a: any, self: unknown) => void; | ||
export declare function trackAtom(a: any): void; | ||
export declare function reportError(e: unknown): () => never; | ||
export declare function thrower(e: unknown): () => never; | ||
export declare function invalidateSubs(atom: any, isValueAtom: any, newState?: AtomState): void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.invalidateSubs = exports.reportError = exports.trackAtom = exports.removeAtom = void 0; | ||
exports.invalidateSubs = exports.thrower = exports.trackAtom = exports.removeAtom = void 0; | ||
var runtime_1 = require("./runtime"); | ||
@@ -18,3 +18,3 @@ var types_1 = require("./types"); | ||
exports.trackAtom = trackAtom; | ||
function reportError(e) { | ||
function thrower(e) { | ||
return function () { | ||
@@ -24,3 +24,3 @@ throw e; | ||
} | ||
exports.reportError = reportError; | ||
exports.thrower = thrower; | ||
function invalidateSubs(atom, isValueAtom, newState) { | ||
@@ -31,3 +31,3 @@ if (newState === void 0) { newState = types_1.AtomState.Stale; } | ||
if (a.isEffect) | ||
runtime_1.runtime.addEffect(a); | ||
runtime_1.runtime.addEffect(a.run); | ||
a.state = types_1.AtomState.InvalidatedAndComputing; | ||
@@ -40,3 +40,3 @@ return; | ||
if (a.isEffect) | ||
runtime_1.runtime.addEffect(a); | ||
runtime_1.runtime.addEffect(a.run); | ||
a.subs.size && invalidateSubs(a, isValueAtom, types_1.AtomState.PossiblyStale); | ||
@@ -43,0 +43,0 @@ }); |
@@ -5,9 +5,2 @@ export declare class Id { | ||
export declare const EID: Id; | ||
export declare class Track { | ||
a?: any; | ||
m?: any; | ||
v?: any; | ||
constructor(a?: any, m?: any, v?: any); | ||
} | ||
export declare const ET: Track; | ||
export interface Dependency { | ||
@@ -14,0 +7,0 @@ actualize(): void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.defaultEffectOptions = exports.defaultAtomOptions = exports.AtomState = exports.ET = exports.Track = exports.EID = exports.Id = void 0; | ||
exports.defaultEffectOptions = exports.defaultAtomOptions = exports.AtomState = exports.EID = exports.Id = void 0; | ||
var Id = /** @class */ (function () { | ||
@@ -11,12 +11,2 @@ function Id() { | ||
exports.EID = new Id(); | ||
var Track = /** @class */ (function () { | ||
function Track(a, m, v) { | ||
this.a = a; | ||
this.m = m; | ||
this.v = v; | ||
} | ||
return Track; | ||
}()); | ||
exports.Track = Track; | ||
exports.ET = new Track(); | ||
var AtomState; | ||
@@ -23,0 +13,0 @@ (function (AtomState) { |
{ | ||
"name": "@dhmk/atom", | ||
"version": "2.0.1-test.9", | ||
"version": "2.0.1-test.10", | ||
"description": "Lightweight mobx-like observable values, computed values and side-effects", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
67758
1815