Comparing version 0.4.18 to 0.4.19
import { FC } from 'react'; | ||
import { expr, box, sel, transaction } from 'reactive-box'; | ||
export { prop, cache, action, on, sync, cycle, effect, shared, initial, observe, useValue, useLocal, useShared, free, mock, unmock, box, sel, expr, transaction, Ensurable, }; | ||
export { prop, cache, action, on, sync, cycle, effect, shared, initial, observe, useValue, useLocal, useShared, useScoped, Scope, free, mock, unmock, box, sel, expr, transaction, Ensurable, }; | ||
declare type Ensurable<T> = T | void; | ||
@@ -23,7 +23,9 @@ declare function action<T = undefined>(init?: T): { | ||
declare function initial(data: any): void; | ||
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; | ||
declare function mock<M>(target: (new (init?: any) => M) | ((init?: any) => M), mocked: M): M; | ||
declare function unmock(target: (new (init?: any) => any) | ((init?: any) => any), ...targets: ((new (init?: any) => any) | ((init?: any) => any))[]): void; | ||
declare function shared<M>(target: (new (init?: any) => M) | ((init?: any) => M)): M; | ||
declare const 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>(Class: (new (...args: T) => M) | ((...args: T) => 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: (() => T) | { | ||
@@ -30,0 +32,0 @@ 0: () => T; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
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; | ||
exports.transaction = exports.expr = exports.sel = exports.box = exports.unmock = exports.mock = exports.free = exports.Scope = exports.useScoped = 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"); | ||
@@ -18,2 +18,5 @@ Object.defineProperty(exports, "expr", { enumerable: true, get: function () { return reactive_box_1.expr; } }); | ||
let useMemo; | ||
let useContext; | ||
let createContext; | ||
let createElement; | ||
/* istanbul ignore next */ | ||
@@ -25,5 +28,8 @@ if (react) { | ||
useMemo = react.useMemo; | ||
useContext = react.useContext; | ||
createContext = react.createContext; | ||
createElement = react.createElement; | ||
} | ||
else { | ||
useRef = useReducer = useEffect = useMemo = (() => { | ||
useRef = useReducer = useEffect = useMemo = useContext = createContext = createElement = (() => { | ||
throw new Error('Missed "react" dependency'); | ||
@@ -38,2 +44,3 @@ }); | ||
let is_observe; | ||
let scope_context; | ||
function action(init) { | ||
@@ -113,27 +120,18 @@ let resolve; | ||
exports.initial = initial; | ||
function mock(Class, mocked) { | ||
shareds.set(Class, mocked); | ||
function mock(target, mocked) { | ||
shareds.set(target, mocked); | ||
return mocked; | ||
} | ||
exports.mock = mock; | ||
function unmock(Class, ...Classes) { | ||
Classes.concat(Class).forEach(Class => shareds.delete(Class)); | ||
function unmock(target, ...targets) { | ||
targets.concat(target).forEach(target => shareds.delete(target)); | ||
} | ||
exports.unmock = unmock; | ||
function shared(Class) { | ||
let instance = shareds.get(Class); | ||
function shared(target) { | ||
let instance = shareds.get(target); | ||
if (!instance) { | ||
const stack = context_unsubs; | ||
context_unsubs = []; | ||
try { | ||
instance = | ||
typeof Class.prototype === 'undefined' | ||
? Class(initial_data) | ||
: new Class(initial_data); | ||
} | ||
finally { | ||
shared_unsubs.push(...context_unsubs); | ||
context_unsubs = stack; | ||
} | ||
shareds.set(Class, instance); | ||
const h = inst(target, [initial_data]); | ||
instance = h[0]; | ||
shared_unsubs.push(...h[1]); | ||
shareds.set(target, instance); | ||
} | ||
@@ -143,2 +141,48 @@ return instance; | ||
exports.shared = shared; | ||
function inst(target, args) { | ||
let instance; | ||
const stack = context_unsubs; | ||
context_unsubs = []; | ||
try { | ||
instance = | ||
typeof target.prototype === 'undefined' | ||
? target(...args) | ||
: new target(...args); | ||
} | ||
finally { | ||
const unsubs = context_unsubs; | ||
context_unsubs = stack; | ||
return [instance, unsubs]; | ||
} | ||
} | ||
function call_array(arr) { | ||
arr.forEach(fn => fn()); | ||
} | ||
function get_scope_context() { | ||
return scope_context ? scope_context : (scope_context = createContext()); | ||
} | ||
const useScoped = (target) => { | ||
const context_data = useContext(get_scope_context()); | ||
if (!context_data) { | ||
throw new Error('"Scope" parent component didn\'t find'); | ||
} | ||
let instance; | ||
if (context_data[0].has(target)) { | ||
instance = context_data[0].get(target); | ||
} | ||
else { | ||
const h = inst(target, [initial_data]); | ||
instance = h[0]; | ||
context_data[1].push(...h[1]); | ||
context_data[0].set(target, instance); | ||
} | ||
return useValue(instance); | ||
}; | ||
exports.useScoped = useScoped; | ||
const Scope = ({ children }) => { | ||
const h = useMemo(() => [new Map(), []], []); | ||
useEffect(() => () => call_array(h[1]), []); | ||
return createElement(get_scope_context().Provider, { value: h }, children); | ||
}; | ||
exports.Scope = Scope; | ||
function useForceUpdate() { | ||
@@ -165,24 +209,9 @@ return useReducer(() => [], [])[1]; | ||
exports.observe = observe; | ||
function useLocal(Class, deps = []) { | ||
if (!Array.isArray(deps)) { | ||
throw new Error('TypeError: deps argument should be an array in "use" call'); | ||
} | ||
function useLocal(target, deps = []) { | ||
const h = useMemo(() => { | ||
let inst, unsubs; | ||
const stack = context_unsubs; | ||
context_unsubs = []; | ||
try { | ||
inst = | ||
typeof Class.prototype === 'undefined' | ||
? Class(...deps) | ||
: new Class(...deps); | ||
} | ||
finally { | ||
unsubs = context_unsubs; | ||
context_unsubs = stack; | ||
} | ||
return [inst, () => () => unsubs.forEach(fn => fn())]; | ||
}, [Class, ...deps]); | ||
const i = inst(target, deps); | ||
return [i[0], () => () => call_array(i[1])]; | ||
}, deps); | ||
useEffect(h[1], [h]); | ||
return useValue(h[0]); | ||
return useValue(h[0], [h]); | ||
} | ||
@@ -193,2 +222,4 @@ exports.useLocal = useLocal; | ||
const h = useMemo(() => { | ||
if (!target) | ||
return [target, () => { }]; | ||
if (target[0]) | ||
@@ -212,3 +243,3 @@ target = target[0]; // box or selector or custom reactive | ||
} | ||
}, [target, ...deps]); | ||
}, deps); | ||
is_observe || useEffect(h[1], [h]); | ||
@@ -224,3 +255,3 @@ return h[2] ? h[0]() : h[0]; | ||
try { | ||
shared_unsubs.forEach((fn) => fn()); | ||
call_array(shared_unsubs); | ||
} | ||
@@ -227,0 +258,0 @@ finally { |
{ | ||
"name": "realar", | ||
"version": "0.4.18", | ||
"version": "0.4.19", | ||
"description": "React state manager", | ||
@@ -88,3 +88,3 @@ "repository": { | ||
}, | ||
"gitHead": "7205877207fa66441ed39b4a5e5e8e7be62fd905" | ||
"gitHead": "72effd67f8285c74f0ddddfc4e493d3f753219d8" | ||
} |
@@ -172,3 +172,3 @@ # Realar | ||
_Documentation not ready yet for `on`, `action`, `sel`, `shared`, `sync`, `cycle`, `effect`, `initial`, `mock`, `unmock`, `free`, `useLocal`, `useValue`, `useShared`, `observe`, `transaction`, `cache` functions. It's coming soon._ | ||
_Documentation not ready yet for `on`, `action`, `sel`, `shared`, `sync`, `cycle`, `effect`, `initial`, `mock`, `unmock`, `free`, `useLocal`, `useValue`, `useShared`, `useScoped`, `Scope`, `observe`, `transaction`, `cache` functions. It's coming soon._ | ||
@@ -175,0 +175,0 @@ ### Demos |
125
src/index.ts
@@ -1,2 +0,2 @@ | ||
import React, { FC } from 'react'; | ||
import React, { Context, FC } from 'react'; | ||
import { expr, box, sel, transaction } from 'reactive-box'; | ||
@@ -18,2 +18,4 @@ | ||
useShared, | ||
useScoped, | ||
Scope, | ||
free, | ||
@@ -39,2 +41,5 @@ mock, | ||
let useMemo: typeof React.useMemo; | ||
let useContext: typeof React.useContext; | ||
let createContext: typeof React.createContext; | ||
let createElement: typeof React.createElement; | ||
@@ -47,4 +52,7 @@ /* istanbul ignore next */ | ||
useMemo = react.useMemo; | ||
useContext = react.useContext; | ||
createContext = react.createContext; | ||
createElement = react.createElement; | ||
} else { | ||
useRef = useReducer = useEffect = useMemo = (() => { | ||
useRef = useReducer = useEffect = useMemo = useContext = createContext = createElement = (() => { | ||
throw new Error('Missed "react" dependency'); | ||
@@ -61,2 +69,3 @@ }) as any; | ||
let is_observe: any; | ||
let scope_context: any; | ||
@@ -160,4 +169,4 @@ type Ensurable<T> = T | void; | ||
function mock<M>(Class: (new (init?: any) => M) | ((init?: any) => M), mocked: M): M { | ||
shareds.set(Class, mocked); | ||
function mock<M>(target: (new (init?: any) => M) | ((init?: any) => M), mocked: M): M { | ||
shareds.set(target, mocked); | ||
return mocked; | ||
@@ -167,24 +176,15 @@ } | ||
function unmock( | ||
Class: (new (init?: any) => any) | ((init?: any) => any), | ||
...Classes: ((new (init?: any) => any) | ((init?: any) => any))[] | ||
target: (new (init?: any) => any) | ((init?: any) => any), | ||
...targets: ((new (init?: any) => any) | ((init?: any) => any))[] | ||
) { | ||
Classes.concat(Class).forEach(Class => shareds.delete(Class)); | ||
targets.concat(target).forEach(target => shareds.delete(target)); | ||
} | ||
function shared<M>(Class: (new (init?: any) => M) | ((init?: any) => M)): M { | ||
let instance = shareds.get(Class); | ||
function shared<M>(target: (new (init?: any) => M) | ((init?: any) => M)): M { | ||
let instance = shareds.get(target); | ||
if (!instance) { | ||
const stack = context_unsubs; | ||
context_unsubs = []; | ||
try { | ||
instance = | ||
typeof Class.prototype === 'undefined' | ||
? (Class as (init?: any) => M)(initial_data) | ||
: new (Class as new (init?: any) => M)(initial_data); | ||
} finally { | ||
shared_unsubs.push(...context_unsubs); | ||
context_unsubs = stack; | ||
} | ||
shareds.set(Class, instance); | ||
const h = inst(target, [initial_data]); | ||
instance = h[0]; | ||
shared_unsubs.push(...h[1]); | ||
shareds.set(target, instance); | ||
} | ||
@@ -194,2 +194,54 @@ return instance; | ||
function inst<M, K extends any[]>( | ||
target: (new (...args: K) => M) | ((...args: K) => M), | ||
args: K | ||
): [M, (() => void)[]] { | ||
let instance; | ||
const stack = context_unsubs; | ||
context_unsubs = []; | ||
try { | ||
instance = | ||
typeof target.prototype === 'undefined' | ||
? (target as any)(...args) | ||
: new (target as any)(...args); | ||
} finally { | ||
const unsubs = context_unsubs; | ||
context_unsubs = stack; | ||
return [instance, unsubs]; | ||
} | ||
} | ||
function call_array(arr: (() => void)[]) { | ||
arr.forEach(fn => fn()); | ||
} | ||
function get_scope_context(): Context<any> { | ||
return scope_context ? scope_context : (scope_context = (createContext as any)()); | ||
} | ||
const useScoped = <M>(target: (new (init?: any) => M) | ((init?: any) => M)): M => { | ||
const context_data = useContext(get_scope_context()); | ||
if (!context_data) { | ||
throw new Error('"Scope" parent component didn\'t find'); | ||
} | ||
let instance; | ||
if (context_data[0].has(target)) { | ||
instance = context_data[0].get(target); | ||
} else { | ||
const h = inst(target, [initial_data]); | ||
instance = h[0]; | ||
context_data[1].push(...h[1]); | ||
context_data[0].set(target, instance); | ||
} | ||
return useValue(instance); | ||
}; | ||
const Scope: FC = ({ children }) => { | ||
const h = useMemo(() => [new Map(), []], []) as any; | ||
useEffect(() => () => call_array(h[1]), []); | ||
return createElement(get_scope_context().Provider, { value: h }, children); | ||
}; | ||
function useForceUpdate() { | ||
@@ -217,26 +269,12 @@ return useReducer(() => [], [])[1] as () => void; | ||
function useLocal<T extends unknown[], M>( | ||
Class: (new (...args: T) => M) | ((...args: T) => M), | ||
target: (new (...args: T) => M) | ((...args: T) => M), | ||
deps = [] as T | ||
): M { | ||
if (!Array.isArray(deps)) { | ||
throw new Error('TypeError: deps argument should be an array in "use" call'); | ||
} | ||
const h = useMemo(() => { | ||
let inst, unsubs: (() => void)[]; | ||
const stack = context_unsubs; | ||
context_unsubs = []; | ||
try { | ||
inst = | ||
typeof Class.prototype === 'undefined' | ||
? (Class as any)(...(deps as any)) | ||
: (new (Class as any)(...(deps as any)) as any); | ||
} finally { | ||
unsubs = context_unsubs; | ||
context_unsubs = stack; | ||
} | ||
return [inst, () => () => unsubs.forEach(fn => fn())]; | ||
}, [Class, ...deps]); | ||
const i = inst(target, deps); | ||
return [i[0], () => () => call_array(i[1])] as any; | ||
}, deps); | ||
useEffect(h[1], [h]); | ||
return useValue(h[0]); | ||
return useValue(h[0], [h]); | ||
} | ||
@@ -247,2 +285,3 @@ | ||
const h = useMemo(() => { | ||
if (!target) return [target, () => {}]; | ||
if ((target as any)[0]) target = (target as any)[0]; // box or selector or custom reactive | ||
@@ -264,3 +303,3 @@ | ||
} | ||
}, [target, ...deps]); | ||
}, deps); | ||
@@ -277,3 +316,3 @@ is_observe || useEffect(h[1], [h]); | ||
try { | ||
shared_unsubs.forEach((fn: () => void) => fn()); | ||
call_array(shared_unsubs); | ||
} finally { | ||
@@ -280,0 +319,0 @@ shareds.clear(); |
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
36530
616