@zeix/cause-effect 0.18.2 → 0.18.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  import { type Cleanup, type EffectCallback, type MaybeCleanup, type Signal } from '../graph';
2
2
  type MaybePromise<T> = T | Promise<T>;
3
- type MatchHandlers<T extends Signal<unknown & {}>[]> = {
3
+ type MatchHandlers<T extends readonly Signal<unknown & {}>[]> = {
4
4
  ok: (values: {
5
5
  [K in keyof T]: T[K] extends Signal<infer V> ? V : never;
6
6
  }) => MaybePromise<MaybeCleanup>;
@@ -44,5 +44,5 @@ declare function createEffect(fn: EffectCallback): Cleanup;
44
44
  * @since 0.15.0
45
45
  * @throws RequiredOwnerError If called without an active owner.
46
46
  */
47
- declare function match<T extends Signal<unknown & {}>[]>(signals: T, handlers: MatchHandlers<T>): MaybeCleanup;
47
+ declare function match<T extends readonly Signal<unknown & {}>[]>(signals: readonly [...T], handlers: MatchHandlers<T>): MaybeCleanup;
48
48
  export { type MaybePromise, type MatchHandlers, createEffect, match };
@@ -0,0 +1,53 @@
1
+ import { type Signal, type SignalOptions } from '../graph';
2
+ /**
3
+ * A signal that delegates its value to a swappable backing signal.
4
+ *
5
+ * Slots provide a stable reactive source at a fixed position (e.g. an object property)
6
+ * while allowing the backing signal to be replaced without breaking subscribers.
7
+ * The object shape is compatible with `Object.defineProperty()` descriptors:
8
+ * `get`, `set`, `configurable`, and `enumerable` are used by the property definition;
9
+ * `replace()` and `current()` are kept on the slot object for integration-layer control.
10
+ *
11
+ * @template T - The type of value held by the delegated signal.
12
+ */
13
+ type Slot<T extends {}> = {
14
+ readonly [Symbol.toStringTag]: 'Slot';
15
+ /** Descriptor field: allows the property to be redefined or deleted. */
16
+ configurable: true;
17
+ /** Descriptor field: the property shows up during enumeration. */
18
+ enumerable: true;
19
+ /** Reads the current value from the delegated signal, tracking dependencies. */
20
+ get(): T;
21
+ /** Writes a value to the delegated signal. Throws `ReadonlySignalError` if the delegated signal is read-only. */
22
+ set(next: T): void;
23
+ /** Swaps the backing signal, invalidating all downstream subscribers. Narrowing (`U extends T`) is allowed. */
24
+ replace<U extends T>(next: Signal<U>): void;
25
+ /** Returns the currently delegated signal. */
26
+ current(): Signal<T>;
27
+ };
28
+ /**
29
+ * Creates a slot signal that delegates its value to a swappable backing signal.
30
+ *
31
+ * A slot acts as a stable reactive source that can be used as a property descriptor
32
+ * via `Object.defineProperty(target, key, slot)`. Subscribers link to the slot itself,
33
+ * so replacing the backing signal with `replace()` invalidates them without breaking
34
+ * existing edges. Setter calls forward to the current backing signal when it is writable.
35
+ *
36
+ * @since 0.18.3
37
+ * @template T - The type of value held by the delegated signal.
38
+ * @param initialSignal - The initial signal to delegate to.
39
+ * @param options - Optional configuration for the slot.
40
+ * @param options.equals - Custom equality function. Defaults to strict equality (`===`).
41
+ * @param options.guard - Type guard to validate values passed to `set()`.
42
+ * @returns A `Slot<T>` object usable both as a property descriptor and as a reactive signal.
43
+ */
44
+ declare function createSlot<T extends {}>(initialSignal: Signal<T>, options?: SignalOptions<T>): Slot<T>;
45
+ /**
46
+ * Checks if a value is a Slot signal.
47
+ *
48
+ * @since 0.18.3
49
+ * @param value - The value to check
50
+ * @returns True if the value is a Slot
51
+ */
52
+ declare function isSlot<T extends {} = unknown & {}>(value: unknown): value is Slot<T>;
53
+ export { createSlot, isSlot, type Slot };