value-enhancer
Advanced tools
Comparing version
@@ -1,2 +0,142 @@ | ||
export type { ReadonlyVal, Val, ValCompare, ValSubscriber, ValDisposer, ValConfig, } from "./typings"; | ||
export { val, derive, combine, subscribe, reaction, unsubscribe, } from "./value-enhancer"; | ||
interface ReadonlyVal<TValue = any> { | ||
/** value */ | ||
readonly value: TValue; | ||
/** | ||
* Subscribe to value changes without immediate emission. | ||
* @param subscriber | ||
* @param eager by default subscribers will be notified on next tick. set `true` to notify subscribers of value changes synchronously. | ||
* @returns a disposer function that cancels the subscription | ||
*/ | ||
reaction(subscriber: ValSubscriber<TValue>, eager?: boolean): ValDisposer; | ||
/** | ||
* Subscribe to value changes with immediate emission. | ||
* @param subscriber | ||
* @param eager by default subscribers will be notified on next tick. set `true` to notify subscribers of value changes synchronously. | ||
* @returns a disposer function that cancels the subscription | ||
*/ | ||
subscribe(subscriber: ValSubscriber<TValue>, eager?: boolean): ValDisposer; | ||
/** | ||
* Remove the given subscriber. | ||
* Remove all if no subscriber provided. | ||
* @param subscriber | ||
*/ | ||
unsubscribe(subscriber?: (...args: any[]) => any): void; | ||
} | ||
interface Val<TValue = any> extends ReadonlyVal<TValue> { | ||
value: TValue; | ||
/** set new value */ | ||
set(value: TValue): void; | ||
} | ||
declare type ValCompare<TValue = any> = (newValue: TValue, oldValue: TValue) => boolean; | ||
declare type ValSubscriber<TValue = any> = (newValue: TValue) => void; | ||
declare type ValDisposer = () => void; | ||
interface ValConfig<TValue = any> { | ||
/** | ||
* Compare two values. Default `===`. | ||
*/ | ||
compare?: ValCompare<TValue>; | ||
} | ||
declare type TValInputsValueTuple<TValInputs extends readonly ReadonlyVal[]> = Readonly<{ | ||
[K in keyof TValInputs]: ExtractValValue<TValInputs[K]>; | ||
}>; | ||
declare type ExtractValValue<TVal> = TVal extends ReadonlyVal<infer TValue> ? TValue : never; | ||
interface CreateVal { | ||
/** | ||
* Creates a writable val. | ||
* @returns A val with undefined value. | ||
*/ | ||
(): Val<undefined>; | ||
/** | ||
* Creates a writable val. | ||
* @param value Initial value. | ||
* @param config Custom config. | ||
*/ | ||
<TValue = any>(value: TValue, config?: ValConfig<TValue>): Val<TValue>; | ||
} | ||
declare type DeriveValTransform<TValue = any, TDerivedValue = any> = (newValue: TValue) => TDerivedValue; | ||
interface CreateDerive { | ||
/** | ||
* Derive a new val with same value from the given val. | ||
* @param val Input value. | ||
* @returns An readonly val with same value as the input val. | ||
*/ | ||
<TSrcValue = any, TValue = any>(val: ReadonlyVal<TSrcValue>): ReadonlyVal<TValue>; | ||
/** | ||
* Derive a new val with transformed value from the given val. | ||
* @param val Input value. | ||
* @param transform A pure function that takes an input value and returns a new value. | ||
* @param config custom config for the combined val. | ||
* @returns An readonly val with transformed value from the input val. | ||
*/ | ||
<TSrcValue = any, TValue = any>(val: ReadonlyVal<TSrcValue>, transform: DeriveValTransform<TSrcValue, TValue>, config?: ValConfig<TValue>): ReadonlyVal<TValue>; | ||
} | ||
declare type CombineValTransform<TDerivedValue = any, TValues extends readonly any[] = any[], TMeta = any> = (newValues: TValues, oldValues?: TValues, meta?: TMeta) => TDerivedValue; | ||
interface CreateCombine { | ||
/** | ||
* Combines an array of vals into a single val with the array of values. | ||
* @param valInputs An array of vals to combine. | ||
* @returns A readonly val with the combined values. | ||
*/ | ||
<TValInputs extends readonly ReadonlyVal[] = ReadonlyVal[]>(valInputs: readonly [...TValInputs]): ReadonlyVal<[...TValInputsValueTuple<TValInputs>]>; | ||
/** | ||
* Combines an array of vals into a single val with transformed value. | ||
* @param valInputs An array of vals to combine. | ||
* @param transform A pure function that takes an array of values and returns a new value. | ||
* @param config custom config for the combined val. | ||
* @returns A readonly val with the transformed values. | ||
*/ | ||
<TValInputs extends readonly ReadonlyVal[] = ReadonlyVal[], TValue = any>(valInputs: readonly [...TValInputs], transform: CombineValTransform<TValue, [ | ||
...TValInputsValueTuple<TValInputs> | ||
]>, config?: ValConfig<TValue>): ReadonlyVal<TValue>; | ||
} | ||
/** | ||
* Creates a writable val. | ||
* @param value Initial value. | ||
* @param config Custom config. | ||
*/ | ||
declare const val: CreateVal; | ||
/** | ||
* Derive a new val with transformed value from the given val. | ||
* @param val Input value. | ||
* @param transform A pure function that takes an input value and returns a new value. Default identity. | ||
* @param config custom config for the combined val. | ||
* @returns An readonly val with transformed value from the input val. | ||
*/ | ||
declare const derive: CreateDerive; | ||
/** | ||
* Combines an array of vals into a single val with transformed value. | ||
* @param valInputs An array of vals to combine. | ||
* @param transform A pure function that takes an array of values and returns a new value. Default identity. | ||
* @param config custom config for the combined val. | ||
* @returns A readonly val with the transformed values. | ||
*/ | ||
declare const combine: CreateCombine; | ||
/** | ||
* Subscribe to value changes with immediate emission. | ||
* @param val | ||
* @param subscriber | ||
* @param eager by default subscribers will be notified on next tick. set `true` to notify subscribers of value changes synchronously. | ||
* @returns a disposer function that cancels the subscription | ||
*/ | ||
declare const subscribe: <TValue>(val: ReadonlyVal<TValue>, subscriber: ValSubscriber<TValue>, eager?: boolean | undefined) => ValDisposer; | ||
/** | ||
* Subscribe to value changes without immediate emission. | ||
* @param val | ||
* @param subscriber | ||
* @param eager by default subscribers will be notified on next tick. set `true` to notify subscribers of value changes synchronously. | ||
* @returns a disposer function that cancels the subscription | ||
*/ | ||
declare const reaction: <TValue>(val: ReadonlyVal<TValue>, subscriber: ValSubscriber<TValue>, eager?: boolean | undefined) => ValDisposer; | ||
/** | ||
* Remove the given subscriber. | ||
* Remove all if no subscriber provided. | ||
* @param val | ||
* @param subscriber | ||
*/ | ||
declare const unsubscribe: <TValue>(val: ReadonlyVal<TValue>, subscriber?: ((...args: any[]) => any) | undefined) => void; | ||
export { ReadonlyVal, Val, ValCompare, ValConfig, ValDisposer, ValSubscriber, combine, derive, reaction, subscribe, unsubscribe, val }; |
@@ -206,8 +206,13 @@ 'use strict'; | ||
get value() { | ||
if (this._d || this._v === INIT_VALUE || this._u.b.size <= 0) { | ||
if (this._v === INIT_VALUE || this._u.b.size <= 0) { | ||
this._v = this._t(this._l.value); | ||
this._u.s = true; | ||
} else if (this._d) { | ||
const value = this._t(this._l.value); | ||
this._u.s = this._d > 0 && !this._m(value, this._v); | ||
this._v = value; | ||
this._d = 0; | ||
if (!this._m(value, this._v)) { | ||
this._u.s = true; | ||
this._v = value; | ||
} | ||
} | ||
this._d = 0; | ||
return this._v; | ||
@@ -243,8 +248,13 @@ } | ||
get value() { | ||
if (this._d || this._v === INIT_VALUE || this._u.b.size <= 0) { | ||
if (this._v === INIT_VALUE || this._u.b.size <= 0) { | ||
this._v = this._t(getValues(this._l)); | ||
this._u.s = true; | ||
} else if (this._d) { | ||
const value = this._t(getValues(this._l)); | ||
this._u.s = this._d > 0 && !this._m(value, this._v); | ||
this._v = value; | ||
this._d = 0; | ||
if (!this._m(value, this._v)) { | ||
this._u.s = true; | ||
this._v = value; | ||
} | ||
} | ||
this._d = 0; | ||
return this._v; | ||
@@ -251,0 +261,0 @@ } |
{ | ||
"name": "value-enhancer", | ||
"version": "2.0.5", | ||
"version": "2.0.6", | ||
"private": false, | ||
@@ -42,7 +42,7 @@ "description": "A tiny library to enhance value with reactive wrapper.", | ||
"types": "cross-env NODE_ENV=production tsc --declaration --emitDeclarationOnly --jsx react --esModuleInterop --outDir dist", | ||
"build": "cross-env NODE_ENV=production tsup-node src/index.ts && npm run types", | ||
"build": "cross-env NODE_ENV=production tsup-node src/index.ts", | ||
"build:min": "cross-env NODE_ENV=production MINIFY=true tsup-node src/index.ts && node scripts/gzip.mjs", | ||
"build:dev": "cross-env NODE_ENV=development tsup-node src/index.ts && npm run types", | ||
"build:dev": "cross-env NODE_ENV=development tsup-node src/index.ts", | ||
"release": "standard-version" | ||
} | ||
} |
@@ -48,13 +48,13 @@ import { ReadonlyValImpl } from "./readonly-val"; | ||
public override get value(): TValue { | ||
if ( | ||
this._dirtyLevel_ || | ||
this._value_ === INIT_VALUE || | ||
this._subs_.subscribers_.size <= 0 | ||
) { | ||
if (this._value_ === INIT_VALUE || this._subs_.subscribers_.size <= 0) { | ||
this._value_ = this._transform_(getValues(this._sVal_)); | ||
this._subs_.shouldExec_ = true; | ||
} else if (this._dirtyLevel_) { | ||
const value = this._transform_(getValues(this._sVal_)); | ||
this._subs_.shouldExec_ = | ||
this._dirtyLevel_ > 0 && !this._compare_(value, this._value_); | ||
this._value_ = value; | ||
this._dirtyLevel_ = 0; | ||
if (!this._compare_(value, this._value_)) { | ||
this._subs_.shouldExec_ = true; | ||
this._value_ = value; | ||
} | ||
} | ||
this._dirtyLevel_ = 0; | ||
return this._value_; | ||
@@ -61,0 +61,0 @@ } |
@@ -37,13 +37,13 @@ import { ReadonlyValImpl } from "./readonly-val"; | ||
public override get value(): TValue { | ||
if ( | ||
this._dirtyLevel_ || | ||
this._value_ === INIT_VALUE || | ||
this._subs_.subscribers_.size <= 0 | ||
) { | ||
if (this._value_ === INIT_VALUE || this._subs_.subscribers_.size <= 0) { | ||
this._value_ = this._transform_(this._sVal_.value); | ||
this._subs_.shouldExec_ = true; | ||
} else if (this._dirtyLevel_) { | ||
const value = this._transform_(this._sVal_.value); | ||
this._subs_.shouldExec_ = | ||
this._dirtyLevel_ > 0 && !this._compare_(value, this._value_); | ||
this._value_ = value; | ||
this._dirtyLevel_ = 0; | ||
if (!this._compare_(value, this._value_)) { | ||
this._subs_.shouldExec_ = true; | ||
this._value_ = value; | ||
} | ||
} | ||
this._dirtyLevel_ = 0; | ||
return this._value_; | ||
@@ -50,0 +50,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
94083
-3.21%18
-33.33%1223
-5.19%