@livestore/livestore 0.2.0 → 0.3.0-dev.11

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 (163) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/dist/SqliteDbWrapper.d.ts +54 -0
  3. package/dist/SqliteDbWrapper.d.ts.map +1 -0
  4. package/dist/SqliteDbWrapper.js +212 -0
  5. package/dist/SqliteDbWrapper.js.map +1 -0
  6. package/dist/SynchronousDatabaseWrapper.d.ts +20 -6
  7. package/dist/SynchronousDatabaseWrapper.d.ts.map +1 -1
  8. package/dist/SynchronousDatabaseWrapper.js +38 -6
  9. package/dist/SynchronousDatabaseWrapper.js.map +1 -1
  10. package/dist/__tests__/fixture.d.ts +252 -0
  11. package/dist/__tests__/fixture.d.ts.map +1 -0
  12. package/dist/__tests__/fixture.js +18 -0
  13. package/dist/__tests__/fixture.js.map +1 -0
  14. package/dist/effect/LiveStore.d.ts +16 -12
  15. package/dist/effect/LiveStore.d.ts.map +1 -1
  16. package/dist/effect/LiveStore.js +14 -14
  17. package/dist/effect/LiveStore.js.map +1 -1
  18. package/dist/index.d.ts +6 -7
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +4 -4
  21. package/dist/index.js.map +1 -1
  22. package/dist/live-queries/base-class.d.ts +64 -21
  23. package/dist/live-queries/base-class.d.ts.map +1 -1
  24. package/dist/live-queries/base-class.js +56 -13
  25. package/dist/live-queries/base-class.js.map +1 -1
  26. package/dist/live-queries/computed.d.ts +7 -7
  27. package/dist/live-queries/computed.d.ts.map +1 -1
  28. package/dist/live-queries/computed.js +35 -11
  29. package/dist/live-queries/computed.js.map +1 -1
  30. package/dist/live-queries/{sql.d.ts → db-query.d.ts} +19 -14
  31. package/dist/live-queries/db-query.d.ts.map +1 -0
  32. package/dist/live-queries/db-query.js +244 -0
  33. package/dist/live-queries/db-query.js.map +1 -0
  34. package/dist/live-queries/db-query.test.d.ts +2 -0
  35. package/dist/live-queries/db-query.test.d.ts.map +1 -0
  36. package/dist/live-queries/db-query.test.js +123 -0
  37. package/dist/live-queries/db-query.test.js.map +1 -0
  38. package/dist/live-queries/db.d.ts +12 -15
  39. package/dist/live-queries/db.d.ts.map +1 -1
  40. package/dist/live-queries/db.js +72 -48
  41. package/dist/live-queries/db.js.map +1 -1
  42. package/dist/live-queries/db.test.js +18 -15
  43. package/dist/live-queries/db.test.js.map +1 -1
  44. package/dist/live-queries/graphql.d.ts +8 -8
  45. package/dist/live-queries/graphql.d.ts.map +1 -1
  46. package/dist/live-queries/graphql.js +35 -9
  47. package/dist/live-queries/graphql.js.map +1 -1
  48. package/dist/live-queries/make-ref.d.ts +20 -0
  49. package/dist/live-queries/make-ref.d.ts.map +1 -0
  50. package/dist/live-queries/make-ref.js +33 -0
  51. package/dist/live-queries/make-ref.js.map +1 -0
  52. package/dist/reactive.d.ts +15 -13
  53. package/dist/reactive.d.ts.map +1 -1
  54. package/dist/reactive.js +15 -9
  55. package/dist/reactive.js.map +1 -1
  56. package/dist/row-query-utils.d.ts +4 -4
  57. package/dist/row-query-utils.d.ts.map +1 -1
  58. package/dist/row-query-utils.js +14 -10
  59. package/dist/row-query-utils.js.map +1 -1
  60. package/dist/store/create-store.d.ts +13 -12
  61. package/dist/store/create-store.d.ts.map +1 -1
  62. package/dist/store/create-store.js +27 -33
  63. package/dist/store/create-store.js.map +1 -1
  64. package/dist/store/devtools.d.ts +3 -3
  65. package/dist/store/devtools.d.ts.map +1 -1
  66. package/dist/store/devtools.js +56 -34
  67. package/dist/store/devtools.js.map +1 -1
  68. package/dist/store/store-types.d.ts +18 -18
  69. package/dist/store/store-types.d.ts.map +1 -1
  70. package/dist/store/store-types.js.map +1 -1
  71. package/dist/store/store.d.ts +57 -38
  72. package/dist/store/store.d.ts.map +1 -1
  73. package/dist/store/store.js +225 -188
  74. package/dist/store/store.js.map +1 -1
  75. package/dist/store/store.test.d.ts +2 -0
  76. package/dist/store/store.test.d.ts.map +1 -0
  77. package/dist/store/store.test.js +27 -0
  78. package/dist/store/store.test.js.map +1 -0
  79. package/dist/utils/dev.d.ts.map +1 -1
  80. package/dist/utils/dev.js +3 -2
  81. package/dist/utils/dev.js.map +1 -1
  82. package/dist/utils/expo.d.ts +2 -0
  83. package/dist/utils/expo.d.ts.map +1 -0
  84. package/dist/utils/expo.js +8 -0
  85. package/dist/utils/expo.js.map +1 -0
  86. package/dist/utils/function-string.d.ts +7 -0
  87. package/dist/utils/function-string.d.ts.map +1 -0
  88. package/dist/utils/function-string.js +9 -0
  89. package/dist/utils/function-string.js.map +1 -0
  90. package/dist/utils/stack-info.d.ts.map +1 -1
  91. package/dist/utils/stack-info.js +6 -1
  92. package/dist/utils/stack-info.js.map +1 -1
  93. package/dist/utils/stack-info.test.js +54 -1
  94. package/dist/utils/stack-info.test.js.map +1 -1
  95. package/dist/utils/tests/fixture.d.ts +2 -6
  96. package/dist/utils/tests/fixture.d.ts.map +1 -1
  97. package/dist/utils/tests/fixture.js +7 -13
  98. package/dist/utils/tests/fixture.js.map +1 -1
  99. package/dist/utils/tests/mod.d.ts +1 -0
  100. package/dist/utils/tests/mod.d.ts.map +1 -1
  101. package/dist/utils/tests/mod.js +1 -0
  102. package/dist/utils/tests/mod.js.map +1 -1
  103. package/dist/utils/tests/otel.d.ts +60 -1
  104. package/dist/utils/tests/otel.d.ts.map +1 -1
  105. package/dist/utils/tests/otel.js +65 -4
  106. package/dist/utils/tests/otel.js.map +1 -1
  107. package/package.json +12 -12
  108. package/src/{SynchronousDatabaseWrapper.ts → SqliteDbWrapper.ts} +59 -13
  109. package/src/ambient.d.ts +1 -1
  110. package/src/effect/LiveStore.ts +32 -33
  111. package/src/index.ts +14 -7
  112. package/src/live-queries/__snapshots__/{db.test.ts.snap → db-query.test.ts.snap} +220 -69
  113. package/src/live-queries/base-class.ts +160 -40
  114. package/src/live-queries/computed.ts +45 -19
  115. package/src/live-queries/{db.test.ts → db-query.test.ts} +23 -12
  116. package/src/live-queries/{db.ts → db-query.ts} +124 -61
  117. package/src/live-queries/graphql.ts +47 -21
  118. package/src/live-queries/make-ref.ts +47 -0
  119. package/src/reactive.ts +52 -27
  120. package/src/row-query-utils.ts +29 -18
  121. package/src/store/create-store.ts +106 -113
  122. package/src/store/devtools.ts +65 -39
  123. package/src/store/store-types.ts +20 -18
  124. package/src/store/store.ts +361 -290
  125. package/src/utils/dev.ts +4 -2
  126. package/src/utils/function-string.ts +12 -0
  127. package/src/utils/stack-info.test.ts +58 -1
  128. package/src/utils/stack-info.ts +6 -1
  129. package/src/utils/tests/fixture.ts +6 -16
  130. package/src/utils/tests/mod.ts +1 -0
  131. package/src/utils/tests/otel.ts +71 -5
  132. package/dist/live-queries/sql.d.ts.map +0 -1
  133. package/dist/live-queries/sql.js +0 -175
  134. package/dist/live-queries/sql.js.map +0 -1
  135. package/dist/live-queries/sql.test.d.ts +0 -2
  136. package/dist/live-queries/sql.test.d.ts.map +0 -1
  137. package/dist/live-queries/sql.test.js +0 -285
  138. package/dist/live-queries/sql.test.js.map +0 -1
  139. package/dist/reactiveQueries/base-class.d.ts +0 -64
  140. package/dist/reactiveQueries/base-class.d.ts.map +0 -1
  141. package/dist/reactiveQueries/base-class.js +0 -31
  142. package/dist/reactiveQueries/base-class.js.map +0 -1
  143. package/dist/reactiveQueries/computed.d.ts +0 -26
  144. package/dist/reactiveQueries/computed.d.ts.map +0 -1
  145. package/dist/reactiveQueries/computed.js +0 -38
  146. package/dist/reactiveQueries/computed.js.map +0 -1
  147. package/dist/reactiveQueries/graphql.d.ts +0 -49
  148. package/dist/reactiveQueries/graphql.d.ts.map +0 -1
  149. package/dist/reactiveQueries/graphql.js +0 -122
  150. package/dist/reactiveQueries/graphql.js.map +0 -1
  151. package/dist/reactiveQueries/sql.d.ts +0 -62
  152. package/dist/reactiveQueries/sql.d.ts.map +0 -1
  153. package/dist/reactiveQueries/sql.js +0 -175
  154. package/dist/reactiveQueries/sql.js.map +0 -1
  155. package/dist/reactiveQueries/sql.test.d.ts +0 -2
  156. package/dist/reactiveQueries/sql.test.d.ts.map +0 -1
  157. package/dist/reactiveQueries/sql.test.js +0 -285
  158. package/dist/reactiveQueries/sql.test.js.map +0 -1
  159. package/dist/row-query.d.ts +0 -16
  160. package/dist/row-query.d.ts.map +0 -1
  161. package/dist/row-query.js +0 -30
  162. package/dist/row-query.js.map +0 -1
  163. package/src/global-state.ts +0 -20
