@zerolith/state 0.0.0-stage → 1.1.0
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/LICENSE +201 -0
- package/README.md +31 -2
- package/dist/history.d.ts +38 -0
- package/dist/history.d.ts.map +1 -0
- package/dist/history.js +68 -0
- package/dist/history.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/production/history.js +2 -0
- package/dist/production/history.js.map +1 -0
- package/dist/production/index.js +2 -0
- package/dist/production/index.js.map +1 -0
- package/dist/production/selector.js +2 -0
- package/dist/production/selector.js.map +1 -0
- package/dist/production/stored.js +2 -0
- package/dist/production/stored.js.map +1 -0
- package/dist/production/timing.js +2 -0
- package/dist/production/timing.js.map +1 -0
- package/dist/selector.d.ts +18 -0
- package/dist/selector.d.ts.map +1 -0
- package/dist/selector.js +48 -0
- package/dist/selector.js.map +1 -0
- package/dist/stored.d.ts +32 -0
- package/dist/stored.d.ts.map +1 -0
- package/dist/stored.js +130 -0
- package/dist/stored.js.map +1 -0
- package/dist/timing.d.ts +21 -0
- package/dist/timing.d.ts.map +1 -0
- package/dist/timing.js +70 -0
- package/dist/timing.js.map +1 -0
- package/package.json +51 -5
- package/src/history.ts +105 -0
- package/src/index.ts +16 -0
- package/src/selector.ts +57 -0
- package/src/stored.ts +173 -0
- package/src/timing.ts +82 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,32 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @zerolith/state
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Signals with a memory, over the core alone: a signal kept in storage and across tabs, undo and redo for a signal, debounced and throttled signals, and a selector that tells each row whether it is the selected one.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { signal } from "zerolith";
|
|
7
|
+
import { debounced, history, selector, stored } from "@zerolith/state";
|
|
8
|
+
|
|
9
|
+
const theme = stored<"light" | "dark">("theme", "light");
|
|
10
|
+
const edits = history(signal(""));
|
|
11
|
+
const settledQuery = debounced(signal(""), 300);
|
|
12
|
+
const selected = selector(signal<number | null>(null));
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
- `stored` reads its value from storage as JSON, writes every set, and follows other tabs.
|
|
16
|
+
- `history` gives `checkpoint()`, `undo()`, and `redo()`, with `canUndo` and `canRedo` as signals.
|
|
17
|
+
- `debounced` and `throttled` follow a signal after a quiet spell or at most so often.
|
|
18
|
+
- `selector` gives each key a signal that is true for the selected one, so a selection among a thousand rows updates two.
|
|
19
|
+
|
|
20
|
+
The guide is on the [docs site](https://zerolith.dev/guide/state/).
|
|
21
|
+
|
|
22
|
+
Install with `npm install @zerolith/state`. The API is frozen at 1.0: a breaking change comes only in a major, after a deprecation notice, and the [support policy](https://github.com/molexxxx/zerolith/blob/main/docs/policy/support.md) says how long a line gets fixes.
|
|
23
|
+
|
|
24
|
+
## Documentation
|
|
25
|
+
|
|
26
|
+
- The [state guide](https://zerolith.dev/guide/state/) explains it with examples that run as tests.
|
|
27
|
+
- The [reference](https://zerolith.dev/reference/zerolith-state/) lists every export, generated from the source.
|
|
28
|
+
- New to zerolith? The [tutorial](https://zerolith.dev/learn/) builds an app step by step, and the [playground](https://zerolith.dev/playground/) runs code in the browser.
|
|
29
|
+
|
|
30
|
+
## License
|
|
31
|
+
|
|
32
|
+
Apache-2.0. See `LICENSE`.
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Undo and redo for a signal, from explicit checkpoints, as an editor
|
|
3
|
+
* keeps them.
|
|
4
|
+
*/
|
|
5
|
+
import { type ReadableSignal, type Signal } from "zerolith";
|
|
6
|
+
/** Options for `history`. */
|
|
7
|
+
export interface HistoryOptions {
|
|
8
|
+
/** How many checkpoints are kept; the oldest goes when one more is saved. Defaults to 50, which a value that is not a finite number keeps. */
|
|
9
|
+
limit?: number;
|
|
10
|
+
}
|
|
11
|
+
/** The undo and redo of one signal. */
|
|
12
|
+
export interface History<T> {
|
|
13
|
+
/** Saves the signal's current value as the point `undo()` goes back to, and drops what `redo()` could have brought back. */
|
|
14
|
+
checkpoint(): void;
|
|
15
|
+
/** Sets the signal back to the last checkpoint, keeping the current value for `redo()`; false when there is no checkpoint. */
|
|
16
|
+
undo(): boolean;
|
|
17
|
+
/** Sets the signal forward to the value the last `undo()` left; false when there is none. */
|
|
18
|
+
redo(): boolean;
|
|
19
|
+
/** True while `undo()` has somewhere to go, for a button to follow. */
|
|
20
|
+
readonly canUndo: ReadableSignal<boolean>;
|
|
21
|
+
/** True while `redo()` has somewhere to go. */
|
|
22
|
+
readonly canRedo: ReadableSignal<boolean>;
|
|
23
|
+
/** Forgets every checkpoint and every value `redo()` could bring back; the signal keeps its value. */
|
|
24
|
+
dispose(): void;
|
|
25
|
+
/** The signal the history is of. */
|
|
26
|
+
readonly source: Signal<T>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Undo and redo for `source` from explicit checkpoints: call
|
|
30
|
+
* `checkpoint()` before a change worth undoing, as an editor does before
|
|
31
|
+
* each edit, and `undo()` sets the signal back to it while `redo()` brings
|
|
32
|
+
* the undone value back. A new checkpoint after an undo starts a new
|
|
33
|
+
* branch, as in most editors, so what was undone is gone. `limit`
|
|
34
|
+
* checkpoints are kept, 50 by default. Each move writes the signal and
|
|
35
|
+
* both flags in one batch, so an effect over them runs once.
|
|
36
|
+
*/
|
|
37
|
+
export declare function history<T>(source: Signal<T>, options?: HistoryOptions): History<T>;
|
|
38
|
+
//# sourceMappingURL=history.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"history.d.ts","sourceRoot":"","sources":["../src/history.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,KAAK,cAAc,EAAE,KAAK,MAAM,EAA4B,MAAM,UAAU,CAAC;AAEtF,6BAA6B;AAC7B,MAAM,WAAW,cAAc;IAE3B,8IAA8I;IAC9I,KAAK,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,uCAAuC;AACvC,MAAM,WAAW,OAAO,CAAC,CAAC;IAEtB,4HAA4H;IAC5H,UAAU,IAAI,IAAI,CAAC;IACnB,8HAA8H;IAC9H,IAAI,IAAI,OAAO,CAAC;IAChB,6FAA6F;IAC7F,IAAI,IAAI,OAAO,CAAC;IAChB,uEAAuE;IACvE,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC,OAAO,CAAC,CAAC;IAC1C,+CAA+C;IAC/C,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC,OAAO,CAAC,CAAC;IAC1C,sGAAsG;IACtG,OAAO,IAAI,IAAI,CAAC;IAChB,oCAAoC;IACpC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC;CAC9B;AAED;;;;;;;;GAQG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,OAAO,GAAE,cAAmB,GAAG,OAAO,CAAC,CAAC,CAAC,CA+DtF"}
|
package/dist/history.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Undo and redo for a signal, from explicit checkpoints, as an editor
|
|
3
|
+
* keeps them.
|
|
4
|
+
*/
|
|
5
|
+
import { batch, signal, untracked } from "zerolith";
|
|
6
|
+
/**
|
|
7
|
+
* Undo and redo for `source` from explicit checkpoints: call
|
|
8
|
+
* `checkpoint()` before a change worth undoing, as an editor does before
|
|
9
|
+
* each edit, and `undo()` sets the signal back to it while `redo()` brings
|
|
10
|
+
* the undone value back. A new checkpoint after an undo starts a new
|
|
11
|
+
* branch, as in most editors, so what was undone is gone. `limit`
|
|
12
|
+
* checkpoints are kept, 50 by default. Each move writes the signal and
|
|
13
|
+
* both flags in one batch, so an effect over them runs once.
|
|
14
|
+
*/
|
|
15
|
+
export function history(source, options = {}) {
|
|
16
|
+
const limit = Number.isFinite(options.limit) ? Math.max(1, Math.floor(options.limit)) : 50;
|
|
17
|
+
const past = [];
|
|
18
|
+
const future = [];
|
|
19
|
+
const canUndo = signal(false);
|
|
20
|
+
const canRedo = signal(false);
|
|
21
|
+
const update = () => {
|
|
22
|
+
canUndo.set(past.length > 0);
|
|
23
|
+
canRedo.set(future.length > 0);
|
|
24
|
+
};
|
|
25
|
+
return {
|
|
26
|
+
source,
|
|
27
|
+
canUndo,
|
|
28
|
+
canRedo,
|
|
29
|
+
checkpoint() {
|
|
30
|
+
past.push(untracked(() => source.get()));
|
|
31
|
+
if (past.length > limit) {
|
|
32
|
+
past.shift();
|
|
33
|
+
}
|
|
34
|
+
future.length = 0;
|
|
35
|
+
batch(update);
|
|
36
|
+
},
|
|
37
|
+
undo() {
|
|
38
|
+
if (past.length === 0) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
const previous = past.pop();
|
|
42
|
+
future.push(untracked(() => source.get()));
|
|
43
|
+
batch(() => {
|
|
44
|
+
source.set(previous);
|
|
45
|
+
update();
|
|
46
|
+
});
|
|
47
|
+
return true;
|
|
48
|
+
},
|
|
49
|
+
redo() {
|
|
50
|
+
if (future.length === 0) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
const next = future.pop();
|
|
54
|
+
past.push(untracked(() => source.get()));
|
|
55
|
+
batch(() => {
|
|
56
|
+
source.set(next);
|
|
57
|
+
update();
|
|
58
|
+
});
|
|
59
|
+
return true;
|
|
60
|
+
},
|
|
61
|
+
dispose() {
|
|
62
|
+
past.length = 0;
|
|
63
|
+
future.length = 0;
|
|
64
|
+
batch(update);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=history.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"history.js","sourceRoot":"","sources":["../src/history.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAoC,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AA4BtF;;;;;;;;GAQG;AACH,MAAM,UAAU,OAAO,CAAI,MAAiB,EAAE,OAAO,GAAmB,EAAE;IAEtE,MAAM,KAAK,GAAG,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAe,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IACrG,MAAM,IAAI,GAAQ,EAAE,CAAC;IACrB,MAAM,MAAM,GAAQ,EAAE,CAAC;IACvB,MAAM,OAAO,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;IAC9B,MAAM,OAAO,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;IAC9B,MAAM,MAAM,GAAG,GAAS,EAAE;QAEtB,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;QAC7B,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IACnC,CAAC,CAAC;IACF,OAAO;QACH,MAAM;QACN,OAAO;QACP,OAAO;QACP,UAAU;YAEN,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;YACzC,IAAI,IAAI,CAAC,MAAM,GAAG,KAAK,EACvB,CAAC;gBACG,IAAI,CAAC,KAAK,EAAE,CAAC;YACjB,CAAC;YACD,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC;YAClB,KAAK,CAAC,MAAM,CAAC,CAAC;QAClB,CAAC;QACD,IAAI;YAEA,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EACrB,CAAC;gBACG,OAAO,KAAK,CAAC;YACjB,CAAC;YACD,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,EAAO,CAAC;YACjC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;YAC3C,KAAK,CAAC,GAAG,EAAE;gBAEP,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;gBACrB,MAAM,EAAE,CAAC;YACb,CAAC,CAAC,CAAC;YACH,OAAO,IAAI,CAAC;QAChB,CAAC;QACD,IAAI;YAEA,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EACvB,CAAC;gBACG,OAAO,KAAK,CAAC;YACjB,CAAC;YACD,MAAM,IAAI,GAAG,MAAM,CAAC,GAAG,EAAO,CAAC;YAC/B,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;YACzC,KAAK,CAAC,GAAG,EAAE;gBAEP,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;gBACjB,MAAM,EAAE,CAAC;YACb,CAAC,CAAC,CAAC;YACH,OAAO,IAAI,CAAC;QAChB,CAAC;QACD,OAAO;YAEH,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;YAChB,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC;YAClB,KAAK,CAAC,MAAM,CAAC,CAAC;QAClB,CAAC;KACJ,CAAC;AACN,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zerolith/state: signals with a memory, over the core alone. A signal
|
|
3
|
+
* kept in storage and in step across tabs, undo and redo for a signal,
|
|
4
|
+
* signals that follow another after a quiet spell or at most so often, and
|
|
5
|
+
* a selector that tells each row whether it is the selected one. The public
|
|
6
|
+
* surface is assembled here.
|
|
7
|
+
*/
|
|
8
|
+
export type { StoredOptions } from "./stored.js";
|
|
9
|
+
export { stored } from "./stored.js";
|
|
10
|
+
export type { History, HistoryOptions } from "./history.js";
|
|
11
|
+
export { history } from "./history.js";
|
|
12
|
+
export { debounced, throttled } from "./timing.js";
|
|
13
|
+
export { selector } from "./selector.js";
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,YAAY,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,YAAY,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC5D,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvC,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAEnD,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAGrC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvC,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAEnD,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{batch as e,signal as t,untracked as n}from"zerolith";export function history(r,i={}){let a=Number.isFinite(i.limit)?Math.max(1,Math.floor(i.limit)):50,o=[],s=[],c=t(!1),l=t(!1),u=()=>{c.set(o.length>0),l.set(s.length>0)};return{source:r,canUndo:c,canRedo:l,checkpoint(){o.push(n(()=>r.get())),o.length>a&&o.shift(),s.length=0,e(u)},undo(){if(o.length===0)return!1;let t=o.pop();return s.push(n(()=>r.get())),e(()=>{r.set(t),u()}),!0},redo(){if(s.length===0)return!1;let t=s.pop();return o.push(n(()=>r.get())),e(()=>{r.set(t),u()}),!0},dispose(){o.length=0,s.length=0,e(u)}}}
|
|
2
|
+
//# sourceMappingURL=history.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"history.js","sources":["../../src/history.ts"],"sourcesContent":["/**\n * Undo and redo for a signal, from explicit checkpoints, as an editor\n * keeps them.\n */\nimport { type ReadableSignal, type Signal, batch, signal, untracked } from \"zerolith\";\n\n/** Options for `history`. */\nexport interface HistoryOptions\n{\n /** How many checkpoints are kept; the oldest goes when one more is saved. Defaults to 50, which a value that is not a finite number keeps. */\n limit?: number;\n}\n\n/** The undo and redo of one signal. */\nexport interface History<T>\n{\n /** Saves the signal's current value as the point `undo()` goes back to, and drops what `redo()` could have brought back. */\n checkpoint(): void;\n /** Sets the signal back to the last checkpoint, keeping the current value for `redo()`; false when there is no checkpoint. */\n undo(): boolean;\n /** Sets the signal forward to the value the last `undo()` left; false when there is none. */\n redo(): boolean;\n /** True while `undo()` has somewhere to go, for a button to follow. */\n readonly canUndo: ReadableSignal<boolean>;\n /** True while `redo()` has somewhere to go. */\n readonly canRedo: ReadableSignal<boolean>;\n /** Forgets every checkpoint and every value `redo()` could bring back; the signal keeps its value. */\n dispose(): void;\n /** The signal the history is of. */\n readonly source: Signal<T>;\n}\n\n/**\n * Undo and redo for `source` from explicit checkpoints: call\n * `checkpoint()` before a change worth undoing, as an editor does before\n * each edit, and `undo()` sets the signal back to it while `redo()` brings\n * the undone value back. A new checkpoint after an undo starts a new\n * branch, as in most editors, so what was undone is gone. `limit`\n * checkpoints are kept, 50 by default. Each move writes the signal and\n * both flags in one batch, so an effect over them runs once.\n */\nexport function history<T>(source: Signal<T>, options: HistoryOptions = {}): History<T>\n{\n const limit = Number.isFinite(options.limit) ? Math.max(1, Math.floor(options.limit as number)) : 50;\n const past: T[] = [];\n const future: T[] = [];\n const canUndo = signal(false);\n const canRedo = signal(false);\n const update = (): void =>\n {\n canUndo.set(past.length > 0);\n canRedo.set(future.length > 0);\n };\n return {\n source,\n canUndo,\n canRedo,\n checkpoint(): void\n {\n past.push(untracked(() => source.get()));\n if (past.length > limit)\n {\n past.shift();\n }\n future.length = 0;\n batch(update);\n },\n undo(): boolean\n {\n if (past.length === 0)\n {\n return false;\n }\n const previous = past.pop() as T;\n future.push(untracked(() => source.get()));\n batch(() =>\n {\n source.set(previous);\n update();\n });\n return true;\n },\n redo(): boolean\n {\n if (future.length === 0)\n {\n return false;\n }\n const next = future.pop() as T;\n past.push(untracked(() => source.get()));\n batch(() =>\n {\n source.set(next);\n update();\n });\n return true;\n },\n dispose(): void\n {\n past.length = 0;\n future.length = 0;\n batch(update);\n }\n };\n}\n"],"names":["limit","options","past","future","canUndo","signal","canRedo","update","untracked","source","batch","previous","next"],"mappings":"AAIA,OAA2C,WAAO,YAAQ,mBAAiB,WAqC3E,OAAM,SAAU,QAAW,EAAmB,EAA0B,CAAA,EAAE,CAEtE,IAAMA,EAAQ,OAAO,SAASC,EAAQ,KAAK,EAAI,KAAK,IAAI,EAAG,KAAK,MAAMA,EAAQ,KAAe,CAAC,EAAI,GAC5FC,EAAY,CAAA,EACZC,EAAc,CAAA,EACdC,EAAUC,EAAO,EAAK,EACtBC,EAAUD,EAAO,EAAK,EACtBE,MAAoB,CAEtBH,EAAQ,IAAIF,EAAK,OAAS,CAAC,EAC3BI,EAAQ,IAAIH,EAAO,OAAS,CAAC,CACjC,EACA,MAAO,CACH,SACA,UACA,UACA,YAAU,CAEND,EAAK,KAAKM,MAAgBC,EAAO,IAAG,CAAE,CAAC,EACnCP,EAAK,OAASF,GAEdE,EAAK,MAAK,EAEd,EAAO,OAAS,EAChBQ,EAAMH,CAAM,CAChB,EACA,MAAI,CAEA,GAAIL,EAAK,SAAW,EAEhB,MAAO,GAEX,IAAMS,EAAWT,EAAK,IAAG,EAOzB,OANAC,EAAO,KAAKK,MAAgBC,EAAO,IAAG,CAAE,CAAC,EACzCC,MAAW,CAEPD,EAAO,IAAIE,CAAQ,EACnBJ,EAAM,CACV,CAAC,EACM,EACX,EACA,MAAI,CAEA,GAAIJ,EAAO,SAAW,EAElB,MAAO,GAEX,IAAMS,EAAOT,EAAO,IAAG,EAOvB,OANAD,EAAK,KAAKM,MAAgBC,EAAO,IAAG,CAAE,CAAC,EACvCC,MAAW,CAEPD,EAAO,IAAIG,CAAI,EACfL,EAAM,CACV,CAAC,EACM,EACX,EACA,SAAO,CAEH,EAAK,OAAS,EACd,EAAO,OAAS,EAChBG,EAAMH,CAAM,CAChB,CACH,CACL"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/index.ts"],"sourcesContent":["/**\n * @zerolith/state: signals with a memory, over the core alone. A signal\n * kept in storage and in step across tabs, undo and redo for a signal,\n * signals that follow another after a quiet spell or at most so often, and\n * a selector that tells each row whether it is the selected one. The public\n * surface is assembled here.\n */\nexport type { StoredOptions } from \"./stored.js\";\nexport { stored } from \"./stored.js\";\n\nexport type { History, HistoryOptions } from \"./history.js\";\nexport { history } from \"./history.js\";\n\nexport { debounced, throttled } from \"./timing.js\";\n\nexport { selector } from \"./selector.js\";\n"],"names":[],"mappings":"AAQA,OAAS,WAAc,cAGvB,OAAS,YAAe,eAExB,OAAS,UAAW,cAAiB,cAErC,OAAS,aAAgB"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{effect as e,signal as t,untracked as n}from"zerolith";function r(e,t){return e===t||e!==e&&t!==t}export function selector(i){let a=new Map,o=new FinalizationRegistry(e=>{a.get(e)?.deref()===void 0&&a.delete(e)}),s=n(()=>i.get());return e(()=>{let e=i.get();r(e,s)||(a.get(s)?.deref()?.set(!1),a.get(e)?.deref()?.set(!0),s=e)}),e=>{let n=a.get(e)?.deref();return n===void 0&&(n=t(r(e,s)),a.set(e,new WeakRef(n)),o.register(n,e)),n}}
|
|
2
|
+
//# sourceMappingURL=selector.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"selector.js","sources":["../../src/selector.ts"],"sourcesContent":["/**\n * A selector over a signal: one boolean signal per key, true for the key\n * the source holds, so a selection among many rows updates two rows\n * rather than every row's own comparison.\n */\nimport { type ReadableSignal, type Signal, effect, signal, untracked } from \"zerolith\";\n\n/** True when `Map` would treat `a` and `b` as one key. */\nfunction same(a: unknown, b: unknown): boolean\n{\n return a === b || (a !== a && b !== b);\n}\n\n/**\n * A per-key view of `source`: `selected(key)` is a signal that is true\n * while `source` holds `key`, and a change of `source` updates the two\n * keys concerned alone, so a list of a thousand rows that each bind\n * `selected(row.id)` runs two bindings on a selection, not a thousand\n * comparisons. Keys are compared as `Map` keys are. A key's signal is\n * shared by everyone who asks for it, and the selector keeps no hold of\n * its own: the signal lives while a view reads it and goes with the row\n * that did.\n */\nexport function selector<T>(source: ReadableSignal<T>): (key: T) => ReadableSignal<boolean>\n{\n const flags = new Map<T, WeakRef<Signal<boolean>>>();\n const gone = new FinalizationRegistry<T>((key) =>\n {\n if (flags.get(key)?.deref() === undefined)\n {\n flags.delete(key);\n }\n });\n let current = untracked(() => source.get());\n effect(() =>\n {\n const next = source.get();\n if (same(next, current))\n {\n return;\n }\n flags.get(current)?.deref()?.set(false);\n flags.get(next)?.deref()?.set(true);\n current = next;\n });\n return (key) =>\n {\n let flag = flags.get(key)?.deref();\n if (flag === undefined)\n {\n flag = signal(same(key, current));\n flags.set(key, new WeakRef(flag));\n gone.register(flag, key);\n }\n return flag;\n };\n}\n"],"names":["same","a","b","flags","gone","key","current","untracked","source","effect","next","flag","signal"],"mappings":"AAKA,OAA2C,YAAQ,YAAQ,mBAAiB,WAG5E,SAASA,EAAK,EAAY,EAAU,CAEhC,OAAOC,IAAMC,GAAMD,IAAMA,GAAKC,IAAMA,CACxC,CAYA,OAAM,SAAU,SAAY,EAAyB,CAEjD,IAAMC,EAAQ,IAAI,IACZC,EAAO,IAAI,qBAAyB,GAAO,CAEzCD,EAAM,IAAIE,CAAG,CAAC,EAAE,MAAK,IAAO,QAE5BF,EAAM,OAAOE,CAAG,CAExB,CAAC,EACGC,EAAUC,MAAgBC,EAAO,IAAG,CAAE,EAY1C,OAXAC,MAAY,CAER,IAAMC,EAAOF,EAAO,IAAG,EACnBR,EAAKU,EAAMJ,CAAO,IAItBH,EAAM,IAAIG,CAAO,CAAC,EAAE,MAAK,CAAE,EAAE,IAAI,EAAK,EACtCH,EAAM,IAAIO,CAAI,CAAC,EAAE,MAAK,CAAE,EAAE,IAAI,EAAI,EAClC,EAAUA,EACd,CAAC,EACO,GAAO,CAEX,IAAIC,EAAOR,EAAM,IAAIE,CAAG,CAAC,EAAE,MAAK,EAOhC,OANIM,IAAS,SAET,EAAOC,EAAOZ,EAAKK,EAAKC,CAAO,CAAC,EAChCH,EAAM,IAAIE,EAAK,IAAI,QAAQM,CAAI,CAAC,EAChCP,EAAK,SAASO,EAAMN,CAAG,GAEpBM,CACX,CACJ"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{diagnostic as e,effect as t,hasOwner as n,onDispose as r,report as i,signal as a}from"zerolith";const o=new Set,s=new Map;function c(t,n){try{let e=t===`session`?globalThis.sessionStorage:globalThis.localStorage;return e.getItem(``),e}catch{o.has(t)||(o.add(t),i(e(`state_storage_unavailable`,`warning`,{storage:t,key:n})));return}}function l(t,n,r){if(t===null)return r;try{return JSON.parse(t)}catch{return i(e(`state_storage_unreadable`,`warning`,{key:n})),r}}function u(e){try{return JSON.stringify(e)}catch{return}}export function stored(o,d,f={}){let p=f.storage??`local`,m=c(p,o),h=a(m===void 0?d:l(m.getItem(o),o,d),f.equals===void 0?void 0:{equals:f.equals});if(m===void 0)return h;let g=`${p}:${o}`,_=(s.get(g)??0)+1;s.set(g,_),_>1&&i(e(`state_key_duplicate`,`warning`,{storage:p,key:o}));let v=!0,y=!1;t(()=>{let t=h.get();if(v||y){v=!1;return}let n=u(t);if(n===void 0&&t!==void 0)i(e(`state_value_not_json`,`warning`,{key:o}));else try{n===void 0?m.removeItem(o):m.setItem(o,n)}catch{i(e(`state_storage_unavailable`,`warning`,{storage:p,key:o}))}});let b=e=>{if(!(e.storageArea!==m||e.key!==null&&e.key!==o)){y=!0;try{h.set(l(e.newValue,o,d))}finally{y=!1}}};return addEventListener(`storage`,b),n()&&r(()=>{removeEventListener(`storage`,b);let e=(s.get(g)??1)-1;e>0?s.set(g,e):s.delete(g)}),h}
|
|
2
|
+
//# sourceMappingURL=stored.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stored.js","sources":["../../src/stored.ts"],"sourcesContent":["/**\n * A signal kept in the browser's storage under a key, as JSON, and in step\n * across tabs through the `storage` event.\n */\nimport { type Signal, diagnostic, effect, hasOwner, onDispose, report, signal } from \"zerolith\";\n\n/** Options for `stored`. */\nexport interface StoredOptions<T>\n{\n /** Which storage keeps the value: `localStorage`, shared across tabs and kept across sessions, or `sessionStorage`, this tab's alone. Defaults to local. */\n storage?: \"local\" | \"session\";\n /** When two values count as the same, so a set of an equal value changes nothing; the signal's own option. */\n equals?: (previous: T, next: T) => boolean;\n}\n\n/** The storage kinds reported unavailable on this page, each once. */\nconst unavailable = new Set<string>();\n\n/** How many stored signals are live for each key of each storage, so a second one for a key is reported. */\nconst live = new Map<string, number>();\n\n/** The storage of a kind, or undefined where the page has none or refuses it, which is reported once per kind with the first key asked for. */\nfunction storageOf(kind: \"local\" | \"session\", key: string): Storage | undefined\n{\n try\n {\n const store = kind === \"session\" ? globalThis.sessionStorage : globalThis.localStorage;\n store.getItem(\"\");\n return store;\n }\n catch\n {\n if (!unavailable.has(kind))\n {\n unavailable.add(kind);\n report(diagnostic(\"state_storage_unavailable\", \"warning\", { storage: kind, key }));\n }\n return undefined;\n }\n}\n\n/** The value kept as `raw` under `key`, or `initial` where nothing is kept or what is kept does not parse, which is reported. */\nfunction parse<T>(raw: string | null, key: string, initial: T): T\n{\n if (raw === null)\n {\n return initial;\n }\n try\n {\n return JSON.parse(raw) as T;\n }\n catch\n {\n report(diagnostic(\"state_storage_unreadable\", \"warning\", { key }));\n return initial;\n }\n}\n\n/** The JSON for `next`, or undefined for a value JSON cannot hold: a BigInt or a cycle, which throws, and a function or a symbol, which it leaves out. */\nfunction toJson(next: unknown): string | undefined\n{\n try\n {\n return JSON.stringify(next);\n }\n catch\n {\n return undefined;\n }\n}\n\n/**\n * A writable signal kept in storage under `key` as JSON: read back on\n * creation, written on every set, and set again when another tab writes\n * the key, so every tab shows the same value. A missing value takes\n * `initial` quietly; one that does not parse takes `initial` and reports\n * `state_storage_unreadable`. Setting `undefined` removes the key, so it\n * reads back as `initial`; a value JSON cannot hold, such as a BigInt,\n * stays in memory and reports `state_value_not_json`. Where the page has\n * no storage, as on the server, the signal is a plain one and\n * `state_storage_unavailable` is reported once, as it is for each write\n * the storage refuses. Create each key once and share the signal: two\n * stored signals for one key in the same tab do not hear each other,\n * since the storage event tells other tabs alone, and the second reports\n * `state_key_duplicate`. The write and the listener for other tabs are\n * disposed with the enclosing owner when there is one, after which the\n * signal keeps its value in memory alone; created in plain code, they\n * live for the page.\n */\nexport function stored<T>(key: string, initial: T, options: StoredOptions<T> = {}): Signal<T>\n{\n const kind = options.storage ?? \"local\";\n const store = storageOf(kind, key);\n const value = signal<T>(store === undefined ? initial : parse(store.getItem(key), key, initial), options.equals === undefined ? undefined : { equals: options.equals });\n if (store === undefined)\n {\n return value;\n }\n const id = `${kind}:${key}`;\n const count = (live.get(id) ?? 0) + 1;\n live.set(id, count);\n if (count > 1)\n {\n report(diagnostic(\"state_key_duplicate\", \"warning\", { storage: kind, key }));\n }\n let first = true;\n let echo = false;\n effect(() =>\n {\n const next = value.get();\n if (first || echo)\n {\n first = false;\n return;\n }\n const json = toJson(next);\n if (json === undefined && next !== undefined)\n {\n report(diagnostic(\"state_value_not_json\", \"warning\", { key }));\n return;\n }\n try\n {\n if (json === undefined)\n {\n store.removeItem(key);\n }\n else\n {\n store.setItem(key, json);\n }\n }\n catch\n {\n report(diagnostic(\"state_storage_unavailable\", \"warning\", { storage: kind, key }));\n }\n });\n const onStorage = (event: StorageEvent): void =>\n {\n if (event.storageArea !== store || (event.key !== null && event.key !== key))\n {\n return;\n }\n echo = true;\n try\n {\n value.set(parse(event.newValue, key, initial));\n }\n finally\n {\n echo = false;\n }\n };\n addEventListener(\"storage\", onStorage);\n if (hasOwner())\n {\n onDispose(() =>\n {\n removeEventListener(\"storage\", onStorage);\n const left = (live.get(id) ?? 1) - 1;\n if (left > 0)\n {\n live.set(id, left);\n }\n else\n {\n live.delete(id);\n }\n });\n }\n return value;\n}\n"],"names":["unavailable","live","storageOf","store","kind","report","diagnostic","parse","raw","initial","toJson","next","options","key","value","signal","id","count","first","echo","effect","json","onStorage","event","hasOwner","onDispose","left"],"mappings":"AAIA,OAAsB,gBAAY,YAAQ,cAAU,eAAW,YAAQ,gBAAc,WAYrF,MAAMA,EAAc,IAAI,IAGlBC,EAAO,IAAI,IAGjB,SAASC,EAAU,EAA2B,EAAW,CAErD,GACA,CACI,IAAMC,EAAQC,IAAS,UAAY,WAAW,eAAiB,WAAW,aAE1E,OADAD,EAAM,QAAQ,EAAE,EACTA,CACX,MAEA,CACSH,EAAY,IAAII,CAAI,IAErBJ,EAAY,IAAII,CAAI,EACpBC,EAAOC,EAAW,4BAA6B,UAAW,CAAE,QAASF,EAAM,KAAG,CAAE,CAAC,GAErF,MACJ,CACJ,CAGA,SAASG,EAAS,EAAoB,EAAa,EAAU,CAEzD,GAAIC,IAAQ,KAER,OAAOC,EAEX,GACA,CACI,OAAO,KAAK,MAAMD,CAAG,CACzB,MAEA,CAEI,OADAH,EAAOC,EAAW,2BAA4B,UAAW,CAAE,KAAG,CAAE,CAAC,EAC1DG,CACX,CACJ,CAGA,SAASC,EAAO,EAAa,CAEzB,GACA,CACI,OAAO,KAAK,UAAUC,CAAI,CAC9B,MAEA,CACI,MACJ,CACJ,CAoBA,OAAM,SAAU,OAAU,EAAa,EAAY,EAA4B,CAAA,EAAE,CAE7E,IAAMP,EAAOQ,EAAQ,SAAW,QAC1BT,EAAQD,EAAUE,EAAMS,CAAG,EAC3BC,EAAQC,EAAUZ,IAAU,OAAYM,EAAUF,EAAMJ,EAAM,QAAQU,CAAG,EAAGA,EAAKJ,CAAO,EAAGG,EAAQ,SAAW,OAAY,OAAY,CAAE,OAAQA,EAAQ,MAAM,CAAE,EACtK,GAAIT,IAAU,OAEV,OAAOW,EAEX,IAAME,EAAK,GAAGZ,EAAI,GAAIS,IAChBI,GAAShB,EAAK,IAAIe,CAAE,GAAK,GAAK,EACpCf,EAAK,IAAIe,EAAIC,CAAK,EACdA,EAAQ,GAERZ,EAAOC,EAAW,sBAAuB,UAAW,CAAE,QAASF,EAAM,KAAG,CAAE,CAAC,EAE/E,IAAIc,EAAQ,GACRC,EAAO,GACXC,MAAY,CAER,IAAMT,EAAOG,EAAM,IAAG,EACtB,GAAII,GAASC,EACb,CACI,EAAQ,GACR,MACJ,CACA,IAAME,EAAOX,EAAOC,CAAI,EACxB,GAAIU,IAAS,QAAaV,IAAS,OAE/BN,EAAOC,EAAW,uBAAwB,UAAW,CAAE,KAAG,CAAE,CAAC,OAGjE,GACA,CACQe,IAAS,OAETlB,EAAM,WAAWU,CAAG,EAIpBV,EAAM,QAAQU,EAAKQ,CAAI,CAE/B,MAEA,CACIhB,EAAOC,EAAW,4BAA6B,UAAW,CAAE,QAASF,EAAM,KAAG,CAAE,CAAC,CACrF,CACJ,CAAC,EACD,IAAMkB,EAAa,GAA6B,CAE5C,GAAI,IAAM,cAAgBnB,GAAUoB,EAAM,MAAQ,MAAQA,EAAM,MAAQV,GAIxE,GAAO,GACP,GACA,CACIC,EAAM,IAAIP,EAAMgB,EAAM,SAAUV,EAAKJ,CAAO,CAAC,CACjD,QAEA,CACI,EAAO,EACX,CARW,CASf,EAkBA,OAjBA,iBAAiB,UAAWa,CAAS,EACjCE,EAAQ,GAERC,MAAe,CAEX,oBAAoB,UAAWH,CAAS,EACxC,IAAMI,GAAQzB,EAAK,IAAIe,CAAE,GAAK,GAAK,EAC/BU,EAAO,EAEPzB,EAAK,IAAIe,EAAIU,CAAI,EAIjBzB,EAAK,OAAOe,CAAE,CAEtB,CAAC,EAEEF,CACX"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{effect as e,hasOwner as t,onDispose as n,signal as r,untracked as i}from"zerolith";export function debounced(t,n){let a=r(i(()=>t.get())),o=!0;return e(()=>{let e=t.get();if(o){o=!1;return}let r=setTimeout(()=>a.set(e),n);return()=>clearTimeout(r)}),a}export function throttled(a,o){let s=r(i(()=>a.get())),c=-1/0,l,u,d=!0,f=()=>{l!==void 0&&(clearTimeout(l),l=void 0)};return e(()=>{let e=a.get();if(d){d=!1;return}let t=Date.now();t-c>=o?(f(),c=t,s.set(e)):(u=e,l??=setTimeout(()=>{l=void 0,c=Date.now(),s.set(u)},o-(t-c)))}),t()&&n(f),s}
|
|
2
|
+
//# sourceMappingURL=timing.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timing.js","sources":["../../src/timing.ts"],"sourcesContent":["/**\n * Signals that follow another in their own time: after a quiet spell, or\n * at most so often.\n */\nimport { type ReadableSignal, effect, hasOwner, onDispose, signal, untracked } from \"zerolith\";\n\n/**\n * A signal that follows `source` once it has been quiet for `ms`\n * milliseconds, starting at its current value, so a search runs for the\n * text a reader stopped typing rather than for every keystroke. The\n * pending change is dropped with the enclosing owner when there is one.\n */\nexport function debounced<T>(source: ReadableSignal<T>, ms: number): ReadableSignal<T>\n{\n const value = signal(untracked(() => source.get()));\n let first = true;\n effect(() =>\n {\n const next = source.get();\n if (first)\n {\n first = false;\n return undefined;\n }\n const timer = setTimeout(() => value.set(next), ms);\n return () => clearTimeout(timer);\n });\n return value;\n}\n\n/**\n * A signal that follows `source` at most once per `ms` milliseconds: the\n * first change in a window shows at once, and the last one in it shows\n * when the window ends, so a scroll position drives work at a steady rate.\n * A change still pending is dropped when a newer one shows after the\n * window, and with the enclosing owner when there is one.\n */\nexport function throttled<T>(source: ReadableSignal<T>, ms: number): ReadableSignal<T>\n{\n const value = signal(untracked(() => source.get()));\n let last = Number.NEGATIVE_INFINITY;\n let timer: ReturnType<typeof setTimeout> | undefined;\n let pending: T;\n let first = true;\n const clear = (): void =>\n {\n if (timer !== undefined)\n {\n clearTimeout(timer);\n timer = undefined;\n }\n };\n effect(() =>\n {\n const next = source.get();\n if (first)\n {\n first = false;\n return;\n }\n const now = Date.now();\n if (now - last >= ms)\n {\n clear();\n last = now;\n value.set(next);\n return;\n }\n pending = next;\n timer ??= setTimeout(() =>\n {\n timer = undefined;\n last = Date.now();\n value.set(pending);\n }, ms - (now - last));\n });\n if (hasOwner())\n {\n onDispose(clear);\n }\n return value;\n}\n"],"names":["value","signal","untracked","source","first","effect","next","timer","ms","last","pending","clear","now","hasOwner","onDispose"],"mappings":"AAIA,OAA8B,YAAQ,cAAU,eAAW,YAAQ,mBAAiB,WAQpF,OAAM,SAAU,UAAa,EAA2B,EAAU,CAE9D,IAAMA,EAAQC,EAAOC,MAAgBC,EAAO,IAAG,CAAE,CAAC,EAC9CC,EAAQ,GAYZ,OAXAC,MAAY,CAER,IAAMC,EAAOH,EAAO,IAAG,EACvB,GAAIC,EACJ,CACI,EAAQ,GACR,MACJ,CACA,IAAMG,EAAQ,eAAiBP,EAAM,IAAIM,CAAI,EAAGE,CAAE,EAClD,UAAa,aAAaD,CAAK,CACnC,CAAC,EACMP,CACX,CASA,OAAM,SAAU,UAAa,EAA2B,EAAU,CAE9D,IAAMA,EAAQC,EAAOC,MAAgBC,EAAO,IAAG,CAAE,CAAC,EAC9CM,EAAO,KACPF,EACAG,EACAN,EAAQ,GACNO,MAAmB,CAEjBJ,IAAU,SAEV,aAAaA,CAAK,EAClB,EAAQ,OAEhB,EA6BA,OA5BAF,MAAY,CAER,IAAMC,EAAOH,EAAO,IAAG,EACvB,GAAIC,EACJ,CACI,EAAQ,GACR,MACJ,CACA,IAAMQ,EAAM,KAAK,IAAG,EAChBA,EAAMH,GAAQD,GAEdG,EAAK,EACL,EAAOC,EACPZ,EAAM,IAAIM,CAAI,IAGlB,EAAUA,EACV,IAAU,eAAgB,CAEtB,EAAQ,OACR,EAAO,KAAK,IAAG,EACfN,EAAM,IAAIU,CAAO,CACrB,EAAGF,GAAMI,EAAMH,EAAK,EACxB,CAAC,EACGI,EAAQ,GAERC,EAAUH,CAAK,EAEZX,CACX"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A selector over a signal: one boolean signal per key, true for the key
|
|
3
|
+
* the source holds, so a selection among many rows updates two rows
|
|
4
|
+
* rather than every row's own comparison.
|
|
5
|
+
*/
|
|
6
|
+
import { type ReadableSignal } from "zerolith";
|
|
7
|
+
/**
|
|
8
|
+
* A per-key view of `source`: `selected(key)` is a signal that is true
|
|
9
|
+
* while `source` holds `key`, and a change of `source` updates the two
|
|
10
|
+
* keys concerned alone, so a list of a thousand rows that each bind
|
|
11
|
+
* `selected(row.id)` runs two bindings on a selection, not a thousand
|
|
12
|
+
* comparisons. Keys are compared as `Map` keys are. A key's signal is
|
|
13
|
+
* shared by everyone who asks for it, and the selector keeps no hold of
|
|
14
|
+
* its own: the signal lives while a view reads it and goes with the row
|
|
15
|
+
* that did.
|
|
16
|
+
*/
|
|
17
|
+
export declare function selector<T>(source: ReadableSignal<T>): (key: T) => ReadableSignal<boolean>;
|
|
18
|
+
//# sourceMappingURL=selector.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"selector.d.ts","sourceRoot":"","sources":["../src/selector.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,KAAK,cAAc,EAA0C,MAAM,UAAU,CAAC;AAQvF;;;;;;;;;GASG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,cAAc,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,KAAK,cAAc,CAAC,OAAO,CAAC,CAiC1F"}
|
package/dist/selector.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A selector over a signal: one boolean signal per key, true for the key
|
|
3
|
+
* the source holds, so a selection among many rows updates two rows
|
|
4
|
+
* rather than every row's own comparison.
|
|
5
|
+
*/
|
|
6
|
+
import { effect, signal, untracked } from "zerolith";
|
|
7
|
+
/** True when `Map` would treat `a` and `b` as one key. */
|
|
8
|
+
function same(a, b) {
|
|
9
|
+
return a === b || (a !== a && b !== b);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* A per-key view of `source`: `selected(key)` is a signal that is true
|
|
13
|
+
* while `source` holds `key`, and a change of `source` updates the two
|
|
14
|
+
* keys concerned alone, so a list of a thousand rows that each bind
|
|
15
|
+
* `selected(row.id)` runs two bindings on a selection, not a thousand
|
|
16
|
+
* comparisons. Keys are compared as `Map` keys are. A key's signal is
|
|
17
|
+
* shared by everyone who asks for it, and the selector keeps no hold of
|
|
18
|
+
* its own: the signal lives while a view reads it and goes with the row
|
|
19
|
+
* that did.
|
|
20
|
+
*/
|
|
21
|
+
export function selector(source) {
|
|
22
|
+
const flags = new Map();
|
|
23
|
+
const gone = new FinalizationRegistry((key) => {
|
|
24
|
+
if (flags.get(key)?.deref() === undefined) {
|
|
25
|
+
flags.delete(key);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
let current = untracked(() => source.get());
|
|
29
|
+
effect(() => {
|
|
30
|
+
const next = source.get();
|
|
31
|
+
if (same(next, current)) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
flags.get(current)?.deref()?.set(false);
|
|
35
|
+
flags.get(next)?.deref()?.set(true);
|
|
36
|
+
current = next;
|
|
37
|
+
});
|
|
38
|
+
return (key) => {
|
|
39
|
+
let flag = flags.get(key)?.deref();
|
|
40
|
+
if (flag === undefined) {
|
|
41
|
+
flag = signal(same(key, current));
|
|
42
|
+
flags.set(key, new WeakRef(flag));
|
|
43
|
+
gone.register(flag, key);
|
|
44
|
+
}
|
|
45
|
+
return flag;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=selector.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"selector.js","sourceRoot":"","sources":["../src/selector.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAoC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAEvF,0DAA0D;AAC1D,SAAS,IAAI,CAAC,CAAU,EAAE,CAAU;IAEhC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;AAC3C,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,QAAQ,CAAI,MAAyB;IAEjD,MAAM,KAAK,GAAG,IAAI,GAAG,EAA+B,CAAC;IACrD,MAAM,IAAI,GAAG,IAAI,oBAAoB,CAAI,CAAC,GAAG,EAAE,EAAE;QAE7C,IAAI,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,KAAK,SAAS,EACzC,CAAC;YACG,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACtB,CAAC;IACL,CAAC,CAAC,CAAC;IACH,IAAI,OAAO,GAAG,SAAS,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,CAAC;IAC5C,MAAM,CAAC,GAAG,EAAE;QAER,MAAM,IAAI,GAAG,MAAM,CAAC,GAAG,EAAE,CAAC;QAC1B,IAAI,IAAI,CAAC,IAAI,EAAE,OAAO,CAAC,EACvB,CAAC;YACG,OAAO;QACX,CAAC;QACD,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,KAAK,EAAE,EAAE,GAAG,CAAC,KAAK,CAAC,CAAC;QACxC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,EAAE,GAAG,CAAC,IAAI,CAAC,CAAC;QACpC,OAAO,GAAG,IAAI,CAAC;IACnB,CAAC,CAAC,CAAC;IACH,OAAO,CAAC,GAAG,EAAE,EAAE;QAEX,IAAI,IAAI,GAAG,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,CAAC;QACnC,IAAI,IAAI,KAAK,SAAS,EACtB,CAAC;YACG,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,CAAC;YAClC,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;YAClC,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;QAC7B,CAAC;QACD,OAAO,IAAI,CAAC;IAChB,CAAC,CAAC;AACN,CAAC"}
|
package/dist/stored.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A signal kept in the browser's storage under a key, as JSON, and in step
|
|
3
|
+
* across tabs through the `storage` event.
|
|
4
|
+
*/
|
|
5
|
+
import { type Signal } from "zerolith";
|
|
6
|
+
/** Options for `stored`. */
|
|
7
|
+
export interface StoredOptions<T> {
|
|
8
|
+
/** Which storage keeps the value: `localStorage`, shared across tabs and kept across sessions, or `sessionStorage`, this tab's alone. Defaults to local. */
|
|
9
|
+
storage?: "local" | "session";
|
|
10
|
+
/** When two values count as the same, so a set of an equal value changes nothing; the signal's own option. */
|
|
11
|
+
equals?: (previous: T, next: T) => boolean;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* A writable signal kept in storage under `key` as JSON: read back on
|
|
15
|
+
* creation, written on every set, and set again when another tab writes
|
|
16
|
+
* the key, so every tab shows the same value. A missing value takes
|
|
17
|
+
* `initial` quietly; one that does not parse takes `initial` and reports
|
|
18
|
+
* `state_storage_unreadable`. Setting `undefined` removes the key, so it
|
|
19
|
+
* reads back as `initial`; a value JSON cannot hold, such as a BigInt,
|
|
20
|
+
* stays in memory and reports `state_value_not_json`. Where the page has
|
|
21
|
+
* no storage, as on the server, the signal is a plain one and
|
|
22
|
+
* `state_storage_unavailable` is reported once, as it is for each write
|
|
23
|
+
* the storage refuses. Create each key once and share the signal: two
|
|
24
|
+
* stored signals for one key in the same tab do not hear each other,
|
|
25
|
+
* since the storage event tells other tabs alone, and the second reports
|
|
26
|
+
* `state_key_duplicate`. The write and the listener for other tabs are
|
|
27
|
+
* disposed with the enclosing owner when there is one, after which the
|
|
28
|
+
* signal keeps its value in memory alone; created in plain code, they
|
|
29
|
+
* live for the page.
|
|
30
|
+
*/
|
|
31
|
+
export declare function stored<T>(key: string, initial: T, options?: StoredOptions<T>): Signal<T>;
|
|
32
|
+
//# sourceMappingURL=stored.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stored.d.ts","sourceRoot":"","sources":["../src/stored.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,KAAK,MAAM,EAA2D,MAAM,UAAU,CAAC;AAEhG,4BAA4B;AAC5B,MAAM,WAAW,aAAa,CAAC,CAAC;IAE5B,4JAA4J;IAC5J,OAAO,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IAC9B,8GAA8G;IAC9G,MAAM,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC;CAC9C;AA2DD;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,MAAM,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAE,aAAa,CAAC,CAAC,CAAM,GAAG,MAAM,CAAC,CAAC,CAAC,CAkF5F"}
|