Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | import { type BRAND } from "./utils"; /** * Custom config for the Readable/Writable. */ export interface Config<TValue = any> { /** * Compare two values. Default `Object.is`. * `false` to disable equality check. */ readonly equal?: Equal<TValue> | false; /** * Name for debugging. */ readonly name?: string; /** * A callback invoked when a value is needed to be disposed. * * A value is considered for disposal when: * - it is replaced by another value (a new value is set). * - the Readable is disposed. * * @param oldValue The value that is needed to be disposed. */ readonly onDisposeValue?: (oldValue: TValue) => void; } export type Disposer = () => void; export type Equal<TValue = any> = (newValue: TValue, oldValue: TValue) => boolean; export interface Get { <T = any>($: ReadableLike<T>): T; <T = any, U = any>($: ReadableLike<T> | U): T | U; } /** * A Readable is a reactive value that can be read and subscribed to. */ export interface Readable<TValue = any> { readonly name?: string; /** * A version representation of the value. * If two versions of a $ is not equal(`Object.is`), it means the `value` has changed (event if the `value` is equal). */ readonly $version: Version; /** * @internal */ readonly [BRAND]: BRAND; /** * @internal */ deps_?: Map<Readable, Version>; /** * Current value of the $. */ readonly value: TValue; /** * Get current value. */ get: () => TValue; /** @internal */ onReaction_(subscriber: Subscriber<TValue>): void; /** * Subscribe to value changes without immediate emission. * @param subscriber * @returns a disposer function that cancels the subscription */ reaction(subscriber: Subscriber<TValue>): Disposer; /** * Subscribe to value changes with immediate emission. * @param subscriber * @returns a disposer function that cancels the subscription */ subscribe(subscriber: Subscriber<TValue>): Disposer; /** * Remove the given subscriber or all subscribers if no subscriber is provided. * @param subscriber Optional subscriber function to remove. * If not provided, all subscribers will be removed. */ unsubscribe(subscriber?: (...args: any[]) => any): void; } export interface OwnedReadable<TValue = any> extends Readable<TValue> { /** * Remove all subscribers and lock. */ dispose(): void; } export interface ReadableProvider<TValue = any> { readonly $: Readable<TValue>; } export type ReadableLike<TValue = any> = Readable<TValue> | ReadableProvider<TValue>; export type SetValue<TValue = any> = (value: TValue) => void; export type Subscriber<TValue = any> = (newValue: TValue) => void; export type Unwrap<T> = T extends ReadableLike<infer TValue> ? TValue : T; export type Version = number; /** * A Readable with a writable `value` property and a `set` method. */ export interface Writable<TValue = any> extends Readable<TValue> { /** Current value of the Writable */ value: TValue; /** Set new value */ set: (value: TValue) => void; } export interface OwnedWritable<TValue = any> extends Writable<TValue> { /** * Remove all subscribers and lock. */ dispose(): void; } |