conclure-quarx
Advanced tools
Comparing version 0.5.6 to 0.6.0
{ | ||
"name": "conclure-quarx", | ||
"version": "0.5.6", | ||
"version": "0.6.0", | ||
"description": "Reactive flows", | ||
"type": "module", | ||
"main": "src/index.js", | ||
"module": "src/index.js", | ||
"exports": { | ||
".": "./src/index.js", | ||
"./computed": "./src/computed-async.js" | ||
}, | ||
"dependencies": { | ||
"conclure": "^2, >=2.1.0", | ||
"quarx": "^4, >=4.1.1" | ||
"quarx": "^5" | ||
}, | ||
@@ -21,4 +27,2 @@ "devDependencies": { | ||
}, | ||
"main": "src/index.js", | ||
"module": "src/index.js", | ||
"directories": { | ||
@@ -25,0 +29,0 @@ "test": "tests" |
import { createAtom, autorun, Quarx } from 'quarx'; | ||
import { conclude, inProgress, isFlow, isIterator, isEffect } from 'conclure'; | ||
import * as Combinators from 'conclure/combinators'; | ||
import { conclude, isFlow, isIterator, isEffect } from 'conclure'; | ||
@@ -9,18 +8,14 @@ if (!Quarx.reactiveFlows) { | ||
const supportedEffects = new Set(Object.values(Combinators)); | ||
export const reactiveFlow = it => { | ||
const flowType = isFlow(it); | ||
if (flowType === isIterator) { | ||
if (!flowType && it && typeof it === 'object') { | ||
(Array.isArray(it) ? it : Object.values(it)).forEach(reactiveFlow); | ||
} | ||
else if (flowType === isIterator) { | ||
makeReactive(it).reportObserved(); | ||
} | ||
else if (flowType === isEffect && supportedEffects.has(it.fn)) { | ||
const flows = it.args[0]; | ||
for (let flow of (Array.isArray(flows) ? flows : Object.values(flows))) { | ||
reactiveFlow(flow); | ||
} | ||
else if (flowType === isEffect) { | ||
reactiveFlow(it.args); | ||
} | ||
return it; | ||
@@ -54,3 +49,3 @@ } | ||
} | ||
}); | ||
}, { name: `${name}[${step}]`}); | ||
@@ -73,71 +68,8 @@ steps.push(dispose); | ||
export class Stale extends Error {}; | ||
export function computedAsync(evaluate, options = {}) { | ||
const { | ||
name = 'computedAsync', | ||
equals = (a, b) => a === b, | ||
onStale | ||
} = options; | ||
let result, error, cancel; | ||
function start() { | ||
const stop = autorun(computation); | ||
return () => { | ||
if (cancel) cancel(); | ||
cancel = undefined; | ||
stop(); | ||
}; | ||
export function autorunFlow(computation, options = {}) { | ||
const { name = 'autorunFlow' } = options; | ||
const onError = options.onError || function(e) { | ||
Quarx.error(`[Quarx-async]: uncaught exception in ${name}:`, e); | ||
} | ||
const atom = createAtom(start, { name }); | ||
function set(e, r) { | ||
error = e; | ||
if (!error) { | ||
if (equals(result, r)) return; | ||
result = r; | ||
} | ||
atom.reportChanged(); | ||
} | ||
function computation() { | ||
try { | ||
if (cancel) cancel(); | ||
const value = evaluate(); | ||
reactiveFlow(value); | ||
cancel = conclude(value, set); | ||
if (isFlow(value) && inProgress(value)) { | ||
set(new Stale(name)); | ||
} | ||
} | ||
catch (err) { | ||
set(err); | ||
} | ||
} | ||
return { | ||
get: () => { | ||
if (!atom.reportObserved()) { | ||
computation(); | ||
}; | ||
if (error instanceof Stale && typeof onStale === 'function') return onStale(); | ||
if (error) throw error; | ||
return result; | ||
} | ||
}; | ||
} | ||
export function autorunFlow(computation, options = {}) { | ||
const { | ||
name = 'autorunFlow', | ||
} = options; | ||
let cancel; | ||
@@ -148,3 +80,3 @@ | ||
cancel = conclude(reactiveFlow(computation()), () => {}); | ||
cancel = conclude(reactiveFlow(computation()), e => e && onError(e)); | ||
}, { name }); | ||
@@ -151,0 +83,0 @@ |
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
4915
5
118
Updatedquarx@^5