@mmstack/primitives 19.3.2 → 19.3.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.
- package/README.md +72 -12
- package/fesm2022/mmstack-primitives.mjs +101 -3
- package/fesm2022/mmstack-primitives.mjs.map +1 -1
- package/index.d.ts +2 -0
- package/lib/pooled.d.ts +59 -0
- package/lib/provided-pools.d.ts +64 -0
- package/lib/stored.d.ts +0 -1
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -5,6 +5,8 @@ export { nestedEffect } from './lib/effect';
|
|
|
5
5
|
export * from './lib/mappers';
|
|
6
6
|
export * from './lib/mutable';
|
|
7
7
|
export * from './lib/pipeable/public_api';
|
|
8
|
+
export * from './lib/pooled';
|
|
9
|
+
export * from './lib/provided-pools';
|
|
8
10
|
export * from './lib/sensors';
|
|
9
11
|
export * from './lib/store';
|
|
10
12
|
export * from './lib/stored';
|
package/lib/pooled.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { type CreateSignalOptions, type Signal } from '@angular/core';
|
|
2
|
+
/**
|
|
3
|
+
* Derives a value (`U`) from a freshly-reset buffer (`T`). Mutate the buffer
|
|
4
|
+
* and either return it directly or derive a different value from it. Naming
|
|
5
|
+
* matches Angular's `linkedSignal` `computation` convention.
|
|
6
|
+
*/
|
|
7
|
+
export type Computation<T, U> = (buffer: T) => U;
|
|
8
|
+
/**
|
|
9
|
+
* Options for {@link pooled}.
|
|
10
|
+
*/
|
|
11
|
+
export type CreatePooledOptions<T, U = T> = {
|
|
12
|
+
/** Factory for a buffer slot. Called at most twice in total per pool. */
|
|
13
|
+
create: () => T;
|
|
14
|
+
/**
|
|
15
|
+
* Restores a dirty buffer in-place, or returns a replacement instance. Not
|
|
16
|
+
* called on freshly-created buffers — only on reused ones.
|
|
17
|
+
*/
|
|
18
|
+
reset: (dirty: T) => void | T;
|
|
19
|
+
/** Writes into the buffer and returns the derived value. */
|
|
20
|
+
computation: Computation<T, U>;
|
|
21
|
+
/**
|
|
22
|
+
* Pre-allocate both buffer slots at construction. Use when `create()` is
|
|
23
|
+
* expensive and you'd rather pay the cost up front than on the first reads.
|
|
24
|
+
* @default false
|
|
25
|
+
*/
|
|
26
|
+
eager?: boolean;
|
|
27
|
+
} & CreateSignalOptions<U>;
|
|
28
|
+
/**
|
|
29
|
+
* A `Signal<U>` backed by a two-slot object pool: `create` is called at most
|
|
30
|
+
* twice over the pool's lifetime, and the two `T` instances are swapped on
|
|
31
|
+
* every recomputation with `reset` invoked on the dirty one before
|
|
32
|
+
* `computation` writes into it. Consecutive reads return different identities,
|
|
33
|
+
* so the default `Object.is` equality still flags changes.
|
|
34
|
+
*
|
|
35
|
+
* **Retention contract:** the returned value is only valid until the next
|
|
36
|
+
* recomputation of this signal. The container is recycled and `reset`,
|
|
37
|
+
* mutating any reference you still hold — do not store the result, pass it to
|
|
38
|
+
* async code, or hand it to consumers that outlive the current reactive tick.
|
|
39
|
+
*
|
|
40
|
+
* For collection buffers prefer the presets: {@link pooledArray},
|
|
41
|
+
* {@link pooledMap}, {@link pooledSet}.
|
|
42
|
+
*
|
|
43
|
+
* @see [Angular `linkedSignal`](https://angular.dev/api/core/linkedSignal) — carries previous *state* forward; complementary, not a substitute.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* const source = signal<{ active: boolean }[]>([]);
|
|
48
|
+
*
|
|
49
|
+
* const counters = pooled<{ total: number; active: number }>({
|
|
50
|
+
* create: () => ({ total: 0, active: 0 }),
|
|
51
|
+
* reset: (c) => { c.total = 0; c.active = 0; },
|
|
52
|
+
* computation: (c) => {
|
|
53
|
+
* for (const item of source()) { c.total++; if (item.active) c.active++; }
|
|
54
|
+
* return c;
|
|
55
|
+
* },
|
|
56
|
+
* });
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export declare function pooled<T, U = T>({ create, reset, computation, ...opt }: CreatePooledOptions<T, U>): Signal<U>;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { CreateSignalOptions, Signal } from '@angular/core';
|
|
2
|
+
import { type Computation, type CreatePooledOptions } from './pooled';
|
|
3
|
+
/**
|
|
4
|
+
* Options for the preset pool helpers. Same shape as
|
|
5
|
+
* {@link CreatePooledOptions}, with `create` and `reset` made optional — each
|
|
6
|
+
* helper supplies its own defaults.
|
|
7
|
+
*/
|
|
8
|
+
export type CreateProvidedPooledOptions<T, U = T> = Omit<CreatePooledOptions<T, U>, 'create' | 'reset'> & Partial<Pick<CreatePooledOptions<T, U>, 'create' | 'reset'>>;
|
|
9
|
+
/**
|
|
10
|
+
* Array-buffer preset for {@link pooled}. Recycles a single array per slot;
|
|
11
|
+
* cleared via `arr.length = 0` between reads.
|
|
12
|
+
*
|
|
13
|
+
* Two overloads: a {@link Computation} shorthand (most common) or the full
|
|
14
|
+
* options object when you need `eager` or custom `create`/`reset`.
|
|
15
|
+
*
|
|
16
|
+
* Generic inference defaults `T` to `unknown[]` — annotate the callback or
|
|
17
|
+
* pass the type argument for tighter element types.
|
|
18
|
+
*
|
|
19
|
+
* @see {@link pooled} for the retention contract.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* const activeIds = pooledArray<number[]>((buf) => {
|
|
24
|
+
* for (const item of items()) if (item.active) buf.push(item.id);
|
|
25
|
+
* return buf;
|
|
26
|
+
* });
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare function pooledArray<T extends unknown[], U = T>(computation: Computation<T, U>, opt?: CreateSignalOptions<U>): Signal<U>;
|
|
30
|
+
export declare function pooledArray<T extends unknown[], U = T>(opt: CreateProvidedPooledOptions<T, U>): Signal<U>;
|
|
31
|
+
/**
|
|
32
|
+
* Set-buffer preset for {@link pooled}. Recycles a single `Set` per slot;
|
|
33
|
+
* cleared via `.clear()` between reads. Overload shape mirrors
|
|
34
|
+
* {@link pooledArray}.
|
|
35
|
+
*
|
|
36
|
+
* @see {@link pooled} for the retention contract.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* const distinctRoles = pooledSet<Set<string>>((buf) => {
|
|
41
|
+
* for (const u of users()) buf.add(u.role);
|
|
42
|
+
* return buf;
|
|
43
|
+
* });
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export declare function pooledSet<T extends Set<unknown>, U = T>(computation: Computation<T, U>, opt?: CreateSignalOptions<U>): Signal<U>;
|
|
47
|
+
export declare function pooledSet<T extends Set<unknown>, U = T>(opt: CreateProvidedPooledOptions<T, U>): Signal<U>;
|
|
48
|
+
/**
|
|
49
|
+
* Map-buffer preset for {@link pooled}. Recycles a single `Map` per slot;
|
|
50
|
+
* cleared via `.clear()` between reads. Overload shape mirrors
|
|
51
|
+
* {@link pooledArray}.
|
|
52
|
+
*
|
|
53
|
+
* @see {@link pooled} for the retention contract.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* const byId = pooledMap<Map<number, User>>((buf) => {
|
|
58
|
+
* for (const u of users()) buf.set(u.id, u);
|
|
59
|
+
* return buf;
|
|
60
|
+
* });
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export declare function pooledMap<T extends Map<unknown, unknown>, U = T>(computation: Computation<T, U>, opt?: CreateSignalOptions<U>): Signal<U>;
|
|
64
|
+
export declare function pooledMap<T extends Map<unknown, unknown>, U = T>(opt: CreateProvidedPooledOptions<T, U>): Signal<U>;
|
package/lib/stored.d.ts
CHANGED