@@ -2,7 +2,11 @@
2
2
 
3
3
  exports[`otel > otel 3`] = `
4
4
  {
5
- "_name": "test",
5
+ "_name": "createStore",
6
+ "attributes": {
7
+ "debugInstanceId": "test",
8
+ "storeId": "default",
9
+ },
6
10
  "children": [
7
11
  {
8
12
  "_name": "livestore.in-memory-db:execute",
@@ -17,39 +21,26 @@ exports[`otel > otel 3`] = `
17
21
  ",
18
22
  },
19
23
  },
24
+ {
25
+ "_name": "LiveStore:sync",
26
+ },
20
27
  {
21
28
  "_name": "LiveStore:mutations",
22
29
  "children": [
23
30
  {
24
31
  "_name": "LiveStore:mutate",
25
32
  "attributes": {
26
- "livestore.mutateLabel": "mutate",
33
+ "livestore.mutationEventTags": [
34
+ "livestore.RawSql",
35
+ ],
36
+ "livestore.mutationEventsCount": 1,
27
37
  },
28
38
  "children": [
29
39
  {
30
- "_name": "LiveStore:processWrites",
40
+ "_name": "livestore.in-memory-db:execute",
31
41
  "attributes": {
32
- "livestore.mutateLabel": "mutate",
42
+ "sql.query": "INSERT INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0)",
33
43
  },
34
- "children": [
35
- {
36
- "_name": "LiveStore:mutateWithoutRefresh",
37
- "attributes": {
38
- "livestore.args": "{
39
- "sql": "INSERT INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0)"
40
- }",
41
- "livestore.mutation": "livestore.RawSql",
42
- },
43
- "children": [
44
- {
45
- "_name": "livestore.in-memory-db:execute",
46
- "attributes": {
47
- "sql.query": "INSERT INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0)",
48
- },
49
- },
50
- ],
51
- },
52
- ],
53
44
  },
54
45
  ],
55
46
  },
@@ -98,9 +89,185 @@ exports[`otel > otel 3`] = `
98
89
  }
99
90
  `;
100
91
 
92
+ exports[`otel > with thunks 1`] = `
93
+ {
94
+ "atoms": [
95
+ {
96
+ "_tag": "ref",
97
+ "id": "node-1",
98
+ "isDestroyed": false,
99
+ "isDirty": false,
100
+ "label": "tableRef:todos",
101
+ "meta": {
102
+ "liveStoreRefType": "table",
103
+ },
104
+ "previousResult": {
105
+ "_tag": "Some",
106
+ "value": "null",
107
+ },
108
+ "refreshes": 0,
109
+ "sub": [],
110
+ "super": [],
111
+ },
112
+ {
113
+ "_tag": "ref",
114
+ "id": "node-2",
115
+ "isDestroyed": false,
116
+ "isDirty": false,
117
+ "label": "tableRef:app",
118
+ "meta": {
119
+ "liveStoreRefType": "table",
120
+ },
121
+ "previousResult": {
122
+ "_tag": "Some",
123
+ "value": "null",
124
+ },
125
+ "refreshes": 0,
126
+ "sub": [],
127
+ "super": [],
128
+ },
129
+ ],
130
+ "deferredEffects": [],
131
+ "effects": [],
132
+ }
133
+ `;
134
+
101
135
  exports[`otel > with thunks 3`] = `
102
136
  {
103
- "_name": "test",
137
+ "atoms": [
138
+ {
139
+ "_tag": "ref",
140
+ "id": "node-1",
141
+ "isDestroyed": false,
142
+ "isDirty": false,
143
+ "label": "tableRef:todos",
144
+ "meta": {
145
+ "liveStoreRefType": "table",
146
+ },
147
+ "previousResult": {
148
+ "_tag": "Some",
149
+ "value": "null",
150
+ },
151
+ "refreshes": 0,
152
+ "sub": [],
153
+ "super": [],
154
+ },
155
+ {
156
+ "_tag": "ref",
157
+ "id": "node-2",
158
+ "isDestroyed": false,
159
+ "isDirty": false,
160
+ "label": "tableRef:app",
161
+ "meta": {
162
+ "liveStoreRefType": "table",
163
+ },
164
+ "previousResult": {
165
+ "_tag": "Some",
166
+ "value": "null",
167
+ },
168
+ "refreshes": 0,
169
+ "sub": [],
170
+ "super": [],
171
+ },
172
+ ],
173
+ "deferredEffects": [],
174
+ "effects": [],
175
+ }
176
+ `;
177
+
178
+ exports[`otel > with thunks 4`] = `
179
+ {
180
+ "atoms": [
181
+ {
182
+ "_tag": "ref",
183
+ "id": "node-1",
184
+ "isDestroyed": false,
185
+ "isDirty": false,
186
+ "label": "tableRef:todos",
187
+ "meta": {
188
+ "liveStoreRefType": "table",
189
+ },
190
+ "previousResult": {
191
+ "_tag": "Some",
192
+ "value": "null",
193
+ },
194
+ "refreshes": 1,
195
+ "sub": [],
196
+ "super": [],
197
+ },
198
+ {
199
+ "_tag": "ref",
200
+ "id": "node-2",
201
+ "isDestroyed": false,
202
+ "isDirty": false,
203
+ "label": "tableRef:app",
204
+ "meta": {
205
+ "liveStoreRefType": "table",
206
+ },
207
+ "previousResult": {
208
+ "_tag": "Some",
209
+ "value": "null",
210
+ },
211
+ "refreshes": 0,
212
+ "sub": [],
213
+ "super": [],
214
+ },
215
+ ],
216
+ "deferredEffects": [],
217
+ "effects": [],
218
+ }
219
+ `;
220
+
221
+ exports[`otel > with thunks 6`] = `
222
+ {
223
+ "atoms": [
224
+ {
225
+ "_tag": "ref",
226
+ "id": "node-1",
227
+ "isDestroyed": false,
228
+ "isDirty": false,
229
+ "label": "tableRef:todos",
230
+ "meta": {
231
+ "liveStoreRefType": "table",
232
+ },
233
+ "previousResult": {
234
+ "_tag": "Some",
235
+ "value": "null",
236
+ },
237
+ "refreshes": 1,
238
+ "sub": [],
239
+ "super": [],
240
+ },
241
+ {
242
+ "_tag": "ref",
243
+ "id": "node-2",
244
+ "isDestroyed": false,
245
+ "isDirty": false,
246
+ "label": "tableRef:app",
247
+ "meta": {
248
+ "liveStoreRefType": "table",
249
+ },
250
+ "previousResult": {
251
+ "_tag": "Some",
252
+ "value": "null",
253
+ },
254
+ "refreshes": 0,
255
+ "sub": [],
256
+ "super": [],
257
+ },
258
+ ],
259
+ "deferredEffects": [],
260
+ "effects": [],
261
+ }
262
+ `;
263
+
264
+ exports[`otel > with thunks 7`] = `
265
+ {
266
+ "_name": "createStore",
267
+ "attributes": {
268
+ "debugInstanceId": "test",
269
+ "storeId": "default",
270
+ },
104
271
  "children": [
105
272
  {
106
273
  "_name": "livestore.in-memory-db:execute",
@@ -115,39 +282,26 @@ exports[`otel > with thunks 3`] = `
115
282
  ",
116
283
  },
