Comparing version 2.1.2 to 3.0.0
@@ -25,7 +25,16 @@ var __defProp = Object.defineProperty; | ||
var batchDepth = 0; | ||
var noop = () => { | ||
}; | ||
function createAtom(name, onBecomeObserved) { | ||
function tryCatch(fn, onError) { | ||
try { | ||
fn(); | ||
} catch (e) { | ||
onError(e); | ||
} | ||
} | ||
function createAtom(onBecomeObserved, options = {}) { | ||
const {name = "atom"} = options; | ||
const onError = options.onError || function(e) { | ||
console.error(`[Quarx]: uncaught exception disposing ${name}:`, e); | ||
}; | ||
const observers = new Map(); | ||
let onBecomeUnobserved, actualize; | ||
let dispose, actualize; | ||
return { | ||
@@ -37,6 +46,8 @@ reportObserved() { | ||
if (!observers.size) { | ||
if (onBecomeUnobserved && pendingDispose.has(onBecomeUnobserved)) { | ||
pendingDispose.delete(onBecomeUnobserved); | ||
} else | ||
onBecomeUnobserved = onBecomeObserved && onBecomeObserved() || noop; | ||
if (dispose && pendingDispose.has(dispose)) { | ||
pendingDispose.delete(dispose); | ||
} else if (onBecomeObserved) { | ||
const cleanup = onBecomeObserved(); | ||
dispose = () => cleanup && tryCatch(cleanup, onError); | ||
} | ||
} | ||
@@ -47,4 +58,4 @@ if (!observers.has(invalidate)) { | ||
observers.delete(invalidate); | ||
if (!observers.size) | ||
pendingDispose.add(onBecomeUnobserved); | ||
if (!observers.size && dispose) | ||
pendingDispose.add(dispose); | ||
}, | ||
@@ -74,3 +85,3 @@ actualize() { | ||
const onError = options.onError || function(e) { | ||
console.log(`[Quarx]: uncaught exception in ${name}:`, e); | ||
console.error(`[Quarx]: uncaught exception in ${name}:`, e); | ||
}; | ||
@@ -104,7 +115,3 @@ let dependencies = new Set(); | ||
stack.push({link, invalidate, actualize}); | ||
try { | ||
computation(); | ||
} catch (e) { | ||
onError(e); | ||
} | ||
tryCatch(computation, onError); | ||
stack.pop(); | ||
@@ -144,6 +151,10 @@ for (let dep of previousDeps) { | ||
} | ||
function batch(t) { | ||
function batch(fn) { | ||
++batchDepth; | ||
t(); | ||
if (--batchDepth === 0) | ||
try { | ||
fn(); | ||
} finally { | ||
--batchDepth; | ||
} | ||
if (!batchDepth) | ||
hydrate(); | ||
@@ -153,5 +164,7 @@ } | ||
stack.push(null); | ||
const result = fn(); | ||
stack.pop(); | ||
return result; | ||
try { | ||
return fn(); | ||
} finally { | ||
stack.pop(); | ||
} | ||
} | ||
@@ -166,3 +179,3 @@ | ||
let result, error; | ||
const atom = createAtom(name, () => autorun(computation)); | ||
const atom = createAtom(() => autorun(computation), {name}); | ||
function computation() { | ||
@@ -199,3 +212,3 @@ try { | ||
} = options; | ||
const atom = createAtom(name); | ||
const atom = createAtom(null, {name}); | ||
return { | ||
@@ -202,0 +215,0 @@ set(newValue) { |
@@ -9,5 +9,10 @@ declare module 'quarx' { | ||
export function createAtom(name?: string, onBecomeObserved?: () => Disposer | void): Atom; | ||
export function autorun(computation: () => void): Disposer; | ||
export interface CoreOptions { | ||
name?: string; | ||
onError?: () => void; | ||
} | ||
export function createAtom(onBecomeObserved?: () => Disposer | void, options?: CoreOptions): Atom; | ||
export function autorun(computation: () => void, options?: CoreOptions): Disposer; | ||
export function batch(changes: () => void): void; | ||
@@ -14,0 +19,0 @@ export function untracked<T>(fn: () => T): T; |
@@ -7,3 +7,3 @@ { | ||
"name": "quarx", | ||
"version": "2.1.2", | ||
"version": "3.0.0", | ||
"description": "Simple dependency graph engine, MobX inspired", | ||
@@ -10,0 +10,0 @@ "main": "dist/index.js", |
@@ -67,5 +67,10 @@ # 🜉 Quarx | ||
export function createAtom(name?: string, onBecomeObserved?: () => Disposer | void): Atom; | ||
export function autorun(computation: () => void): Disposer; | ||
export interface CoreOptions { | ||
name?: string; | ||
onError?: () => void; | ||
} | ||
export function createAtom(onBecomeObserved?: () => Disposer | void, options?: CoreOptions): Atom; | ||
export function autorun(computation: () => void, options?: CoreOptions): Disposer; | ||
export function batch(changes: () => void): void; | ||
@@ -72,0 +77,0 @@ export function untracked<T>(fn: () => T): T; |
@@ -9,3 +9,3 @@ import { createAtom } from './core'; | ||
const atom = createAtom(name); | ||
const atom = createAtom(null, { name }); | ||
@@ -12,0 +12,0 @@ return { |
@@ -11,3 +11,3 @@ import { createAtom, autorun } from './core'; | ||
const atom = createAtom(name, () => autorun(computation)); | ||
const atom = createAtom(() => autorun(computation), { name }); | ||
@@ -14,0 +14,0 @@ function computation() { |
@@ -8,7 +8,19 @@ const stack = []; | ||
const noop = () => {}; | ||
function tryCatch(fn, onError) { | ||
try { | ||
fn(); | ||
} | ||
catch (e) { | ||
onError(e); | ||
} | ||
} | ||
export function createAtom(name, onBecomeObserved) { | ||
export function createAtom(onBecomeObserved, options = {}) { | ||
const { name = 'atom' } = options; | ||
const onError = options.onError || function(e) { | ||
console.error(`[Quarx]: uncaught exception disposing ${name}:`, e); | ||
} | ||
const observers = new Map(); | ||
let onBecomeUnobserved, actualize; | ||
let dispose, actualize; | ||
@@ -22,6 +34,9 @@ return { | ||
if (!observers.size) { | ||
if (onBecomeUnobserved && pendingDispose.has(onBecomeUnobserved)) { | ||
pendingDispose.delete(onBecomeUnobserved); | ||
if (dispose && pendingDispose.has(dispose)) { | ||
pendingDispose.delete(dispose); | ||
} | ||
else onBecomeUnobserved = onBecomeObserved && onBecomeObserved() || noop; | ||
else if (onBecomeObserved) { | ||
const cleanup = onBecomeObserved(); | ||
dispose = () => cleanup && tryCatch(cleanup, onError); | ||
} | ||
} | ||
@@ -33,3 +48,3 @@ | ||
observers.delete(invalidate); | ||
if (!observers.size) pendingDispose.add(onBecomeUnobserved); | ||
if (!observers.size && dispose) pendingDispose.add(dispose); | ||
}, | ||
@@ -61,3 +76,3 @@ actualize() { | ||
const onError = options.onError || function(e) { | ||
console.log(`[Quarx]: uncaught exception in ${name}:`, e); | ||
console.error(`[Quarx]: uncaught exception in ${name}:`, e); | ||
} | ||
@@ -97,8 +112,3 @@ | ||
try { | ||
computation(); | ||
} | ||
catch (e) { | ||
onError(e); | ||
} | ||
tryCatch(computation, onError); | ||
@@ -148,6 +158,11 @@ stack.pop(); | ||
export function batch(t) { | ||
export function batch(fn) { | ||
++batchDepth; | ||
t(); | ||
if (--batchDepth === 0) hydrate(); | ||
try { | ||
fn(); | ||
} | ||
finally { | ||
--batchDepth; | ||
} | ||
if (!batchDepth) hydrate(); | ||
} | ||
@@ -157,5 +172,8 @@ | ||
stack.push(null); | ||
const result = fn(); | ||
stack.pop(); | ||
return result; | ||
try { | ||
return fn(); | ||
} | ||
finally { | ||
stack.pop(); | ||
} | ||
} |
19643
448
124