Comparing version 0.4.14 to 0.4.15
import { FC } from 'react'; | ||
import { expr, box, sel, transaction } from 'reactive-box'; | ||
export { prop, cache, action, on, cycle, effect, shared, initial, observe, useValue, useLocal, useShared, free, mock, box, sel, expr, transaction, Ensurable, }; | ||
export { prop, cache, action, on, sync, cycle, effect, shared, initial, observe, useValue, useLocal, useShared, free, mock, unmock, box, sel, expr, transaction, Ensurable, }; | ||
declare type Ensurable<T> = T | void; | ||
@@ -16,2 +16,5 @@ declare function action<T = undefined>(init?: T): { | ||
} | [() => T] | (() => T), listener: (value: T, prev?: T) => void): () => void; | ||
declare function sync<T>(target: { | ||
0: () => T; | ||
} | [() => T] | (() => T), listener: (value: T, prev?: T) => void): () => void; | ||
declare function effect(fn: () => void): void; | ||
@@ -22,2 +25,3 @@ declare function effect(fn: () => () => any): () => any; | ||
declare function mock<M>(Class: (new (init?: any) => M) | ((init?: any) => M), mocked: M): M; | ||
declare function unmock(Class: (new (init?: any) => any) | ((init?: any) => any), ...Classes: ((new (init?: any) => any) | ((init?: any) => any))[]): void; | ||
declare function shared<M>(Class: (new (init?: any) => M) | ((init?: any) => M)): M; | ||
@@ -24,0 +28,0 @@ declare function observe<T extends FC>(FunctionComponent: T): T; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.transaction = exports.expr = exports.sel = exports.box = exports.mock = exports.free = exports.useShared = exports.useLocal = exports.useValue = exports.observe = exports.initial = exports.shared = exports.effect = exports.cycle = exports.on = exports.action = exports.cache = exports.prop = void 0; | ||
exports.transaction = exports.expr = exports.sel = exports.box = exports.unmock = exports.mock = exports.free = exports.useShared = exports.useLocal = exports.useValue = exports.observe = exports.initial = exports.shared = exports.effect = exports.cycle = exports.sync = exports.on = exports.action = exports.cache = exports.prop = void 0; | ||
const reactive_box_1 = require("reactive-box"); | ||
@@ -34,2 +34,4 @@ Object.defineProperty(exports, "expr", { enumerable: true, get: function () { return reactive_box_1.expr; } }); | ||
let shared_unsubs = []; | ||
let is_sync; | ||
let is_observe; | ||
function action(init) { | ||
@@ -56,3 +58,5 @@ let resolve; | ||
function on(target, listener) { | ||
const sync_mode = is_sync; | ||
let free; | ||
is_sync = 0; | ||
if (!target) | ||
@@ -79,5 +83,12 @@ return; | ||
context_unsubs.push(unsub); | ||
if (sync_mode) | ||
listener(value); | ||
return unsub; | ||
} | ||
exports.on = on; | ||
function sync(target, listener) { | ||
is_sync = 1; | ||
return on(target, listener); | ||
} | ||
exports.sync = sync; | ||
function effect(fn) { | ||
@@ -107,2 +118,6 @@ const unsub = fn(); | ||
exports.mock = mock; | ||
function unmock(Class, ...Classes) { | ||
Classes.concat(Class).forEach(Class => shareds.delete(Class)); | ||
} | ||
exports.unmock = unmock; | ||
function shared(Class) { | ||
@@ -138,3 +153,10 @@ let instance = shareds.get(Class); | ||
useEffect(() => ref.current[1], []); | ||
return ref.current[0].apply(this, arguments); | ||
const stack = is_observe; | ||
is_observe = 1; | ||
try { | ||
return ref.current[0].apply(this, arguments); | ||
} | ||
finally { | ||
is_observe = stack; | ||
} | ||
}; | ||
@@ -168,3 +190,3 @@ } | ||
function useValue(target, deps = []) { | ||
const forceUpdate = useForceUpdate(); | ||
const forceUpdate = is_observe || useForceUpdate(); | ||
const h = useMemo(() => { | ||
@@ -174,8 +196,13 @@ if (target[0]) | ||
if (typeof target === 'function') { | ||
const [run, stop] = reactive_box_1.expr(target, () => { | ||
forceUpdate(); | ||
if (is_observe) { | ||
return [target, 0, 1]; | ||
} | ||
else { | ||
const [run, stop] = reactive_box_1.expr(target, () => { | ||
forceUpdate(); | ||
run(); | ||
}); | ||
run(); | ||
}); | ||
run(); | ||
return [target, () => stop, 1]; | ||
return [target, () => stop, 1]; | ||
} | ||
} | ||
@@ -186,3 +213,3 @@ else { | ||
}, [target, ...deps]); | ||
useEffect(h[1], [h]); | ||
is_observe || useEffect(h[1], [h]); | ||
return h[2] ? h[0]() : h[0]; | ||
@@ -189,0 +216,0 @@ } |
{ | ||
"name": "realar", | ||
"version": "0.4.14", | ||
"version": "0.4.15", | ||
"description": "React state manager", | ||
@@ -88,3 +88,3 @@ "repository": { | ||
}, | ||
"gitHead": "8d388c9a17e7b252c00dec084e300e924b9dddc3" | ||
"gitHead": "672c0f53038190f26cf9bd36f253b5422c777a95" | ||
} |
@@ -69,3 +69,3 @@ # Realar | ||
_Documentation not ready yet for `action`, `cache`, `on`, `cycle`, `effect`, `shared`, `initial`, `mock`, `free`, `useLocal`, `observe`, `useValue`, `useShared`, `transaction`, `box`, `sel` functions. It's coming soon._ | ||
_Documentation not ready yet for `action`, `cache`, `on`, `sync`, `cycle`, `effect`, `shared`, `initial`, `mock`, `unmock`, `free`, `useLocal`, `observe`, `useValue`, `useShared`, `transaction`, `box`, `sel` functions. It's coming soon._ | ||
@@ -72,0 +72,0 @@ ### Demos |
@@ -9,2 +9,3 @@ import React, { FC } from 'react'; | ||
on, | ||
sync, | ||
cycle, | ||
@@ -20,2 +21,3 @@ effect, | ||
mock, | ||
unmock, | ||
box, | ||
@@ -56,2 +58,4 @@ sel, | ||
let shared_unsubs = [] as any; | ||
let is_sync: any; | ||
let is_observe: any; | ||
@@ -100,4 +104,7 @@ type Ensurable<T> = T | void; | ||
function on(target: any, listener: (value: any, prev?: any) => void): () => void { | ||
const sync_mode = is_sync; | ||
let free: (() => void) | undefined; | ||
is_sync = 0; | ||
if (!target) return; | ||
@@ -122,5 +129,14 @@ else if (target[0]) { | ||
if (context_unsubs) context_unsubs.push(unsub); | ||
if (sync_mode) listener(value); | ||
return unsub; | ||
} | ||
function sync<T>( | ||
target: { 0: () => T } | [() => T] | (() => T), | ||
listener: (value: T, prev?: T) => void | ||
): () => void { | ||
is_sync = 1; | ||
return on(target, listener); | ||
} | ||
function effect(fn: () => void): void; | ||
@@ -150,2 +166,9 @@ function effect(fn: () => () => any): () => any; | ||
function unmock( | ||
Class: (new (init?: any) => any) | ((init?: any) => any), | ||
...Classes: ((new (init?: any) => any) | ((init?: any) => any))[] | ||
) { | ||
Classes.concat(Class).forEach(Class => shareds.delete(Class)); | ||
} | ||
function shared<M>(Class: (new (init?: any) => M) | ((init?: any) => M)): M { | ||
@@ -181,3 +204,10 @@ let instance = shareds.get(Class); | ||
useEffect(() => ref.current![1], []); | ||
return ref.current[0].apply(this, arguments); | ||
const stack = is_observe; | ||
is_observe = 1; | ||
try { | ||
return ref.current[0].apply(this, arguments); | ||
} finally { | ||
is_observe = stack; | ||
} | ||
} as any; | ||
@@ -214,3 +244,3 @@ } | ||
function useValue<T>(target: (() => T) | { 0: () => T } | [() => T], deps: any[] = []): T { | ||
const forceUpdate = useForceUpdate(); | ||
const forceUpdate = is_observe || useForceUpdate(); | ||
const h = useMemo(() => { | ||
@@ -220,8 +250,12 @@ if ((target as any)[0]) target = (target as any)[0]; // box or selector or custom reactive | ||
if (typeof target === 'function') { | ||
const [run, stop] = expr(target as any, () => { | ||
forceUpdate(); | ||
if (is_observe) { | ||
return [target, 0, 1]; | ||
} else { | ||
const [run, stop] = expr(target as any, () => { | ||
forceUpdate(); | ||
run(); | ||
}); | ||
run(); | ||
}); | ||
run(); | ||
return [target, () => stop, 1]; | ||
return [target, () => stop, 1]; | ||
} | ||
} else { | ||
@@ -232,3 +266,3 @@ return [target as any, () => {}]; | ||
useEffect(h[1], [h]); | ||
is_observe || useEffect(h[1], [h]); | ||
return h[2] ? h[0]() : h[0]; | ||
@@ -235,0 +269,0 @@ } |
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
30178
550