@evolu/common 6.0.1-preview.3 → 6.0.1-preview.30

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.
Files changed (173) hide show
  1. package/dist/src/Array.d.ts +58 -5
  2. package/dist/src/Array.d.ts.map +1 -1
  3. package/dist/src/Array.js +53 -5
  4. package/dist/src/Assert.d.ts +6 -16
  5. package/dist/src/Assert.d.ts.map +1 -1
  6. package/dist/src/Assert.js +6 -18
  7. package/dist/src/Brand.d.ts +75 -0
  8. package/dist/src/Brand.d.ts.map +1 -0
  9. package/dist/src/Brand.js +1 -0
  10. package/dist/src/Buffer.d.ts +1 -1
  11. package/dist/src/Buffer.d.ts.map +1 -1
  12. package/dist/src/Buffer.js +8 -7
  13. package/dist/src/Cache.d.ts +44 -0
  14. package/dist/src/Cache.d.ts.map +1 -0
  15. package/dist/src/Cache.js +52 -0
  16. package/dist/src/Callbacks.d.ts +45 -12
  17. package/dist/src/Callbacks.d.ts.map +1 -1
  18. package/dist/src/Callbacks.js +14 -7
  19. package/dist/src/Console.d.ts +31 -6
  20. package/dist/src/Console.d.ts.map +1 -1
  21. package/dist/src/Console.js +72 -9
  22. package/dist/src/Crypto.d.ts +61 -34
  23. package/dist/src/Crypto.d.ts.map +1 -1
  24. package/dist/src/Crypto.js +32 -45
  25. package/dist/src/Evolu/Db.d.ts +158 -65
  26. package/dist/src/Evolu/Db.d.ts.map +1 -1
  27. package/dist/src/Evolu/Db.js +286 -694
  28. package/dist/src/Evolu/Diff.d.ts +3 -3
  29. package/dist/src/Evolu/Diff.d.ts.map +1 -1
  30. package/dist/src/Evolu/Diff.js +7 -5
  31. package/dist/src/Evolu/Evolu.d.ts +208 -133
  32. package/dist/src/Evolu/Evolu.d.ts.map +1 -1
  33. package/dist/src/Evolu/Evolu.js +188 -183
  34. package/dist/src/Evolu/Internal.d.ts +0 -2
  35. package/dist/src/Evolu/Internal.d.ts.map +1 -1
  36. package/dist/src/Evolu/Internal.js +0 -2
  37. package/dist/src/Evolu/LocalAuth.d.ts +150 -0
  38. package/dist/src/Evolu/LocalAuth.d.ts.map +1 -0
  39. package/dist/src/Evolu/LocalAuth.js +174 -0
  40. package/dist/src/Evolu/Owner.d.ts +264 -120
  41. package/dist/src/Evolu/Owner.d.ts.map +1 -1
  42. package/dist/src/Evolu/Owner.js +130 -104
  43. package/dist/src/Evolu/Platform.d.ts +9 -7
  44. package/dist/src/Evolu/Platform.d.ts.map +1 -1
  45. package/dist/src/Evolu/Protocol.d.ts +277 -232
  46. package/dist/src/Evolu/Protocol.d.ts.map +1 -1
  47. package/dist/src/Evolu/Protocol.js +603 -378
  48. package/dist/src/Evolu/Public.d.ts +6 -8
  49. package/dist/src/Evolu/Public.d.ts.map +1 -1
  50. package/dist/src/Evolu/Public.js +2 -3
  51. package/dist/src/Evolu/PublicKysely.js +3 -3
  52. package/dist/src/Evolu/Query.d.ts +2 -1
  53. package/dist/src/Evolu/Query.d.ts.map +1 -1
  54. package/dist/src/Evolu/Relay.d.ts +92 -7
  55. package/dist/src/Evolu/Relay.d.ts.map +1 -1
  56. package/dist/src/Evolu/Relay.js +243 -76
  57. package/dist/src/Evolu/Schema.d.ts +129 -73
  58. package/dist/src/Evolu/Schema.d.ts.map +1 -1
  59. package/dist/src/Evolu/Schema.js +169 -89
  60. package/dist/src/Evolu/Storage.d.ts +212 -26
  61. package/dist/src/Evolu/Storage.d.ts.map +1 -1
  62. package/dist/src/Evolu/Storage.js +137 -79
  63. package/dist/src/Evolu/Sync.d.ts +68 -13
  64. package/dist/src/Evolu/Sync.d.ts.map +1 -1
  65. package/dist/src/Evolu/Sync.js +422 -20
  66. package/dist/src/Evolu/Timestamp.d.ts +85 -27
  67. package/dist/src/Evolu/Timestamp.d.ts.map +1 -1
  68. package/dist/src/Evolu/Timestamp.js +77 -18
  69. package/dist/src/Identicon.d.ts +35 -0
  70. package/dist/src/Identicon.d.ts.map +1 -0
  71. package/dist/src/Identicon.js +143 -0
  72. package/dist/src/Instances.d.ts +34 -0
  73. package/dist/src/Instances.d.ts.map +1 -0
  74. package/dist/src/Instances.js +44 -0
  75. package/dist/src/ManyToManyMap.d.ts +71 -10
  76. package/dist/src/ManyToManyMap.d.ts.map +1 -1
  77. package/dist/src/ManyToManyMap.js +41 -6
  78. package/dist/src/Number.d.ts +4 -3
  79. package/dist/src/Number.d.ts.map +1 -1
  80. package/dist/src/Number.js +5 -4
  81. package/dist/src/Platform.d.ts +20 -0
  82. package/dist/src/Platform.d.ts.map +1 -0
  83. package/dist/src/Platform.js +22 -0
  84. package/dist/src/Random.d.ts +3 -2
  85. package/dist/src/Random.d.ts.map +1 -1
  86. package/dist/src/Resources.d.ts +118 -0
  87. package/dist/src/Resources.d.ts.map +1 -0
  88. package/dist/src/Resources.js +197 -0
  89. package/dist/src/Result.d.ts +184 -52
  90. package/dist/src/Result.d.ts.map +1 -1
  91. package/dist/src/Result.js +30 -241
  92. package/dist/src/Skiplist.js +2 -1
  93. package/dist/src/Sqlite.d.ts +63 -5
  94. package/dist/src/Sqlite.d.ts.map +1 -1
  95. package/dist/src/Sqlite.js +110 -9
  96. package/dist/src/Task.d.ts +586 -0
  97. package/dist/src/Task.d.ts.map +1 -0
  98. package/dist/src/Task.js +469 -0
  99. package/dist/src/Time.d.ts +66 -1
  100. package/dist/src/Time.d.ts.map +1 -1
  101. package/dist/src/Time.js +99 -5
  102. package/dist/src/Type.d.ts +621 -340
  103. package/dist/src/Type.d.ts.map +1 -1
  104. package/dist/src/Type.js +665 -464
  105. package/dist/src/Types.d.ts +1 -75
  106. package/dist/src/Types.d.ts.map +1 -1
  107. package/dist/src/WebSocket.d.ts +5 -2
  108. package/dist/src/WebSocket.d.ts.map +1 -1
  109. package/dist/src/WebSocket.js +12 -18
  110. package/dist/src/Worker.d.ts +39 -11
  111. package/dist/src/Worker.d.ts.map +1 -1
  112. package/dist/src/Worker.js +22 -4
  113. package/dist/src/index.d.ts +7 -2
  114. package/dist/src/index.d.ts.map +1 -1
  115. package/dist/src/index.js +7 -2
  116. package/package.json +14 -13
  117. package/src/Array.ts +76 -11
  118. package/src/Assert.ts +6 -24
  119. package/src/Brand.ts +75 -0
  120. package/src/Buffer.ts +7 -7
  121. package/src/Cache.ts +85 -0
  122. package/src/Callbacks.ts +62 -22
  123. package/src/Console.ts +91 -11
  124. package/src/Crypto.ts +97 -82
  125. package/src/Evolu/Db.ts +514 -1020
  126. package/src/Evolu/Diff.ts +7 -5
  127. package/src/Evolu/Evolu.ts +464 -355
  128. package/src/Evolu/Internal.ts +0 -2
  129. package/src/Evolu/LocalAuth.ts +463 -0
  130. package/src/Evolu/Owner.ts +369 -228
  131. package/src/Evolu/Platform.ts +9 -9
  132. package/src/Evolu/Protocol.ts +859 -676
  133. package/src/Evolu/Public.ts +7 -14
  134. package/src/Evolu/PublicKysely.ts +3 -3
  135. package/src/Evolu/Query.ts +2 -1
  136. package/src/Evolu/Relay.ts +420 -92
  137. package/src/Evolu/Schema.ts +391 -191
  138. package/src/Evolu/Storage.ts +451 -118
  139. package/src/Evolu/Sync.ts +720 -37
  140. package/src/Evolu/Timestamp.ts +88 -35
  141. package/src/Identicon.ts +197 -0
  142. package/src/Instances.ts +90 -0
  143. package/src/ManyToManyMap.ts +124 -24
  144. package/src/Number.ts +6 -10
  145. package/src/Platform.ts +26 -0
  146. package/src/Random.ts +3 -2
  147. package/src/Resources.ts +367 -0
  148. package/src/Result.ts +191 -54
  149. package/src/Skiplist.ts +1 -1
  150. package/src/Sqlite.ts +122 -17
  151. package/src/Task.ts +901 -0
  152. package/src/Time.ts +180 -5
  153. package/src/Type.ts +1083 -727
  154. package/src/Types.ts +1 -77
  155. package/src/WebSocket.ts +27 -25
  156. package/src/Worker.ts +72 -23
  157. package/src/index.ts +7 -2
  158. package/dist/src/Evolu/Config.d.ts +0 -69
  159. package/dist/src/Evolu/Config.d.ts.map +0 -1
  160. package/dist/src/Evolu/Config.js +0 -9
  161. package/dist/src/Evolu/Kysely.d.ts +0 -6
  162. package/dist/src/Evolu/Kysely.d.ts.map +0 -1
  163. package/dist/src/Evolu/Kysely.js +0 -21
  164. package/dist/src/NanoId.d.ts +0 -27
  165. package/dist/src/NanoId.d.ts.map +0 -1
  166. package/dist/src/NanoId.js +0 -6
  167. package/dist/src/Promise.d.ts +0 -180
  168. package/dist/src/Promise.d.ts.map +0 -1
  169. package/dist/src/Promise.js +0 -176
  170. package/src/Evolu/Config.ts +0 -83
  171. package/src/Evolu/Kysely.ts +0 -38
  172. package/src/NanoId.ts +0 -39
  173. package/src/Promise.ts +0 -295
