atom.io 0.14.2 → 0.14.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.
@@ -0,0 +1,459 @@
1
+ declare class Subject<T> {
2
+ Subscriber: (value: T) => void;
3
+ subscribers: Map<string, this[`Subscriber`]>;
4
+ subscribe(key: string, subscriber: this[`Subscriber`]): () => void;
5
+ private unsubscribe;
6
+ next(value: T): void;
7
+ }
8
+ declare class StatefulSubject<T> extends Subject<T> {
9
+ state: T;
10
+ constructor(initialState: T);
11
+ next(value: T): void;
12
+ }
13
+
14
+ type Selector<T> = {
15
+ key: string;
16
+ type: `selector`;
17
+ family?: FamilyMetadata;
18
+ install: (store: Store) => void;
19
+ subject: Subject<{
20
+ newValue: T;
21
+ oldValue: T;
22
+ }>;
23
+ get: () => T;
24
+ set: (newValue: T | ((oldValue: T) => T)) => void;
25
+ };
26
+ type ReadonlySelector<T> = {
27
+ key: string;
28
+ type: `readonly_selector`;
29
+ family?: FamilyMetadata;
30
+ install: (store: Store) => void;
31
+ subject: Subject<{
32
+ newValue: T;
33
+ oldValue: T;
34
+ }>;
35
+ get: () => T;
36
+ };
37
+
38
+ type Transaction<ƒ extends ƒn> = {
39
+ key: string;
40
+ type: `transaction`;
41
+ install: (store: Store) => void;
42
+ subject: Subject<TransactionUpdate<ƒ>>;
43
+ run: (...parameters: Parameters<ƒ>) => ReturnType<ƒ>;
44
+ };
45
+
46
+ type TransactionMeta<ƒ extends ƒn> = {
47
+ phase: `applying` | `building`;
48
+ time: number;
49
+ update: TransactionUpdate<ƒ>;
50
+ };
51
+
52
+ type primitive = boolean | number | string | null;
53
+
54
+ type Serializable = primitive | Readonly<{
55
+ [key: string]: Serializable;
56
+ }> | ReadonlyArray<Serializable>;
57
+ type Object$1<Key extends string = string, Value extends Serializable = Serializable> = Record<Key, Value>;
58
+
59
+ type Refinement<Unrefined, Refined extends Unrefined> = (value: Unrefined) => value is Refined;
60
+ type Cardinality = `1:1` | `1:n` | `n:n`;
61
+
62
+ interface JunctionEntries<Content extends Object$1 | null> extends Object$1 {
63
+ readonly relations: [string, string[]][];
64
+ readonly contents: [string, Content][];
65
+ }
66
+ interface JunctionSchema<ASide extends string, BSide extends string> extends Object$1 {
67
+ readonly between: [a: ASide, b: BSide];
68
+ readonly cardinality: Cardinality;
69
+ }
70
+ type BaseExternalStoreConfiguration = {
71
+ addRelation: (a: string, b: string) => void;
72
+ deleteRelation: (a: string, b: string) => void;
73
+ replaceRelationsSafely: (a: string, bs: string[]) => void;
74
+ replaceRelationsUnsafely: (a: string, bs: string[]) => void;
75
+ getRelatedKeys: (key: string) => Set<string> | undefined;
76
+ has: (a: string, b?: string) => boolean;
77
+ };
78
+ type ExternalStoreWithContentConfiguration<Content extends Object$1> = {
79
+ getContent: (contentKey: string) => Content | undefined;
80
+ setContent: (contentKey: string, content: Content) => void;
81
+ deleteContent: (contentKey: string) => void;
82
+ };
83
+ type Empty<Obj extends object> = {
84
+ [Key in keyof Obj]?: undefined;
85
+ };
86
+ type ExternalStoreConfiguration<Content extends Object$1 | null> = Content extends Object$1 ? BaseExternalStoreConfiguration & ExternalStoreWithContentConfiguration<Content> : BaseExternalStoreConfiguration & Empty<ExternalStoreWithContentConfiguration<Object$1>>;
87
+ type JunctionAdvancedConfiguration<Content extends Object$1 | null> = {
88
+ externalStore?: ExternalStoreConfiguration<Content>;
89
+ isContent?: Refinement<unknown, Content>;
90
+ makeContentKey?: (a: string, b: string) => string;
91
+ };
92
+ type JunctionJSON<ASide extends string, BSide extends string, Content extends Object$1 | null> = JunctionEntries<Content> & JunctionSchema<ASide, BSide>;
93
+ declare class Junction<const ASide extends string, const BSide extends string, const Content extends Object$1 | null = null> {
94
+ readonly a: ASide;
95
+ readonly b: BSide;
96
+ readonly cardinality: Cardinality;
97
+ readonly relations: Map<string, Set<string>>;
98
+ readonly contents: Map<string, Content>;
99
+ isContent: Refinement<unknown, Content> | null;
100
+ makeContentKey: (a: string, b: string) => string;
101
+ getRelatedKeys(key: string): Set<string> | undefined;
102
+ protected addRelation(a: string, b: string): void;
103
+ protected deleteRelation(a: string, b: string): void;
104
+ protected replaceRelationsUnsafely(a: string, bs: string[]): void;
105
+ protected replaceRelationsSafely(a: string, bs: string[]): void;
106
+ protected getContentInternal(contentKey: string): Content | undefined;
107
+ protected setContent(contentKey: string, content: Content): void;
108
+ protected deleteContent(contentKey: string): void;
109
+ constructor(data: JunctionSchema<ASide, BSide> & Partial<JunctionEntries<Content>>, config?: JunctionAdvancedConfiguration<Content>);
110
+ toJSON(): JunctionJSON<ASide, BSide, Content>;
111
+ set(a: string, ...rest: Content extends null ? [b: string] : [b: string, content: Content]): this;
112
+ set(relation: {
113
+ [Key in ASide | BSide]: string;
114
+ }, ...rest: Content extends null ? [] | [b?: undefined] : [content: Content]): this;
115
+ delete(a: string, b?: string): this;
116
+ delete(relation: Record<ASide | BSide, string> | Record<ASide, string> | Record<BSide, string>, b?: undefined): this;
117
+ getRelatedKey(key: string): string | undefined;
118
+ replaceRelations(a: string, relations: Content extends null ? string[] : Record<string, Content>, config?: {
119
+ reckless: boolean;
120
+ }): this;
121
+ getContent(a: string, b: string): Content | undefined;
122
+ getRelationEntries(input: Record<ASide, string> | Record<BSide, string>): [string, Content][];
123
+ has(a: string, b?: string): boolean;
124
+ }
125
+
126
+ interface Lineage {
127
+ parent: typeof this | null;
128
+ child: typeof this | null;
129
+ }
130
+
131
+ type Stringified<J extends Serializable> = string & {
132
+ __json: J;
133
+ };
134
+
135
+ interface Transceiver<Signal extends Serializable> {
136
+ do: (update: Signal) => void;
137
+ undo: (update: Signal) => void;
138
+ subscribe: (key: string, fn: (update: Signal) => void) => () => void;
139
+ cacheUpdateNumber: number;
140
+ getUpdateNumber: (update: Signal) => number;
141
+ }
142
+
143
+ /**
144
+ * @internal Give the tracker a transceiver state and a store, and it will
145
+ * subscribe to the transceiver's inner value. When the inner value changes,
146
+ * the tracker will update its own state to reflect the change.
147
+ */
148
+ declare class Tracker<Mutable extends Transceiver<any>> {
149
+ private Update;
150
+ private initializeState;
151
+ private unsubscribeFromInnerValue;
152
+ private observeCore;
153
+ private updateCore;
154
+ mutableState: MutableAtomToken<Mutable, Serializable>;
155
+ latestUpdateState: AtomToken<typeof this.Update | null>;
156
+ constructor(mutableState: MutableAtomToken<Mutable, Serializable>, store: Store);
157
+ }
158
+
159
+ interface MutableAtom<T> extends Atom<T> {
160
+ mutable: true;
161
+ }
162
+
163
+ type OperationProgress = {
164
+ open: false;
165
+ } | {
166
+ open: true;
167
+ done: Set<string>;
168
+ prev: Map<string, any>;
169
+ time: number;
170
+ token: StateToken<any>;
171
+ };
172
+
173
+ type TimelineAtomUpdate = StateUpdate<unknown> & {
174
+ key: string;
175
+ type: `atom_update`;
176
+ timestamp: number;
177
+ family?: FamilyMetadata;
178
+ };
179
+ type TimelineSelectorUpdate = {
180
+ key: string;
181
+ type: `selector_update`;
182
+ timestamp: number;
183
+ atomUpdates: Omit<TimelineAtomUpdate, `timestamp`>[];
184
+ };
185
+ type TimelineTransactionUpdate = TransactionUpdate<ƒn> & {
186
+ key: string;
187
+ type: `transaction_update`;
188
+ timestamp: number;
189
+ };
190
+ type Timeline = {
191
+ type: `timeline`;
192
+ key: string;
193
+ at: number;
194
+ shouldCapture?: (update: TimelineUpdate, timeline: Timeline) => boolean;
195
+ timeTraveling: `into_future` | `into_past` | null;
196
+ history: TimelineUpdate[];
197
+ selectorTime: number | null;
198
+ transactionKey: string | null;
199
+ install: (store: Store) => void;
200
+ subject: Subject<TimelineAtomUpdate | TimelineSelectorUpdate | TimelineTransactionUpdate | `redo` | `undo`>;
201
+ };
202
+
203
+ declare class Store implements Lineage {
204
+ parent: Store | null;
205
+ child: Store | null;
206
+ valueMap: Map<string, any>;
207
+ atoms: Map<string, Atom<any> | MutableAtom<any>>;
208
+ selectors: Map<string, Selector<any>>;
209
+ readonlySelectors: Map<string, ReadonlySelector<any>>;
210
+ trackers: Map<string, Tracker<Transceiver<any>>>;
211
+ families: Map<string, AtomFamily<any, any> | ReadonlySelectorFamily<any, any> | SelectorFamily<any, any>>;
212
+ timelines: Map<string, Timeline>;
213
+ transactions: Map<string, Transaction<ƒn>>;
214
+ atomsThatAreDefault: Set<string>;
215
+ timelineAtoms: Junction<"timelineKey", "atomKey", null>;
216
+ selectorAtoms: Junction<"selectorKey", "atomKey", null>;
217
+ selectorGraph: Junction<"upstreamSelectorKey", "downstreamSelectorKey", {
218
+ source: string;
219
+ }>;
220
+ subject: {
221
+ atomCreation: Subject<AtomToken<unknown>>;
222
+ selectorCreation: Subject<ReadonlySelectorToken<unknown> | SelectorToken<unknown>>;
223
+ transactionCreation: Subject<TransactionToken<ƒn>>;
224
+ timelineCreation: Subject<TimelineToken>;
225
+ transactionApplying: StatefulSubject<TransactionMeta<ƒn> | null>;
226
+ operationStatus: Subject<OperationProgress>;
227
+ };
228
+ operation: OperationProgress;
229
+ transactionMeta: TransactionMeta<ƒn> | null;
230
+ config: {
231
+ name: string;
232
+ };
233
+ loggers: AtomIOLogger[];
234
+ logger: Logger;
235
+ constructor(name: string, store?: Store | null);
236
+ }
237
+
238
+ type Atom<T> = {
239
+ key: string;
240
+ type: `atom`;
241
+ mutable?: boolean;
242
+ family?: FamilyMetadata;
243
+ install: (store: Store) => void;
244
+ subject: Subject<{
245
+ newValue: T;
246
+ oldValue: T;
247
+ }>;
248
+ default: T | (() => T);
249
+ cleanup?: () => void;
250
+ };
251
+
252
+ type AtomFamily<T, K extends Serializable = Serializable> = ((key: K) => AtomToken<T>) & {
253
+ key: string;
254
+ type: `atom_family`;
255
+ subject: Subject<AtomToken<T>>;
256
+ mutable?: boolean;
257
+ };
258
+
259
+ declare const LoggerIconDictionary: {
260
+ readonly "\u231B": "Timeline event fully captured";
261
+ readonly "\u23E9": "Timeline redo";
262
+ readonly "\u23EA": "Timeline undo";
263
+ readonly "\u23ED\uFE0F": "Transaction redo";
264
+ readonly "\u23EE\uFE0F": "Transaction undo";
265
+ readonly "\u23F3": "Timeline event partially captured";
266
+ readonly "\u23F9\uFE0F": "Time-travel complete";
267
+ readonly "\uD83D\uDC81": "Notice";
268
+ readonly "\uD83D\uDD04": "Realtime transaction synchronized";
269
+ readonly "\u2705": "Realtime transaction success";
270
+ readonly "\u2728": "Computation complete";
271
+ readonly "\u274C": "Conflict prevents attempted action";
272
+ readonly "\u2B55": "Operation start";
273
+ readonly "\uD83D\uDC1E": "Possible bug in AtomIO";
274
+ readonly "\uD83D\uDC40": "Subscription added";
275
+ readonly "\uD83D\uDC6A": "Family member added";
276
+ readonly "\uD83D\uDCC1": "Stow update";
277
+ readonly "\uD83D\uDCC3": "Copy mutable";
278
+ readonly "\uD83D\uDCD6": "Read state";
279
+ readonly "\uD83D\uDCDD": "Write state";
280
+ readonly "\uD83D\uDCE2": "Notify subscribers";
281
+ readonly "\uD83D\uDD0C": "Register dependency";
282
+ readonly "\uD83D\uDD0D": "Discover root";
283
+ readonly "\uD83D\uDD25": "Delete state";
284
+ readonly "\uD83D\uDD27": "Create mutable atom";
285
+ readonly "\uD83D\uDD28": "Create immutable atom";
286
+ readonly "\uD83D\uDD34": "Operation complete";
287
+ readonly "\uD83D\uDDD1": "Evict cached value";
288
+ readonly "\uD83D\uDCA5": "Caught";
289
+ readonly "\uD83D\uDE48": "Subscription canceled";
290
+ readonly "\uD83D\uDEC4": "Apply transaction";
291
+ readonly "\uD83D\uDEE0\uFE0F": "Install atom into store";
292
+ readonly "\uD83D\uDEEB": "Begin transaction";
293
+ readonly "\uD83D\uDEEC": "Complete transaction";
294
+ readonly "\uD83E\uDDEE": "Computing selector";
295
+ readonly "\uD83E\uDDF9": "Prepare to evict";
296
+ readonly "\uD83E\uDE82": "Abort transaction";
297
+ };
298
+ type LoggerIcon = keyof typeof LoggerIconDictionary;
299
+ declare const LOG_LEVELS: readonly ["info", "warn", "error"];
300
+ type LogLevel = (typeof LOG_LEVELS)[number];
301
+ type LogFn = (icon: LoggerIcon, tokenType: `atom` | `readonly_selector` | `selector` | `state` | `timeline` | `transaction` | `unknown`, tokenKey: string, message: string, ...rest: unknown[]) => void;
302
+ type LogFilter = (...params: Parameters<LogFn>) => boolean;
303
+ type Logger = Record<LogLevel, LogFn>;
304
+ declare class AtomIOLogger implements Logger {
305
+ logLevel: `error` | `info` | `warn` | null;
306
+ private readonly filter?;
307
+ private readonly logger;
308
+ constructor(logLevel: `error` | `info` | `warn` | null, filter?: LogFilter | undefined, logger?: Logger);
309
+ error: LogFn;
310
+ info: LogFn;
311
+ warn: LogFn;
312
+ }
313
+
314
+ type TransactionToken<_> = {
315
+ key: string;
316
+ type: `transaction`;
317
+ __brand?: _;
318
+ };
319
+ type TransactionUpdate<ƒ extends ƒn> = {
320
+ key: string;
321
+ updates: (KeyedStateUpdate<unknown> | TransactionUpdate<ƒn>)[];
322
+ params: Parameters<ƒ>;
323
+ output: ReturnType<ƒ>;
324
+ };
325
+
326
+ type SelectorFamily<T, K extends Serializable = Serializable> = ((key: K) => SelectorToken<T>) & {
327
+ key: string;
328
+ type: `selector_family`;
329
+ subject: Subject<SelectorToken<T>>;
330
+ };
331
+ type ReadonlySelectorFamily<T, K extends Serializable = Serializable> = ((key: K) => ReadonlySelectorToken<T>) & {
332
+ key: string;
333
+ type: `readonly_selector_family`;
334
+ subject: Subject<ReadonlySelectorToken<T>>;
335
+ };
336
+
337
+ type StateUpdate<T> = {
338
+ newValue: T;
339
+ oldValue: T;
340
+ };
341
+ type KeyedStateUpdate<T> = StateUpdate<T> & {
342
+ key: string;
343
+ family?: FamilyMetadata;
344
+ };
345
+
346
+ type TimelineToken = {
347
+ key: string;
348
+ type: `timeline`;
349
+ };
350
+ type TimelineUpdate = TimelineAtomUpdate | TimelineSelectorUpdate | TimelineTransactionUpdate;
351
+
352
+ type ƒn = (...parameters: any[]) => any;
353
+ type AtomToken<_> = {
354
+ key: string;
355
+ type: `atom`;
356
+ family?: FamilyMetadata;
357
+ __brand?: _;
358
+ };
359
+ interface MutableAtomToken<T extends Transceiver<any>, J extends Serializable> extends AtomToken<T> {
360
+ __asJSON?: J;
361
+ __update?: T extends Transceiver<infer Update> ? Update : never;
362
+ }
363
+ type SelectorToken<_> = {
364
+ key: string;
365
+ type: `selector`;
366
+ family?: FamilyMetadata;
367
+ __brand?: _;
368
+ };
369
+ type StateToken<T> = AtomToken<T> | SelectorToken<T>;
370
+ type ReadonlySelectorToken<_> = {
371
+ key: string;
372
+ type: `readonly_selector`;
373
+ family?: FamilyMetadata;
374
+ __brand?: _;
375
+ };
376
+ type FamilyMetadata = {
377
+ key: string;
378
+ subKey: string;
379
+ };
380
+
381
+ declare function dict<State, Key extends Serializable>(findState: AtomFamily<State, Key> | ReadonlySelectorFamily<State, Key> | SelectorFamily<State, Key>, index: AtomToken<Key[]> | ReadonlySelectorToken<Key[]> | SelectorToken<Key[]>, store?: Store): ReadonlySelectorToken<{
382
+ [K in Stringified<Key>]: State;
383
+ }>;
384
+
385
+ interface JoinOptions<ASide extends string, BSide extends string, Cardinality extends Cardinality, Content extends Object$1 | null> extends Object$1, JunctionSchema<ASide, BSide>, Partial<JunctionEntries<Content>> {
386
+ readonly key: string;
387
+ readonly cardinality: Cardinality;
388
+ }
389
+ type JoinState<ASide extends string, BSide extends string, Cardinality extends Cardinality, Content extends Object$1 | null> = Cardinality extends `1:1` ? (Content extends Object$1 ? {
390
+ readonly [AB in ASide | BSide as AB extends ASide ? `${AB}EntryOf${Capitalize<BSide>}` : `${AB}EntryOf${Capitalize<ASide>}`]: SelectorFamily<[
391
+ string,
392
+ Content
393
+ ] | undefined, string>;
394
+ } : {}) & {
395
+ readonly [AB in ASide | BSide as AB extends ASide ? `${AB}KeyOf${Capitalize<BSide>}` : `${AB}KeyOf${Capitalize<ASide>}`]: SelectorFamily<string | undefined, string>;
396
+ } : Cardinality extends `1:n` ? (Content extends Object$1 ? {
397
+ readonly [A in ASide as `${A}EntryOf${Capitalize<BSide>}`]: SelectorFamily<[
398
+ string,
399
+ Content
400
+ ] | undefined, string>;
401
+ } & {
402
+ readonly [B in BSide as `${B}EntriesOf${Capitalize<ASide>}`]: SelectorFamily<[
403
+ string,
404
+ Content
405
+ ][], string>;
406
+ } : {}) & {
407
+ readonly [A in ASide as `${A}KeyOf${Capitalize<BSide>}`]: SelectorFamily<string | undefined, string>;
408
+ } & {
409
+ readonly [B in BSide as `${B}KeysOf${Capitalize<ASide>}`]: SelectorFamily<string[], string>;
410
+ } : Cardinality extends `n:n` ? (Content extends Object$1 ? {
411
+ readonly [AB in ASide | BSide as AB extends ASide ? `${AB}EntriesOf${Capitalize<BSide>}` : `${AB}EntriesOf${Capitalize<ASide>}`]: SelectorFamily<[
412
+ string,
413
+ Content
414
+ ][], string>;
415
+ } : {}) & {
416
+ readonly [AB in ASide | BSide as AB extends ASide ? `${AB}KeysOf${Capitalize<BSide>}` : `${AB}KeysOf${Capitalize<ASide>}`]: SelectorFamily<string[], string>;
417
+ } : never;
418
+ declare function join<const ASide extends string, const BSide extends string, const Cardinality extends Cardinality>(options: JoinOptions<ASide, BSide, Cardinality, null>, defaultContent?: undefined, store?: Store): {
419
+ relations: Junction<ASide, BSide>;
420
+ findState: JoinState<ASide, BSide, Cardinality, null>;
421
+ };
422
+ declare function join<const ASide extends string, const Cardinality extends `1:1` | `1:n` | `n:n`, const BSide extends string, const Content extends Object$1>(options: JoinOptions<ASide, BSide, Cardinality, Content>, defaultContent: Content, store?: Store): {
423
+ relations: Junction<ASide, BSide, Content>;
424
+ findState: JoinState<ASide, BSide, Cardinality, Content>;
425
+ };
426
+
427
+ declare function struct<Struct extends {
428
+ [key: string]: unknown;
429
+ }, Key extends string>(options: {
430
+ key: Key;
431
+ default: Struct;
432
+ }, store?: Store): [
433
+ {
434
+ [K in keyof Struct as `${Key}${Capitalize<K & string>}State`]: AtomToken<Struct[K]>;
435
+ },
436
+ ReadonlySelectorToken<Struct>
437
+ ];
438
+
439
+ declare function structFamily<Struct extends object, Key extends string>(options: {
440
+ key: Key;
441
+ default: Struct;
442
+ }): [
443
+ {
444
+ [K in keyof Struct as `find${Capitalize<Key & string>}${Capitalize<K & string>}State`]: AtomFamily<Struct[K], string>;
445
+ },
446
+ ReadonlySelectorFamily<Struct>
447
+ ];
448
+
449
+ type Loadable<T> = Promise<T> | T;
450
+ type Fated<T, E extends Error = Error> = Loadable<E | T>;
451
+ /**
452
+ * Utility for handling loadable values
453
+ * @param loadable Loadable value
454
+ * @param fallback Fallback value until Loadable is resolved
455
+ * @returns Fallback value if your loadable is a promise, otherwise the loadable's resolved value
456
+ */
457
+ declare function until<T>(loadable: Loadable<T>, fallback: T): T;
458
+
459
+ export { type Fated, type JoinOptions, type JoinState, type Loadable, dict, join, struct, structFamily, until };
@@ -29,33 +29,27 @@ function useTL(token) {
29
29
  const store = React2.useContext(StoreContext);
30
30
  const id = React2.useId();
31
31
  const timeline = withdraw(token, store);
32
- if (timeline === void 0) {
33
- store.logger.error(
34
- `\u274C`,
35
- `timeline`,
36
- token.key,
37
- `Failed to use timeline because it does not exist`
38
- );
39
- return {
40
- at: NaN,
41
- length: NaN,
42
- undo: () => {
43
- },
44
- redo: () => {
45
- }
46
- };
47
- }
48
- const meta = React2.useRef({
49
- at: timeline.at,
50
- length: timeline.history.length,
32
+ const base = React2.useRef({
51
33
  undo: () => undo(token),
52
34
  redo: () => redo(token)
53
35
  });
36
+ const rebuildMeta = React2.useCallback(() => {
37
+ var _a, _b;
38
+ return Object.assign(
39
+ {
40
+ at: (_a = timeline == null ? void 0 : timeline.at) != null ? _a : NaN,
41
+ length: (_b = timeline == null ? void 0 : timeline.history.length) != null ? _b : NaN
42
+ },
43
+ base.current
44
+ );
45
+ }, []);
46
+ const meta = React2.useRef(rebuildMeta());
54
47
  const retrieve = React2.useCallback(() => {
55
- meta.current.at = timeline.at;
56
- meta.current.length = timeline.history.length;
48
+ if (meta.current.at !== (timeline == null ? void 0 : timeline.at) || meta.current.length !== (timeline == null ? void 0 : timeline.history.length)) {
49
+ meta.current = rebuildMeta();
50
+ }
57
51
  return meta.current;
58
- }, [meta]);
52
+ }, []);
59
53
  return React2.useSyncExternalStore(
60
54
  (dispatch) => subscribeToTimeline(token, dispatch, `use-tl:${id}`, store),
61
55
  retrieve,
@@ -65,4 +59,4 @@ function useTL(token) {
65
59
 
66
60
  export { StoreContext, StoreProvider, useI, useJSON, useO, useTL };
67
61
  //# sourceMappingURL=out.js.map
68
- //# sourceMappingURL=chunk-CWKKQKVQ.js.map
62
+ //# sourceMappingURL=chunk-HQWWV67P.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../react/src/store-context.tsx","../react/src/store-hooks.ts"],"names":["React"],"mappings":";;;;;;;;;;;;;AAEA,YAAY,WAAW;AAQtB;AANM,IAAM,eAAqB,oBAAqB,SAAS,KAAK;AAE9D,IAAM,gBAGR,CAAC,EAAE,UAAU,QAAQ,SAAS,MAAM,MACxC,oBAAC,aAAa,UAAb,EAAsB,OAAO,OAAQ,UAAS;;;ACVhD,YAAYA,YAAW;AAmBhB,SAAS,KACf,OACyD;AACzD,QAAM,QAAc,kBAAW,YAAY;AAC3C,QAAM,SAEI,cAAO,IAAI;AACrB,MAAI,OAAO,YAAY,MAAM;AAC5B,WAAO,UAAU,CAAC,SAAS,SAAS,OAAO,MAAM,KAAK;AAAA,EACvD;AACA,SAAO,OAAO;AACf;AAEO,SAAS,KAAQ,OAAoD;AAC3E,QAAM,QAAc,kBAAW,YAAY;AAC3C,QAAM,KAAW,aAAM;AACvB,SAAa;AAAA,IACZ,CAAC,aAAa,iBAAiB,OAAO,UAAU,SAAS,EAAE,IAAI,KAAK;AAAA,IACpE,MAAM,SAAS,OAAO,KAAK;AAAA,IAC3B,MAAM,SAAS,OAAO,KAAK;AAAA,EAC5B;AACD;AAEO,SAAS,QACf,OACe;AACf,QAAM,YAAY,aAAa,KAAK;AACpC,SAAO,KAAK,SAAS;AACtB;AASO,SAAS,MAAM,OAAoC;AACzD,QAAM,QAAc,kBAAW,YAAY;AAC3C,QAAM,KAAW,aAAM;AACvB,QAAM,WAAW,SAAS,OAAO,KAAK;AACtC,QAAM,OAAa,cAAO;AAAA,IACzB,MAAM,MAAM,KAAK,KAAK;AAAA,IACtB,MAAM,MAAM,KAAK,KAAK;AAAA,EACvB,CAAC;AACD,QAAM,cAAoB,mBAAY,MAAM;AAhE7C;AAiEE,WAAO,OAAO;AAAA,MACb;AAAA,QACC,KAAI,0CAAU,OAAV,YAAgB;AAAA,QACpB,SAAQ,0CAAU,QAAQ,WAAlB,YAA4B;AAAA,MACrC;AAAA,MACA,KAAK;AAAA,IACN;AAAA,EACD,GAAG,CAAC,CAAC;AACL,QAAM,OAAa,cAAqB,YAAY,CAAC;AACrD,QAAM,WAAiB,mBAAY,MAAM;AACxC,QACC,KAAK,QAAQ,QAAO,qCAAU,OAC9B,KAAK,QAAQ,YAAW,qCAAU,QAAQ,SACzC;AACD,WAAK,UAAU,YAAY;AAAA,IAC5B;AACA,WAAO,KAAK;AAAA,EACb,GAAG,CAAC,CAAC;AACL,SAAa;AAAA,IACZ,CAAC,aAAa,oBAAoB,OAAO,UAAU,UAAU,EAAE,IAAI,KAAK;AAAA,IACxE;AAAA,IACA;AAAA,EACD;AACD","sourcesContent":["import type { Store } from \"atom.io/internal\"\nimport { IMPLICIT } from \"atom.io/internal\"\nimport * as React from \"react\"\n\nexport const StoreContext = React.createContext<Store>(IMPLICIT.STORE)\n\nexport const StoreProvider: React.FC<{\n\tchildren: React.ReactNode\n\tstore?: Store\n}> = ({ children, store = IMPLICIT.STORE }) => (\n\t<StoreContext.Provider value={store}>{children}</StoreContext.Provider>\n)\n","import * as React from \"react\"\n\nimport { getState, redo, setState, undo } from \"atom.io\"\nimport type {\n\tMutableAtomToken,\n\tReadonlySelectorToken,\n\tStateToken,\n\tTimelineToken,\n} from \"atom.io\"\n\nimport {\n\tgetJsonToken,\n\tsubscribeToState,\n\tsubscribeToTimeline,\n\twithdraw,\n} from \"atom.io/internal\"\nimport type { Json } from \"atom.io/json\"\nimport { StoreContext } from \"./store-context\"\n\nexport function useI<T>(\n\ttoken: StateToken<T>,\n): <New extends T>(next: New | ((old: T) => New)) => void {\n\tconst store = React.useContext(StoreContext)\n\tconst setter: React.MutableRefObject<\n\t\t(<New extends T>(next: New | ((old: T) => New)) => void) | null\n\t> = React.useRef(null)\n\tif (setter.current === null) {\n\t\tsetter.current = (next) => setState(token, next, store)\n\t}\n\treturn setter.current\n}\n\nexport function useO<T>(token: ReadonlySelectorToken<T> | StateToken<T>): T {\n\tconst store = React.useContext(StoreContext)\n\tconst id = React.useId()\n\treturn React.useSyncExternalStore<T>(\n\t\t(dispatch) => subscribeToState(token, dispatch, `use-o:${id}`, store),\n\t\t() => getState(token, store),\n\t\t() => getState(token, store),\n\t)\n}\n\nexport function useJSON<Serializable extends Json.Serializable>(\n\ttoken: MutableAtomToken<any, Serializable>,\n): Serializable {\n\tconst jsonToken = getJsonToken(token)\n\treturn useO(jsonToken)\n}\n\nexport type TimelineMeta = {\n\tat: number\n\tlength: number\n\tundo: () => void\n\tredo: () => void\n}\n\nexport function useTL(token: TimelineToken): TimelineMeta {\n\tconst store = React.useContext(StoreContext)\n\tconst id = React.useId()\n\tconst timeline = withdraw(token, store)\n\tconst base = React.useRef({\n\t\tundo: () => undo(token),\n\t\tredo: () => redo(token),\n\t})\n\tconst rebuildMeta = React.useCallback(() => {\n\t\treturn Object.assign(\n\t\t\t{\n\t\t\t\tat: timeline?.at ?? NaN,\n\t\t\t\tlength: timeline?.history.length ?? NaN,\n\t\t\t},\n\t\t\tbase.current,\n\t\t)\n\t}, [])\n\tconst meta = React.useRef<TimelineMeta>(rebuildMeta())\n\tconst retrieve = React.useCallback(() => {\n\t\tif (\n\t\t\tmeta.current.at !== timeline?.at ||\n\t\t\tmeta.current.length !== timeline?.history.length\n\t\t) {\n\t\t\tmeta.current = rebuildMeta()\n\t\t}\n\t\treturn meta.current\n\t}, [])\n\treturn React.useSyncExternalStore<TimelineMeta>(\n\t\t(dispatch) => subscribeToTimeline(token, dispatch, `use-tl:${id}`, store),\n\t\tretrieve,\n\t\tretrieve,\n\t)\n}\n"]}
@@ -1,4 +1,4 @@
1
- import { useI, StoreContext } from './chunk-CWKKQKVQ.js';
1
+ import { useI, StoreContext } from './chunk-HQWWV67P.js';
2
2
  import { myIdState__INTERNAL, pullState, pullFamilyMember, pullMutableState, pullMutableFamilyMember, pushState, synchronizeTransactionResults } from './chunk-N7ADBQJG.js';
3
3
  import { runTransaction } from './chunk-PURABO5G.js';
4
4
  import * as React6 from 'react';
@@ -64,4 +64,4 @@ function useServerAction(token) {
64
64
 
65
65
  export { RealtimeContext, RealtimeProvider, usePull, usePullFamilyMember, usePullMutable, usePullMutableFamilyMember, usePush, useServerAction };
66
66
  //# sourceMappingURL=out.js.map
67
- //# sourceMappingURL=chunk-C4YZZNRH.js.map
67
+ //# sourceMappingURL=chunk-MK4OJD24.js.map