uneventful 0.0.11 → 0.0.13

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.
package/dist/signals.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { A as AnyFunction, O as OptionalCleanup, D as DisposeFn, L as SignalSource, Y as Yielding, S as Source, P as PlainFunction, a as Stream } from './types-N2ua11te.js';
2
- import { U as UntilMethod } from './sinks-5TuxCRtX.js';
1
+ import { A as AnyFunction, O as OptionalCleanup, D as DisposeFn, L as SignalSource, Y as Yielding, S as Source, P as PlainFunction, a as Stream } from './types-pElgImr7.js';
2
+ import { U as UntilMethod } from './sinks-B_LfTZgF.js';
3
3
 
4
4
  /**
5
5
  * A decorator function that supports both TC39 and "legacy" decorator protocols
@@ -10,11 +10,11 @@ import { U as UntilMethod } from './sinks-5TuxCRtX.js';
10
10
  * @category Types and Interfaces
11
11
  */
12
12
  type GenericMethodDecorator<F extends AnyFunction> = {
13
- /** TC39 Method Decorator @hidden */
13
+ /** TC39 Method Decorator */
14
14
  (fn: F, ctx?: {
15
15
  kind: "method";
16
16
  }): F;
17
- /** Legacy Method Decorator @hidden */
17
+ /** Legacy Method Decorator */
18
18
  (proto: object, name: string | symbol, desc?: {
19
19
  value?: F;
20
20
  }): void;
@@ -130,20 +130,6 @@ interface RuleFactory {
130
130
  *
131
131
  */
132
132
  factory(scheduleFn: SchedulerFn): RuleFactory;
133
- /**
134
- * @deprecated Use {@link RuleFactory.root} instead
135
- *
136
- * ---
137
- * Create a "detached" or standalone rule, that is not attached to any job.
138
- *
139
- * `r.detached(fn)` is shorthand for calling `detached.run(r, fn)`. (Where
140
- * `r` is a {@link RuleFactory} such as `rule`.)
141
- *
142
- * Note that since the created rule isn't attached to a job, it *must* be
143
- * explicitly stopped, either by calling the returned disposal function or
144
- * by the rule function arranging to stop itself via {@link rule.stop}().
145
- */
146
- detached(fn: () => OptionalCleanup): DisposeFn;
147
133
  /**
148
134
  * Create a standalone rule, not attached to the current job.
149
135
  *
@@ -194,7 +180,7 @@ interface RuleFactory {
194
180
  *
195
181
  * @returns A function that can be called to terminate the rule.
196
182
  *
197
- * @category Reactive Behaviors
183
+ * @category none
198
184
  * @function
199
185
  */
200
186
  declare const rule: ((fn: () => OptionalCleanup) => DisposeFn) & RuleFactory;
@@ -214,7 +200,7 @@ declare const rule: ((fn: () => OptionalCleanup) => DisposeFn) & RuleFactory;
214
200
  * factory you wish to run pending rules for. If not given, the default
215
201
  * {@link rule}() factory is targeted.
216
202
  *
217
- * @category Reactive Behaviors
203
+ * @category Scheduling
218
204
  */
219
205
  declare function runRules(scheduleFn?: SchedulerFn): void;
220
206
  /**
@@ -246,34 +232,132 @@ declare class WriteConflict extends Error {
246
232
  */
247
233
  declare class CircularDependency extends Error {
248
234
  }
235
+
236
+ /** @inline */
237
+ type Get<V> = () => V;
238
+ /** @inline */
239
+ type GetFactory<O extends WeakKey, V = void> = (obj: O) => Get<V>;
240
+ /** @inline */
241
+ type Method<O extends WeakKey, V = void> = (this: O) => V;
242
+ /** @inline */
243
+ type DeferredFn = {
244
+ <Result>(compute: () => Result): Signal<Result>;
245
+ <Instance extends WeakKey, Result>(factory: GetFactory<Instance, Result>): (obj: Instance) => Result;
246
+ };
247
+ /** @inline */
248
+ type DeferredFx = {
249
+ (compute: () => void): Get<void>;
250
+ <Instance extends WeakKey>(factory: GetFactory<Instance, void>): (obj: Instance) => void;
251
+ };
249
252
  /**
250
- * Keep an expression's old value unless there's a semantic change
253
+ * Create a signal that computes a value based on other signals. (Can also be
254
+ * used as a method decorator via `@fn`, with either TC39 or "Legacy"
255
+ * decorators.)
251
256
  *
252
- * By default, reactive values (i.e. {@link cached}(), or {@link value}() with a
253
- * {@link Configurable.setf setf}()) are considered to have "changed" (and thus
254
- * trigger recalculation of their dependents) when they are different according
255
- * to `===` comparison.
257
+ * @param compute The function that will be called to compute the result, if the
258
+ * signals it used in its last invocation have changed since then.
256
259
  *
257
- * This works well for primitive values, but for arrays and objects it's not
258
- * always ideal, because two arrays can have the exact same elements and still
259
- * be different according to `===`. So this function lets you substitute a
260
- * different comparison function (like a deep-equal or shallow-equal) instead.
261
- * (The default is {@link arrayEq}() if no compare function is supplied.)
260
+ * @returns A signal that returns a cached value or recomputes it as necessary,
261
+ * and will be tracked as a dependency for other signals, rules, or effects,
262
+ * when they use it in their calculations.
262
263
  *
263
- * Specifically, if your reactive expression returns `unchangedIf(newVal,
264
- * compare)`, then the expression's previous value will be kept if the compare
265
- * function returns true when called with the old and new values. Otherwise, the
266
- * new value will be used.
264
+ * @template Result The type of value the created signal will provide.
267
265
  *
268
- * @remarks
269
- * - If the reactive expression's last "value" was an error, the new value is
270
- * returned
271
- * - An error will be thrown if this function is called outside a reactive
272
- * expression or from within a {@link peek}() call or {@link action} wrapper.
266
+ * @category none
267
+ */
268
+ declare function fn<Result>(compute: Get<Result>): Signal<Result>;
269
+ /**
270
+ * Create a parameterized reactive function that tracks separate dependencies
271
+ * and caching state for each object it's called on.
272
+ *
273
+ * @returns a one-argument reactive function that is shorthand for looking up
274
+ * and returning the value of a cached signal customized for the given argument.
275
+ *
276
+ * That is, given `rf = fn(a => () => a.bar)`, calling `rf(foo)` is equivalent
277
+ * to calling `fn(() => foo.bar)()`, except that each `foo` gets its own `fn(()
278
+ * => foo.bar)` signal cached, so that dependencies can be properly tracked
279
+ * per instance.
280
+ *
281
+ * @param factory A function that will be called with each new instance passed
282
+ * to the one-argument reactive function. It must return a zero-argument
283
+ * function, customized to compute the value for the instance it was given.
284
+ *
285
+ * @template Instance The type of object the created function will be used with
286
+ * @template Result The type of result the created function will return
273
287
  *
274
- * @category Reactive Values
275
288
  */
276
- declare function unchangedIf<T>(newVal: T, equals?: (v1: T, v2: T) => boolean): T;
289
+ declare function fn<Instance extends WeakKey, Result>(factory: GetFactory<Instance, Result>): (obj: Instance) => Result;
290
+ /** @hidden support for ```fn``()``` */
291
+ declare function fn(t: TemplateStringsArray): DeferredFn;
292
+ /** @hidden TC39 decorator */
293
+ declare function fn<Instance extends WeakKey, Result>(f: Method<Instance, Result>, ctx: {
294
+ kind: "method";
295
+ }): Method<Instance, Result>;
296
+ /** @hidden "Legacy"/"TypeScript Experimental" Decorator */
297
+ declare function fn<Instance extends WeakKey, Result, D extends {
298
+ value?: Method<Instance, Result>;
299
+ }>(clsOrProto: any, name: string | symbol, desc: D): D;
300
+ /**
301
+ * Create a reactive effect (void signal) that runs when observed by a rule
302
+ * (directly or via other reactive functions). (Can also be used as a method
303
+ * decorator via `@fx`, with either TC39 or "Legacy" decorators.)
304
+ *
305
+ * @remarks Note that reactive effects do not actually execute unless they are
306
+ * "in use", i.e. either directly invoked from an active job or called
307
+ * indirectly from a rule or effect that is itself in use.
308
+ *
309
+ * @param effect The function that will be called to start (or restart) the
310
+ * effect, if the reactive values it used during its last run have changed since
311
+ * then.
312
+ *
313
+ * The effect function is run in a job that will restart if its dependencies
314
+ * change, and when the effect is no longer in use. (So you can use e.g.
315
+ * {@link must}() to define rollback actions, and any jobs, subscriptions, etc.
316
+ * you start inside the effect function will likewise be terminated when the
317
+ * effect becomes unobserved or its dependencies change.)
318
+ *
319
+ * @returns a zero-argument reactive function that can be called from any rule,
320
+ * job, or other observed reactive functions, to start or continue the effect.
321
+ *
322
+ * Multiple calls from the same or different observers do not restart the
323
+ * effect; only dependency changes will restart it. If it loses all observers
324
+ * (i.e. fails to be called by any of them, or all the calling jobs end), the
325
+ * effect will stop until it's in use again.
326
+ *
327
+ * @category none
328
+ */
329
+ declare function fx(effect: Get<void>): Get<void>;
330
+ /**
331
+ * Create a parameterized reactive effect that tracks separate dependencies,
332
+ * jobs, and caching state for each object it's called on.
333
+ *
334
+ * @returns a one-argument reactive effect that is shorthand for looking up and
335
+ * calling a cached, zero-argument reactive effect customized for the given
336
+ * argument.
337
+ *
338
+ * That is, given `rx = fx(a => () => a.bar())`, calling `rx(foo)` is equivalent
339
+ * to calling `fx(() => foo.bar())()`, except that each `foo` gets its own
340
+ * `fx(() => foo.bar())` instance cached, so that dependencies can be properly
341
+ * tracked per instance.
342
+ *
343
+ * @param factory A function that will be called with each new instance passed
344
+ * to the returned effect function. It must return a zero-argument effect
345
+ * function, customized to apply the effect to the instance it was given.
346
+ *
347
+ * @template Instance The type of object the effect will be applied to
348
+ *
349
+ */
350
+ declare function fx<Instance extends WeakKey>(factory: GetFactory<Instance>): (ob: Instance) => void;
351
+ /** @hidden support for ```fx``()``` */
352
+ declare function fx(t: TemplateStringsArray): DeferredFx;
353
+ /** @hidden TC39 decorator */
354
+ declare function fx<Instance extends WeakKey>(method: Method<Instance>, ctx: {
355
+ kind: "method";
356
+ }): Method<Instance>;
357
+ /** @hidden "Legacy"/"TypeScript Experimental" Decorator */
358
+ declare function fx<Instance extends WeakKey, D extends {
359
+ value?: Method<Instance>;
360
+ }>(clsOrProto: any, name: string | symbol, desc: D): D;
277
361
 
278
362
  /**
279
363
  * The Signals API for uneventful.
@@ -377,7 +461,7 @@ interface Configurable<T> extends Writable<T> {
377
461
  /**
378
462
  * Create a {@link Configurable} signal with the given inital value
379
463
  *
380
- * @category Reactive Values
464
+ * @category none
381
465
  */
382
466
  declare function value<T>(val?: T): Configurable<T>;
383
467
  /**
@@ -389,7 +473,7 @@ declare function value<T>(val?: T): Configurable<T>;
389
473
  * calling signature below will apply, even if TypeScript doesn't see it that
390
474
  * way!)
391
475
  *
392
- * @category Reactive Values
476
+ * @category none
393
477
  */
394
478
  declare function cached<T>(compute: () => T): Signal<T>;
395
479
  /**
@@ -430,7 +514,7 @@ declare function cached<T extends Signal<any>>(signal: T): T;
430
514
  *
431
515
  * @returns The result of calling `fn(..args)`
432
516
  *
433
- * @category Reactive Values
517
+ * @category Dependency Tracking
434
518
  */
435
519
  declare function peek<F extends PlainFunction>(fn: F, ...args: Parameters<F>): ReturnType<F>;
436
520
  /**
@@ -472,7 +556,7 @@ declare function peek<F extends PlainFunction>(fn: F, ...args: Parameters<F>): R
472
556
  * to the original function, while running with dependency tracking suppressed
473
557
  * (as with {@link peek}()).
474
558
  *
475
- * @category Reactive Behaviors
559
+ * @category Dependency Tracking
476
560
  */
477
561
  declare function action<F extends AnyFunction>(fn: F): F;
478
562
  /** @hidden TC39 Decorator protocol */
@@ -483,6 +567,78 @@ declare function action<F extends AnyFunction>(fn: F, ctx: {
483
567
  declare function action<F extends AnyFunction, D extends {
484
568
  value?: F;
485
569
  }>(clsOrProto: any, name: string | symbol, desc: D): D;
570
+ /**
571
+ * Keep an expression's old value unless there's a semantic change
572
+ *
573
+ * By default, reactive values (i.e. {@link cached}(), or {@link value}() with a
574
+ * {@link Configurable.setf setf}()) are considered to have "changed" (and thus
575
+ * trigger recalculation of their dependents) when they are different according
576
+ * to `===` comparison.
577
+ *
578
+ * This works well for primitive values, but for arrays and objects it's not
579
+ * always ideal, because two arrays can have the exact same elements and still
580
+ * be different according to `===`. So this function lets you substitute a
581
+ * different comparison function (like a deep-equal or shallow-equal) instead.
582
+ *
583
+ * Specifically, if your reactive expression returns `stable(compare, newVal)`,
584
+ * then the expression's previous value will be kept if the `compare` function
585
+ * returns true when called with the old and new values. Otherwise, the new
586
+ * value will be used.
587
+ *
588
+ * @remarks
589
+ * - If the reactive expression's last "value" was an error, the new value is
590
+ * returned.
591
+ * - An error will be thrown if this function is called outside a reactive
592
+ * expression or from within a {@link peek}() call or {@link action} wrapper.
593
+ * - You can use the {@link stabilizer}() function to create custom functions
594
+ * similar to {@link stableJSON}. or {@link stableArray}().
595
+ *
596
+ * @category Dependency Tracking
597
+ */
598
+ declare function stable<T>(equals: (v1: T, v2: T) => boolean, newVal: T): T;
599
+ /**
600
+ * @deprecated This is {@link stable}() but with swapped arguments. Please switch
601
+ * to using stable(), stableArray(), or a stabilizer(), as this function will be removed
602
+ * in a future release.
603
+ *
604
+ * @hidden
605
+ */
606
+ declare function unchangedIf<T>(newVal: T, equals?: (v1: T, v2: T) => boolean): T;
607
+ /**
608
+ * Create a shorthand version of {@link stable}(), pre-bound to a specific comparison
609
+ * function.
610
+ *
611
+ * @param equals The comparison function to use
612
+ * @template T The type of value the comparison function works on
613
+ *
614
+ * @category Dependency Tracking
615
+ */
616
+ declare function stabilizer<T>(equals: <T>(v1: T, v2: T) => boolean): <R extends T>(newVal: R) => R;
617
+ /**
618
+ * Keep an expression's old value if the new value is an array with the same
619
+ * contents.
620
+ *
621
+ * (Shorthand for {@link stable}({@link arrayEq}, ...).)
622
+ *
623
+ * @function
624
+ * @category Dependency Tracking
625
+ */
626
+ declare const stableArray: <R extends unknown[]>(newVal: R) => R;
627
+ /**
628
+ * Keep an expression's old value if its JSON string is the same as the new one.
629
+ *
630
+ * (Shorthand for {@link stable}() with a JSON-string comparison function.)
631
+ *
632
+ * Note that JSON string comparison is slow and memory intensive for more
633
+ * complex data structures, is order-sensitive for object keys, and doesn't
634
+ * support maps or sets. If you need any of those things, you should probably
635
+ * use a {@link stabilizer}() based on
636
+ * [fast-equals](https://github.com/planttheidea/fast-equals) instead.
637
+ *
638
+ * @function
639
+ * @category Dependency Tracking
640
+ */
641
+ declare const stableJSON: <R extends any>(newVal: R) => R;
486
642
  /**
487
643
  * Wait for and return the next truthy value (or error) from a data source (when
488
644
  * processed with `yield *` within a {@link Job}).
@@ -514,4 +670,4 @@ declare function action<F extends AnyFunction, D extends {
514
670
  */
515
671
  declare function until<T>(source: UntilMethod<T> | Stream<T> | (() => T)): Yielding<T>;
516
672
 
517
- export { CircularDependency, type Configurable, type GenericMethodDecorator, type RuleFactory, type SchedulerFn, type Signal, type Writable, WriteConflict, action, cached, peek, rule, runRules, unchangedIf, until, value };
673
+ export { CircularDependency, type Configurable, type GenericMethodDecorator, type RuleFactory, type SchedulerFn, type Signal, type Writable, WriteConflict, action, cached, fn, fx, peek, rule, runRules, stabilizer, stable, stableArray, stableJSON, unchangedIf, until, value };