Comparing version 0.5.24 to 0.5.25
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, StopSignal, ReadySignal, }; | ||
export { value, selector, prop, cache, signal, ready, on, once, effect, un, sync, cycle, loop, pool, stoppable, isolate, shared, initial, observe, useValue, useLocal, useScoped, shared as useShared, Scope, free, mock, unmock, transaction, untrack, Ensurable, Selector, Value, Signal, StopSignal, ReadySignal, }; | ||
declare type Ensurable<T> = T | void; | ||
@@ -11,2 +11,5 @@ declare type Callable<T> = { | ||
} : {}); | ||
declare type Reactionable<T> = { | ||
0: () => T; | ||
} | [() => T] | (() => T); | ||
declare type Selector<T> = { | ||
@@ -21,3 +24,6 @@ 0: () => T; | ||
select(): Selector<T>; | ||
watch(listener: (value: T, prev?: T) => void): () => void; | ||
watch: { | ||
(listener: (value: T, prev?: T) => void): () => void; | ||
once(listener: (value: T, prev?: T) => void): () => void; | ||
}; | ||
}; | ||
@@ -29,2 +35,6 @@ declare type Value<T, K = T> = Callable<T> & { | ||
update: (fn: (state: K) => T) => void; | ||
sub: { | ||
<S>(reactionable: Reactionable<S>, fn: (data?: K, value?: S, prev?: S) => T): () => void; | ||
once<S>(reactionable: Reactionable<S>, fn: (data?: K, value?: S, prev?: S) => T): () => void; | ||
}; | ||
get(): K; | ||
@@ -41,3 +51,6 @@ set(value: T): void; | ||
select(): Selector<K>; | ||
watch(listener: (value: K, prev?: K) => void): () => void; | ||
watch: { | ||
(listener: (value: K, prev?: K) => void): () => void; | ||
once(listener: (value: K, prev?: K) => void): () => void; | ||
}; | ||
reset(): void; | ||
@@ -66,3 +79,6 @@ } & { | ||
select(): Selector<K>; | ||
watch(listener: (value: K extends Ensurable<infer P> ? P : K, prev?: K) => void): () => void; | ||
watch: { | ||
(listener: (value: K extends Ensurable<infer P> ? P : K, prev?: K) => void): () => void; | ||
once(listener: (value: K extends Ensurable<infer P> ? P : K, prev?: K) => void): () => void; | ||
}; | ||
} & E & X & { | ||
@@ -80,9 +96,6 @@ [P in Exclude<keyof Array<void>, 'filter' | number>]: never; | ||
}>; | ||
declare type Reactionable<T> = { | ||
0: () => T; | ||
} | [() => T] | (() => T); | ||
declare type Pool<K> = K & { | ||
count: number; | ||
threads: StopSignal[]; | ||
pending: boolean; | ||
count: Selector<number>; | ||
threads: Selector<StopSignal[]>; | ||
pending: Selector<boolean>; | ||
}; | ||
@@ -95,2 +108,3 @@ declare function value<T = void>(): Value<T>; | ||
var stop: typeof stop_signal; | ||
var ready: typeof import(".").ready; | ||
} | ||
@@ -100,2 +114,3 @@ declare function signal<T = void>(init: T): Signal<T>; | ||
var stop: typeof stop_signal; | ||
var ready: typeof import(".").ready; | ||
} | ||
@@ -109,3 +124,9 @@ declare function ready<T = void>(): ReadySignal<T, Ensurable<T>>; | ||
declare function on<T>(target: Reactionable<Ensurable<T>>, listener: (value: T, prev?: T) => void): () => void; | ||
declare namespace on { | ||
var once: typeof import(".").once; | ||
} | ||
declare function on<T>(target: Reactionable<T>, listener: (value: T, prev?: T) => void): () => void; | ||
declare namespace on { | ||
var once: typeof import(".").once; | ||
} | ||
declare function once<T>(target: Reactionable<Ensurable<T>>, listener: (value: T, prev?: T) => void): () => void; | ||
@@ -116,2 +137,3 @@ declare function once<T>(target: Reactionable<T>, listener: (value: T, prev?: T) => void): () => void; | ||
declare function effect(fn: () => () => any): () => any; | ||
declare function un(unsub: () => void): (() => void); | ||
declare function cycle(body: () => void): () => void; | ||
@@ -124,10 +146,9 @@ declare function isolate(): () => () => void; | ||
declare function shared<M>(target: (new (init?: any) => M) | ((init?: any) => M)): M; | ||
declare function useScoped<M>(target: (new (init?: any) => M | Reactionable<M>) | ((init?: any) => M | Reactionable<M>)): M; | ||
declare function useScoped<M>(target: (new (init?: any) => M) | ((init?: any) => M)): M; | ||
declare const Scope: FC; | ||
declare function observe<T extends FC>(FunctionComponent: T): T; | ||
declare function useLocal<T extends unknown[], M>(target: (new (...args: T) => M | Reactionable<M>) | ((...args: T) => M | Reactionable<M>), deps?: T): M; | ||
declare function useLocal<T extends unknown[], M>(target: (new (...args: T) => M) | ((...args: T) => M), deps?: T): M; | ||
declare function useValue<T>(target: Reactionable<T>, deps?: any[]): T; | ||
declare function useShared<M>(target: (new (init?: any) => M | Reactionable<M>) | ((init?: any) => M | Reactionable<M>)): M; | ||
declare function free(): void; | ||
declare function prop(_proto: any, key: any, descriptor?: any): any; | ||
declare function cache(_proto: any, key: any, descriptor: any): any; |
@@ -12,3 +12,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.untrack = exports.transaction = exports.unmock = exports.mock = exports.free = exports.Scope = exports.useScoped = exports.useShared = exports.useLocal = exports.useValue = exports.observe = exports.initial = exports.shared = exports.isolate = exports.stoppable = exports.pool = exports.loop = exports.cycle = exports.sync = exports.effect = exports.once = exports.on = exports.ready = exports.signal = exports.cache = exports.prop = exports.selector = exports.value = void 0; | ||
exports.untrack = exports.transaction = exports.unmock = exports.mock = exports.free = exports.Scope = exports.useShared = exports.useScoped = exports.useLocal = exports.useValue = exports.observe = exports.initial = exports.shared = exports.isolate = exports.stoppable = exports.pool = exports.loop = exports.cycle = exports.sync = exports.un = exports.effect = exports.once = exports.on = exports.ready = exports.signal = exports.cache = exports.prop = exports.selector = exports.value = void 0; | ||
const reactive_box_1 = require("reactive-box"); | ||
@@ -83,2 +83,3 @@ Object.defineProperty(exports, "transaction", { enumerable: true, get: function () { return reactive_box_1.transaction; } }); | ||
signal.stop = stop_signal; | ||
signal.ready = ready; | ||
function ready(init) { | ||
@@ -134,2 +135,4 @@ let resolved = 0; | ||
val_prop.set = set; | ||
ctx.sub = (s, fn) => on(s, (v, v_prev) => set(fn(get(), v, v_prev))); | ||
ctx.sub.once = (s, fn) => once(s, (v, v_prev) => set(fn(get(), v, v_prev))); | ||
} | ||
@@ -147,2 +150,3 @@ } | ||
ctx.watch = (fn) => on(ctx, fn); | ||
ctx.watch.once = (fn) => once(ctx, fn); | ||
ctx.select = (fn) => selector(fn ? () => fn(get()) : get); | ||
@@ -223,4 +227,3 @@ } | ||
}; | ||
if (context_unsubs) | ||
context_unsubs.push(unsub); | ||
un(unsub); | ||
fn(); | ||
@@ -239,9 +242,9 @@ return unsub; | ||
function pool(body) { | ||
const [get_threads, set_threads] = reactive_box_1.box([]); | ||
const get_count = () => get_threads().length; | ||
const [get_pending] = reactive_box_1.sel(() => get_count() > 0); | ||
const threads = value([]); | ||
const count = threads.select(t => t.length); | ||
const pending = count.select(c => c > 0); | ||
function run() { | ||
const stop = stop_signal(); | ||
isolate(once(stop, () => set_threads(get_threads().filter(ctx => ctx !== stop)))); | ||
set_threads(get_threads().concat(stop)); | ||
isolate(threads.sub.once(stop, t => t.filter(ctx => ctx !== stop))); | ||
threads.update(t => t.concat(stop)); | ||
const stack = stoppable_context; | ||
@@ -264,5 +267,5 @@ stoppable_context = stop; | ||
} | ||
def_prop(run, 'count', { get: get_count }); | ||
def_prop(run, 'threads', { get: get_threads }); | ||
def_prop(run, 'pending', { get: get_pending }); | ||
run.count = count; | ||
run.threads = threads; | ||
run.pending = pending; | ||
return run; | ||
@@ -292,4 +295,3 @@ } | ||
}; | ||
if (context_unsubs) | ||
context_unsubs.push(unsub); | ||
un(unsub); | ||
if (sync_mode) | ||
@@ -300,2 +302,3 @@ listener(value); | ||
exports.on = on; | ||
on.once = once; | ||
function once(target, listener) { | ||
@@ -319,3 +322,6 @@ const unsub = on(target, (value, prev) => { | ||
function effect(fn) { | ||
const unsub = fn(); | ||
return un(fn()); | ||
} | ||
exports.effect = effect; | ||
function un(unsub) { | ||
if (unsub && context_unsubs) | ||
@@ -325,3 +331,3 @@ context_unsubs.push(unsub); | ||
} | ||
exports.effect = effect; | ||
exports.un = un; | ||
function cycle(body) { | ||
@@ -331,3 +337,3 @@ const iter = () => { | ||
stoppable_context = stop_signal(); | ||
isolate(on(stoppable_context, stop)); | ||
isolate(once(stoppable_context, stop)); | ||
try { | ||
@@ -342,5 +348,3 @@ run(); | ||
iter(); | ||
if (context_unsubs) | ||
context_unsubs.push(stop); | ||
return stop; | ||
return un(stop); | ||
} | ||
@@ -390,2 +394,3 @@ exports.cycle = cycle; | ||
exports.shared = shared; | ||
exports.useShared = shared; | ||
function inst(target, args) { | ||
@@ -427,3 +432,3 @@ let instance; | ||
} | ||
return useValue(instance); | ||
return instance; | ||
} | ||
@@ -464,3 +469,3 @@ exports.useScoped = useScoped; | ||
useEffect(h[1], [h]); | ||
return useValue(h[0], [h]); | ||
return h[0]; | ||
} | ||
@@ -496,6 +501,2 @@ exports.useLocal = useLocal; | ||
exports.useValue = useValue; | ||
function useShared(target) { | ||
return useValue(shared(target)); | ||
} | ||
exports.useShared = useShared; | ||
function free() { | ||
@@ -502,0 +503,0 @@ try { |
{ | ||
"name": "realar", | ||
"version": "0.5.24", | ||
"version": "0.5.25", | ||
"description": "React state manager", | ||
@@ -88,3 +88,3 @@ "repository": { | ||
}, | ||
"gitHead": "4c3adfb84ce1564e2a7bee78e0a58c1b761601c9" | ||
"gitHead": "fcfd72b2963e25713744f9b1f57048ceb22c1a4b" | ||
} |
@@ -430,3 +430,3 @@ # Realar | ||
_Documentation not ready yet for `effect`, `loop`, `pool`, `stoppable`, `initial`, `mock`, `unmock`, `free`, `transaction`, `untrack`, `isolate`, `ready`, `once` functions. It's coming soon._ | ||
_Documentation not ready yet for `effect`, `loop`, `pool`, `stoppable`, `initial`, `mock`, `unmock`, `free`, `transaction`, `untrack`, `isolate`, `ready`, `once`, `un` functions. It's coming soon._ | ||
@@ -433,0 +433,0 @@ ### Demos |
@@ -14,2 +14,3 @@ import React, { Context, FC } from 'react'; | ||
effect, | ||
un, | ||
sync, | ||
@@ -26,4 +27,4 @@ cycle, | ||
useLocal, | ||
useShared, | ||
useScoped, | ||
shared as useShared, | ||
Scope, | ||
@@ -95,2 +96,4 @@ free, | ||
type Reactionable<T> = { 0: () => T } | [() => T] | (() => T); | ||
type Selector<T> = { | ||
@@ -105,3 +108,7 @@ 0: () => T; | ||
select(): Selector<T>; | ||
watch(listener: (value: T, prev?: T) => void): () => void; | ||
watch: { | ||
(listener: (value: T, prev?: T) => void): () => void; | ||
once(listener: (value: T, prev?: T) => void): () => void; | ||
}; | ||
}; | ||
@@ -114,2 +121,8 @@ | ||
update: (fn: (state: K) => T) => void; | ||
sub: { | ||
<S>(reactionable: Reactionable<S>, fn: (data?: K, value?: S, prev?: S) => T): () => void; | ||
once<S>(reactionable: Reactionable<S>, fn: (data?: K, value?: S, prev?: S) => T): () => void; | ||
}; | ||
get(): K; | ||
@@ -128,3 +141,6 @@ set(value: T): void; | ||
select(): Selector<K>; | ||
watch(listener: (value: K, prev?: K) => void): () => void; | ||
watch: { | ||
(listener: (value: K, prev?: K) => void): () => void; | ||
once(listener: (value: K, prev?: K) => void): () => void; | ||
}; | ||
reset(): void; | ||
@@ -153,3 +169,7 @@ } & { | ||
select(): Selector<K>; | ||
watch(listener: (value: K extends Ensurable<infer P> ? P : K, prev?: K) => void): () => void; | ||
watch: { | ||
(listener: (value: K extends Ensurable<infer P> ? P : K, prev?: K) => void): () => void; | ||
once(listener: (value: K extends Ensurable<infer P> ? P : K, prev?: K) => void): () => void; | ||
}; | ||
} & E & | ||
@@ -178,7 +198,6 @@ X & | ||
type Reactionable<T> = { 0: () => T } | [() => T] | (() => T); | ||
type Pool<K> = K & { | ||
count: number; | ||
threads: StopSignal[]; | ||
pending: boolean; | ||
count: Selector<number>; | ||
threads: Selector<StopSignal[]>; | ||
pending: Selector<boolean>; | ||
}; | ||
@@ -223,2 +242,3 @@ | ||
signal.stop = stop_signal; | ||
signal.ready = ready; | ||
@@ -281,2 +301,4 @@ function ready<T = void>(): ReadySignal<T, Ensurable<T>>; | ||
val_prop.set = set; | ||
ctx.sub = (s: any, fn: any) => on(s, (v, v_prev) => set(fn(get(), v, v_prev))); | ||
ctx.sub.once = (s: any, fn: any) => once(s, (v, v_prev) => set(fn(get(), v, v_prev))); | ||
} | ||
@@ -295,2 +317,4 @@ } | ||
ctx.watch = (fn: any) => on(ctx, fn); | ||
ctx.watch.once = (fn: any) => once(ctx, fn); | ||
ctx.select = (fn: any) => selector(fn ? () => fn(get()) : get); | ||
@@ -378,3 +402,3 @@ } | ||
}; | ||
if (context_unsubs) context_unsubs.push(unsub); | ||
un(unsub); | ||
fn(); | ||
@@ -391,12 +415,11 @@ return unsub; | ||
function pool<K extends () => Promise<any>>(body: K): Pool<K> { | ||
const [get_threads, set_threads] = box([]); | ||
const get_count = () => get_threads().length; | ||
const [get_pending] = sel(() => get_count() > 0); | ||
const threads = value([]); | ||
const count = threads.select(t => t.length); | ||
const pending = count.select(c => c > 0); | ||
function run() { | ||
const stop = stop_signal(); | ||
isolate(once(stop, () => set_threads(get_threads().filter(ctx => ctx !== stop)))); | ||
isolate(threads.sub.once(stop, t => t.filter(ctx => ctx !== stop))); | ||
threads.update(t => t.concat(stop)); | ||
set_threads(get_threads().concat(stop)); | ||
const stack = stoppable_context; | ||
@@ -419,7 +442,6 @@ stoppable_context = stop; | ||
} | ||
run.count = count; | ||
run.threads = threads; | ||
run.pending = pending; | ||
def_prop(run, 'count', { get: get_count }); | ||
def_prop(run, 'threads', { get: get_threads }); | ||
def_prop(run, 'pending', { get: get_pending }); | ||
return run as any; | ||
@@ -456,3 +478,3 @@ } | ||
}; | ||
if (context_unsubs) context_unsubs.push(unsub); | ||
un(unsub); | ||
if (sync_mode) listener(value); | ||
@@ -462,2 +484,4 @@ return unsub; | ||
on.once = once; | ||
function once<T>( | ||
@@ -487,3 +511,6 @@ target: Reactionable<Ensurable<T>>, | ||
function effect(fn: any) { | ||
const unsub = fn(); | ||
return un(fn()); | ||
} | ||
function un(unsub: () => void): (() => void) { | ||
if (unsub && context_unsubs) context_unsubs.push(unsub); | ||
@@ -497,3 +524,3 @@ return unsub; | ||
stoppable_context = stop_signal(); | ||
isolate(on(stoppable_context, stop)); | ||
isolate(once(stoppable_context, stop)); | ||
try { | ||
@@ -508,4 +535,3 @@ run(); | ||
iter(); | ||
if (context_unsubs) context_unsubs.push(stop); | ||
return stop; | ||
return un(stop); | ||
} | ||
@@ -584,5 +610,3 @@ | ||
function useScoped<M>( | ||
target: (new (init?: any) => M | Reactionable<M>) | ((init?: any) => M | Reactionable<M>) | ||
): M { | ||
function useScoped<M>(target: (new (init?: any) => M) | ((init?: any) => M)): M { | ||
const context_data = useContext(get_scope_context()); | ||
@@ -603,3 +627,3 @@ if (!context_data) { | ||
} | ||
return useValue(instance); | ||
return instance; | ||
} | ||
@@ -635,3 +659,3 @@ | ||
function useLocal<T extends unknown[], M>( | ||
target: (new (...args: T) => M | Reactionable<M>) | ((...args: T) => M | Reactionable<M>), | ||
target: (new (...args: T) => M) | ((...args: T) => M), | ||
deps = [] as T | ||
@@ -645,3 +669,3 @@ ): M { | ||
useEffect(h[1], [h]); | ||
return useValue(h[0], [h]); | ||
return h[0]; | ||
} | ||
@@ -675,8 +699,2 @@ | ||
function useShared<M>( | ||
target: (new (init?: any) => M | Reactionable<M>) | ((init?: any) => M | Reactionable<M>) | ||
): M { | ||
return useValue(shared(target as any)); | ||
} | ||
function free() { | ||
@@ -683,0 +701,0 @@ try { |
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
71688
1287