@@ -1,8 +1,9 @@
1
- import { NanoIdLibDep } from "../NanoId.js";
1
+ import { Brand } from "../Brand.js";
2
+ import { RandomBytesDep } from "../Crypto.js";
2
3
  import { Order } from "../Order.js";
3
4
  import { Result } from "../Result.js";
4
5
  import { TimeDep } from "../Time.js";
5
- import { Brand } from "../Types.js";
6
+ import { InferType } from "../Type.js";
6
7
  export interface TimestampConfig {
7
8
  /**
8
9
  * Maximum physical clock drift allowed in ms.
@@ -14,7 +15,7 @@ export interface TimestampConfig {
14
15
  export interface TimestampConfigDep {
15
16
  readonly timestampConfig: TimestampConfig;
16
17
  }
17
- export type TimestampError = TimestampDriftError | TimestampCounterOverflowError | TimestampDuplicateNodeError | TimestampTimeOutOfRangeError;
18
+ export type TimestampError = TimestampDriftError | TimestampCounterOverflowError | TimestampTimeOutOfRangeError;
18
19
  export interface TimestampDriftError {
19
20
  readonly type: "TimestampDriftError";
20
21
  readonly next: Millis;
@@ -23,10 +24,6 @@ export interface TimestampDriftError {
23
24
  export interface TimestampCounterOverflowError {
24
25
  readonly type: "TimestampCounterOverflowError";
25
26
  }
26
- export interface TimestampDuplicateNodeError {
27
- readonly type: "TimestampDuplicateNodeError";
28
- readonly nodeId: NodeId;
29
- }
30
27
  export interface TimestampTimeOutOfRangeError {
31
28
  readonly type: "TimestampTimeOutOfRangeError";
32
29
  }
@@ -42,11 +39,11 @@ export interface TimestampTimeOutOfRangeError {
42
39
  *
43
40
  * `new Date(281474976710654).toString()` = Tue Aug 02 10889 07:31:49
44
41
  */
45
- export declare const Millis: import("../Type.js").BrandType<import("../Type.js").BrandType<import("../Type.js").Type<"Brand", number & Brand<"Int"> & Brand<"NonNegative">, number, import("../Type.js").NonNegativeError, number & Brand<"Int">, import("../Type.js").NumberError | import("../Type.js").IntError>, `LessThanOrEqualTo${number}`, import("../Type.js").LessThanOrEqualToError<number>, import("../Type.js").NumberError | import("../Type.js").NonNegativeError | import("../Type.js").IntError>, "Millis", import("../Type.js").BrandWithoutRefineError<"Millis", import("../Type.js").NumberError | import("../Type.js").NonNegativeError | import("../Type.js").IntError | import("../Type.js").LessThanOrEqualToError<number>>, never>;
42
+ export declare const Millis: import("../Type.js").BrandType<import("../Type.js").BrandType<import("../Type.js").Type<"Brand", number & Brand<"Int"> & Brand<"NonNegative">, number, import("../Type.js").NonNegativeError, number & Brand<"Int">, import("../Type.js").IntError | import("../Type.js").NumberError>, `LessThanOrEqualTo${number}`, import("../Type.js").LessThanOrEqualToError<number>, import("../Type.js").NonNegativeError | import("../Type.js").IntError | import("../Type.js").NumberError>, "Millis", import("../Type.js").BrandWithoutRefineError<"Millis", import("../Type.js").NonNegativeError | import("../Type.js").IntError | import("../Type.js").NumberError | import("../Type.js").LessThanOrEqualToError<number>>, never>;
46
43
  export type Millis = typeof Millis.Type;
47
44
  export declare const minMillis: Millis;
48
45
  export declare const maxMillis: Millis;
49
- export declare const Counter: import("../Type.js").BrandType<import("../Type.js").BrandType<import("../Type.js").Type<"Brand", number & Brand<"Int"> & Brand<"NonNegative">, number, import("../Type.js").NonNegativeError, number & Brand<"Int">, import("../Type.js").NumberError | import("../Type.js").IntError>, "LessThanOrEqualTo65535", import("../Type.js").LessThanOrEqualToError<65535>, import("../Type.js").NumberError | import("../Type.js").NonNegativeError | import("../Type.js").IntError>, "Counter", import("../Type.js").BrandWithoutRefineError<"Counter", import("../Type.js").NumberError | import("../Type.js").NonNegativeError | import("../Type.js").IntError | import("../Type.js").LessThanOrEqualToError<65535>>, never>;
46
+ export declare const Counter: import("../Type.js").BrandType<import("../Type.js").BrandType<import("../Type.js").Type<"Brand", number & Brand<"Int"> & Brand<"NonNegative">, number, import("../Type.js").NonNegativeError, number & Brand<"Int">, import("../Type.js").IntError | import("../Type.js").NumberError>, "LessThanOrEqualTo65535", import("../Type.js").LessThanOrEqualToError<65535>, import("../Type.js").NonNegativeError | import("../Type.js").IntError | import("../Type.js").NumberError>, "Counter", import("../Type.js").BrandWithoutRefineError<"Counter", import("../Type.js").NonNegativeError | import("../Type.js").IntError | import("../Type.js").NumberError | import("../Type.js").LessThanOrEqualToError<65535>>, never>;
50
47
  export type Counter = typeof Counter.Type;
51
48
  export declare const minCounter: Counter;
52
49
  export declare const maxCounter: Counter;
@@ -63,14 +60,17 @@ export declare const maxCounter: Counter;
63
60
  *
64
61
  * https://lemire.me/blog/2019/12/12/are-64-bit-random-identifiers-free-from-collision
65
62
  *
66
- * What will happen if a different device generates the same NodeId?
63
+ * What happens if different devices generate the same NodeId?
67
64
  *
68
- * If the device belongs to a different owner, nothing will happen because
69
- * different owner have different owner IDs. Timestamps are partitioned by
70
- * OwnerId.
65
+ * If devices with the same NodeId use different owners, no issues occur.
71
66
  *
72
- * If the device belongs to the same owner, the other device will return
73
- * {@link TimestampDuplicateNodeError}.
67
+ * If devices with the same NodeId use the same owner, problems only arise when
68
+ * they generate CRDT messages with identical timestamps (same millis, counter,
69
+ * and NodeId). In this case, the protocol sync algorithm treats them as the
70
+ * same message: the first will be synced with the relay, while the affected
71
+ * message will not be delivered. The affected devices will see different data
72
+ * yet they will think they are synced. This is extremely rare and can be
73
+ * resolved by resetting one device to generate a new NodeId.
74
74
  */
75
75
  export declare const NodeId: import("../Type.js").BrandType<import("../Type.js").Type<"String", string, string, import("../Type.js").StringError, string, import("../Type.js").StringError>, "NodeId", import("../Type.js").RegexError<"NodeId">, import("../Type.js").StringError>;
76
76
  export type NodeId = typeof NodeId.Type;
@@ -79,28 +79,86 @@ export declare const maxNodeId: NodeId;
79
79
  /**
80
80
  * Hybrid Logical Clock timestamp.
81
81
  *
82
+ * Timestamps serve as globally unique, causally ordered identifiers for CRDT
83
+ * messages in Evolu's sync protocol.
84
+ *
85
+ * ### Why Hybrid Logical Clocks
86
+ *
87
+ * Evolu uses Hybrid Logical Clocks (HLC), which combine physical time (millis)
88
+ * with a logical counter. This hybrid approach preserves causality like logical
89
+ * clocks while staying close to physical time for better human
90
+ * interpretability.
91
+ *
92
+ * The counter component ensures causality is maintained even when physical
93
+ * clocks are imperfect. When clocks drift or operations occur concurrently, the
94
+ * counter increments to establish a total order. This means Evolu achieves
95
+ * well-defined, eventually-consistent behavior regardless of physical clock
96
+ * accuracy.
97
+ *
98
+ * Vector clocks can accurately track causality and detect concurrent
99
+ * operations, but they require unbounded space in peer-to-peer systems and
100
+ * crucially, still don't solve our fundamental problem: when they detect
101
+ * operations as concurrent, we still need a deterministic way to choose a
102
+ * winner. Additionally, any deterministic conflict resolution can be gamed by
103
+ * malicious actors.
104
+ *
105
+ * HLC timestamps work well in practice because modern device clocks accurately
106
+ * reflect the order of sequential edits in the common case. Evolu's `maxDrift`
107
+ * configuration protects against buggy clocks and prevents problematic
108
+ * future-dated entries from propagating through the network.
109
+ *
110
+ * ### References
111
+ *
82
112
  * - https://muratbuffalo.blogspot.com/2014/07/hybrid-logical-clocks.html
83
113
  * - https://sergeiturukin.com/2017/06/26/hybrid-logical-clocks.html
84
114
  * - https://jaredforsyth.com/posts/hybrid-logical-clocks/
115
+ * - https://willowprotocol.org/more/timestamps_really/index.html
116
+ *
117
+ * ### Privacy Considerations
118
+ *
119
+ * Timestamps are metadata visible to relays and collaborators. While it can be
120
+ * considered a privacy leak, let us explain why it's necessary, and how to
121
+ * avoid it if maximum privacy is required.
122
+ *
123
+ * With real-time communication, participants always see activity (receiving
124
+ * bytes). We cannot trust anyone not to store that information, so explicitly
125
+ * exposing timestamps doesn't add additional risk.
126
+ *
127
+ * If we really want not to leak user activity, we can implement a local write
128
+ * queue:
129
+ *
130
+ * 1. Write changes immediately to a local-only table
131
+ * 2. Periodically/randomly flush messages to sync tables
132
+ * 3. This decouples user activity from sync timing
133
+ *
134
+ * Tradeoff: It breaks real-time collaboration.
85
135
  */
86
136
  export declare const Timestamp: import("../Type.js").ObjectType<{
87
- millis: import("../Type.js").BrandType<import("../Type.js").BrandType<import("../Type.js").Type<"Brand", number & Brand<"Int"> & Brand<"NonNegative">, number, import("../Type.js").NonNegativeError, number & Brand<"Int">, import("../Type.js").NumberError | import("../Type.js").IntError>, `LessThanOrEqualTo${number}`, import("../Type.js").LessThanOrEqualToError<number>, import("../Type.js").NumberError | import("../Type.js").NonNegativeError | import("../Type.js").IntError>, "Millis", import("../Type.js").BrandWithoutRefineError<"Millis", import("../Type.js").NumberError | import("../Type.js").NonNegativeError | import("../Type.js").IntError | import("../Type.js").LessThanOrEqualToError<number>>, never>;
88
- counter: import("../Type.js").BrandType<import("../Type.js").BrandType<import("../Type.js").Type<"Brand", number & Brand<"Int"> & Brand<"NonNegative">, number, import("../Type.js").NonNegativeError, number & Brand<"Int">, import("../Type.js").NumberError | import("../Type.js").IntError>, "LessThanOrEqualTo65535", import("../Type.js").LessThanOrEqualToError<65535>, import("../Type.js").NumberError | import("../Type.js").NonNegativeError | import("../Type.js").IntError>, "Counter", import("../Type.js").BrandWithoutRefineError<"Counter", import("../Type.js").NumberError | import("../Type.js").NonNegativeError | import("../Type.js").IntError | import("../Type.js").LessThanOrEqualToError<65535>>, never>;
137
+ millis: import("../Type.js").BrandType<import("../Type.js").BrandType<import("../Type.js").Type<"Brand", number & Brand<"Int"> & Brand<"NonNegative">, number, import("../Type.js").NonNegativeError, number & Brand<"Int">, import("../Type.js").IntError | import("../Type.js").NumberError>, `LessThanOrEqualTo${number}`, import("../Type.js").LessThanOrEqualToError<number>, import("../Type.js").NonNegativeError | import("../Type.js").IntError | import("../Type.js").NumberError>, "Millis", import("../Type.js").BrandWithoutRefineError<"Millis", import("../Type.js").NonNegativeError | import("../Type.js").IntError | import("../Type.js").NumberError | import("../Type.js").LessThanOrEqualToError<number>>, never>;
138
+ counter: import("../Type.js").BrandType<import("../Type.js").BrandType<import("../Type.js").Type<"Brand", number & Brand<"Int"> & Brand<"NonNegative">, number, import("../Type.js").NonNegativeError, number & Brand<"Int">, import("../Type.js").IntError | import("../Type.js").NumberError>, "LessThanOrEqualTo65535", import("../Type.js").LessThanOrEqualToError<65535>, import("../Type.js").NonNegativeError | import("../Type.js").IntError | import("../Type.js").NumberError>, "Counter", import("../Type.js").BrandWithoutRefineError<"Counter", import("../Type.js").NonNegativeError | import("../Type.js").IntError | import("../Type.js").NumberError | import("../Type.js").LessThanOrEqualToError<65535>>, never>;
89
139
  nodeId: import("../Type.js").BrandType<import("../Type.js").Type<"String", string, string, import("../Type.js").StringError, string, import("../Type.js").StringError>, "NodeId", import("../Type.js").RegexError<"NodeId">, import("../Type.js").StringError>;
90
140
  }>;
91
- export type Timestamp = typeof Timestamp.Type;
141
+ export interface Timestamp extends InferType<typeof Timestamp> {
142
+ }
143
+ /** Equality function for comparing {@link Timestamp}. */
144
+ export declare const eqTimestamp: import("../Eq.js").Eq<{
145
+ readonly millis: number & Brand<"Int"> & Brand<"NonNegative"> & Brand<`LessThanOrEqualTo${number}`> & Brand<"Millis">;
146
+ readonly counter: number & Brand<"Int"> & Brand<"NonNegative"> & Brand<"LessThanOrEqualTo65535"> & Brand<"Counter">;
147
+ readonly nodeId: string & Brand<"NodeId">;
148
+ }>;
92
149
  export declare const createTimestamp: ({ millis, counter, nodeId, }?: Partial<Timestamp>) => Timestamp;
93
- export declare const createInitialTimestamp: (deps: NanoIdLibDep) => Timestamp;
94
- /** TimestampString is a sortable string version of {@link Timestamp}. */
150
+ export declare const createInitialTimestamp: (deps: RandomBytesDep) => Timestamp;
151
+ /** Sortable string representation of {@link Timestamp}. */
95
152
  export type TimestampString = string & Brand<"TimestampString">;
96
153
  export declare const timestampToTimestampString: (t: Timestamp) => TimestampString;
97
154
  export declare const timestampStringToTimestamp: (timestampString: TimestampString) => Timestamp;
98
155
  export declare const sendTimestamp: (deps: TimeDep & TimestampConfigDep) => (timestamp: Timestamp) => Result<Timestamp, TimestampDriftError | TimestampCounterOverflowError | TimestampTimeOutOfRangeError>;
99
- export declare const receiveTimestamp: (deps: TimeDep & TimestampConfigDep) => (local: Timestamp, remote: Timestamp) => Result<Timestamp, TimestampDriftError | TimestampCounterOverflowError | TimestampDuplicateNodeError | TimestampTimeOutOfRangeError>;
100
- /** BinaryTimestamp is a binary and sortable version of {@link Timestamp} for DB. */
101
- export type BinaryTimestamp = Uint8Array & Brand<"BinaryTimestamp">;
102
- export declare const binaryTimestampLength: number & Brand<"Int"> & Brand<"NonNegative">;
103
- export declare const timestampToBinaryTimestamp: (timestamp: Timestamp) => BinaryTimestamp;
104
- export declare const binaryTimestampToTimestamp: (timestamp: BinaryTimestamp) => Timestamp;
105
- export declare const orderBinaryTimestamp: Order<BinaryTimestamp>;
156
+ export declare const receiveTimestamp: (deps: TimeDep & TimestampConfigDep) => (local: Timestamp, remote: Timestamp) => Result<Timestamp, TimestampDriftError | TimestampCounterOverflowError | TimestampTimeOutOfRangeError>;
157
+ /** Sortable bytes representation of {@link Timestamp}. */
158
+ export declare const TimestampBytes: import("../Type.js").BrandType<import("../Type.js").Type<"Uint8Array", Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>, import("../Type.js").Uint8ArrayError, Uint8Array<ArrayBufferLike>, import("../Type.js").Uint8ArrayError>, "TimestampBytes", import("../Type.js").BrandWithoutRefineError<"TimestampBytes", import("../Type.js").Uint8ArrayError>, never>;
159
+ export type TimestampBytes = typeof TimestampBytes.Type;
160
+ export declare const timestampBytesLength: number & Brand<"Int"> & Brand<"NonNegative">;
161
+ export declare const timestampToTimestampBytes: (timestamp: Timestamp) => TimestampBytes;
162
+ export declare const timestampBytesToTimestamp: (timestamp: TimestampBytes) => Timestamp;
163
+ export declare const orderTimestampBytes: Order<TimestampBytes>;
106
164
  //# sourceM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AAS,KAAG,eAK1B,CAAC;AAEjC,eAAO,MAAM,0BAA0B,GACrC,iBAAiB,eAAe,KAC/B,SASF,CAAC;AA6BF,eAAO,MAAM,aAAa,GACvB,MAAM,OAAO,GAAG,kBAAkB,MAEjC,WAAW,SAAS,KACnB,MAAM,CACP,SAAS,EACP,mBAAmB,GACnB,6BAA6B,GAC7B,4BAA4B,CAgB/B,CAAC;AAEJ,eAAO,MAAM,gBAAgB,GAC1B,MAAM,OAAO,GAAG,kBAAkB,MAEjC,OAAO,SAAS,EAChB,QAAQ,SAAS,KAChB,MAAM,CACP,SAAS,EACP,mBAAmB,GACnB,6BAA6B,GAC7B,4BAA4B,CAqB/B,CAAC;AAEJ,0DAA0D;AAC1D,eAAO,MAAM,cAAc,2WAAsC,CAAC;AAClE,MAAM,MAAM,cAAc,GAAG,OAAO,cAAc,CAAC,IAAI,CAAC;AAExD,eAAO,MAAM,oBAAoB,8CAA6B,CAAC;AAE/D,eAAO,MAAM,yBAAyB,GACpC,WAAW,SAAS,KACnB,cA0BF,CAAC;AAEF,eAAO,MAAM,yBAAyB,GACpC,WAAW,cAAc,KACxB,SAoBF,CAAC;AAEF,eAAO,MAAM,mBAAmB,EAAE,KAAK,CAAC,cAAc,CAAmB,CAAC"}
@@ -1,8 +1,10 @@
1
1
  import { assert } from "../Assert.js";
2
+ import { bytesToHex } from "../Buffer.js";
3
+ import { createEqObject, eqNumber, eqString } from "../Eq.js";
2
4
  import { increment } from "../Number.js";
3
5
  import { orderUint8Array } from "../Order.js";
4
- import { err, getOrThrow, ok } from "../Result.js";
5
- import { brand, lessThanOrEqualTo, NonNegativeInt, object, regex, String, } from "../Type.js";
6
+ import { err, ok } from "../Result.js";
7
+ import { brand, lessThanOrEqualTo, NonNegativeInt, object, regex, String, Uint8Array, } from "../Type.js";
6
8
  /**
7
9
  * Millis is a timestamp in milliseconds, like `Date.now()`, but limited to the
8
10
  * maximum value representable in 6 bytes (281474976710655) minus 1 (reserved
@@ -34,14 +36,17 @@ export const maxCounter = 65535;
34
36
  *
35
37
  * https://lemire.me/blog/2019/12/12/are-64-bit-random-identifiers-free-from-collision
36
38
  *
37
- * What will happen if a different device generates the same NodeId?
39
+ * What happens if different devices generate the same NodeId?
38
40
  *
39
- * If the device belongs to a different owner, nothing will happen because
40
- * different owner have different owner IDs. Timestamps are partitioned by
41
- * OwnerId.
41
+ * If devices with the same NodeId use different owners, no issues occur.
42
42
  *
43
- * If the device belongs to the same owner, the other device will return
44
- * {@link TimestampDuplicateNodeError}.
43
+ * If devices with the same NodeId use the same owner, problems only arise when
44
+ * they generate CRDT messages with identical timestamps (same millis, counter,
45
+ * and NodeId). In this case, the protocol sync algorithm treats them as the
46
+ * same message: the first will be synced with the relay, while the affected
47
+ * message will not be delivered. The affected devices will see different data
48
+ * yet they will think they are synced. This is extremely rare and can be
49
+ * resolved by resetting one device to generate a new NodeId.
45
50
  */
46
51
  export const NodeId = regex("NodeId", /^[a-f0-9]{16}$/)(String);
47
52
  export const minNodeId = "0000000000000000";
@@ -49,19 +54,74 @@ export const maxNodeId = "ffffffffffffffff";
49
54
  /**
50
55
  * Hybrid Logical Clock timestamp.
51
56
  *
57
+ * Timestamps serve as globally unique, causally ordered identifiers for CRDT
58
+ * messages in Evolu's sync protocol.
59
+ *
60
+ * ### Why Hybrid Logical Clocks
61
+ *
62
+ * Evolu uses Hybrid Logical Clocks (HLC), which combine physical time (millis)
63
+ * with a logical counter. This hybrid approach preserves causality like logical
64
+ * clocks while staying close to physical time for better human
65
+ * interpretability.
66
+ *
67
+ * The counter component ensures causality is maintained even when physical
68
+ * clocks are imperfect. When clocks drift or operations occur concurrently, the
69
+ * counter increments to establish a total order. This means Evolu achieves
70
+ * well-defined, eventually-consistent behavior regardless of physical clock
71
+ * accuracy.
72
+ *
73
+ * Vector clocks can accurately track causality and detect concurrent
74
+ * operations, but they require unbounded space in peer-to-peer systems and
75
+ * crucially, still don't solve our fundamental problem: when they detect
76
+ * operations as concurrent, we still need a deterministic way to choose a
77
+ * winner. Additionally, any deterministic conflict resolution can be gamed by
78
+ * malicious actors.
79
+ *
80
+ * HLC timestamps work well in practice because modern device clocks accurately
81
+ * reflect the order of sequential edits in the common case. Evolu's `maxDrift`
82
+ * configuration protects against buggy clocks and prevents problematic
83
+ * future-dated entries from propagating through the network.
84
+ *
85
+ * ### References
86
+ *
52
87
  * - https://muratbuffalo.blogspot.com/2014/07/hybrid-logical-clocks.html
53
88
  * - https://sergeiturukin.com/2017/06/26/hybrid-logical-clocks.html
54
89
  * - https://jaredforsyth.com/posts/hybrid-logical-clocks/
90
+ * - https://willowprotocol.org/more/timestamps_really/index.html
91
+ *
92
+ * ### Privacy Considerations
93
+ *
94
+ * Timestamps are metadata visible to relays and collaborators. While it can be
95
+ * considered a privacy leak, let us explain why it's necessary, and how to
96
+ * avoid it if maximum privacy is required.
97
+ *
98
+ * With real-time communication, participants always see activity (receiving
99
+ * bytes). We cannot trust anyone not to store that information, so explicitly
100
+ * exposing timestamps doesn't add additional risk.
101
+ *
102
+ * If we really want not to leak user activity, we can implement a local write
103
+ * queue:
104
+ *
105
+ * 1. Write changes immediately to a local-only table
106
+ * 2. Periodically/randomly flush messages to sync tables
107
+ * 3. This decouples user activity from sync timing
108
+ *
109
+ * Tradeoff: It breaks real-time collaboration.
55
110
  */
56
111
  export const Timestamp = object({
57
112
  millis: Millis,
58
113
  counter: Counter,
59
114
  nodeId: NodeId,
60
115
  });
116
+ /** Equality function for comparing {@link Timestamp}. */
117
+ export const eqTimestamp = createEqObject({
118
+ millis: eqNumber,
119
+ counter: eqNumber,
120
+ nodeId: eqString,
121
+ });
61
122
  export const createTimestamp = ({ millis = minMillis, counter = minCounter, nodeId = minNodeId, } = {}) => ({ millis, counter, nodeId });
62
- const hexAlphabet = "0123456789abcdef";
63
123
  export const createInitialTimestamp = (deps) => {
64
- const nodeId = deps.nanoIdLib.customAlphabet(hexAlphabet, 16)();
124
+ const nodeId = bytesToHex(deps.randomBytes.create(8));
65
125
  return createTimestamp({ nodeId });
66
126
  };
67
127
  export const timestampToTimestampString = (t) => [
@@ -115,9 +175,6 @@ export const sendTimestamp = (deps) => (timestamp) => {
115
175
  });
116
176
  };
