@livestore/livestore 0.0.12 → 0.0.15

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/README.md +7 -7
  2. package/dist/.tsbuildinfo +1 -0
  3. package/dist/QueryCache.d.ts +20 -0
  4. package/dist/QueryCache.d.ts.map +1 -0
  5. package/dist/QueryCache.js +71 -0
  6. package/dist/QueryCache.js.map +1 -0
  7. package/dist/__tests__/react/fixture.d.ts +25 -0
  8. package/dist/__tests__/react/fixture.d.ts.map +1 -0
  9. package/dist/__tests__/react/fixture.js +60 -0
  10. package/dist/__tests__/react/fixture.js.map +1 -0
  11. package/dist/__tests__/react/useLiveStoreComponent.test.d.ts +2 -0
  12. package/dist/__tests__/react/useLiveStoreComponent.test.d.ts.map +1 -0
  13. package/dist/__tests__/react/useLiveStoreComponent.test.js +78 -0
  14. package/dist/__tests__/react/useLiveStoreComponent.test.js.map +1 -0
  15. package/dist/__tests__/reactive.test.d.ts +2 -0
  16. package/dist/__tests__/reactive.test.d.ts.map +1 -0
  17. package/dist/__tests__/reactive.test.js +197 -0
  18. package/dist/__tests__/reactive.test.js.map +1 -0
  19. package/dist/bounded-collections.d.ts +34 -0
  20. package/dist/bounded-collections.d.ts.map +1 -0
  21. package/dist/bounded-collections.js +103 -0
  22. package/dist/bounded-collections.js.map +1 -0
  23. package/dist/componentKey.d.ts +20 -0
  24. package/dist/componentKey.d.ts.map +1 -0
  25. package/dist/componentKey.js +3 -0
  26. package/dist/componentKey.js.map +1 -0
  27. package/dist/effect/LiveStore.d.ts +36 -0
  28. package/dist/effect/LiveStore.d.ts.map +1 -0
  29. package/dist/effect/LiveStore.js +41 -0
  30. package/dist/effect/LiveStore.js.map +1 -0
  31. package/dist/effect/index.d.ts +2 -0
  32. package/dist/effect/index.d.ts.map +1 -0
  33. package/dist/effect/index.js +2 -0
  34. package/dist/effect/index.js.map +1 -0
  35. package/dist/events.d.ts +7 -0
  36. package/dist/events.d.ts.map +1 -0
  37. package/dist/events.js +2 -0
  38. package/dist/events.js.map +1 -0
  39. package/dist/inMemoryDatabase.d.ts +56 -0
  40. package/dist/inMemoryDatabase.d.ts.map +1 -0
  41. package/dist/inMemoryDatabase.js +223 -0
  42. package/dist/inMemoryDatabase.js.map +1 -0
  43. package/dist/index.d.ts +20 -0
  44. package/dist/index.d.ts.map +1 -0
  45. package/dist/index.js +9 -0
  46. package/dist/index.js.map +1 -0
  47. package/dist/migrations.d.ts +16 -0
  48. package/dist/migrations.d.ts.map +1 -0
  49. package/dist/migrations.js +67 -0
  50. package/dist/migrations.js.map +1 -0
  51. package/dist/otel.d.ts +4 -0
  52. package/dist/otel.d.ts.map +1 -0
  53. package/dist/otel.js +6 -0
  54. package/dist/otel.js.map +1 -0
  55. package/dist/react/LiveStoreContext.d.ts +11 -0
  56. package/dist/react/LiveStoreContext.d.ts.map +1 -0
  57. package/dist/react/LiveStoreContext.js +10 -0
  58. package/dist/react/LiveStoreContext.js.map +1 -0
  59. package/dist/react/LiveStoreProvider.d.ts +20 -0
  60. package/dist/react/LiveStoreProvider.d.ts.map +1 -0
  61. package/dist/react/LiveStoreProvider.js +52 -0
  62. package/dist/react/LiveStoreProvider.js.map +1 -0
  63. package/dist/react/index.d.ts +8 -0
  64. package/dist/react/index.d.ts.map +1 -0
  65. package/dist/react/index.js +6 -0
  66. package/dist/react/index.js.map +1 -0
  67. package/dist/react/useGraphQL.d.ts +13 -0
  68. package/dist/react/useGraphQL.d.ts.map +1 -0
  69. package/dist/react/useGraphQL.js +85 -0
  70. package/dist/react/useGraphQL.js.map +1 -0
  71. package/dist/react/useLiveStoreComponent.d.ts +75 -0
  72. package/dist/react/useLiveStoreComponent.d.ts.map +1 -0
  73. package/dist/react/useLiveStoreComponent.js +317 -0
  74. package/dist/react/useLiveStoreComponent.js.map +1 -0
  75. package/dist/react/useQuery.d.ts +3 -0
  76. package/dist/react/useQuery.d.ts.map +1 -0
  77. package/dist/react/useQuery.js +38 -0
  78. package/dist/react/useQuery.js.map +1 -0
  79. package/dist/react/utils/useStateRefWithReactiveInput.d.ts +13 -0
  80. package/dist/react/utils/useStateRefWithReactiveInput.d.ts.map +1 -0
  81. package/dist/react/utils/useStateRefWithReactiveInput.js +38 -0
  82. package/dist/react/utils/useStateRefWithReactiveInput.js.map +1 -0
  83. package/dist/reactive.d.ts +140 -0
  84. package/dist/reactive.d.ts.map +1 -0
  85. package/dist/reactive.js +302 -0
  86. package/dist/reactive.js.map +1 -0
  87. package/dist/reactiveQueries/base-class.d.ts +27 -0
  88. package/dist/reactiveQueries/base-class.d.ts.map +1 -0
  89. package/dist/reactiveQueries/base-class.js +23 -0
  90. package/dist/reactiveQueries/base-class.js.map +1 -0
  91. package/dist/reactiveQueries/graphql.d.ts +25 -0
  92. package/dist/reactiveQueries/graphql.d.ts.map +1 -0
  93. package/dist/reactiveQueries/graphql.js +18 -0
  94. package/dist/reactiveQueries/graphql.js.map +1 -0
  95. package/dist/reactiveQueries/js.d.ts +19 -0
  96. package/dist/reactiveQueries/js.d.ts.map +1 -0
  97. package/dist/reactiveQueries/js.js +13 -0
  98. package/dist/reactiveQueries/js.js.map +1 -0
  99. package/dist/reactiveQueries/sql.d.ts +31 -0
  100. package/dist/reactiveQueries/sql.d.ts.map +1 -0
  101. package/dist/reactiveQueries/sql.js +32 -0
  102. package/dist/reactiveQueries/sql.js.map +1 -0
  103. package/dist/schema.d.ts +81 -0
  104. package/dist/schema.d.ts.map +1 -0
  105. package/dist/schema.js +46 -0
  106. package/dist/schema.js.map +1 -0
  107. package/dist/storage/in-memory/index.d.ts +15 -0
  108. package/dist/storage/in-memory/index.d.ts.map +1 -0
  109. package/dist/storage/in-memory/index.js +14 -0
  110. package/dist/storage/in-memory/index.js.map +1 -0
  111. package/dist/storage/index.d.ts +14 -0
  112. package/dist/storage/index.d.ts.map +1 -0
  113. package/dist/storage/index.js +9 -0
  114. package/dist/storage/index.js.map +1 -0
  115. package/dist/storage/tauri/index.d.ts +19 -0
  116. package/dist/storage/tauri/index.d.ts.map +1 -0
  117. package/dist/storage/tauri/index.js +38 -0
  118. package/dist/storage/tauri/index.js.map +1 -0
  119. package/dist/storage/utils/idb.d.ts +10 -0
  120. package/dist/storage/utils/idb.d.ts.map +1 -0
  121. package/dist/storage/utils/idb.js +58 -0
  122. package/dist/storage/utils/idb.js.map +1 -0
  123. package/dist/storage/web-worker/index.d.ts +27 -0
  124. package/dist/storage/web-worker/index.d.ts.map +1 -0
  125. package/dist/storage/web-worker/index.js +74 -0
  126. package/dist/storage/web-worker/index.js.map +1 -0
  127. package/dist/storage/web-worker/worker.d.ts +13 -0
  128. package/dist/storage/web-worker/worker.d.ts.map +1 -0
  129. package/dist/storage/web-worker/worker.js +110 -0
  130. package/dist/storage/web-worker/worker.js.map +1 -0
  131. package/dist/store.d.ts +199 -0
  132. package/dist/store.d.ts.map +1 -0
  133. package/dist/store.js +603 -0
  134. package/dist/store.js.map +1 -0
  135. package/dist/util.d.ts +28 -0
  136. package/dist/util.d.ts.map +1 -0
  137. package/dist/util.js +55 -0
  138. package/dist/util.js.map +1 -0
  139. package/package.json +46 -19
  140. package/src/__tests__/react/fixture.tsx +23 -32
  141. package/src/__tests__/reactive.test.ts +3 -4
  142. package/src/effect/LiveStore.ts +22 -31
  143. package/src/events.ts +1 -1
  144. package/src/inMemoryDatabase.ts +115 -140
  145. package/src/index.ts +20 -20
  146. package/src/migrations.ts +119 -0
  147. package/src/otel.ts +0 -11
  148. package/src/react/LiveStoreProvider.tsx +24 -23
  149. package/src/react/index.ts +10 -1
  150. package/src/react/useGraphQL.ts +28 -2
  151. package/src/react/useLiveStoreComponent.ts +134 -50
  152. package/src/react/useQuery.ts +56 -0
  153. package/src/reactive.ts +6 -4
  154. package/src/reactiveQueries/base-class.ts +9 -3
  155. package/src/reactiveQueries/graphql.ts +4 -4
  156. package/src/reactiveQueries/js.ts +2 -2
  157. package/src/reactiveQueries/sql.ts +6 -6
  158. package/src/schema.ts +69 -145
  159. package/src/storage/in-memory/index.ts +21 -0
  160. package/src/storage/index.ts +27 -0
  161. package/src/{backends/tauri.ts → storage/tauri/index.ts} +14 -28
  162. package/src/storage/web-worker/index.ts +116 -0
  163. package/src/{backends/web-worker.ts → storage/web-worker/worker.ts} +17 -52
  164. package/src/store.ts +171 -98
  165. package/src/util.ts +13 -3
  166. package/tsconfig.json +1 -3
  167. package/src/backends/base.ts +0 -67
  168. package/src/backends/index.ts +0 -98
  169. package/src/backends/noop.ts +0 -32
  170. package/src/backends/web-in-memory.ts +0 -65
  171. package/src/backends/web.ts +0 -97
  172. package/src/react/useGlobalQuery.ts +0 -40
  173. /package/src/{backends → storage}/utils/idb.ts +0 -0
