Comparing version 0.5.22 to 0.5.23
import { FC } from 'react'; | ||
import { transaction, untrack } from 'reactive-box'; | ||
export { value, selector, prop, cache, signal, ready, on, once, effect, sync, cycle, loop, pool, stoppable, isolate, shared, initial, observe, useValue, useLocal, useShared, useScoped, Scope, free, mock, unmock, transaction, untrack, Ensurable, Selector, Value, Signal }; | ||
export { value, selector, prop, cache, signal, ready, on, once, effect, sync, cycle, loop, pool, stoppable, isolate, shared, initial, observe, useValue, useLocal, useShared, useScoped, Scope, free, mock, unmock, transaction, untrack, Ensurable, Selector, Value, Signal, StopSignal, ReadySignal, }; | ||
declare type Ensurable<T> = T | void; | ||
declare type Callable<T> = { | ||
name: never; | ||
(data: T): void; | ||
@@ -28,3 +29,3 @@ } & (T extends void ? { | ||
set(value: T): void; | ||
} & [() => K, (value: T) => void] & { | ||
} & { | ||
wrap<P>(set: () => T, get: (data: K) => P): Value<void, P>; | ||
@@ -34,2 +35,3 @@ wrap<P, M = T>(set: (data: M) => T, get: (data: K) => P): Value<M, P>; | ||
wrap<M = T>(set: (data: M) => T): Value<M, K>; | ||
filter(fn: (data: T) => any): Value<T, K>; | ||
view<P>(get: (data: K) => P): Value<T, P>; | ||
@@ -40,4 +42,9 @@ select<P>(get: (data: K) => P): Selector<P>; | ||
reset(): void; | ||
}; | ||
declare type Signal<T, K = T, E = {}, R = { | ||
} & { | ||
[P in Exclude<keyof Array<void>, 'filter' | number>]: never; | ||
} & [ | ||
() => K, | ||
(value: T) => void | ||
]; | ||
declare type Signal<T, K = T, X = {}, E = { | ||
reset(): void; | ||
@@ -49,13 +56,24 @@ }> = Callable<T> & Pick<Promise<T>, 'then' | 'catch' | 'finally'> & { | ||
get(): K; | ||
} & [() => K, (value: T) => void] & { | ||
wrap<P>(set: () => T, get: (data: K) => P): Signal<void, P, E, R>; | ||
wrap<P, M = T>(set: (data: M) => T, get: (data: K) => P): Signal<M, P, E, R>; | ||
wrap(set: () => T): Signal<void, K, E, R>; | ||
wrap<M = T>(set: (data: M) => T): Signal<M, K, E, R>; | ||
view<P>(get: (data: K) => P): Signal<T, P, E, R>; | ||
} & { | ||
wrap<P>(set: () => T, get: (data: K) => P): Signal<void, P, X, E>; | ||
wrap<P, M = T>(set: (data: M) => T, get: (data: K) => P): Signal<M, P, X, E>; | ||
wrap(set: () => T): Signal<void, K, X, E>; | ||
wrap<M = T>(set: (data: M) => T): Signal<M, K, X, E>; | ||
filter(fn: (data: T) => any): Signal<T, K, X, E>; | ||
view<P>(get: (data: K) => P): Signal<T, P, X, E>; | ||
select<P>(get: (data: K) => P): Selector<P>; | ||
select(): Selector<K>; | ||
watch(listener: (value: K extends Ensurable<infer P> ? P : K, prev?: K) => void): () => void; | ||
} & R & E; | ||
declare type StopSignal = Signal<void, boolean, {}, {}>; | ||
} & E & X & { | ||
[P in Exclude<keyof Array<void>, 'filter' | number>]: never; | ||
} & [ | ||
() => K, | ||
(value: T) => void | ||
]; | ||
declare type StopSignal = Signal<void, boolean, { | ||
stop(): void; | ||
}, {}>; | ||
declare type ReadySignal<T, K = T> = Signal<T, K, { | ||
to(value: T): Signal<void, K>; | ||
}>; | ||
declare type Reactionable<T> = { | ||
@@ -73,9 +91,12 @@ 0: () => T; | ||
declare function signal<T = void>(): Signal<T, Ensurable<T>>; | ||
declare namespace signal { | ||
var stop: typeof stop_signal; | ||
} | ||
declare function signal<T = void>(init: T): Signal<T>; | ||
declare function ready<T = void>(): Signal<T, Ensurable<T>, { | ||
to(value: T): Signal<void, Ensurable<T>>; | ||
}>; | ||
declare function ready<T = void>(init: T): Signal<T, T, { | ||
to(value: T): Signal<void, T>; | ||
}>; | ||
declare namespace signal { | ||
var stop: typeof stop_signal; | ||
} | ||
declare function ready<T = void>(): ReadySignal<T, Ensurable<T>>; | ||
declare function ready<T = void>(init: T): ReadySignal<T>; | ||
declare function stop_signal(): StopSignal; | ||
declare function loop(body: () => Promise<any>): () => void; | ||
@@ -82,0 +103,0 @@ declare function stoppable(): StopSignal; |
@@ -17,6 +17,2 @@ "use strict"; | ||
let react; | ||
try { | ||
react = require('react'); | ||
} | ||
catch (e) { } | ||
let useRef; | ||
@@ -30,3 +26,4 @@ let useReducer; | ||
/* istanbul ignore next */ | ||
if (react) { | ||
try { | ||
react = require('react'); | ||
useRef = react.useRef; | ||
@@ -40,3 +37,3 @@ useReducer = react.useReducer; | ||
} | ||
else { | ||
catch (e) { | ||
useRef = useReducer = useEffect = useMemo = useContext = createContext = createElement = (() => { | ||
@@ -87,2 +84,3 @@ throw new Error('Missed "react" dependency'); | ||
exports.signal = signal; | ||
signal.stop = stop_signal; | ||
function ready(init) { | ||
@@ -114,3 +112,4 @@ let resolved = 0; | ||
try { | ||
return ready(false).to(true); | ||
const ctx = ready(false).to(true); | ||
return (ctx.stop = ctx); | ||
} | ||
@@ -146,2 +145,3 @@ finally { | ||
ctx.wrap = (set, get) => wrap(ctx, set, get); | ||
ctx.filter = (fn) => wrap(ctx, (v) => (fn(v) ? v : stoppable().stop())); | ||
} | ||
@@ -169,6 +169,6 @@ ctx.view = (get) => wrap(ctx, 0, get); | ||
const stack = stoppable_context; | ||
stoppable_context = stop_signal(); | ||
stoppable_context = 1; | ||
try { | ||
data = set(data); | ||
if (!stoppable_context[0]()) | ||
if (stoppable_context === 1 || !stoppable_context[0]()) | ||
source_set(data); | ||
@@ -211,2 +211,4 @@ } | ||
dest.reset = target.reset; | ||
if (target.stop) | ||
target.stop = target; | ||
def_format(dest, get ? () => get(source_get()) : source_get, dest_set || source_set, !target.update, target.to); | ||
@@ -233,3 +235,5 @@ return dest; | ||
if (!stoppable_context) | ||
throw new Error('Parent "pool" or "wrap" didn\'t find'); | ||
throw new Error('Parent context not found'); | ||
if (stoppable_context === 1) | ||
stoppable_context = stop_signal(); | ||
return stoppable_context; | ||
@@ -322,4 +326,16 @@ } | ||
function cycle(body) { | ||
const [run, stop] = reactive_box_1.expr(body); | ||
run(); | ||
const iter = () => { | ||
const stack = stoppable_context; | ||
stoppable_context = 1; | ||
try { | ||
run(); | ||
if (stoppable_context !== 1 && stoppable_context[0]()) | ||
stop(); | ||
} | ||
finally { | ||
stoppable_context = stack; | ||
} | ||
}; | ||
const [run, stop] = reactive_box_1.expr(body, iter); | ||
iter(); | ||
if (context_unsubs) | ||
@@ -326,0 +342,0 @@ context_unsubs.push(stop); |
{ | ||
"name": "realar", | ||
"version": "0.5.22", | ||
"version": "0.5.23", | ||
"description": "React state manager", | ||
@@ -88,3 +88,3 @@ "repository": { | ||
}, | ||
"gitHead": "08b0af6751a11faaccea9069f7d4fd424f3ab550" | ||
"gitHead": "82605bf1ffa2f27e3be0aebba041dfcab03d8600" | ||
} |
132
src/index.ts
@@ -33,7 +33,8 @@ import React, { Context, FC } from 'react'; | ||
untrack, | ||
Ensurable, | ||
Selector, | ||
Value, | ||
Signal | ||
Signal, | ||
StopSignal, | ||
ReadySignal, | ||
}; | ||
@@ -43,6 +44,2 @@ | ||
try { | ||
react = require('react'); | ||
} catch (e) {} | ||
let useRef: typeof React.useRef; | ||
@@ -57,3 +54,5 @@ let useReducer: typeof React.useReducer; | ||
/* istanbul ignore next */ | ||
if (react) { | ||
try { | ||
react = require('react'); | ||
useRef = react.useRef; | ||
@@ -66,3 +65,3 @@ useReducer = react.useReducer; | ||
createElement = react.createElement; | ||
} else { | ||
} catch (e) { | ||
useRef = useReducer = useEffect = useMemo = useContext = createContext = createElement = (() => { | ||
@@ -90,2 +89,3 @@ throw new Error('Missed "react" dependency'); | ||
type Callable<T> = { | ||
name: never; | ||
(data: T): void; | ||
@@ -107,3 +107,2 @@ } & (T extends void | ||
select(): Selector<T>; | ||
watch(listener: (value: T, prev?: T) => void): () => void; | ||
@@ -119,16 +118,21 @@ }; | ||
set(value: T): void; | ||
} & [() => K, (value: T) => void] & { | ||
wrap<P>(set: () => T, get: (data: K) => P): Value<void, P>; | ||
wrap<P, M = T>(set: (data: M) => T, get: (data: K) => P): Value<M, P>; | ||
wrap(set: () => T): Value<void, K>; | ||
wrap<M = T>(set: (data: M) => T): Value<M, K>; | ||
} & { | ||
wrap<P>(set: () => T, get: (data: K) => P): Value<void, P>; | ||
wrap<P, M = T>(set: (data: M) => T, get: (data: K) => P): Value<M, P>; | ||
wrap(set: () => T): Value<void, K>; | ||
wrap<M = T>(set: (data: M) => T): Value<M, K>; | ||
view<P>(get: (data: K) => P): Value<T, P>; | ||
select<P>(get: (data: K) => P): Selector<P>; | ||
select(): Selector<K>; | ||
watch(listener: (value: K, prev?: K) => void): () => void; | ||
reset(): void; | ||
}; | ||
filter(fn: (data: T) => any): Value<T, K>; | ||
type Signal<T, K = T, E = {}, R = { reset(): void }> = Callable<T> & | ||
view<P>(get: (data: K) => P): Value<T, P>; | ||
select<P>(get: (data: K) => P): Selector<P>; | ||
select(): Selector<K>; | ||
watch(listener: (value: K, prev?: K) => void): () => void; | ||
reset(): void; | ||
} & { | ||
[P in Exclude<keyof Array<void>, 'filter' | number>]: never; | ||
} & | ||
[() => K, (value: T) => void]; | ||
type Signal<T, K = T, X = {}, E = { reset(): void }> = Callable<T> & | ||
Pick<Promise<T>, 'then' | 'catch' | 'finally'> & { | ||
@@ -139,16 +143,37 @@ 0: () => K; | ||
get(): K; | ||
} & [() => K, (value: T) => void] & { | ||
wrap<P>(set: () => T, get: (data: K) => P): Signal<void, P, E, R>; | ||
wrap<P, M = T>(set: (data: M) => T, get: (data: K) => P): Signal<M, P, E, R>; | ||
wrap(set: () => T): Signal<void, K, E, R>; | ||
wrap<M = T>(set: (data: M) => T): Signal<M, K, E, R>; | ||
} & { | ||
wrap<P>(set: () => T, get: (data: K) => P): Signal<void, P, X, E>; | ||
wrap<P, M = T>(set: (data: M) => T, get: (data: K) => P): Signal<M, P, X, E>; | ||
wrap(set: () => T): Signal<void, K, X, E>; | ||
wrap<M = T>(set: (data: M) => T): Signal<M, K, X, E>; | ||
view<P>(get: (data: K) => P): Signal<T, P, E, R>; | ||
filter(fn: (data: T) => any): Signal<T, K, X, E>; | ||
view<P>(get: (data: K) => P): Signal<T, P, X, E>; | ||
select<P>(get: (data: K) => P): Selector<P>; | ||
select(): Selector<K>; | ||
watch(listener: (value: K extends Ensurable<infer P> ? P : K, prev?: K) => void): () => void; | ||
} & R & | ||
E; | ||
} & E & | ||
X & | ||
{ | ||
[P in Exclude<keyof Array<void>, 'filter' | number>]: never; | ||
} & | ||
[() => K, (value: T) => void]; | ||
type StopSignal = Signal<void, boolean, {}, {}>; | ||
type StopSignal = Signal< | ||
void, | ||
boolean, | ||
{ | ||
stop(): void; | ||
}, | ||
{} | ||
>; | ||
type ReadySignal<T, K = T> = Signal< | ||
T, | ||
K, | ||
{ | ||
to(value: T): Signal<void, K>; | ||
} | ||
>; | ||
type Reactionable<T> = { 0: () => T } | [() => T] | (() => T); | ||
@@ -197,18 +222,6 @@ type Pool<K> = K & { | ||
function ready<T = void>(): Signal< | ||
T, | ||
Ensurable<T>, | ||
{ | ||
to(value: T): Signal<void, Ensurable<T>>; | ||
} | ||
>; | ||
function ready<T = void>( | ||
init: T | ||
): Signal< | ||
T, | ||
T, | ||
{ | ||
to(value: T): Signal<void, T>; | ||
} | ||
>; | ||
signal.stop = stop_signal; | ||
function ready<T = void>(): ReadySignal<T, Ensurable<T>>; | ||
function ready<T = void>(init: T): ReadySignal<T>; | ||
function ready(init?: any) { | ||
@@ -244,3 +257,4 @@ let resolved = 0; | ||
try { | ||
return ready(false).to(true); | ||
const ctx = ready(false).to(true) as any; | ||
return (ctx.stop = ctx); | ||
} finally { | ||
@@ -277,2 +291,3 @@ no_reset = 0; | ||
ctx.wrap = (set: any, get: any) => wrap(ctx, set, get); | ||
ctx.filter = (fn: any) => wrap(ctx, (v: any) => (fn(v) ? v : stoppable().stop())); | ||
} | ||
@@ -304,7 +319,7 @@ ctx.view = (get: any) => wrap(ctx, 0, get); | ||
const stack = stoppable_context; | ||
stoppable_context = stop_signal(); | ||
stoppable_context = 1; | ||
try { | ||
data = set(data); | ||
if (!stoppable_context[0]()) source_set(data); | ||
if (stoppable_context === 1 || !stoppable_context[0]()) source_set(data); | ||
} finally { | ||
@@ -343,2 +358,3 @@ stoppable_context = stack; | ||
if (target.reset) dest.reset = target.reset; | ||
if (target.stop) target.stop = target; | ||
@@ -370,3 +386,4 @@ def_format( | ||
function stoppable(): StopSignal { | ||
if (!stoppable_context) throw new Error('Parent "pool" or "wrap" didn\'t find'); | ||
if (!stoppable_context) throw new Error('Parent context not found'); | ||
if (stoppable_context === 1) stoppable_context = stop_signal(); | ||
return stoppable_context; | ||
@@ -474,4 +491,15 @@ } | ||
function cycle(body: () => void) { | ||
const [run, stop] = expr(body); | ||
run(); | ||
const iter = () => { | ||
const stack = stoppable_context; | ||
stoppable_context = 1; | ||
try { | ||
run(); | ||
if (stoppable_context !== 1 && stoppable_context[0]()) stop(); | ||
} finally { | ||
stoppable_context = stack; | ||
} | ||
}; | ||
const [run, stop] = expr(body, iter); | ||
iter(); | ||
if (context_unsubs) context_unsubs.push(stop); | ||
@@ -478,0 +506,0 @@ return stop; |
Sorry, the diff of this file is not supported yet
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
70574
1253