value-enhancer
Advanced tools
Comparing version 4.1.0 to 4.1.1
@@ -1,2 +0,2 @@ | ||
import { R as ReadonlyVal } from './typings-a4456a48.js'; | ||
import { R as ReadonlyVal } from './typings-dd45eb69.js'; | ||
@@ -3,0 +3,0 @@ /** |
@@ -1,79 +0,5 @@ | ||
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'; | ||
import { R as ReadonlyVal, V as ValSetValue, a as ValConfig, b as ValInputsValueTuple, F as FlattenVal, c as ValDisposer, d as Val, e as ValSubscriber } from './typings-dd45eb69.js'; | ||
export { f as ValEqual } from './typings-dd45eb69.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. | ||
@@ -146,3 +72,9 @@ * | ||
*/ | ||
declare const isVal: <T>(val: T) => val is T extends ReadonlyValImpl<any> ? T : never; | ||
declare function isVal<T extends ReadonlyVal>(val: T): val is T extends ReadonlyVal ? T : never; | ||
/** | ||
* Checks if `val` is `ReadonlyVal` or `Val`. | ||
* | ||
* @returns `true` if `val` is `ReadonlyVal` or `Val`. | ||
*/ | ||
declare function isVal(val: unknown): val is ReadonlyVal; | ||
@@ -306,2 +238,2 @@ type CombineValTransform<TCombinedValue = any, TValues extends readonly any[] = any[]> = (newValues: TValues) => TCombinedValue; | ||
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 }; | ||
export { CombineValTransform, DerivedValTransform, FlattenVal, ReadonlyVal, Val, ValConfig, ValDisposer, ValSetValue, ValSubscriber, combine, derive, flatten, flattenFrom, from, groupVals, identity, isVal, reaction, readonlyVal, setValue, subscribe, unsubscribe, val }; |
@@ -27,3 +27,5 @@ 'use strict'; | ||
var INIT_VALUE = {}; | ||
var isVal = (val2) => !!val2?.$valCompute; | ||
function isVal(val2) { | ||
return !!(val2 && val2.$valCompute); | ||
} | ||
@@ -30,0 +32,0 @@ // src/scheduler.ts |
{ | ||
"name": "value-enhancer", | ||
"version": "4.1.0", | ||
"version": "4.1.1", | ||
"private": false, | ||
@@ -5,0 +5,0 @@ "description": "A tiny library to enhance value with reactive wrapper.", |
@@ -55,3 +55,5 @@ import type { | ||
innerMaybeVal = maybeVal; | ||
innerVal = isVal(maybeVal) ? maybeVal : null; | ||
innerVal = isVal(maybeVal) | ||
? (maybeVal as unknown as ReadonlyValImpl<TValue>) | ||
: null; | ||
innerDisposer?.(); | ||
@@ -58,0 +60,0 @@ innerDisposer = innerVal && innerVal.$valCompute(notify); |
@@ -19,5 +19,5 @@ export type { | ||
export { from } from "./from"; | ||
export { groupVals, readonlyVal, type ReadonlyValImpl } from "./readonly-val"; | ||
export { groupVals, readonlyVal } from "./readonly-val"; | ||
export { val } from "./val"; | ||
export { reaction, setValue, subscribe, unsubscribe } from "./value-enhancer"; |
@@ -52,5 +52,13 @@ import type { ReadonlyValImpl } from "./readonly-val"; | ||
*/ | ||
export const isVal = <T>( | ||
export function isVal<T extends ReadonlyVal>( | ||
val: T | ||
): val is T extends ReadonlyValImpl ? T : never => | ||
!!(val as ReadonlyValImpl | undefined)?.$valCompute; | ||
): val is T extends ReadonlyVal ? T : never; | ||
/** | ||
* Checks if `val` is `ReadonlyVal` or `Val`. | ||
* | ||
* @returns `true` if `val` is `ReadonlyVal` or `Val`. | ||
*/ | ||
export function isVal(val: unknown): val is ReadonlyVal; | ||
export function isVal(val: unknown): val is ReadonlyVal { | ||
return !!(val && (val as any).$valCompute); | ||
} |
Sorry, the diff of this file is not supported yet
133211
3957