@@ -0,0 +1,317 @@
1
+ import { omit, shouldNeverHappen } from '@livestore/utils';
2
+ import { Schema } from '@livestore/utils/effect';
3
+ import * as otel from '@opentelemetry/api';
4
+ import { SqliteAst, SqliteDsl } from 'effect-db-schema';
5
+ import { isEqual, mapValues } from 'lodash-es';
6
+ import React from 'react';
7
+ import { v4 as uuid } from 'uuid';
8
+ import { labelForKey, tableNameForComponentKey } from '../componentKey.js';
9
+ import { migrateTable } from '../migrations.js';
10
+ import { SCHEMA_META_TABLE } from '../schema.js';
11
+ import { sql } from '../util.js';
12
+ import { useStore } from './LiveStoreContext.js';
13
+ import { useStateRefWithReactiveInput } from './utils/useStateRefWithReactiveInput.js';
14
+ // type ComponentState = {
15
+ // /** Equivalent to `componentKey.key` */
16
+ // id: string
17
+ // [key: string]: string | number | boolean | null
18
+ // }
19
+ /**
20
+ * This is needed because the `React.useMemo` call below, can sometimes be called multiple times 🤷,
21
+ * so we need to "cache" the fact that we've already started a span for this component.
22
+ * The map entry is being removed again in the `React.useEffect` call below.
23
+ */
24
+ const spanAlreadyStartedCache = new Map();
25
+ /**
26
+ * Create reactive queries within a component.
27
+ * @param config.queries A function that returns a map of named reactive queries.
28
+ * @param config.componentKey A function that returns a unique key for this component.
29
+ * @param config.reactDeps A list of React-level dependencies that will refresh the queries.
30
+ */
31
+ export const useLiveStoreComponent = ({ stateSchema: stateSchema_, queries = () => ({}), componentKey: componentKeyConfig, reactDeps = [], }) => {
32
+ // TODO validate schema to make sure each column has a default value
33
+ // TODO we should clean up the state schema handling to remove this special handling for the `id` column
34
+ const stateSchema = React.useMemo(() => (stateSchema_ ? { ...stateSchema_, columns: omit(stateSchema_.columns, 'id') } : undefined), [stateSchema_]);
35
+ // performance.mark('useLiveStoreComponent:start')
36
+ const componentKey = useComponentKey(componentKeyConfig, reactDeps);
37
+ const { store } = useStore();
38
+ const componentKeyLabel = React.useMemo(() => labelForKey(componentKey), [componentKey]);
39
+ // The following `React.useMemo` and `React.useEffect` calls are used to start and end a span for the lifetime of this component.
40
+ const { span, otelContext } = React.useMemo(() => {
41
+ const existingSpan = spanAlreadyStartedCache.get(componentKeyLabel);
42
+ if (existingSpan !== undefined)
43
+ return existingSpan;
44
+ const span = store.otel.tracer.startSpan(`LiveStore:useLiveStoreComponent:${componentKeyLabel}`, {}, store.otel.queriesSpanContext);
45
+ const otelContext = otel.trace.setSpan(otel.context.active(), span);
46
+ spanAlreadyStartedCache.set(componentKeyLabel, { span, otelContext });
47
+ return { span, otelContext };
48
+ }, [componentKeyLabel, store.otel.queriesSpanContext, store.otel.tracer]);
49
+ React.useEffect(() => () => {
50
+ spanAlreadyStartedCache.delete(componentKeyLabel);
51
+ span.end();
52
+ }, [componentKeyLabel, span]);
53
+ const generateQueries = React.useCallback(({ state$, otelContext, registerSubscription, isTemporaryQuery, }) => queries({
54
+ rxSQL: (genQuery, queriedTables, bindValues) => store.querySQL(genQuery, { queriedTables, bindValues, otelContext, componentKey }),
55
+ rxGraphQL: (query, genVariableValues, label) => store.queryGraphQL(query, genVariableValues, { componentKey, label, otelContext }),
56
+ rxJS: (genQuery) => store.queryJS(genQuery, { componentKey, otelContext }),
57
+ state$,
58
+ subscribe: registerSubscription,
59
+ isTemporaryQuery,
60
+ }),
61
+ // NOTE: we don't include the queries function passed in by the user here;
62
+ // the reason is that we don't want to force them to memoize that function.
63
+ // Instead, we just assume that the function always has the same contents.
64
+ // This makes sense for LiveStore because the component config should be static.
65
+ // TODO: document this and consider whether it's the right API surface.
66
+ // eslint-disable-next-line react-hooks/exhaustive-deps
67
+ [store, componentKey]);
68
+ const defaultComponentState = React.useMemo(() => {
69
+ const defaultState = (stateSchema === undefined ? {} : mapValues(stateSchema.columns, (c) => c.default));
70
+ // @ts-expect-error TODO fix typing
71
+ defaultState.id = componentKeyConfig.id;
72
+ return defaultState;
73
+ }, [componentKeyConfig.id, stateSchema]);
74
+ const componentStateEffectSchema = React.useMemo(() => (stateSchema ? SqliteDsl.structSchemaForTable(stateSchema) : Schema.any), [stateSchema]);
75
+ // Step 1:
76
+ // Synchronously create state and queries for initial render pass.
77
+ // We do this in a temporary query context which cleans up after itself, making it idempotent
78
+ // TODO get rid of the temporary query workaround
79
+ const { initialComponentState, initialQueryResults } = React.useMemo(() => {
80
+ return store.otel.tracer.startActiveSpan('LiveStore:useLiveStoreComponent:initial', {}, otelContext, (span) => {
81
+ const otelContext = otel.trace.setSpan(otel.context.active(), span);
82
+ // NOTE `inTempQueryContext` automatically destroys the queries once the callback is done
83
+ return store.inTempQueryContext(() => {
84
+ try {
85
+ // create state query
86
+ let state$;
87
+ if (stateSchema === undefined) {
88
+ // TODO don't set up a query if there's no state schema (keeps the graph more clean)
89
+ state$ = store.queryJS(() => ({}), {
90
+ componentKey,
91
+ otelContext,
92
+ });
93
+ }
94
+ else {
95
+ const componentTableName = tableNameForComponentKey(componentKey);
96
+ const whereClause = componentKey._tag === 'singleton' ? '' : `where id = '${componentKey.id}'`;
97
+ // TODO find a better solution for this
98
+ if (store.tableRefs[componentTableName] === undefined) {
99
+ const schemaHash = SqliteAst.hash(stateSchema.ast);
100
+ const res = store.inMemoryDB.select(sql `SELECT schemaHash FROM ${SCHEMA_META_TABLE} WHERE tableName = '${componentTableName}'`);
101
+ if (res.length === 0 || res[0].schemaHash !== schemaHash) {
102
+ migrateTable({ db: store._proxyDb, tableDef: stateSchema.ast, otelContext, schemaHash });
103
+ }
104
+ store.tableRefs[componentTableName] = store.graph.makeRef(null, {
105
+ equal: () => false,
106
+ label: componentTableName,
107
+ meta: { liveStoreRefType: 'table' },
108
+ });
109
+ }
110
+ state$ = store
111
+ .querySQL(() => sql `select * from ${componentTableName} ${whereClause} limit 1`, {
112
+ queriedTables: [componentTableName],
113
+ componentKey,
114
+ label: `localState:query:${componentKeyLabel}`,
115
+ otelContext,
116
+ })
117
+ // TODO consider to instead of just returning the default value, to write the default component state to the DB
118
+ .pipe((results) => results.length === 1
119
+ ? Schema.parseSync(componentStateEffectSchema)(results[0])
120
+ : defaultComponentState);
121
+ }
122
+ const initialComponentState = state$.results$.result;
123
+ const queries = generateQueries({
124
+ state$: state$,
125
+ otelContext,
126
+ registerSubscription: () => { },
127
+ isTemporaryQuery: true,
128
+ });
129
+ for (const [name, query] of Object.entries(queries)) {
130
+ query.label = name;
131
+ }
132
+ const initialQueryResults = mapValues(queries, (query) => query.results$.result);
133
+ return { initialComponentState, initialQueryResults };
134
+ }
135
+ finally {
136
+ span.end();
137
+ }
138
+ });
139
+ });
140
+ }, [
141
+ store,
142
+ otelContext,
143
+ stateSchema,
144
+ generateQueries,
145
+ componentKey,
146
+ componentKeyLabel,
147
+ componentStateEffectSchema,
148
+ defaultComponentState,
149
+ ]);
150
+ // Now that we've computed the initial state synchronously,
151
+ // we can set up our useState calls w/ a default value populated...
152
+ const [componentStateRef, setComponentState_] = useStateRefWithReactiveInput(initialComponentState);
153
+ const [queryResultsRef, setQueryResults_] = useStateRefWithReactiveInput(initialQueryResults);
154
+ const setState = (stateSchema === undefined
155
+ ? {}
156
+ : // TODO: do we have a better type for the values that can go in SQLite?
157
+ mapValues(stateSchema.columns, (column, columnName) => (value) => {
158
+ // Don't update the state if it's the same as the value already seen in the component
159
+ // @ts-expect-error TODO fix typing
160
+ if (componentStateRef.current[columnName] === value)
161
+ return;
162
+ const encodedValue = Schema.encodeSync(column.type.codec)(value);
163
+ if (['componentKey', 'columnNames'].includes(columnName)) {
164
+ shouldNeverHappen(`Can't use reserved column name ${columnName}`);
165
+ }
166
+ return store.applyEvent('updateComponentState', {
167
+ componentKey,
168
+ columnNames: [columnName],
169
+ [columnName]: encodedValue,
170
+ });
171
+ }));
172
+ setState.setMany = (columnValues) => {
173
+ // TODO use hashing instead
174
+ // Don't update the state if it's the same as the value already seen in the component
175
+ // @ts-expect-error TODO fix typing
176
+ if (Object.entries(columnValues).every(([columnName, value]) => componentStateRef.current[columnName] === value)) {
177
+ return;
178
+ }
179
+ const columnNames = Object.keys(columnValues);
180
+ return store.applyEvent('updateComponentState', { componentKey, columnNames, ...columnValues });
181
+ };
182
+ // OK, now all the synchronous work is done;
183
+ // time to set up our long-running queries in an effect
184
+ React.useEffect(() => {
185
+ return store.otel.tracer.startActiveSpan('LiveStore:useLiveStoreComponent:long-running', { attributes: {} }, otelContext, (span) => {
186
+ const otelContext = otel.trace.setSpan(otel.context.active(), span);
187
+ const unsubs = [];
188
+ // create state query
189
+ let state$;
190
+ if (stateSchema === undefined) {
191
+ // TODO remove this query
192
+ state$ = store.queryJS(() => ({}), {
193
+ componentKey,
194
+ otelContext,
195
+ label: 'empty-component-state',
196
+ });
197
+ }
198
+ else {
199
+ const componentTableName = tableNameForComponentKey(componentKey);
200
+ insertRowForComponentInstance({ store, componentKey, stateSchema });
201
+ const whereClause = componentKey._tag === 'singleton' ? '' : `where id = '${componentKey.id}'`;
202
+ state$ = store
203
+ .querySQL(() => sql `select * from ${componentTableName} ${whereClause} limit 1`, {
204
+ queriedTables: [componentTableName],
205
+ componentKey,
206
+ label: `localState:query:${componentKeyLabel}`,
207
+ otelContext,
208
+ })
209
+ // TODO consider to instead of just returning the default value, to write the default component state to the DB
210
+ .pipe((results) => results.length === 1 ? Schema.parseSync(componentStateEffectSchema)(results[0]) : defaultComponentState);
211
+ }
212
+ unsubs.push(store.subscribe(state$, (results) => {
213
+ if (isEqual(results, componentStateRef.current) === false) {
214
+ setComponentState_(results);
215
+ }
216
+ }, undefined, { label: `useLiveStoreComponent:localState:subscribe:${state$.label}` }));
217
+ const registerSubscription = (query$, callback, onUnsubscribe) => {
218
+ unsubs.push(store.subscribe(query$, (results) => {
219
+ callback(results);
220
+ }, onUnsubscribe, { label: `useLiveStoreComponent:query:manual-subscribe:${query$.label}` }));
221
+ };
222
+ const queries = generateQueries({ state$, otelContext, registerSubscription, isTemporaryQuery: false });
223
+ for (const [key, query] of Object.entries(queries)) {
224
+ // Use the field name given to this query in the useQueries hook as its label
225
+ query.label = key;
226
+ unsubs.push(store.subscribe(query, (results) => {
227
+ const newQueryResults = { ...queryResultsRef.current, [key]: results };
228
+ if (isEqual(newQueryResults, queryResultsRef.current) === false) {
229
+ setQueryResults_(newQueryResults);
230
+ }
231
+ }, undefined, { label: `useLiveStoreComponent:query:subscribe:${query.label}` }));
232
+ }
233
+ return () => {
234
+ for (const unsub of unsubs) {
235
+ unsub();
236
+ }
237
+ span.end();
238
+ };
239
+ });
240
+ // NOTE excluding `setComponentState_` and `setQueryResults_` from the deps array as it seems to cause an infinite loop
241
+ // This should probably be improved
242
+ // eslint-disable-next-line react-hooks/exhaustive-deps
243
+ }, [
244
+ store,
245
+ componentKey,
246
+ stateSchema,
247
+ defaultComponentState,
248
+ generateQueries,
249
+ otelContext,
250
+ componentStateRef,
251
+ // setComponentState_,
252
+ // setQueryResults_,
253
+ ]);
254
+ // Very important: remove any queries / other resources associated w/ this component
255
+ React.useEffect(() => () => store.unmountComponent(componentKey), [store, componentKey]);
256
+ // performance.mark('useLiveStoreComponent:end')
257
+ // performance.measure(`useLiveStoreComponent:${componentKey.type}`, 'useLiveStoreComponent:start', 'useLiveStoreComponent:end')
258
+ const state = componentStateRef.current;
259
+ const useLiveStoreJsonState = (jsonStringKey, parse = (_) => _) => {
260
+ const value = React.useMemo(() => {
261
+ return parse(JSON.parse(state[jsonStringKey]));
262
+ // eslint-disable-next-line react-hooks/exhaustive-deps
263
+ }, [state[jsonStringKey], parse]);
264
+ const setValue = React.useCallback((newValOrFn) => {
265
+ const newVal = typeof newValOrFn === 'function'
266
+ ? // NOTE we're using the ref instead of the value because we want to be sure
267
+ // we're using the latest value when the setter is called
268
+ newValOrFn(parse(JSON.parse(componentStateRef.current[jsonStringKey])))
269
+ : newValOrFn;
270
+ setState[jsonStringKey](JSON.stringify(newVal));
271
+ }, [parse, jsonStringKey]);
272
+ return [value, setValue];
273
+ };
274
+ return {
275
+ queryResults: queryResultsRef.current,
276
+ state,
277
+ setState,
278
+ useLiveStoreJsonState,
279
+ };
280
+ };
281
+ export const useComponentKey = ({ name, id }, deps = []) => React.useMemo(() => {
282
+ switch (id) {
283
+ case 'singleton': {
284
+ return { _tag: 'singleton', componentName: name, id: 'singleton' };
285
+ }
286
+ case '__ephemeral__': {
287
+ return { _tag: 'ephemeral', componentName: name, id: uuid() };
288
+ }
289
+ default: {
290
+ return { _tag: 'custom', componentName: name, id };
291
+ }
292
+ }
293
+ // eslint-disable-next-line react-hooks/exhaustive-deps
294
+ }, [...deps, id, name]);
295
+ /**
296
+ * Create a row storing the state for a component instance, if none exists yet.
297
+ * Initialized with default values, and keyed on the component key.
298
+ */
299
+ const insertRowForComponentInstance = ({ store, componentKey, stateSchema, }) => {
300
+ const columnNames = ['id', ...Object.keys(stateSchema.columns)];
301
+ const columnValues = columnNames.map((name) => `$${name}`).join(', ');
302
+ const tableName = tableNameForComponentKey(componentKey);
303
+ const insertQuery = sql `insert into ${tableName} (${columnNames.join(', ')}) select ${columnValues} where not exists(select 1 from ${tableName} where id = '${componentKey.id}')`;
304
+ void store.execute(insertQuery, {
305
+ ...mapValues(stateSchema.columns, (column) => prepareValueForSql(column.default ?? null)),
306
+ id: componentKey.id,
307
+ }, [tableName]);
308
+ };
309
+ const prepareValueForSql = (value) => {
310
+ if (typeof value === 'string' || typeof value === 'number' || value === null) {
311
+ return value;
312
+ }
313
+ else {
314
+ return value ? 1 : 0;
315
+ }
316
+ };
317
+ //# sourceMappingUR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AAC,CAAA;AAEzB;;;GAGG;AACH,MAAM,6BAA6B,GAAG,CAAC,EACrC,KAAK,EACL,YAAY,EACZ,WAAW,GAKZ,EAAE,EAAE;IACH,MAAM,WAAW,GAAG,CAAC,IAAI,EAAE,GAAG,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC,CAAA;IAC/D,MAAM,YAAY,GAAG,WAAW,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAErE,MAAM,SAAS,GAAG,wBAAwB,CAAC,YAAY,CAAC,CAAA;IACxD,MAAM,WAAW,GAAG,GAAG,CAAA,eAAe,SAAS,KAAK,WAAW,CAAC,IAAI,CAClE,IAAI,CACL,YAAY,YAAY,mCAAmC,SAAS,gBAAgB,YAAY,CAAC,EAAE,IAAI,CAAA;IAExG,KAAK,KAAK,CAAC,OAAO,CAChB,WAAW,EACX;QACE,GAAG,SAAS,CAAC,WAAW,CAAC,OAAO,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,kBAAkB,CAAC,MAAM,CAAC,OAAO,IAAI,IAAI,CAAC,CAAC;QACzF,EAAE,EAAE,YAAY,CAAC,EAAE;KACpB,EACD,CAAC,SAAS,CAAC,CACZ,CAAA;AACH,CAAC,CAAA;AAED,MAAM,kBAAkB,GAAG,CAAC,KAAuC,EAAE,EAAE;IACrE,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,EAAE;QAC5E,OAAO,KAAK,CAAA;KACb;SAAM;QACL,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;KACrB;AACH,CAAC,CAAA"}
@@ -0,0 +1,3 @@
1
+ import type { LiveStoreQuery, QueryResult, Store } from '../store.js';
2
+ export declare const useQuery: <Q extends LiveStoreQuery>(queryDef: (store: Store) => Q) => QueryResult<Q>;
3
+ //# sourceMappingURL=useQuery.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useQuery.d.ts","sourceRoot":"","sources":["../../src/react/useQuery.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,aAAa,CAAA;AAMrE,eAAO,MAAM,QAAQ,+CAAgD,KAAK,yBA6CzE,CAAA"}
@@ -0,0 +1,38 @@
1
+ import React from 'react';
2
+ import { labelForKey } from '../componentKey.js';
3
+ import { useStore } from './LiveStoreContext.js';
4
+ // TODO get rid of the query cache in favour of the new side-effect-free query definition approach https://www.notion.so/schickling/New-query-definition-approach-1097a78ef0e9495bac25f90417374756?pvs=4
5
+ const queryCache = new Map();
6
+ export const useQuery = (queryDef) => {
7
+ const { store } = useStore();
8
+ const query = React.useMemo(() => {
9
+ if (queryCache.has(queryDef))
10
+ return queryCache.get(queryDef);
11
+ const query = queryDef(store);
12
+ queryCache.set(queryDef, query);
13
+ return query;
14
+ }, [store, queryDef]);
15
+ // We know the query has a result by the time we use it; so we can synchronously populate a default state
16
+ const [value, setValue] = React.useState(query.results$.result);
17
+ // Subscribe to future updates for this query
18
+ React.useEffect(() => {
19
+ return store.otel.tracer.startActiveSpan(`LiveStore:useQuery:${labelForKey(query.componentKey)}:${query.label}`, { attributes: { label: query.label } }, query.otelContext, (span) => {
20
+ const cancel = store.subscribe(query, (v) => {
21
+ // NOTE: we return a reference to the result object within LiveStore;
22
+ // this implies that app code must not mutate the results, or else
23
+ // there may be weird reactivity bugs.
24
+ return setValue(v);
25
+ }, undefined, { label: query.label });
26
+ return () => {
27
+ // // NOTE destroying the whole query will also unsubscribe it
28
+ // query.destroy()
29
+ // TODO for now we'll still `cancel` manually, but we should remove this once we have some kind of
30
+ // ARC-based system
31
+ cancel();
32
+ span.end();
33
+ };
34
+ });
35
+ }, [query, store]);
36
+ return value;
37
+ };
38
+ //# sourceMappingURL=useQuery.js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EAAE,EAAE;YACP,MAAM,MAAM,GAAG,KAAK,CAAC,SAAS,CAC5B,KAAK,EACL,CAAC,CAAC,EAAE,EAAE;gBACJ,qEAAqE;gBACrE,kEAAkE;gBAClE,sCAAsC;gBACtC,OAAO,QAAQ,CAAC,CAAC,CAAC,CAAA;YACpB,CAAC,EACD,SAAS,EACT,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,EAAE,CACvB,CAAA;YACD,OAAO,GAAG,EAAE;gBACV,8DAA8D;gBAC9D,kBAAkB;gBAElB,kGAAkG;gBAClG,mBAAmB;gBACnB,MAAM,EAAE,CAAA;gBACR,IAAI,CAAC,GAAG,EAAE,CAAA;YACZ,CAAC,CAAA;QACH,CAAC,CACF,CAAA;IACH,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAA;IAElB,OAAO,KAAK,CAAA;AACd,CAAC,CAAA"}
@@ -0,0 +1,13 @@
1
+ import React from 'react';
2
+ /**
3
+ * A variant of `React.useState` which allows the `inputState` to change over time as well.
4
+ * Important: This hook is synchronous / single-render-pass (i.e. doesn't use `useEffect` or `setState` directly).
5
+ *
6
+ * Notes:
7
+ * - The output state is always reset to the input state in case the input state changes (i.e. the previous "external" `setStateAndRerender` call is forgotten)
8
+ * - This hook might not work properly with React Suspense
9
+ * - Also see this Tweet for more potential problems: https://twitter.com/schickling/status/1677317711104278528
10
+ *
11
+ */
12
+ export declare const useStateRefWithReactiveInput: <T>(inputState: T) => [React.MutableRefObject<T>, (newState: T | ((prev: T) => T)) => void];
13
+ //# sourceMappingURL=useStateRefWithReactiveInput.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useStateRefWithReactiveInput.d.ts","sourceRoot":"","sources":["../../../src/react/utils/useStateRefWithReactiveInput.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAEzB;;;;;;;;;GASG;AACH,eAAO,MAAM,4BAA4B,wFAE0B,IAAI,CAwBtE,CAAA"}
@@ -0,0 +1,38 @@
1
+ import React from 'react';
2
+ /**
3
+ * A variant of `React.useState` which allows the `inputState` to change over time as well.
4
+ * Important: This hook is synchronous / single-render-pass (i.e. doesn't use `useEffect` or `setState` directly).
5
+ *
6
+ * Notes:
7
+ * - The output state is always reset to the input state in case the input state changes (i.e. the previous "external" `setStateAndRerender` call is forgotten)
8
+ * - This hook might not work properly with React Suspense
9
+ * - Also see this Tweet for more potential problems: https://twitter.com/schickling/status/1677317711104278528
10
+ *
11
+ */
12
+ export const useStateRefWithReactiveInput = (inputState) => {
13
+ const [_, rerender] = React.useState(0);
14
+ const lastKnownInputStateRef = React.useRef(inputState);
15
+ const stateRef = React.useRef(inputState);
16
+ if (lastKnownInputStateRef.current !== inputState) {
17
+ lastKnownInputStateRef.current = inputState;
18
+ // NOTE we don't need to re-render here, because the component is already re-rendering due to the `inputState` change
19
+ stateRef.current = inputState;
20
+ }
21
+ const setStateAndRerender = React.useCallback((newState) => {
22
+ // @ts-expect-error https://github.com/microsoft/TypeScript/issues/37663
23
+ const val = typeof newState === 'function' ? newState(stateRef.current) : newState;
24
+ stateRef.current = val;
25
+ rerender((c) => c + 1);
26
+ }, [rerender]);
27
+ return [stateRef, setStateAndRerender];
28
+ };
29
+ // Down-side of this implementation: Double render pass due to `setState` call (which forces a re-render)
30
+ // Keeping around for now in case `useStateRefWithReactiveInput` doesn't work out
31
+ // const _useStateWithReactiveInput = <T>(inputState: T): [T, (newState: T | ((prev: T) => T)) => void] => {
32
+ // const [externalState, setExternalState] = React.useState(inputState)
33
+ // if (externalState !== inputState) {
34
+ // setExternalState(inputState)
35
+ // }
36
+ // return [externalState, setExternalState]
37
+ // }
38
+ //# sourceMappingURL=useStateRefWithReactiveInput.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useStateRefWithReactiveInput.js","sourceRoot":"","sources":["../../../src/react/utils/useStateRefWithReactiveInput.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAEzB;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,4BAA4B,GAAG,CAC1C,UAAa,EAC0D,EAAE;IACzE,MAAM,CAAC,CAAC,EAAE,QAAQ,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAA;IAEvC,MAAM,sBAAsB,GAAG,KAAK,CAAC,MAAM,CAAI,UAAU,CAAC,CAAA;IAC1D,MAAM,QAAQ,GAAG,KAAK,CAAC,MAAM,CAAI,UAAU,CAAC,CAAA;IAE5C,IAAI,sBAAsB,CAAC,OAAO,KAAK,UAAU,EAAE;QACjD,sBAAsB,CAAC,OAAO,GAAG,UAAU,CAAA;QAE3C,qHAAqH;QACrH,QAAQ,CAAC,OAAO,GAAG,UAAU,CAAA;KAC9B;IAED,MAAM,mBAAmB,GAAG,KAAK,CAAC,WAAW,CAC3C,CAAC,QAA8B,EAAE,EAAE;QACjC,wEAAwE;QACxE,MAAM,GAAG,GAAG,OAAO,QAAQ,KAAK,UAAU,CAAC,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAA;QAClF,QAAQ,CAAC,OAAO,GAAG,GAAG,CAAA;QACtB,QAAQ,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAA;IACxB,CAAC,EACD,CAAC,QAAQ,CAAC,CACX,CAAA;IAED,OAAO,CAAC,QAAQ,EAAE,mBAAmB,CAAC,CAAA;AACxC,CAAC,CAAA;AAED,yGAAyG;AACzG,iFAAiF;AACjF,4GAA4G;AAC5G,yEAAyE;AAEzE,wCAAwC;AACxC,mCAAmC;AACnC,MAAM;AAEN,6CAA6C;AAC7C,IAAI"}
@@ -0,0 +1,140 @@
1
+ import type { PrettifyFlat } from '@livestore/utils';
2
+ import type * as otel from '@opentelemetry/api';
3
+ import { BoundArray } from './bounded-collections.js';
4
+ export type GetAtom = <T>(atom: Atom<T>) => T;
5
+ export type Ref<T> = {
6
+ _tag: 'ref';
7
+ id: string;
8
+ result: T;
9
+ height: 0;
10
+ getResult: () => T;
11
+ sub: Set<Atom<any>>;
12
+ super: Set<Atom<any> | Effect>;
13
+ label?: string;
14
+ /** Container for meta information (e.g. the LiveStore Store) */
15
+ meta?: any;
16
+ equal: (a: T, b: T) => boolean;
17
+ };
18
+ type BaseThunk<T> = {
19
+ _tag: 'thunk';
20
+ id: string;
21
+ height: number;
22
+ getResult: (get: GetAtom, addDebugInfo: (debugInfo: any) => void) => T;
23
+ sub: Set<Atom<any>>;
24
+ super: Set<Atom<any> | Effect>;
25
+ label?: string;
26
+ /** Container for meta information (e.g. the LiveStore Store) */
27
+ meta?: any;
28
+ equal: (a: T, b: T) => boolean;
29
+ };
30
+ export type Thunk<T> = BaseThunk<T> & {
31
+ result: T;
32
+ };
33
+ export type Atom<T> = Ref<T> | Thunk<T>;
34
+ export type Effect = {
35
+ _tag: 'effect';
36
+ id: string;
37
+ doEffect: (get: GetAtom) => void;
38
+ sub: Set<Atom<any>>;
39
+ };
40
+ export type Taggable<T extends string = string> = {
41
+ _tag: T;
42
+ };
43
+ export type ReactiveGraphOptions = {
44
+ effectsWrapper?: (runEffects: () => void) => void;
45
+ otelTracer: otel.Tracer;
46
+ };
47
+ export type AtomDebugInfo<TDebugThunkInfo extends Taggable> = {
48
+ atom: SerializedAtom;
49
+ resultChanged: boolean;
50
+ durationMs: number;
51
+ debugInfo: TDebugThunkInfo;
52
+ };
53
+ export type RefreshDebugInfo<TDebugRefreshReason extends Taggable, TDebugThunkInfo extends Taggable> = {
54
+ /** Currently only used for easier handling in React (e.g. as key) */
55
+ id: string;
56
+ reason: TDebugRefreshReason;
57
+ refreshedAtoms: AtomDebugInfo<TDebugThunkInfo>[];
58
+ skippedRefresh: boolean;
59
+ durationMs: number;
60
+ /** Note we're using a regular `Date.now()` timestamp here as it's faster to produce and we don't need the fine accuracy */
61
+ completedTimestamp: number;
62
+ graphSnapshot: ReactiveGraphSnapshot;
63
+ };
64
+ export type RefreshReasonWithGenericReasons<T extends Taggable> = T | {
65
+ _tag: 'makeThunk';
66
+ label?: string;
67
+ } | {
68
+ _tag: 'makeEffect';
69
+ label?: string;
70
+ } | {
71
+ _tag: 'unknown';
72
+ };
73
+ export declare const unknownRefreshReason: () => {
74
+ _tag: "unknown";
75
+ };
76
+ export type SerializedAtom = Readonly<PrettifyFlat<Pick<Atom<unknown>, '_tag' | 'height' | 'id' | 'label' | 'meta' | 'result'> & {
77
+ sub: string[];
78
+ super: string[];
79
+ }>>;
80
+ export type SerializedEffect = Readonly<PrettifyFlat<Pick<Effect, '_tag' | 'id'>>>;
81
+ type ReactiveGraphSnapshot = {
82
+ readonly atoms: SerializedAtom[];
83
+ readonly effects: SerializedEffect[];
84
+ /** IDs of atoms and effects that are dirty */
85
+ readonly dirtyNodes: string[];
86
+ };
87
+ export declare class ReactiveGraph<TDebugRefreshReason extends Taggable, TDebugThunkInfo extends Taggable> {
88
+ private atoms;
89
+ private effects;
90
+ private otelTracer;
91
+ readonly dirtyNodes: Set<Atom<any> | Effect>;
92
+ effectsWrapper: (runEffects: () => void) => void;
93
+ debugRefreshInfos: BoundArray<RefreshDebugInfo<RefreshReasonWithGenericReasons<TDebugRefreshReason>, TDebugThunkInfo>>;
94
+ constructor(options: ReactiveGraphOptions);
95
+ makeRef<T>(val: T, options?: {
96
+ label?: string;
97
+ meta?: unknown;
98
+ equal?: (a: T, b: T) => boolean;
99
+ }): Ref<T>;
100
+ makeThunk<T>(getResult: (get: GetAtom, addDebugInfo: (debugInfo: TDebugThunkInfo) => void) => T, options: {
101
+ label?: string;
102
+ meta?: any;
103
+ equal?: (a: T, b: T) => boolean;
104
+ /** Debug info for initializing the thunk (i.e. running it the first time) */
105
+ debugRefreshReason?: RefreshReasonWithGenericReasons<TDebugRefreshReason>;
106
+ } | undefined, otelContext: otel.Context): Thunk<T>;
107
+ destroy(node: Atom<any> | Effect): void;
108
+ makeEffect(doEffect: (get: GetAtom) => void, options: {
109
+ label?: string;
110
+ } | undefined, otelContext: otel.Context): Effect;
111
+ setRef<T>(ref: Ref<T>, val: T, options: {
112
+ otelHint?: string;
113
+ skipRefresh?: boolean;
114
+ debugRefreshReason?: TDebugRefreshReason;
115
+ } | undefined, otelContext: otel.Context): void;
116
+ setRefs<T>(refs: [Ref<T>, T][], options: {
117
+ otelHint?: string;
118
+ skipRefresh?: boolean;
119
+ debugRefreshReason?: TDebugRefreshReason;
120
+ } | undefined, otelContext: otel.Context): void;
121
+ get<T>(atom: Atom<T>, context: Atom<any> | Effect): T;
122
+ /**
123
+ * Update the graph to be consistent with the current values of the root atoms.
124
+ * Generally we run this after a ref is updated.
125
+ * At the end of the refresh, we run any effects that were scheduled.
126
+ *
127
+ * @param roots Root atoms to start the refresh from
128
+ */
129
+ refresh(options: {
130
+ otelHint?: string;
131
+ debugRefreshReason?: RefreshReasonWithGenericReasons<TDebugRefreshReason>;
132
+ } | undefined, otelContext: otel.Context): void;
133
+ label(atom: Atom<any> | Effect): string;
134
+ addEdge(superComp: Atom<any> | Effect, subComp: Atom<any>): void;
135
+ removeEdge(superComp: Atom<any> | Effect, subComp: Atom<any>): void;
136
+ updateAtomHeight(atom: Atom<any> | Effect): void;
137
+ private getSnapshot;
138
+ }
139
+ export {};
140
+ //# sourceMappingURL=reactive.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reactive.d.ts","sourceRoot":"","sources":["../src/reactive.ts"],"names":[],"mappings":"AAyBA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAA;AAEpD,OAAO,KAAK,KAAK,IAAI,MAAM,oBAAoB,CAAA;AAG/C,OAAO,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAA;AAKrD,MAAM,MAAM,OAAO,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,CAAA;AAE7C,MAAM,MAAM,GAAG,CAAC,CAAC,IAAI;IACnB,IAAI,EAAE,KAAK,CAAA;IACX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