value-enhancer
Advanced tools
Comparing version 2.4.0 to 2.4.1
@@ -5,15 +5,2 @@ 'use strict'; | ||
// src/utils.ts | ||
var identity = (value) => value; | ||
var getValue = (val2) => val2.value; | ||
var getValues = (valInputs) => valInputs.map(getValue); | ||
var invoke = (fn, value) => { | ||
try { | ||
fn(value); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}; | ||
var INIT_VALUE = {}; | ||
// src/scheduler.ts | ||
@@ -36,2 +23,15 @@ var nextTick = /* @__PURE__ */ Promise.resolve(); | ||
// src/utils.ts | ||
var identity = (value) => value; | ||
var getValue = (val2) => val2.value; | ||
var getValues = (valInputs) => valInputs.map(getValue); | ||
var invoke = (fn, value) => { | ||
try { | ||
fn(value); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}; | ||
var INIT_VALUE = {}; | ||
// src/subscribers.ts | ||
@@ -284,32 +284,41 @@ var Subscribers = class { | ||
constructor(val2, get = identity, config) { | ||
const getValue2 = () => get(val2.value).value; | ||
super(getValue2(), config, () => { | ||
this._v = getValue2(); | ||
let innerVal; | ||
const getValue2 = () => { | ||
if (!innerVal || this._u.b.size <= 0) { | ||
innerVal = get(val2.value); | ||
} | ||
return innerVal.value; | ||
}; | ||
super(INIT_VALUE, config, () => { | ||
innerVal = get(val2.value); | ||
if (this._v === INIT_VALUE) { | ||
this._v = innerVal.value; | ||
} else { | ||
this._d = this._d || 1; | ||
} | ||
const markDirty = () => { | ||
if (!this._y) { | ||
this._y = true; | ||
if (this._d < 2) { | ||
this._d = 2; | ||
this._u.i(); | ||
} | ||
}; | ||
let innerDisposer = get(val2.value)._c( | ||
markDirty | ||
); | ||
let innerDisposer = innerVal._c(markDirty); | ||
const outerDisposer = val2._c(() => { | ||
innerDisposer && innerDisposer(); | ||
innerDisposer = get(val2.value)._c( | ||
markDirty | ||
); | ||
innerDisposer(); | ||
innerVal = get(val2.value); | ||
innerDisposer = innerVal._c(markDirty); | ||
markDirty(); | ||
}); | ||
return () => { | ||
innerDisposer && innerDisposer(); | ||
innerDisposer(); | ||
outerDisposer(); | ||
}; | ||
}); | ||
this._y = false; | ||
this._g = getValue2; | ||
} | ||
get value() { | ||
if (this._y || this._u.b.size <= 0) { | ||
this._y = false; | ||
if (this._v === INIT_VALUE) { | ||
this._v = this._g(); | ||
this._u.s = true; | ||
} else if (this._d || this._u.b.size <= 0) { | ||
const value = this._g(); | ||
@@ -321,2 +330,3 @@ if (!this.compare(value, this._v)) { | ||
} | ||
this._d = 0; | ||
return this._v; | ||
@@ -327,4 +337,4 @@ } | ||
} | ||
_y; | ||
_g; | ||
_d = 0; | ||
}; | ||
@@ -341,2 +351,3 @@ function unwrap(val2, get, config) { | ||
exports.ReadonlyValImpl = ReadonlyValImpl; | ||
exports.combine = combine; | ||
@@ -343,0 +354,0 @@ exports.derive = derive; |
@@ -35,2 +35,3 @@ interface ReadonlyVal<TValue = any> { | ||
declare type ValDisposer = () => void; | ||
declare type ValOnStart = () => void | ValDisposer | undefined; | ||
interface ValConfig<TValue = any> { | ||
@@ -52,2 +53,64 @@ /** | ||
declare enum SubscriberMode { | ||
Async = 1, | ||
Eager = 2, | ||
Computed = 3 | ||
} | ||
declare class Subscribers<TValue = any> { | ||
constructor(val: ReadonlyVal<TValue>, start?: (() => void | ValDisposer | undefined) | null); | ||
invoke_(): void; | ||
add_(subscriber: ValSubscriber, mode: SubscriberMode): () => void; | ||
remove_(subscriber: ValSubscriber): void; | ||
clear_(): void; | ||
exec_(mode: SubscriberMode): void; | ||
shouldExec_: boolean; | ||
readonly subscribers_: Map<ValSubscriber<TValue>, SubscriberMode>; | ||
private _stop_; | ||
private _getValue_; | ||
private [SubscriberMode.Async]; | ||
private [SubscriberMode.Eager]; | ||
private [SubscriberMode.Computed]; | ||
private readonly _notReadySubscribers_; | ||
private _start_?; | ||
private _startDisposer_?; | ||
} | ||
declare class ReadonlyValImpl<TValue = any> implements ReadonlyVal<TValue> { | ||
protected _subs_: Subscribers<TValue>; | ||
protected _value_: TValue; | ||
protected _set_: (value: TValue) => void; | ||
constructor(value: TValue, { compare, eager }?: ValConfig<TValue>, start?: ValOnStart); | ||
get value(): TValue; | ||
eager: boolean; | ||
compare(newValue: TValue, oldValue: TValue): boolean; | ||
reaction(subscriber: ValSubscriber<TValue>, eager?: boolean): ValDisposer; | ||
subscribe(subscriber: ValSubscriber<TValue>, eager?: boolean): ValDisposer; | ||
/** | ||
* @internal | ||
* For computed vals | ||
*/ | ||
_compute_(subscriber: ValSubscriber<void>): ValDisposer; | ||
unsubscribe(subscriber?: (...args: any[]) => any): void; | ||
/** | ||
* @returns the string representation of `this.value`. | ||
* | ||
* @example | ||
* ```js | ||
* const v$ = val(val(val(1))); | ||
* console.log(`${v$}`); // "1" | ||
* ``` | ||
*/ | ||
toString(): string; | ||
/** | ||
* @returns the JSON representation of `this.value`. | ||
* | ||
* @example | ||
* ```js | ||
* const v$ = val(val(val({ a: 1 }))); | ||
* JSON.stringify(v$); // '{"a":1}' | ||
* ``` | ||
*/ | ||
toJSON(key: string): unknown; | ||
} | ||
/** Returns the value passed in. */ | ||
@@ -168,2 +231,2 @@ declare const identity: <TValue>(value: TValue) => TValue; | ||
export { ReadonlyVal, Val, ValCompare, ValConfig, ValDisposer, ValSubscriber, combine, derive, identity, reaction, setValue, subscribe, unsubscribe, unwrap, val }; | ||
export { ReadonlyVal, ReadonlyValImpl, Val, ValCompare, ValConfig, ValDisposer, ValSubscriber, combine, derive, identity, reaction, setValue, subscribe, unsubscribe, unwrap, val }; |
@@ -5,15 +5,2 @@ 'use strict'; | ||
// src/utils.ts | ||
var identity = (value) => value; | ||
var getValue = (val2) => val2.value; | ||
var getValues = (valInputs) => valInputs.map(getValue); | ||
var invoke = (fn, value) => { | ||
try { | ||
fn(value); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}; | ||
var INIT_VALUE = {}; | ||
// src/scheduler.ts | ||
@@ -36,2 +23,15 @@ var nextTick = /* @__PURE__ */ Promise.resolve(); | ||
// src/utils.ts | ||
var identity = (value) => value; | ||
var getValue = (val2) => val2.value; | ||
var getValues = (valInputs) => valInputs.map(getValue); | ||
var invoke = (fn, value) => { | ||
try { | ||
fn(value); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}; | ||
var INIT_VALUE = {}; | ||
// src/subscribers.ts | ||
@@ -284,32 +284,41 @@ var Subscribers = class { | ||
constructor(val2, get = identity, config) { | ||
const getValue2 = () => get(val2.value).value; | ||
super(getValue2(), config, () => { | ||
this._v = getValue2(); | ||
let innerVal; | ||
const getValue2 = () => { | ||
if (!innerVal || this._u.b.size <= 0) { | ||
innerVal = get(val2.value); | ||
} | ||
return innerVal.value; | ||
}; | ||
super(INIT_VALUE, config, () => { | ||
innerVal = get(val2.value); | ||
if (this._v === INIT_VALUE) { | ||
this._v = innerVal.value; | ||
} else { | ||
this._d = this._d || 1; | ||
} | ||
const markDirty = () => { | ||
if (!this._y) { | ||
this._y = true; | ||
if (this._d < 2) { | ||
this._d = 2; | ||
this._u.i(); | ||
} | ||
}; | ||
let innerDisposer = get(val2.value)._c( | ||
markDirty | ||
); | ||
let innerDisposer = innerVal._c(markDirty); | ||
const outerDisposer = val2._c(() => { | ||
innerDisposer && innerDisposer(); | ||
innerDisposer = get(val2.value)._c( | ||
markDirty | ||
); | ||
innerDisposer(); | ||
innerVal = get(val2.value); | ||
innerDisposer = innerVal._c(markDirty); | ||
markDirty(); | ||
}); | ||
return () => { | ||
innerDisposer && innerDisposer(); | ||
innerDisposer(); | ||
outerDisposer(); | ||
}; | ||
}); | ||
this._y = false; | ||
this._g = getValue2; | ||
} | ||
get value() { | ||
if (this._y || this._u.b.size <= 0) { | ||
this._y = false; | ||
if (this._v === INIT_VALUE) { | ||
this._v = this._g(); | ||
this._u.s = true; | ||
} else if (this._d || this._u.b.size <= 0) { | ||
const value = this._g(); | ||
@@ -321,2 +330,3 @@ if (!this.compare(value, this._v)) { | ||
} | ||
this._d = 0; | ||
return this._v; | ||
@@ -327,4 +337,4 @@ } | ||
} | ||
_y; | ||
_g; | ||
_d = 0; | ||
}; | ||
@@ -341,2 +351,3 @@ function unwrap(val2, get, config) { | ||
exports.ReadonlyValImpl = ReadonlyValImpl; | ||
exports.combine = combine; | ||
@@ -343,0 +354,0 @@ exports.derive = derive; |
{ | ||
"name": "value-enhancer", | ||
"version": "2.4.0", | ||
"version": "2.4.1", | ||
"private": false, | ||
@@ -47,3 +47,3 @@ "description": "A tiny library to enhance value with reactive wrapper.", | ||
"lint": "eslint --ext .ts,.tsx . && prettier --check .", | ||
"test": "jest --coverage", | ||
"test": "jest", | ||
"docs": "typedoc --cname value-enhancer.js.org --includeVersion --excludePrivate --excludeProtected --excludeInternal --out docs src/index.ts", | ||
@@ -50,0 +50,0 @@ "types": "cross-env NODE_ENV=production tsc --declaration --emitDeclarationOnly --jsx react --esModuleInterop --outDir dist", |
@@ -10,2 +10,4 @@ export type { | ||
export { ReadonlyValImpl } from "./readonly-val"; | ||
export { identity } from "./utils"; | ||
@@ -12,0 +14,0 @@ |
import { ReadonlyValImpl } from "./readonly-val"; | ||
import type { ReadonlyVal, Val, ValConfig } from "./typings"; | ||
import { identity } from "./utils"; | ||
import { identity, INIT_VALUE } from "./utils"; | ||
@@ -14,24 +14,34 @@ class UnwrapValImpl<TSrcValue = any, TValue = any> | ||
) { | ||
const getValue = () => get(val.value).value; | ||
let innerVal: ReadonlyVal<TValue> | undefined; | ||
const getValue = () => { | ||
if (!innerVal || this._subs_.subscribers_.size <= 0) { | ||
innerVal = get(val.value); | ||
} | ||
return innerVal.value; | ||
}; | ||
super(getValue(), config, () => { | ||
this._value_ = getValue(); | ||
super(INIT_VALUE, config, () => { | ||
innerVal = get(val.value); | ||
if (this._value_ === INIT_VALUE) { | ||
this._value_ = innerVal.value; | ||
} else { | ||
this._dirtyLevel_ = this._dirtyLevel_ || 1; | ||
} | ||
const markDirty = () => { | ||
if (!this._dirty_) { | ||
this._dirty_ = true; | ||
if (this._dirtyLevel_ < 2) { | ||
this._dirtyLevel_ = 2; | ||
this._subs_.invoke_(); | ||
} | ||
}; | ||
let innerDisposer = (get(val.value) as ReadonlyValImpl)._compute_( | ||
markDirty | ||
); | ||
let innerDisposer = (innerVal as ReadonlyValImpl)._compute_(markDirty); | ||
const outerDisposer = (val as ReadonlyValImpl)._compute_(() => { | ||
innerDisposer && innerDisposer(); | ||
innerDisposer = (get(val.value) as ReadonlyValImpl)._compute_( | ||
markDirty | ||
); | ||
innerDisposer(); | ||
innerVal = get(val.value); | ||
innerDisposer = (innerVal as ReadonlyValImpl)._compute_(markDirty); | ||
markDirty(); | ||
}); | ||
return () => { | ||
innerDisposer && innerDisposer(); | ||
innerDisposer(); | ||
outerDisposer(); | ||
@@ -41,3 +51,2 @@ }; | ||
this._dirty_ = false; | ||
this._getValue_ = getValue; | ||
@@ -47,4 +56,6 @@ } | ||
public override get value(): TValue { | ||
if (this._dirty_ || this._subs_.subscribers_.size <= 0) { | ||
this._dirty_ = false; | ||
if (this._value_ === INIT_VALUE) { | ||
this._value_ = this._getValue_(); | ||
this._subs_.shouldExec_ = true; | ||
} else if (this._dirtyLevel_ || this._subs_.subscribers_.size <= 0) { | ||
const value = this._getValue_(); | ||
@@ -56,2 +67,3 @@ if (!this.compare(value, this._value_)) { | ||
} | ||
this._dirtyLevel_ = 0; | ||
return this._value_; | ||
@@ -65,4 +77,4 @@ } | ||
private _dirty_: boolean; | ||
private _getValue_: () => TValue; | ||
private _dirtyLevel_ = 0; | ||
} | ||
@@ -69,0 +81,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
122024
1931