value-enhancer
Advanced tools
Comparing version 4.0.2 to 4.0.3
@@ -1,2 +0,2 @@ | ||
import { R as ReadonlyVal } from './typings-8a4e5768.js'; | ||
import { R as ReadonlyVal } from './typings-a4456a48.js'; | ||
@@ -3,0 +3,0 @@ /** |
@@ -1,4 +0,139 @@ | ||
import { R as ReadonlyVal, V as ValInputsValueTuple, a as ValConfig, F as FlattenVal, b as ValDisposer, c as ValSetValue, d as Val, e as ValSubscriber } from './typings-8a4e5768.js'; | ||
export { f as ValEqual } from './typings-8a4e5768.js'; | ||
import { V as ValDisposer, a as ValSubscriber, R as ReadonlyVal, b as ValConfig, c as ValSetValue, d as ValInputsValueTuple, F as FlattenVal, e as Val } from './typings-a4456a48.js'; | ||
export { f as ValEqual } from './typings-a4456a48.js'; | ||
declare enum SubscriberMode { | ||
Async = 1, | ||
Eager = 2, | ||
Computed = 3 | ||
} | ||
/** | ||
* A function that is called when a val get its first subscriber. | ||
* The returned disposer will be called when the last subscriber unsubscribed from the val. | ||
*/ | ||
type ValOnStart = (subs: Subscribers) => void | ValDisposer | undefined; | ||
/** | ||
* Manage subscribers for a val. | ||
*/ | ||
declare class Subscribers<TValue = any> implements Subscribers { | ||
#private; | ||
constructor(getValue: () => TValue, start?: ValOnStart | null); | ||
dirty: boolean; | ||
notify(): void; | ||
add(subscriber: ValSubscriber, mode: SubscriberMode): () => void; | ||
remove(subscriber: ValSubscriber): void; | ||
clear(): void; | ||
exec(mode: SubscriberMode): void; | ||
readonly subs: Map<ValSubscriber<TValue>, SubscriberMode>; | ||
private [SubscriberMode.Async]; | ||
private [SubscriberMode.Eager]; | ||
private [SubscriberMode.Computed]; | ||
} | ||
/** | ||
* Bare minimum implementation of a readonly val. | ||
* Generally, you should use `readonlyVal` and `ReadonlyVal` instead of this class. | ||
*/ | ||
declare class ReadonlyValImpl<TValue = any> implements ReadonlyVal<TValue> { | ||
#private; | ||
/** | ||
* Manage subscribers for a val. | ||
*/ | ||
protected _subs: Subscribers<TValue>; | ||
/** | ||
* @param get A pure function that returns the current value of the val. | ||
* @param config Custom config for the val. | ||
* @param start A function that is called when a val get its first subscriber. | ||
* The returned disposer will be called when the last subscriber unsubscribed from the val. | ||
*/ | ||
constructor(get: () => TValue, { equal, eager }?: ValConfig<TValue>, start?: ValOnStart); | ||
get value(): TValue; | ||
get: (this: void) => TValue; | ||
$equal?: (this: void, newValue: TValue, oldValue: TValue) => boolean; | ||
reaction(subscriber: ValSubscriber<TValue>, eager?: boolean | undefined): ValDisposer; | ||
subscribe(subscriber: ValSubscriber<TValue>, eager?: boolean | undefined): ValDisposer; | ||
$valCompute(subscriber: ValSubscriber<void>): ValDisposer; | ||
unsubscribe(subscriber?: (...args: any[]) => any): void; | ||
dispose(): 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; | ||
} | ||
/** | ||
* Creates a readonly val with the given value. | ||
* | ||
* @returns A tuple with the readonly val and a function to set the value. | ||
*/ | ||
declare function readonlyVal<TValue = undefined>(): [ | ||
ReadonlyVal<TValue | undefined>, | ||
ValSetValue<TValue | undefined> | ||
]; | ||
/** | ||
* Creates a readonly val with the given value. | ||
* | ||
* @param value Value for the val | ||
* @param config Custom config for the val. | ||
* @returns A tuple with the readonly val and a function to set the value. | ||
*/ | ||
declare function readonlyVal<TValue = any>(value: TValue, config?: ValConfig<TValue>): [ReadonlyVal<TValue>, ValSetValue<TValue>]; | ||
/** | ||
* Takes an object of key-value pairs containing `ReadonlyVal` instances and their corresponding `ValSetValue` functions, | ||
* and returns a tuple containing an array of the `ReadonlyVal` instances and a function to set their values. | ||
* | ||
* @example | ||
* ```ts | ||
* const [vals, setVals] = groupVals({ | ||
* a: readonlyVal(1), | ||
* b: readonlyVal(2), | ||
* c: readonlyVal(3), | ||
* }); | ||
* | ||
* vals.a.value; // 1 | ||
* | ||
* setVals.a(2); | ||
* ``` | ||
* | ||
* This is useful for classes that have multiple `ReadonlyVal` instances as properties. | ||
* | ||
* ```ts | ||
* export interface Foo$ { | ||
* a: ReadonlyVal<number>; | ||
* b: ReadonlyVal<number>; | ||
* c: ReadonlyVal<number>; | ||
* } | ||
* | ||
* export class Foo { | ||
* public $: Foo$; | ||
* private setVals: { [K in keyof Foo$]: ValSetValue<FlattenVal<Foo$[K]>> }; | ||
* | ||
* public constructor() { | ||
* const [vals, setVals] = groupVals({ | ||
* a: readonlyVal(1), | ||
* b: readonlyVal(2), | ||
* c: readonlyVal(3), | ||
* }); | ||
* | ||
* this.$ = vals; | ||
* this.setVals = setVals; | ||
* } | ||
* ``` | ||
*/ | ||
declare const groupVals: <TValues extends {}>(valPairs: { [K in keyof TValues]: [ReadonlyVal<TValues[K]>, ValSetValue<TValues[K]>]; }) => [{ [K_1 in keyof TValues]: ReadonlyVal<TValues[K_1]>; }, { [K_2 in keyof TValues]: ValSetValue<TValues[K_2]>; }]; | ||
/** Returns the value passed in. */ | ||
@@ -11,3 +146,3 @@ declare const identity: <TValue>(value: TValue) => TValue; | ||
*/ | ||
declare const isVal: <T>(val: T) => val is T extends ReadonlyVal<any> ? T : never; | ||
declare const isVal: <T>(val: T) => val is T extends ReadonlyValImpl<any> ? T : never; | ||
@@ -130,63 +265,2 @@ type CombineValTransform<TCombinedValue = any, TValues extends readonly any[] = any[]> = (newValues: TValues) => TCombinedValue; | ||
/** | ||
* Creates a readonly val with the given value. | ||
* | ||
* @returns A tuple with the readonly val and a function to set the value. | ||
*/ | ||
declare function readonlyVal<TValue = undefined>(): [ | ||
ReadonlyVal<TValue | undefined>, | ||
ValSetValue<TValue | undefined> | ||
]; | ||
/** | ||
* Creates a readonly val with the given value. | ||
* | ||
* @param value Value for the val | ||
* @param config Custom config for the val. | ||
* @returns A tuple with the readonly val and a function to set the value. | ||
*/ | ||
declare function readonlyVal<TValue = any>(value: TValue, config?: ValConfig<TValue>): [ReadonlyVal<TValue>, ValSetValue<TValue>]; | ||
/** | ||
* Takes an object of key-value pairs containing `ReadonlyVal` instances and their corresponding `ValSetValue` functions, | ||
* and returns a tuple containing an array of the `ReadonlyVal` instances and a function to set their values. | ||
* | ||
* @example | ||
* ```ts | ||
* const [vals, setVals] = groupVals({ | ||
* a: readonlyVal(1), | ||
* b: readonlyVal(2), | ||
* c: readonlyVal(3), | ||
* }); | ||
* | ||
* vals.a.value; // 1 | ||
* | ||
* setVals.a(2); | ||
* ``` | ||
* | ||
* This is useful for classes that have multiple `ReadonlyVal` instances as properties. | ||
* | ||
* ```ts | ||
* export interface Foo$ { | ||
* a: ReadonlyVal<number>; | ||
* b: ReadonlyVal<number>; | ||
* c: ReadonlyVal<number>; | ||
* } | ||
* | ||
* export class Foo { | ||
* public $: Foo$; | ||
* private setVals: { [K in keyof Foo$]: ValSetValue<FlattenVal<Foo$[K]>> }; | ||
* | ||
* public constructor() { | ||
* const [vals, setVals] = groupVals({ | ||
* a: readonlyVal(1), | ||
* b: readonlyVal(2), | ||
* c: readonlyVal(3), | ||
* }); | ||
* | ||
* this.$ = vals; | ||
* this.setVals = setVals; | ||
* } | ||
* ``` | ||
*/ | ||
declare const groupVals: <TValues extends {}>(valPairs: { [K in keyof TValues]: [ReadonlyVal<TValues[K]>, ValSetValue<TValues[K]>]; }) => [{ [K_1 in keyof TValues]: ReadonlyVal<TValues[K_1]>; }, { [K_2 in keyof TValues]: ValSetValue<TValues[K_2]>; }]; | ||
/** | ||
* Creates a writable val. | ||
@@ -233,2 +307,2 @@ * @returns A val with undefined value. | ||
export { CombineValTransform, DerivedValTransform, FlattenVal, ReadonlyVal, Val, ValConfig, ValDisposer, ValSetValue, ValSubscriber, combine, derive, flatten, flattenFrom, from, groupVals, identity, isVal, reaction, readonlyVal, setValue, subscribe, unsubscribe, val }; | ||
export { CombineValTransform, DerivedValTransform, FlattenVal, ReadonlyVal, ReadonlyValImpl, Val, ValConfig, ValDisposer, ValSetValue, ValSubscriber, combine, derive, flatten, flattenFrom, from, groupVals, identity, isVal, reaction, readonlyVal, setValue, subscribe, unsubscribe, val }; |
{ | ||
"name": "value-enhancer", | ||
"version": "4.0.2", | ||
"version": "4.0.3", | ||
"private": false, | ||
@@ -5,0 +5,0 @@ "description": "A tiny library to enhance value with reactive wrapper.", |
@@ -0,1 +1,2 @@ | ||
import type { ReadonlyValImpl } from "./readonly-val"; | ||
import type { ReadonlyVal, ValConfig, ValInputsValueTuple } from "./typings"; | ||
@@ -43,3 +44,3 @@ | ||
export function combine< | ||
TValInputs extends readonly ReadonlyVal[] = ReadonlyVal[], | ||
TValInputs extends readonly ReadonlyValImpl[] = ReadonlyValImpl[], | ||
TValue = any | ||
@@ -46,0 +47,0 @@ >( |
@@ -0,1 +1,2 @@ | ||
import type { ReadonlyValImpl } from "./readonly-val"; | ||
import type { ReadonlyVal, ValConfig } from "./typings"; | ||
@@ -30,4 +31,8 @@ | ||
): ReadonlyVal<TValue>; | ||
export function derive<TSrcValue = any, TValue = any>( | ||
val: ReadonlyVal<TSrcValue>, | ||
export function derive< | ||
TSrcValue = any, | ||
TValue = any, | ||
TSrcVal extends ReadonlyValImpl<TSrcValue> = ReadonlyValImpl | ||
>( | ||
val: TSrcVal, | ||
transform: DerivedValTransform< | ||
@@ -34,0 +39,0 @@ TSrcValue, |
@@ -27,3 +27,3 @@ import type { | ||
let innerMaybeVal: TValOrValue | undefined; | ||
let innerVal: ReadonlyVal<TValue> | undefined | null; | ||
let innerVal: ReadonlyValImpl<TValue> | undefined | null; | ||
let innerDisposer: ValDisposer | undefined | null; | ||
@@ -30,0 +30,0 @@ |
@@ -0,1 +1,2 @@ | ||
import type { ReadonlyValImpl } from "./readonly-val"; | ||
import type { FlattenVal, ReadonlyVal, ValConfig } from "./typings"; | ||
@@ -49,4 +50,8 @@ | ||
): ReadonlyVal<FlattenVal<TValOrValue>>; | ||
export function flatten<TSrcValue = any, TValOrValue = any>( | ||
val: ReadonlyVal<TSrcValue>, | ||
export function flatten< | ||
TSrcValue = any, | ||
TValOrValue = any, | ||
TSrcVal extends ReadonlyValImpl = ReadonlyValImpl<TSrcValue> | ||
>( | ||
val: TSrcVal, | ||
get: (value: TSrcValue) => TValOrValue = identity as any, | ||
@@ -53,0 +58,0 @@ config?: ValConfig<FlattenVal<TValOrValue>> |
@@ -19,5 +19,5 @@ export type { | ||
export { from } from "./from"; | ||
export { groupVals, readonlyVal } from "./readonly-val"; | ||
export { groupVals, readonlyVal, type ReadonlyValImpl } from "./readonly-val"; | ||
export { val } from "./val"; | ||
export { reaction, setValue, subscribe, unsubscribe } from "./value-enhancer"; |
@@ -15,2 +15,3 @@ import type { ValOnStart } from "./subscribers"; | ||
* Bare minimum implementation of a readonly val. | ||
* Generally, you should use `readonlyVal` and `ReadonlyVal` instead of this class. | ||
*/ | ||
@@ -17,0 +18,0 @@ export class ReadonlyValImpl<TValue = any> implements ReadonlyVal<TValue> { |
@@ -0,1 +1,4 @@ | ||
/** | ||
* A ReadonlyVal contains a readonly `value` and does not have a `set` method. | ||
*/ | ||
export interface ReadonlyVal<TValue = any> { | ||
@@ -6,4 +9,2 @@ /** Current value of the val */ | ||
get(this: void): TValue; | ||
/** Compare two values. Default `Object.is`. */ | ||
$equal?: (this: void, newValue: TValue, oldValue: TValue) => boolean; | ||
/** | ||
@@ -24,8 +25,2 @@ * Subscribe to value changes without immediate emission. | ||
/** | ||
* Subscribe to value changes and get invoked before {@link ReadonlyVal#subscribe} and {@link ReadonlyVal#reaction}. | ||
* @param subscriber | ||
* @returns a disposer function that cancels the subscription | ||
*/ | ||
$valCompute(subscriber: ValSubscriber<void>): ValDisposer; | ||
/** | ||
* Remove the given subscriber. | ||
@@ -42,2 +37,5 @@ * Remove all if no subscriber provided. | ||
/** | ||
* A Val contains a writable `value` property and a `set` method. | ||
*/ | ||
export interface Val<TValue = any> extends ReadonlyVal<TValue> { | ||
@@ -44,0 +42,0 @@ /** Current value of the val */ |
@@ -0,1 +1,2 @@ | ||
import type { ReadonlyValImpl } from "./readonly-val"; | ||
import type { ReadonlyVal, ValInputsValueTuple } from "./typings"; | ||
@@ -9,3 +10,3 @@ | ||
export const valInputsEqual = < | ||
TValInputs extends readonly ReadonlyVal[] = ReadonlyVal[] | ||
TValInputs extends readonly ReadonlyValImpl[] = ReadonlyValImpl[] | ||
>( | ||
@@ -52,3 +53,5 @@ valInputs: readonly [...TValInputs], | ||
*/ | ||
export const isVal = <T>(val: T): val is T extends ReadonlyVal ? T : never => | ||
!!(val as ReadonlyVal | undefined)?.$valCompute; | ||
export const isVal = <T>( | ||
val: T | ||
): val is T extends ReadonlyValImpl ? T : never => | ||
!!(val as ReadonlyValImpl | undefined)?.$valCompute; |
132152
3894