Comparing version 0.4.6 to 0.4.7
import { FC } from 'react'; | ||
import { expr, box, sel, transaction } from 'reactive-box'; | ||
export { action, prop, cache, on, cycle, effect, shared, initial, observe, useValue, useLocal, useShared, free, mock, box, sel, expr, transaction, }; | ||
declare function action<T = void>(init?: T): { | ||
(data?: T): void; | ||
export { prop, cache, action, on, cycle, effect, shared, initial, observe, useValue, useLocal, useShared, free, mock, box, sel, expr, transaction, Ensurable, }; | ||
declare type Ensurable<T> = T | void; | ||
declare function action<T = undefined>(init?: T): { | ||
(data: T): void; | ||
(): void; | ||
0: () => T | undefined; | ||
0: () => Ensurable<T>; | ||
}; | ||
declare function prop(_proto: any, key: any, descriptor?: any): any; | ||
declare function cache(_proto: any, key: any, descriptor: any): any; | ||
declare function on<T>(target: (() => T) | { | ||
declare function on<T>(target: { | ||
0: () => Ensurable<T>; | ||
} | [() => Ensurable<T>] | (() => Ensurable<T>), listener: (value: T, prev?: T) => void): () => void; | ||
declare function on<T>(target: { | ||
0: () => T; | ||
} | [() => T], listener: (value: T, prev?: T) => void): () => void; | ||
} | [() => T] | (() => T), listener: (value: T, prev?: T) => void): () => void; | ||
declare function effect(fn: () => void): void; | ||
@@ -24,3 +26,3 @@ declare function effect(fn: () => () => any): () => any; | ||
0: () => T; | ||
} | [() => T]): T; | ||
} | [() => T], deps?: any[]): T; | ||
declare function useShared<T>(target: () => (() => T) | { | ||
@@ -30,1 +32,3 @@ 0: () => T; | ||
declare function free(): void; | ||
declare function prop(_proto: any, key: any, descriptor?: any): any; | ||
declare function cache(_proto: any, key: any, descriptor: any): any; |
"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.cache = exports.prop = exports.action = void 0; | ||
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; | ||
const react_1 = require("react"); | ||
@@ -23,3 +23,3 @@ const reactive_box_1 = require("reactive-box"); | ||
}; | ||
fn[0] = () => get()[0]; | ||
fn[0] = (() => get()[0]); | ||
promisify(); | ||
@@ -35,29 +35,2 @@ function promisify() { | ||
exports.action = action; | ||
function boxProperty(target, key, initializer) { | ||
const [get, set] = reactive_box_1.box(initializer && initializer()); | ||
Object.defineProperty(target, key, { get, set }); | ||
} | ||
function prop(_proto, key, descriptor) { | ||
return { | ||
get() { | ||
boxProperty(this, key, descriptor === null || descriptor === void 0 ? void 0 : descriptor.initializer); | ||
return this[key]; | ||
}, | ||
set(value) { | ||
boxProperty(this, key, descriptor === null || descriptor === void 0 ? void 0 : descriptor.initializer); | ||
this[key] = value; | ||
}, | ||
}; | ||
} | ||
exports.prop = prop; | ||
function cache(_proto, key, descriptor) { | ||
return { | ||
get() { | ||
const [get] = reactive_box_1.sel(descriptor.get); | ||
Object.defineProperty(this, key, { get }); | ||
return this[key]; | ||
}, | ||
}; | ||
} | ||
exports.cache = cache; | ||
function on(target, listener) { | ||
@@ -166,11 +139,10 @@ let free; | ||
return [inst, () => () => unsubs.forEach(fn => fn())]; | ||
}, deps); | ||
react_1.useEffect(h[1], deps); | ||
return h[0]; | ||
}, [Class, ...deps]); | ||
react_1.useEffect(h[1], [h]); | ||
return useValue(h[0]); | ||
} | ||
exports.useLocal = useLocal; | ||
function useValue(target) { | ||
function useValue(target, deps = []) { | ||
const forceUpdate = useForceUpdate(); | ||
const ref = react_1.useRef(); | ||
if (!ref.current) { | ||
const h = react_1.useMemo(() => { | ||
if (target[0]) | ||
@@ -184,11 +156,10 @@ target = target[0]; // box or selector or custom reactive | ||
run(); | ||
ref.current = [stop, target]; | ||
return [target, () => stop, 1]; | ||
} | ||
else { | ||
ref.current = [void 0, target]; | ||
return [target, () => { }]; | ||
} | ||
} | ||
const cur = ref.current; | ||
react_1.useEffect(() => cur[0], []); | ||
return cur[0] ? cur[1]() : cur[1]; | ||
}, [target, ...deps]); | ||
react_1.useEffect(h[1], [h]); | ||
return h[2] ? h[0]() : h[0]; | ||
} | ||
@@ -210,2 +181,29 @@ exports.useValue = useValue; | ||
exports.free = free; | ||
function boxProperty(target, key, initializer) { | ||
const [get, set] = reactive_box_1.box(initializer && initializer()); | ||
Object.defineProperty(target, key, { get, set }); | ||
} | ||
function prop(_proto, key, descriptor) { | ||
return { | ||
get() { | ||
boxProperty(this, key, descriptor === null || descriptor === void 0 ? void 0 : descriptor.initializer); | ||
return this[key]; | ||
}, | ||
set(value) { | ||
boxProperty(this, key, descriptor === null || descriptor === void 0 ? void 0 : descriptor.initializer); | ||
this[key] = value; | ||
}, | ||
}; | ||
} | ||
exports.prop = prop; | ||
function cache(_proto, key, descriptor) { | ||
return { | ||
get() { | ||
const [get] = reactive_box_1.sel(descriptor.get); | ||
Object.defineProperty(this, key, { get }); | ||
return this[key]; | ||
}, | ||
}; | ||
} | ||
exports.cache = cache; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "realar", | ||
"version": "0.4.6", | ||
"version": "0.4.7", | ||
"description": "React state manager", | ||
@@ -91,3 +91,3 @@ "repository": { | ||
}, | ||
"gitHead": "60c507b53010e2af3fa1e7ba307d7e8f608d3e2d" | ||
"gitHead": "c236de18db19744004b8ef12c925982602abbd5a" | ||
} |
115
src/index.ts
@@ -11,5 +11,5 @@ import { useRef, useReducer, useEffect, useMemo, FC } from 'react'; | ||
export { | ||
action, | ||
prop, | ||
cache, | ||
action, | ||
on, | ||
@@ -30,10 +30,13 @@ cycle, | ||
transaction, | ||
Ensurable, | ||
}; | ||
function action<T = void>( | ||
type Ensurable<T> = T | void; | ||
function action<T = undefined>( | ||
init?: T | ||
): { | ||
(data?: T): void; | ||
(data: T): void; | ||
(): void; | ||
0: () => T | undefined; | ||
0: () => Ensurable<T>; | ||
} { | ||
@@ -50,3 +53,3 @@ let resolve: (v: T) => void; | ||
fn[0] = () => get()[0]; | ||
fn[0] = (() => get()[0]) as () => Ensurable<T>; | ||
@@ -65,50 +68,27 @@ promisify(); | ||
function boxProperty(target: any, key: any, initializer: any) { | ||
const [get, set] = box(initializer && initializer()); | ||
Object.defineProperty(target, key, { get, set }); | ||
} | ||
function prop(_proto: any, key: any, descriptor?: any): any { | ||
return { | ||
get() { | ||
boxProperty(this, key, descriptor?.initializer); | ||
return this[key]; | ||
}, | ||
set(value: any) { | ||
boxProperty(this, key, descriptor?.initializer); | ||
this[key] = value; | ||
}, | ||
}; | ||
} | ||
function cache(_proto: any, key: any, descriptor: any): any { | ||
return { | ||
get() { | ||
const [get] = sel(descriptor.get); | ||
Object.defineProperty(this, key, { get }); | ||
return this[key]; | ||
}, | ||
}; | ||
} | ||
function on<T>( | ||
target: (() => T) | { 0: () => T } | [() => T], | ||
target: { 0: () => Ensurable<T> } | [() => Ensurable<T>] | (() => Ensurable<T>), | ||
listener: (value: T, prev?: T) => void | ||
) { | ||
): () => void; | ||
function on<T>( | ||
target: { 0: () => T } | [() => T] | (() => T), | ||
listener: (value: T, prev?: T) => void | ||
): () => void; | ||
function on(target: any, listener: (value: any, prev?: any) => void): () => void { | ||
let free: (() => void) | undefined; | ||
if (!target) return; | ||
else if ((target as any)[0]) { | ||
target = (target as any)[0]; // box or selector or custom reactive | ||
else if (target[0]) { | ||
target = target[0]; // box or selector or custom reactive | ||
} else { | ||
[target, free] = sel(target as any); | ||
[target, free] = sel(target); | ||
} | ||
let value: T; | ||
let value: any; | ||
const [run, stop] = expr(target as any, () => { | ||
const [run, stop] = expr(target, () => { | ||
const prev = value; | ||
listener((value = run() as any), prev); | ||
listener((value = run()), prev); | ||
}); | ||
value = run() as any; | ||
value = run(); | ||
const unsub = () => { | ||
@@ -201,12 +181,11 @@ if (free) free(); | ||
return [inst, () => () => unsubs.forEach(fn => fn())]; | ||
}, deps); | ||
}, [Class, ...deps]); | ||
useEffect(h[1], deps); | ||
return h[0]; | ||
useEffect(h[1], [h]); | ||
return useValue(h[0]); | ||
} | ||
function useValue<T>(target: (() => T) | { 0: () => T } | [() => T]): T { | ||
function useValue<T>(target: (() => T) | { 0: () => T } | [() => T], deps: any[] = []): T { | ||
const forceUpdate = useForceUpdate(); | ||
const ref = useRef<[() => void, any, any?]>(); | ||
if (!ref.current) { | ||
const h = useMemo(() => { | ||
if ((target as any)[0]) target = (target as any)[0]; // box or selector or custom reactive | ||
@@ -220,10 +199,10 @@ | ||
run(); | ||
ref.current = [stop, target]; | ||
return [target, () => stop, 1]; | ||
} else { | ||
ref.current = [void 0, target]; | ||
return [target as any, () => {}]; | ||
} | ||
} | ||
const cur = ref.current; | ||
useEffect(() => cur[0], []); | ||
return cur[0] ? cur[1]() : cur[1]; | ||
}, [target, ...deps]); | ||
useEffect(h[1], [h]); | ||
return h[2] ? h[0]() : h[0]; | ||
} | ||
@@ -243,1 +222,29 @@ | ||
} | ||
function boxProperty(target: any, key: any, initializer: any) { | ||
const [get, set] = box(initializer && initializer()); | ||
Object.defineProperty(target, key, { get, set }); | ||
} | ||
function prop(_proto: any, key: any, descriptor?: any): any { | ||
return { | ||
get() { | ||
boxProperty(this, key, descriptor?.initializer); | ||
return this[key]; | ||
}, | ||
set(value: any) { | ||
boxProperty(this, key, descriptor?.initializer); | ||
this[key] = value; | ||
}, | ||
}; | ||
} | ||
function cache(_proto: any, key: any, descriptor: any): any { | ||
return { | ||
get() { | ||
const [get] = sel(descriptor.get); | ||
Object.defineProperty(this, key, { get }); | ||
return this[key]; | ||
}, | ||
}; | ||
} |
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
26496
448