117
284
  },
285
+ {
286
+ "_name": "LiveStore:sync",
287
+ },
118
288
  {
119
289
  "_name": "LiveStore:mutations",
120
290
  "children": [
121
291
  {
122
292
  "_name": "LiveStore:mutate",
123
293
  "attributes": {
124
- "livestore.mutateLabel": "mutate",
294
+ "livestore.mutationEventTags": [
295
+ "livestore.RawSql",
296
+ ],
297
+ "livestore.mutationEventsCount": 1,
125
298
  },
126
299
  "children": [
127
300
  {
128
- "_name": "LiveStore:processWrites",
301
+ "_name": "livestore.in-memory-db:execute",
129
302
  "attributes": {
130
- "livestore.mutateLabel": "mutate",
303
+ "sql.query": "INSERT INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0)",
131
304
  },
132
- "children": [
133
- {
134
- "_name": "LiveStore:mutateWithoutRefresh",
135
- "attributes": {
136
- "livestore.args": "{
137
- "sql": "INSERT INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0)"
138
- }",
139
- "livestore.mutation": "livestore.RawSql",
140
- },
141
- "children": [
142
- {
143
- "_name": "livestore.in-memory-db:execute",
144
- "attributes": {
145
- "sql.query": "INSERT INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0)",
146
- },
147
- },
148
- ],
149
- },
150
- ],
151
305
  },
152
306
  ],
153
307
  },
@@ -183,6 +337,9 @@ exports[`otel > with thunks 3`] = `
183
337
  "sql.rowsCount": 1,
184
338
  },
185
339
  "children": [
340
+ {
341
+ "_name": "js:where-filter",
342
+ },
186
343
  {
187
344
  "_name": "sql-in-memory-select",
188
345
  "attributes": {
@@ -201,7 +358,11 @@ exports[`otel > with thunks 3`] = `
201
358
 
202
359
  exports[`otel > with thunks with query builder and without labels 3`] = `
203
360
  {
204
- "_name": "test",
361
+ "_name": "createStore",
362
+ "attributes": {
363
+ "debugInstanceId": "test",
364
+ "storeId": "default",
365
+ },
205
366
  "children": [
206
367
  {
207
368
  "_name": "livestore.in-memory-db:execute",
@@ -216,39 +377,26 @@ exports[`otel > with thunks with query builder and without labels 3`] = `
216
377
  ",
217
378
  },
