Comparing version 5.1.0 to 5.2.0
@@ -27,3 +27,3 @@ type ObservableOptions<T> = { | ||
type Subscribable<R> = { | ||
subscribe: (subscriber: (result: R) => void, onError?: (error: any) => void) => () => void; | ||
subscribe: (subscriber: (result: R) => void, onError?: (error: any) => void, onStale?: (flow: unknown) => void) => () => void; | ||
} | ||
@@ -54,4 +54,4 @@ | ||
declare module 'quarx/adapters' { | ||
export function fromObservable<R>(obs: Observable<R>, options?: CoreOptions): Subscribable<R>; | ||
export function subscribable<R>(computation: () => R, options?: CoreOptions): Subscribable<R>; | ||
export function toObservable<R>(subs: Subscribable<R>, options?: ObservableOptions<R>): Observable<R>; | ||
} |
{ | ||
"name": "quarx", | ||
"version": "5.1.0", | ||
"version": "5.2.0", | ||
"description": "Simple tiny reactivity engine", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -118,6 +118,6 @@ # 🜉 Quarx | ||
type Subscribable<R> = { | ||
subscribe: (subscriber: (result: R) => void, onError?: (error: any) => void) => () => void; | ||
subscribe: (subscriber: (result: R) => void, onError?: (error: any) => void, onStale?: (flow: unknown) => void) => () => void; | ||
} | ||
export function fromObservable<R>(obs: Observable<R>, options?: CoreOptions): Subscribable<R>; | ||
export function subscribable<R>(computation: () => R, options?: CoreOptions): Subscribable<R>; | ||
export function toObservable<R>(subs: Subscribable<R>, options?: ObservableOptions<R>): Observable<R>; | ||
@@ -124,0 +124,0 @@ ``` |
import { autorun, createAtom, untrack } from './core.js'; | ||
export function fromObservable(obs, options = {}) { | ||
export function subscribable(evaluate, options = {}) { | ||
return { | ||
subscribe(subscriber, onError = options.onError || console.error) { | ||
return autorun(() => subscriber(obs.get()), { | ||
subscribe(subscriber, onError = options.onError) { | ||
return autorun(() => subscriber(evaluate()), { | ||
onError, | ||
name: options.name || 'fromObservable' | ||
name: options.name || 'subscribable' | ||
}); | ||
@@ -16,3 +16,3 @@ } | ||
let result, error; | ||
const off = subscribe(value => result = value, e => error = e); | ||
const off = subscribe(value => result = value, e => error = e, e => error = e); | ||
off(); | ||
@@ -41,3 +41,3 @@ if (error) throw error; | ||
const atom = createAtom(() => subscribe(r => set(null, r), set), { name }); | ||
const atom = createAtom(() => subscribe(r => set(null, r), set, set), { name }); | ||
@@ -44,0 +44,0 @@ return { |
@@ -1,2 +0,2 @@ | ||
import { createAtom, autorun } from './core.js'; | ||
import { subscribable, toObservable } from './adapters.js'; | ||
@@ -9,26 +9,4 @@ export function computed(evaluate, options = {}) { | ||
let result, error; | ||
function set(e, r) { | ||
if (e && error === e) return; | ||
if (!e && equals(result, r)) return; | ||
[result, error] = [r, e]; | ||
atom.reportChanged(); | ||
} | ||
const atom = createAtom( | ||
() => autorun(() => set(null, evaluate()), { name, onError: set }), | ||
{ name: 'result:' + name } | ||
); | ||
return { | ||
get() { | ||
if (!atom.reportObserved()) { | ||
return evaluate(); | ||
}; | ||
if (error) throw error; | ||
return result; | ||
} | ||
}; | ||
const subs = subscribable(evaluate, { name }); | ||
return toObservable(subs, { name: 'result:' + name, equals }); | ||
} |
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
20139
362