117
177
  export const receiveTimestamp = (deps) => (local, remote) => {
118
- if (local.nodeId === remote.nodeId) {
119
- return err({ type: "TimestampDuplicateNodeError", nodeId: local.nodeId });
120
- }
121
178
  const millis = getNextMillis(deps)([local.millis, remote.millis]);
122
179
  if (!millis.ok)
123
180
  return millis;
@@ -136,11 +193,13 @@ export const receiveTimestamp = (deps) => (local, remote) => {
136
193
  nodeId: local.nodeId,
137
194
  });
138
195
  };
139
- export const binaryTimestampLength = getOrThrow(NonNegativeInt.from(16));
140
- export const timestampToBinaryTimestamp = (timestamp) => {
196
+ /** Sortable bytes representation of {@link Timestamp}. */
197
+ export const TimestampBytes = brand("TimestampBytes", Uint8Array);
198
+ export const timestampBytesLength = NonNegativeInt.orThrow(16);
199
+ export const timestampToTimestampBytes = (timestamp) => {
141
200
  const { millis, counter, nodeId } = timestamp;
142
201
  // 6 bytes for millis, 2 bytes for counter, 8 bytes for nodeId.
143
- const value = new Uint8Array(16);
202
+ const value = new globalThis.Uint8Array(16);
144
203
  // Encode `millis` into the first 6 bytes.
145
204
  const millisBigInt = BigInt(millis);
146
205
  value[0] = Number((millisBigInt >> 40n) & 0xffn);
@@ -159,7 +218,7 @@ export const timestampToBinaryTimestamp = (timestamp) => {
159
218
  }
160
219
  return value;
161
220
  };
162
- export const binaryTimestampToTimestamp = (timestamp) => {
221
+ export const timestampBytesToTimestamp = (timestamp) => {
163
222
  // Decode `millis` from the first 6 bytes.
164
223
  const millis = (BigInt(timestamp[0]) << 40n) |
165
224
  (BigInt(timestamp[1]) << 32n) |
@@ -176,4 +235,4 @@ export const binaryTimestampToTimestamp = (timestamp) => {
176
235
  }
177
236
  return { millis: Number(millis), counter, nodeId };
178
237
  };
179
- export const orderBinaryTimestamp = orderUint8Array;
238
+ export const orderTimestampBytes = orderUint8Array;
@@ -0,0 +1,35 @@
1
+ import type { Brand } from "./Brand.js";
2
+ import { Id } from "./Type.js";
3
+ /**
4
+ * SVG string representing a visual identicon for an {@link Id}, created with
5
+ * {@link createIdenticon}.
6
+ */
7
+ export type Identicon = string & Brand<"Identicon">;
8
+ /** {@link Identicon} style. */
9
+ export type IdenticonStyle = "github" | "quadrant" | "gradient" | "sutnar";
10
+ /**
11
+ * Creates a deterministic identicon SVG from an {@link Id}.
12
+ *
13
+ * Works with any {@link Id} including branded IDs like `OwnerId`, etc.
14
+ *
15
+ * Available styles:
16
+ *
17
+ * - `"github"` (default): 5x5 grid with horizontal mirroring (GitHub-style)
18
+ * - `"quadrant"`: 2x2 grid with direct RGB color mapping from bytes
19
+ * - `"gradient"`: Diagonal stripes with smooth color gradients
20
+ * - `"sutnar"`: Three compositional variants with adaptive colors
21
+ *
22
+ * ### Example
23
+ *
24
+ * ```ts
25
+ * const svg = createIdenticon(id);
26
+ * const quadrantStyle = createIdenticon(id, "quadrant");
27
+ * const gradientStyle = createIdenticon(id, "gradient");
28
+ * const sutnarStyle = createIdenticon(id, "sutnar");
29
+ *
30
+ * // Works with branded IDs
31
+ * const ownerSvg = createIdenticon(ownerId);
32
+ * ```
33
+ */
34
+ export declare const createIdenticon: (id: Id, style?: IdenticonStyle) => Identicon;
35
+ //# sourceMappingURL=Identicon.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Identicon.d.ts","sourceRoot":"","sources":["../../src/Identicon.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,EAAE,EAAE,EAAe,MAAM,WAAW,CAAC;AAG5C;;;GAGG;AACH,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,KAAK,CAAC,WAAW,CAAC,CAAC;AAEpD,+BAA+B;AAC/B,MAAM,MAAM,cAAc,GAAG,QAAQ,GAAG,UAAU,GAAG,UAAU,GAAG,QAAQ,CAAC;AAE3E;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,eAAO,MAAM,eAAe,GAC1B,IAAI,EAAE,EACN,QAAO,cAAyB,KAC/B,SA4JF,CAAC"}
@@ -0,0 +1,143 @@
1
+ import { idToIdBytes } from "./Type.js";
2
+ import { md5 } from "@noble/hashes/legacy.js";
3
+ /**
4
+ * Creates a deterministic identicon SVG from an {@link Id}.
5
+ *
6
+ * Works with any {@link Id} including branded IDs like `OwnerId`, etc.
7
+ *
8
+ * Available styles:
9
+ *
10
+ * - `"github"` (default): 5x5 grid with horizontal mirroring (GitHub-style)
11
+ * - `"quadrant"`: 2x2 grid with direct RGB color mapping from bytes
12
+ * - `"gradient"`: Diagonal stripes with smooth color gradients
13
+ * - `"sutnar"`: Three compositional variants with adaptive colors
14
+ *
15
+ * ### Example
16
+ *
17
+ * ```ts
18
+ * const svg = createIdenticon(id);
19
+ * const quadrantStyle = createIdenticon(id, "quadrant");
20
+ * const gradientStyle = createIdenticon(id, "gradient");
21
+ * const sutnarStyle = createIdenticon(id, "sutnar");
22
+ *
23
+ * // Works with branded IDs
24
+ * const ownerSvg = createIdenticon(ownerId);
25
+ * ```
26
+ */
27
+ export const createIdenticon = (id, style = "github") => {
28
+ const bytes = idToIdBytes(id);
29
+ switch (style) {
30
+ case "github": {
31
+ // GitHub-style identicon: MD5 hash the bytes first
32
+ const hashedBytes = md5(bytes);
33
+ // Map function for value ranges
34
+ const map = (value, inMin, inMax, outMin, outMax) => ((value - inMin) * (outMax - outMin)) / (inMax - inMin) + outMin;
35
+ // Extract 12-bit hue from bytes[12] (lower 4 bits) + bytes[13]
36
+ const h = ((hashedBytes[12] & 0x0f) << 8) | hashedBytes[13];
37
+ const hue = map(h, 0, 4095, 0, 360);
38
+ const saturation = 65 - map(hashedBytes[14], 0, 255, 0, 20);
39
+ const lightness = 75 - map(hashedBytes[15], 0, 255, 0, 20);
40
+ const fgColor = `hsl(${hue},${saturation}%,${lightness}%)`;
41
+ const bgColor = `hsl(${hue},${saturation}%,90%)`;
42
+ let rects = `<rect width="5" height="5" fill="${bgColor}"/>`;
43
+ // Extract nibbles and generate pattern
44
+ let nibbleIndex = 0;
45
+ for (let x = 2; x >= 0; x--) {
46
+ for (let y = 0; y < 5; y++) {
47
+ const byte = hashedBytes[Math.floor(nibbleIndex / 2)];
48
+ const nibble = nibbleIndex % 2 === 0 ? byte >> 4 : byte & 0x0f;
49
+ const paint = nibble % 2 === 0;
50
+ nibbleIndex++;
51
+ if (paint) {
52
+ rects += `<rect x="${x}" y="${y}" width="1" height="1" fill="${fgColor}"/>`;
53
+ const mx = 4 - x;
54
+ if (mx !== x) {
55
+ rects += `<rect x="${mx}" y="${y}" width="1" height="1" fill="${fgColor}"/>`;
56
+ }
57
+ }
58
+ }
59
+ }
60
+ return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 5 5" shape-rendering="crispEdges">${rects}</svg>`;
61
+ }
62
+ case "quadrant": {
63
+ const toHex = (b) => b.toString(16).padStart(2, "0");
64
+ let rects = "";
65
+ for (let i = 0; i < 4; i++) {
66
+ const x = i % 2;
67
+ const y = Math.floor(i / 2);
68
+ const r = bytes[i * 3];
69
+ const g = bytes[i * 3 + 1];
70
+ const b = bytes[i * 3 + 2];
71
+ const color = `#${toHex(r)}${toHex(g)}${toHex(b)}`;
72
+ rects += `<rect x="${x}" y="${y}" width="1" height="1" fill="${color}"/>`;
73
+ }
74
+ return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 2 2">${rects}</svg>`;
75
+ }
76
+ case "gradient": {
77
+ // Smooth color gradients with diagonal stripes.
78
+ const toHex = (b) => b.toString(16).padStart(2, "0");
79
+ // Generate colors from bytes.
80
+ const color1 = `#${toHex(bytes[0])}${toHex(bytes[1])}${toHex(bytes[2])}`;
81
+ const color2 = `#${toHex(bytes[3])}${toHex(bytes[4])}${toHex(bytes[5])}`;
82
+ const color3 = `#${toHex(bytes[6])}${toHex(bytes[7])}${toHex(bytes[8])}`;
83
+ let defs = "";
84
+ let shapes = "";
85
+ // Diagonal stripes with gradient.
86
+ defs += `<linearGradient id="grad1-${id}" x1="0%" y1="0%" x2="0%" y2="100%">`;
87
+ defs += `<stop offset="0%" style="stop-color:${color1};stop-opacity:1" />`;
88
+ defs += `<stop offset="100%" style="stop-color:${color2};stop-opacity:1" />`;
89
+ defs += `</linearGradient>`;
90
+ defs += `<linearGradient id="grad2-${id}" x1="0%" y1="0%" x2="0%" y2="100%">`;
91
+ defs += `<stop offset="0%" style="stop-color:${color2};stop-opacity:1" />`;
92
+ defs += `<stop offset="100%" style="stop-color:${color3};stop-opacity:1" />`;
93
+ defs += `</linearGradient>`;
94
+ shapes += `<rect width="100" height="100" fill="url(#grad1-${id})"/>`;
95
+ const stripeWidth = 15 + (bytes[9] / 255) * 20;
96
+ const angle = 30 + (bytes[10] / 255) * 60;
97
+ shapes += `<rect x="20" y="-50" width="${stripeWidth}" height="200" fill="url(#grad2-${id})" transform="rotate(${angle} 50 50)" opacity="0.7"/>`;
98
+ shapes += `<rect x="60" y="-50" width="${stripeWidth}" height="200" fill="url(#grad2-${id})" transform="rotate(${angle} 50 50)" opacity="0.5"/>`;
99
+ return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs>${defs}</defs>${shapes}</svg>`;
100
+ }
101
+ case "sutnar": {
102
+ // Three compositional variants with adaptive colors.
103
+ const hue = (bytes[0] / 255) * 360;
104
+ const saturation = 50 + (bytes[1] / 255) * 30;
105
+ const lightness = 50 + (bytes[2] / 255) * 20;
106
+ // Generate palette from base hue with variations
107
+ const toHsl = (h, s, l) => `hsl(${h},${s}%,${l}%)`;
108
+ const color1 = toHsl(hue, saturation, lightness);
109
+ const color2 = toHsl((hue + 120) % 360, saturation, lightness);
110
+ const color3 = toHsl((hue + 240) % 360, saturation, lightness);
111
+ const color4 = toHsl(hue, saturation * 0.3, lightness * 0.5);
112
+ const color5 = toHsl(hue, saturation * 0.5, Math.min(lightness * 1.3, 90));
113
+ const palette = [color1, color2, color3, color4, color5];
114
+ // Layout variant based on first byte.
115
+ const variant = bytes[3] % 3;
116
+ let shapes = "";
117
+ // Almost white background with subtle tint.
118
+ shapes += `<rect width="100" height="100" fill="${toHsl(hue, 10, 95)}"/>`;
119
+ if (variant === 0) {
120
+ // Composition A: Circle + horizontal bar.
121
+ const circleColor = palette[bytes[4] % palette.length];
122
+ const barColor = palette[(bytes[4] + 1) % palette.length];
123
+ shapes += `<circle cx="30" cy="50" r="22" fill="${circleColor}"/>`;
124
+ shapes += `<rect x="60" y="40" width="35" height="20" fill="${barColor}"/>`;
125
+ }
126
+ else if (variant === 1) {
127
+ // Composition B: Vertical bar + circle.
128
+ const barColor = palette[bytes[5] % palette.length];
129
+ const circleColor = palette[(bytes[5] + 1) % palette.length];
130
+ shapes += `<rect x="15" y="10" width="18" height="80" fill="${barColor}"/>`;
131
+ shapes += `<circle cx="70" cy="50" r="15" fill="${circleColor}"/>`;
132
+ }
133
+ else {
134
+ // Composition C: Square + circle.
135
+ const squareColor = palette[bytes[6] % palette.length];
136
+ const circleColor = palette[(bytes[6] + 1) % palette.length];
137
+ shapes += `<rect x="20" y="20" width="30" height="30" fill="${squareColor}"/>`;
138
+ shapes += `<circle cx="70" cy="70" r="18" fill="${circleColor}"/>`;
139
+ }
140
+ return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">${shapes}</svg>`;
141
+ }
142
+ }
143
+ };
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Manages disposable instances by key, ensuring exactly one instance per key.
3
+ *
4
+ * Use cases:
5
+ *
6
+ * - One mutex per key to prevent concurrent writes
7
+ * - Preserving state during hot module reloading
8
+ *
9
+ * **Important:** Do not use this as global shared state. Use it locally or pass
10
+ * it as a dependency instead. The only exception is for hot reloading support,
11
+ * where Evolu uses it to ensure only one instance exists across module reloads
12
+ * (having two Evolu instances with the same name would mean two SQLite
13
+ * connections to the same file, which could corrupt data).
14
+ */
15
+ export interface Instances<K extends string, T extends Disposable> extends Disposable {
16
+ /**
17
+ * Ensures an instance exists for the given key, creating it if necessary. If
18
+ * the instance already exists, the optional `onCacheHit` callback is invoked
19
+ * to update the existing instance.
20
+ */
21
+ readonly ensure: (key: K, create: () => T, onCacheHit?: (instance: T) => void) => T;
22
+ /** Gets an instance by key, or returns `null` if it doesn't exist. */
23
+ readonly get: (key: K) => T | null;
24
+ /** Checks if an instance exists for the given key. */
25
+ readonly has: (key: K) => boolean;
26
+ /**
27
+ * Deletes and disposes an instance by key. Returns `true` if the instance
28
+ * existed and was deleted, `false` otherwise.
29
+ */
30
+ readonly delete: (key: K) => boolean;
31
+ }
32
+ /** Creates an {@link Instances}. */
33
+ export declare const createInstances: <K extends string, T extends Disposable>() => Instances<K, T>;
34
+ //# sourceMappingURL=Instances.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Instances.d.ts","sourceRoot":"","sources":["../../src/Instances.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,MAAM,WAAW,SAAS,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,UAAU,CAC/D,SAAQ,UAAU;IAClB;;;;OAIG;IACH,QAAQ,CAAC,MAAM,EAAE,CACf,GAAG,EAAE,CAAC,EACN,MAAM,EAAE,MAAM,CAAC,EACf,UAAU,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC,KAAK,IAAI,KAC/B,CAAC,CAAC;IAEP,sEAAsE;IACtE,QAAQ,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC;IAEnC,sDAAsD;IACtD,QAAQ,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,OAAO,CAAC;IAElC;;;OAGG;IACH,QAAQ,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,OAAO,CAAC;CACtC;AAED,oCAAoC;AACpC,eAAO,MAAM,eAAe,GAC1B,CAAC,SAAS,MAAM,EAChB,CAAC,SAAS,UAAU,OACjB,SAAS,CAAC,CAAC,EAAE,CAAC,CA6ClB,CAAC"}
@@ -0,0 +1,44 @@
1
+ /** Creates an {@link Instances}. */
2
+ export const createInstances = () => {
3
+ const instances = new Map();
4
+ return {
5
+ ensure: (key, create, onCacheHit) => {
6
+ let instance = instances.get(key);
7
+ if (instance == null) {
8
+ instance = create();
9
+ instances.set(key, instance);
10
+ }
11
+ else if (onCacheHit) {
12
+ onCacheHit(instance);
13
+ }
14
+ return instance;
15
+ },
16
+ get: (key) => instances.get(key) ?? null,
17
+ has: (key) => instances.has(key),
18
+ delete: (key) => {
19
+ const instance = instances.get(key);
20
+ if (instance == null)
21
+ return false;
22
+ instances.delete(key);
23
+ instance[Symbol.dispose]();
24
+ return true;
25
+ },
26
+ [Symbol.dispose]: () => {
27
+ const errors = [];
28
+ for (const instance of instances.values()) {
29
+ try {
30
+ instance[Symbol.dispose]();
31
+ }
32
+ catch (error) {
33
+ errors.push(error);
34
+ }
35
+ }
36
+ instances.clear();
37
+ if (errors.length === 1)
38
+ throw errors[0];
39
+ if (errors.length > 1) {
40
+ throw new AggregateError(errors, "Multiple disposal errors occurred");
41
+ }
42
+ },
43
+ };
44
+ };