218
379
  },
380
+ {
381
+ "_name": "LiveStore:sync",
382
+ },
219
383
  {
220
384
  "_name": "LiveStore:mutations",
221
385
  "children": [
222
386
  {
223
387
  "_name": "LiveStore:mutate",
224
388
  "attributes": {
225
- "livestore.mutateLabel": "mutate",
389
+ "livestore.mutationEventTags": [
390
+ "livestore.RawSql",
391
+ ],
392
+ "livestore.mutationEventsCount": 1,
226
393
  },
227
394
  "children": [
228
395
  {
229
- "_name": "LiveStore:processWrites",
396
+ "_name": "livestore.in-memory-db:execute",
230
397
  "attributes": {
231
- "livestore.mutateLabel": "mutate",
398
+ "sql.query": "INSERT INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0)",
232
399
  },
233
- "children": [
234
- {
235
- "_name": "LiveStore:mutateWithoutRefresh",
236
- "attributes": {
237
- "livestore.args": "{
238
- "sql": "INSERT INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0)"
239
- }",
240
- "livestore.mutation": "livestore.RawSql",
241
- },
242
- "children": [
243
- {
244
- "_name": "livestore.in-memory-db:execute",
245
- "attributes": {
246
- "sql.query": "INSERT INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0)",
247
- },
248
- },
249
- ],
250
- },
251
- ],
252
400
  },
253
401
  ],
254
402
  },
@@ -284,6 +432,9 @@ exports[`otel > with thunks with query builder and without labels 3`] = `
284
432
  "sql.rowsCount": 1,
285
433
  },
286
434
  "children": [
435
+ {
436
+ "_name": "js:() => ({ completed: false })",
437
+ },
287
438
  {
288
439
  "_name": "sql-in-memory-select",
289
440
  "attributes": {
@@ -1,59 +1,119 @@
1
1
  import type { QueryInfo } from '@livestore/common'
2
+ import { isNotNil } from '@livestore/utils'
3
+ import { GlobalValue } from '@livestore/utils/effect'
2
4
  import type * as otel from '@opentelemetry/api'
3
5
 
4
- import { type Atom, type GetAtom, ReactiveGraph, throwContextNotSetError, type Thunk } from '../reactive.js'
6
+ import * as RG from '../reactive.js'
5
7
  import type { Store } from '../store/store.js'
6
8
  import type { QueryDebugInfo, RefreshReason } from '../store/store-types.js'
7
9
  import type { StackInfo } from '../utils/stack-info.js'
8
10
 
9
- export type ReactivityGraph = ReactiveGraph<RefreshReason, QueryDebugInfo, QueryContext>
11
+ export type ReactivityGraph = RG.ReactiveGraph<RefreshReason, QueryDebugInfo, ReactivityGraphContext>
10
12
 
11
13
  export const makeReactivityGraph = (): ReactivityGraph =>
12
- new ReactiveGraph<RefreshReason, QueryDebugInfo, QueryContext>()
14
+ new RG.ReactiveGraph<RefreshReason, QueryDebugInfo, ReactivityGraphContext>()
13
15
 
14
- export type QueryContext = {
16
+ export const defCounterRef = GlobalValue.globalValue('livestore-def-counter', () => ({ current: 0 }))
17
+
18
+ type LiveQueryDefHash = string
19
+
20
+ export type LiveQueryRCMap = Map<LiveQueryDefHash, RcRef<LiveQueryAny | ILiveQueryRef<any>>>
21
+
22
+ export type ReactivityGraphContext = {
15
23
  store: Store
24
+ liveQueryRCMap: LiveQueryRCMap
25
+ /** Back-reference to the reactivity graph for convenience */
26
+ reactivityGraph: WeakRef<ReactivityGraph>
16
27
  otelTracer: otel.Tracer
17
28
  rootOtelContext: otel.Context
18
29
  effectsWrapper: (run: () => void) => void
19
30
  }
20
31
 
21
- export type UnsubscribeQuery = () => void
22
-
23
- export type GetResult<TQuery extends LiveQueryAny> =
24
- TQuery extends LiveQuery<infer TResult, infer _1> ? TResult : unknown
32
+ export type GetResult<TQuery extends LiveQueryDefAny | LiveQueryAny> =
33
+ TQuery extends LiveQuery<infer TResult, infer _1>
34
+ ? TResult
35
+ : TQuery extends LiveQueryDef<infer TResult, infer _1>
36
+ ? TResult
37
+ : unknown
25
38
 
26
39
  let queryIdCounter = 0
27
40
 
28
41
  export type LiveQueryAny = LiveQuery<any, QueryInfo>
42
+ export type LiveQueryDefAny = LiveQueryDef<any, any>
43
+
44
+ export interface ILiveQueryRefDef<T> {
45
+ _tag: 'live-ref-def'
46
+ defaultValue: T
47
+ make: (ctx: ReactivityGraphContext) => RcRef<ILiveQueryRef<T>>
48
+ }
49
+
50
+ export interface ILiveQueryRef<T> {
51
+ _tag: 'live-ref'
52
+ reactivityGraph: ReactivityGraph
53
+ ref: RG.Ref<T, ReactivityGraphContext, RefreshReason>
54
+ set: (value: T) => void
55
+ get: () => T
56
+ destroy: () => void
57
+ }
29
58
 
30
59
  export const TypeId = Symbol.for('LiveQuery')
31
60
  export type TypeId = typeof TypeId
32
61
 
62
+ export interface RcRef<T> {
63
+ rc: number
64
+ value: T
65
+ deref: () => void
66
+ }
67
+
68
+ export type DepKey = string | number | ReadonlyArray<string | number | undefined | null>
69
+
70
+ export const depsToString = (deps: DepKey): string => {
71
+ if (typeof deps === 'string' || typeof deps === 'number') {
72
+ return deps.toString()
73
+ }
74
+ return deps.filter(isNotNil).join(',')
75
+ }
76
+
77
+ export interface LiveQueryDef<TResult, TQueryInfo extends QueryInfo = QueryInfo.None> {
78
+ _tag: 'def'
79
+ // TODO do we need both id and hash?
80
+ /** A unique identifier for the query definition */
81
+ id: number
82
+ /** Creates a new LiveQuery instance bound to a specific store/reactivityGraph */
83
+ make: (ctx: ReactivityGraphContext, otelContext?: otel.Context) => RcRef<LiveQuery<TResult, TQueryInfo>>
84
+ label: string
85
+ hash: string
86
+ queryInfo: TQueryInfo
87
+ }
88
+
89
+ /**
90
+ * A LiveQuery is stateful
91
+ */
33
92
  export interface LiveQuery<TResult, TQueryInfo extends QueryInfo = QueryInfo.None> {
34
93
  id: number
35
94
  _tag: 'computed' | 'db' | 'graphql'
36
95
  [TypeId]: TypeId
37
96
 
97
+ // reactivityGraph: ReactivityGraph
98
+
38
99
  /** This should only be used on a type-level and doesn't hold any value during runtime */
39
100
  '__result!': TResult
40
101
 
41
102
  /** A reactive thunk representing the query results */
42
- results$: Thunk<TResult, QueryContext, RefreshReason>
103
+ results$: RG.Thunk<TResult, ReactivityGraphContext, RefreshReason>
43
104
 
44
105
  label: string
45
106
 
46
- run: (otelContext?: otel.Context, debugRefreshReason?: RefreshReason) => TResult
47
-
48
- runAndDestroy: (otelContext?: otel.Context, debugRefreshReason?: RefreshReason) => TResult
107
+ run: (args: { otelContext?: otel.Context; debugRefreshReason?: RefreshReason }) => TResult
49
108
 
50
- destroy(): void
109
+ destroy: () => void
110
+ isDestroyed: boolean
51
111
 
52
- subscribe(
53
- onNewValue: (value: TResult) => void,
54
- onUnsubsubscribe?: () => void,
55
- options?: { label?: string; otelContext?: otel.Context },
56
- ): () => void
112
+ // subscribe(
113
+ // onNewValue: (value: TResult) => void,
114
+ // onUnsubsubscribe?: () => void,
115
+ // options?: { label?: string; otelContext?: otel.Context },
116
+ // ): () => void
57
117
 
58
118
  activeSubscriptions: Set<StackInfo>
59
119
 
@@ -75,11 +135,11 @@ export abstract class LiveStoreQueryBase<TResult, TQueryInfo extends QueryInfo>
75
135
  /** Human-readable label for the query for debugging */
76
136
  abstract label: string
77
137
 
78
- abstract results$: Thunk<TResult, QueryContext, RefreshReason>
138
+ abstract results$: RG.Thunk<TResult, ReactivityGraphContext, RefreshReason>
79
139
 
80
140
  activeSubscriptions: Set<StackInfo> = new Set()
81
141
 
82
- protected abstract reactivityGraph: ReactivityGraph
142
+ abstract readonly reactivityGraph: ReactivityGraph
83
143
 
84
144
  abstract queryInfo: TQueryInfo
85
145
 
@@ -89,33 +149,93 @@ export abstract class LiveStoreQueryBase<TResult, TQueryInfo extends QueryInfo>
89
149
 
90
150
  executionTimes: number[] = []
91
151
 
152
+ // TODO double check if this is needed
153
+ isDestroyed = false
92
154
  abstract destroy: () => void
93
155
 
94
- run = (otelContext?: otel.Context, debugRefreshReason?: RefreshReason): TResult =>
95
- this.results$.computeResult(otelContext, debugRefreshReason)
96
-
97
- runAndDestroy = (otelContext?: otel.Context, debugRefreshReason?: RefreshReason): TResult => {
98
- const result = this.run(otelContext, debugRefreshReason)
99
- this.destroy()
100
- return result
156
+ run = (args: { otelContext?: otel.Context; debugRefreshReason?: RefreshReason }): TResult => {
157
+ return this.results$.computeResult(args.otelContext, args.debugRefreshReason)
101
158
  }
102
159
 
103
- subscribe = (
104
- onNewValue: (value: TResult) => void,
105
- onUnsubsubscribe?: () => void,
106
- options?: { label?: string; otelContext?: otel.Context } | undefined,
107
- ): (() => void) =>
108
- this.reactivityGraph.context?.store.subscribe(this, onNewValue, onUnsubsubscribe, options) ??
109
- throwContextNotSetError(this.reactivityGraph)
110
- }
160
+ protected dependencyQueriesRef: DependencyQueriesRef = new Set()
111
161
 
112
- export type GetAtomResult = <T>(atom: Atom<T, any, RefreshReason> | LiveQuery<T, any>) => T
162
+ // subscribe = (
163
+ // onNewValue: (value: TResult) => void,
164
+ // onUnsubsubscribe?: () => void,
165
+ // options?: { label?: string; otelContext?: otel.Context } | undefined,
166
+ // ): (() => void) =>
167
+ // this.reactivityGraph.context?.store.subscribe(this, onNewValue, onUnsubsubscribe, options) ??
168
+ // RG.throwContextNotSetError(this.reactivityGraph)
169
+ }
113
170
 
114
- export const makeGetAtomResult = (get: GetAtom, otelContext: otel.Context) => {
115
- const getAtom: GetAtomResult = (atom) => {
116
- if (atom._tag === 'thunk' || atom._tag === 'ref') return get(atom, otelContext)
117
- return get(atom.results$, otelContext)
171
+ export type GetAtomResult = <T>(
172
+ atom:
173
+ | RG.Atom<T, any, RefreshReason>
174
+ | LiveQueryDef<T, any>
175
+ | LiveQuery<T, any>
176
+ | ILiveQueryRef<T>
177
+ | ILiveQueryRefDef<T>,
178
+ otelContext?: otel.Context | undefined,
179
+ debugRefreshReason?: RefreshReason | undefined,
180
+ ) => T
181
+
182
+ export type DependencyQueriesRef = Set<RcRef<LiveQueryAny | ILiveQueryRef<any>>>
183
+
184
+ export const makeGetAtomResult = (
185
+ get: RG.GetAtom,
186
+ ctx: ReactivityGraphContext,
187
+ otelContext: otel.Context,
188
+ dependencyQueriesRef: DependencyQueriesRef,
189
+ ) => {
190
+ // NOTE we're using the `otelContext` from `makeGetAtomResult` here, not the `otelContext` from `getAtom`
191
+ const getAtom: GetAtomResult = (atom, _otelContext, debugRefreshReason) => {
192
+ // ReactivityGraph atoms case
193
+ if (atom._tag === 'thunk' || atom._tag === 'ref') return get(atom, otelContext, debugRefreshReason)
194
+
195
+ // LiveQueryDef case
196
+ if (atom._tag === 'def' || atom._tag === 'live-ref-def') {
197
+ const query = atom.make(ctx)
198
+ dependencyQueriesRef.add(query)
199
+ // TODO deref the query on destroy
200
+ return getAtom(query.value, _otelContext, debugRefreshReason)
201
+ }
202
+
203
+ // LiveQueryRef case
204
+ if (atom._tag === 'live-ref') return get(atom.ref, otelContext, debugRefreshReason)
205
+
206
+ // LiveQuery case
207
+ return get(atom.results$, otelContext, debugRefreshReason)
118
208
  }
119
209
 
120
210
  return getAtom
121
211
  }
212
+
213
+ export const withRCMap = <T extends LiveQueryAny | ILiveQueryRef<any>>(
214
+ id: string,
215
+ make: (ctx: ReactivityGraphContext, otelContext?: otel.Context) => T,
216
+ ): ((ctx: ReactivityGraphContext, otelContext?: otel.Context) => RcRef<T>) => {
217
+ return (ctx, otelContext) => {
218
+ let item = ctx.liveQueryRCMap.get(id)
219
+ if (item) {
220
+ item.rc++
221
+ return item as RcRef<T>
222
+ }
223
+
224
+ const query$ = make(ctx, otelContext)
225
+
226
+ item = {
227
+ rc: 1,
228
+ value: query$,
229
+ deref: () => {
230
+ item!.rc--
231
+ if (item!.rc === 0) {
232
+ item!.value.destroy()
233
+ }
234
+ ctx.liveQueryRCMap.delete(id)
235
+ },
236
+ }
237
+ ctx.liveQueryRCMap.set(id, item)
238
+
239
+ return item as RcRef<T>
240
+ }
241
+ }