All files / src interface.ts

0% Statements 0/0
0% Branches 0/0
0% Functions 0/0
0% Lines 0/0

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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154                                                                                                                                                                                                                                                                                                                   
import { type Scheduler } from "./schedulers";
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.
 *
 * @category Readable
 */
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>;
  /**
   * Indicates whether the Readable has been disposed.
   */
  readonly disposed: boolean;
  /**
   * Current value of the $.
   */
  readonly value: TValue;
  /**
   * Get current value.
   */
  get: () => TValue;
  /** @internal */
  onReaction_(subscriber: Subscriber<TValue>, scheduler?: Scheduler): void;
  /**
   * Subscribe to value changes without immediate emission.
   * @param subscriber
   * @param scheduler Optional scheduler to control when the subscriber is called.
   * @returns a disposer function that cancels the subscription
   */
  reaction(subscriber: Subscriber<TValue>, scheduler?: Scheduler): Disposer;
  /**
   * Subscribe to value changes with immediate emission.
   * @param subscriber
   * @param scheduler Optional scheduler to control when the subscriber is called.
   * @returns a disposer function that cancels the subscription
   */
  subscribe(subscriber: Subscriber<TValue>, scheduler?: Scheduler): Disposer;
  /**
   * Remove the given subscriber or all subscribers if no subscriber is provided.
   * @param subscriber Optional subscriber function to remove.
   * @param scheduler Optional scheduler associated with the subscriber.
   * If not provided, all subscribers will be removed.
   */
  unsubscribe(subscriber?: (...args: any[]) => any, scheduler?: Scheduler): void;
}
 
/**
 * An OwnedReadable is a {@link Readable} with a `dispose` method that removes all subscribers and locks the Readable.
 *
 * @category Readable
 */
export interface OwnedReadable<TValue = any> extends Readable<TValue> {
  /**
   * Remove all subscribers and lock.
   */
  dispose(): void;
}
 
/**
 * A Readable provider is an object that provides a {@link Readable} `$` property.
 *
 * @category Readable
 */
export interface ReadableProvider<TValue = any> {
  readonly $: Readable<TValue>;
}
 
/**
 * A {@link Readable} or a {@link ReadableProvider}.
 *
 * @category Readable
 */
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 Writable is a {@link Readable} with a writable `value` property and a `set` method that updates the value.
 *
 * @category Writable
 */
export interface Writable<TValue = any> extends Readable<TValue> {
  /** Current value of the Writable */
  value: TValue;
  /** Set new value */
  set: (value: TValue) => void;
}
 
/**
 * An OwnedWritable is a {@link Writable} with a `dispose` method that removes all subscribers and locks the Writable.
 *
 * @category Writable
 */
export interface OwnedWritable<TValue = any> extends Writable<TValue> {
  /**
   * Remove all subscribers and lock.
   */
  dispose(): void;
}