@livestore/livestore 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (205) hide show
  1. package/README.md +108 -0
  2. package/dist/.tsbuildinfo +1 -0
  3. package/dist/LiveRiffleStore.d.ts +42 -0
  4. package/dist/LiveRiffleStore.d.ts.map +1 -0
  5. package/dist/LiveRiffleStore.js +36 -0
  6. package/dist/LiveRiffleStore.js.map +1 -0
  7. package/dist/QueryCache.d.ts +20 -0
  8. package/dist/QueryCache.d.ts.map +1 -0
  9. package/dist/QueryCache.js +71 -0
  10. package/dist/QueryCache.js.map +1 -0
  11. package/dist/__tests__/react/fixture.d.ts +141 -0
  12. package/dist/__tests__/react/fixture.d.ts.map +1 -0
  13. package/dist/__tests__/react/fixture.js +72 -0
  14. package/dist/__tests__/react/fixture.js.map +1 -0
  15. package/dist/__tests__/react/useLiveStoreComponent.test.d.ts +2 -0
  16. package/dist/__tests__/react/useLiveStoreComponent.test.d.ts.map +1 -0
  17. package/dist/__tests__/react/useLiveStoreComponent.test.js +78 -0
  18. package/dist/__tests__/react/useLiveStoreComponent.test.js.map +1 -0
  19. package/dist/__tests__/react/useRiffleComponent.test.d.ts +2 -0
  20. package/dist/__tests__/react/useRiffleComponent.test.d.ts.map +1 -0
  21. package/dist/__tests__/react/useRiffleComponent.test.js +78 -0
  22. package/dist/__tests__/react/useRiffleComponent.test.js.map +1 -0
  23. package/dist/__tests__/reactive.test.d.ts +2 -0
  24. package/dist/__tests__/reactive.test.d.ts.map +1 -0
  25. package/dist/__tests__/reactive.test.js +167 -0
  26. package/dist/__tests__/reactive.test.js.map +1 -0
  27. package/dist/backends/base.d.ts +13 -0
  28. package/dist/backends/base.d.ts.map +1 -0
  29. package/dist/backends/base.js +53 -0
  30. package/dist/backends/base.js.map +1 -0
  31. package/dist/backends/index.d.ts +41 -0
  32. package/dist/backends/index.d.ts.map +1 -0
  33. package/dist/backends/index.js +38 -0
  34. package/dist/backends/index.js.map +1 -0
  35. package/dist/backends/noop.d.ts +18 -0
  36. package/dist/backends/noop.d.ts.map +1 -0
  37. package/dist/backends/noop.js +21 -0
  38. package/dist/backends/noop.js.map +1 -0
  39. package/dist/backends/tauri.d.ts +24 -0
  40. package/dist/backends/tauri.d.ts.map +1 -0
  41. package/dist/backends/tauri.js +48 -0
  42. package/dist/backends/tauri.js.map +1 -0
  43. package/dist/backends/utils/idb.d.ts +10 -0
  44. package/dist/backends/utils/idb.d.ts.map +1 -0
  45. package/dist/backends/utils/idb.js +58 -0
  46. package/dist/backends/utils/idb.js.map +1 -0
  47. package/dist/backends/web-in-memory.d.ts +24 -0
  48. package/dist/backends/web-in-memory.d.ts.map +1 -0
  49. package/dist/backends/web-in-memory.js +46 -0
  50. package/dist/backends/web-in-memory.js.map +1 -0
  51. package/dist/backends/web-worker.d.ts +17 -0
  52. package/dist/backends/web-worker.d.ts.map +1 -0
  53. package/dist/backends/web-worker.js +139 -0
  54. package/dist/backends/web-worker.js.map +1 -0
  55. package/dist/backends/web.d.ts +28 -0
  56. package/dist/backends/web.d.ts.map +1 -0
  57. package/dist/backends/web.js +64 -0
  58. package/dist/backends/web.js.map +1 -0
  59. package/dist/bounded-collections.d.ts +34 -0
  60. package/dist/bounded-collections.d.ts.map +1 -0
  61. package/dist/bounded-collections.js +103 -0
  62. package/dist/bounded-collections.js.map +1 -0
  63. package/dist/componentKey.d.ts +20 -0
  64. package/dist/componentKey.d.ts.map +1 -0
  65. package/dist/componentKey.js +3 -0
  66. package/dist/componentKey.js.map +1 -0
  67. package/dist/effect/LiveStore.d.ts +42 -0
  68. package/dist/effect/LiveStore.d.ts.map +1 -0
  69. package/dist/effect/LiveStore.js +36 -0
  70. package/dist/effect/LiveStore.js.map +1 -0
  71. package/dist/effect/index.d.ts +2 -0
  72. package/dist/effect/index.d.ts.map +1 -0
  73. package/dist/effect/index.js +2 -0
  74. package/dist/effect/index.js.map +1 -0
  75. package/dist/events.d.ts +7 -0
  76. package/dist/events.d.ts.map +1 -0
  77. package/dist/events.js +2 -0
  78. package/dist/events.js.map +1 -0
  79. package/dist/inMemoryDatabase.d.ts +65 -0
  80. package/dist/inMemoryDatabase.d.ts.map +1 -0
  81. package/dist/inMemoryDatabase.js +241 -0
  82. package/dist/inMemoryDatabase.js.map +1 -0
  83. package/dist/index.d.ts +20 -0
  84. package/dist/index.d.ts.map +1 -0
  85. package/dist/index.js +10 -0
  86. package/dist/index.js.map +1 -0
  87. package/dist/otel.d.ts +5 -0
  88. package/dist/otel.d.ts.map +1 -0
  89. package/dist/otel.js +17 -0
  90. package/dist/otel.js.map +1 -0
  91. package/dist/react/LiveStoreContext.d.ts +11 -0
  92. package/dist/react/LiveStoreContext.d.ts.map +1 -0
  93. package/dist/react/LiveStoreContext.js +10 -0
  94. package/dist/react/LiveStoreContext.js.map +1 -0
  95. package/dist/react/LiveStoreProvider.d.ts +21 -0
  96. package/dist/react/LiveStoreProvider.d.ts.map +1 -0
  97. package/dist/react/LiveStoreProvider.js +48 -0
  98. package/dist/react/LiveStoreProvider.js.map +1 -0
  99. package/dist/react/RiffleProvider.d.ts +21 -0
  100. package/dist/react/RiffleProvider.d.ts.map +1 -0
  101. package/dist/react/RiffleProvider.js +48 -0
  102. package/dist/react/RiffleProvider.js.map +1 -0
  103. package/dist/react/StoreContext.d.ts +11 -0
  104. package/dist/react/StoreContext.d.ts.map +1 -0
  105. package/dist/react/StoreContext.js +10 -0
  106. package/dist/react/StoreContext.js.map +1 -0
  107. package/dist/react/index.d.ts +7 -0
  108. package/dist/react/index.d.ts.map +1 -0
  109. package/dist/react/index.js +6 -0
  110. package/dist/react/index.js.map +1 -0
  111. package/dist/react/useGlobalQuery.d.ts +3 -0
  112. package/dist/react/useGlobalQuery.d.ts.map +1 -0
  113. package/dist/react/useGlobalQuery.js +25 -0
  114. package/dist/react/useGlobalQuery.js.map +1 -0
  115. package/dist/react/useGraphQL.d.ts +11 -0
  116. package/dist/react/useGraphQL.d.ts.map +1 -0
  117. package/dist/react/useGraphQL.js +68 -0
  118. package/dist/react/useGraphQL.js.map +1 -0
  119. package/dist/react/useLiveStoreComponent.d.ts +70 -0
  120. package/dist/react/useLiveStoreComponent.d.ts.map +1 -0
  121. package/dist/react/useLiveStoreComponent.js +261 -0
  122. package/dist/react/useLiveStoreComponent.js.map +1 -0
  123. package/dist/react/useRiffleComponent.d.ts +70 -0
  124. package/dist/react/useRiffleComponent.d.ts.map +1 -0
  125. package/dist/react/useRiffleComponent.js +261 -0
  126. package/dist/react/useRiffleComponent.js.map +1 -0
  127. package/dist/react/useRiffleJsonHook.d.ts +4 -0
  128. package/dist/react/useRiffleJsonHook.d.ts.map +1 -0
  129. package/dist/react/useRiffleJsonHook.js +21 -0
  130. package/dist/react/useRiffleJsonHook.js.map +1 -0
  131. package/dist/react/utils/useStateRefWithReactiveInput.d.ts +13 -0
  132. package/dist/react/utils/useStateRefWithReactiveInput.d.ts.map +1 -0
  133. package/dist/react/utils/useStateRefWithReactiveInput.js +38 -0
  134. package/dist/react/utils/useStateRefWithReactiveInput.js.map +1 -0
  135. package/dist/reactive.d.ts +140 -0
  136. package/dist/reactive.d.ts.map +1 -0
  137. package/dist/reactive.js +301 -0
  138. package/dist/reactive.js.map +1 -0
  139. package/dist/reactiveQueries/base-class.d.ts +24 -0
  140. package/dist/reactiveQueries/base-class.d.ts.map +1 -0
  141. package/dist/reactiveQueries/base-class.js +22 -0
  142. package/dist/reactiveQueries/base-class.js.map +1 -0
  143. package/dist/reactiveQueries/graphql.d.ts +25 -0
  144. package/dist/reactiveQueries/graphql.d.ts.map +1 -0
  145. package/dist/reactiveQueries/graphql.js +14 -0
  146. package/dist/reactiveQueries/graphql.js.map +1 -0
  147. package/dist/reactiveQueries/js.d.ts +19 -0
  148. package/dist/reactiveQueries/js.d.ts.map +1 -0
  149. package/dist/reactiveQueries/js.js +13 -0
  150. package/dist/reactiveQueries/js.js.map +1 -0
  151. package/dist/reactiveQueries/sql.d.ts +31 -0
  152. package/dist/reactiveQueries/sql.d.ts.map +1 -0
  153. package/dist/reactiveQueries/sql.js +28 -0
  154. package/dist/reactiveQueries/sql.js.map +1 -0
  155. package/dist/schema.d.ts +163 -0
  156. package/dist/schema.d.ts.map +1 -0
  157. package/dist/schema.js +92 -0
  158. package/dist/schema.js.map +1 -0
  159. package/dist/store.d.ts +175 -0
  160. package/dist/store.d.ts.map +1 -0
  161. package/dist/store.js +546 -0
  162. package/dist/store.js.map +1 -0
  163. package/dist/util.d.ts +24 -0
  164. package/dist/util.d.ts.map +1 -0
  165. package/dist/util.js +51 -0
  166. package/dist/util.js.map +1 -0
  167. package/package.json +52 -0
  168. package/src/QueryCache.ts +81 -0
  169. package/src/__tests__/react/fixture.tsx +106 -0
  170. package/src/__tests__/react/useLiveStoreComponent.test.tsx +111 -0
  171. package/src/__tests__/reactive.test.ts +227 -0
  172. package/src/ambient.d.ts +7 -0
  173. package/src/backends/base.ts +67 -0
  174. package/src/backends/index.ts +94 -0
  175. package/src/backends/noop.ts +32 -0
  176. package/src/backends/tauri.ts +74 -0
  177. package/src/backends/utils/idb.ts +71 -0
  178. package/src/backends/web-in-memory.ts +65 -0
  179. package/src/backends/web-worker.ts +176 -0
  180. package/src/backends/web.ts +96 -0
  181. package/src/bounded-collections.ts +112 -0
  182. package/src/componentKey.ts +9 -0
  183. package/src/effect/LiveStore.ts +123 -0
  184. package/src/effect/index.ts +7 -0
  185. package/src/events.ts +8 -0
  186. package/src/inMemoryDatabase.ts +347 -0
  187. package/src/index.ts +47 -0
  188. package/src/otel.ts +20 -0
  189. package/src/react/LiveStoreContext.ts +23 -0
  190. package/src/react/LiveStoreProvider.tsx +93 -0
  191. package/src/react/index.ts +11 -0
  192. package/src/react/useGlobalQuery.ts +40 -0
  193. package/src/react/useGraphQL.ts +113 -0
  194. package/src/react/useLiveStoreComponent.ts +493 -0
  195. package/src/react/utils/useStateRefWithReactiveInput.ts +51 -0
  196. package/src/reactive.ts +538 -0
  197. package/src/reactiveQueries/base-class.ts +49 -0
  198. package/src/reactiveQueries/graphql.ts +52 -0
  199. package/src/reactiveQueries/js.ts +38 -0
  200. package/src/reactiveQueries/sql.ts +65 -0
  201. package/src/schema.ts +219 -0
  202. package/src/store.ts +889 -0
  203. package/src/util.ts +59 -0
  204. package/tsconfig.json +15 -0
  205. package/vitest.config.js +13 -0
@@ -0,0 +1,493 @@
1
+ import type { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core'
2
+ import { type LiteralUnion, omit, shouldNeverHappen } from '@livestore/utils'
3
+ import * as otel from '@opentelemetry/api'
4
+ import { isEqual, mapValues } from 'lodash-es'
5
+ import type { DependencyList } from 'react'
6
+ import React from 'react'
7
+ import { v4 as uuid } from 'uuid'
8
+
9
+ import type { ComponentKey } from '../componentKey.js'
10
+ import { labelForKey, tableNameForComponentKey } from '../componentKey.js'
11
+ import type { GetAtom } from '../reactive.js'
12
+ import type { LiveStoreGraphQLQuery } from '../reactiveQueries/graphql.js'
13
+ import type { LiveStoreJSQuery } from '../reactiveQueries/js.js'
14
+ import type { LiveStoreSQLQuery } from '../reactiveQueries/sql.js'
15
+ import type { ComponentStateSchema } from '../schema.js'
16
+ import type { BaseGraphQLContext, LiveStoreQuery, QueryResult, Store } from '../store.js'
17
+ import { sql } from '../util.js'
18
+ import { useStore } from './LiveStoreContext.js'
19
+ import { useStateRefWithReactiveInput } from './utils/useStateRefWithReactiveInput.js'
20
+
21
+ export interface QueryDefinitions {
22
+ [queryName: string]: LiveStoreQuery
23
+ }
24
+ export type QueryResults<TQuery> = { [queryName in keyof TQuery]: QueryResult<TQuery[queryName]> }
25
+
26
+ export type ReactiveSQL = <TResult>(
27
+ genQuery: (get: GetAtom) => string,
28
+ queriedTables: string[],
29
+ ) => LiveStoreSQLQuery<TResult>
30
+ export type ReactiveGraphQL = <
31
+ TResult extends Record<string, any>,
32
+ TVariables extends Record<string, any>,
33
+ TContext extends BaseGraphQLContext,
34
+ >(
35
+ query: DocumentNode<TResult, TVariables>,
36
+ genVariableValues: (get: GetAtom) => TVariables,
37
+ label?: string,
38
+ ) => LiveStoreGraphQLQuery<TResult, TVariables, TContext>
39
+
40
+ type RegisterSubscription = <TQuery extends LiveStoreQuery>(
41
+ query: TQuery,
42
+ onNewValue: (value: QueryResult<TQuery>) => void,
43
+ onUnsubscribe?: () => void,
44
+ ) => void
45
+
46
+ type GenQueries<TQueries, TStateResult> = (args: {
47
+ rxSQL: ReactiveSQL
48
+ rxGraphQL: ReactiveGraphQL
49
+ globalQueries: QueryDefinitions
50
+ state$: LiveStoreJSQuery<TStateResult>
51
+ /** Registers a subscription */
52
+ subscribe: RegisterSubscription
53
+ isTemporaryQuery: boolean
54
+ }) => TQueries
55
+
56
+ export type UseLiveStoreComponentProps<TQueries, TComponentState> = {
57
+ stateSchema?: ComponentStateSchema<TComponentState>
58
+ queries?: GenQueries<TQueries, TComponentState>
59
+ reactDeps?: React.DependencyList
60
+ componentKey: ComponentKeyConfig
61
+ }
62
+
63
+ export type ComponentKeyConfig = {
64
+ /**
65
+ * Name of the Component
66
+ *
67
+ * TODO we should eventually derive this info automatically from the component (TBD how though...)
68
+ */
69
+ name: string
70
+ id: LiteralUnion<'singleton' | '__ephemeral__', string>
71
+ }
72
+
73
+ type ComponentState = {
74
+ /** Equivalent to `componentKey.key` */
75
+ id: string
76
+ [key: string]: string | number | boolean | null
77
+ }
78
+
79
+ /**
80
+ * This is needed because the `React.useMemo` call below, can sometimes be called multiple times 🤷,
81
+ * so we need to "cache" the fact that we've already started a span for this component.
82
+ * The map entry is being removed again in the `React.useEffect` call below.
83
+ */
84
+ const spanAlreadyStartedCache = new Map<string, { span: otel.Span; otelCtx: otel.Context }>()
85
+
86
+ type UseLiveStoreJsonState<TState> = <TResult>(
87
+ jsonStringKey: keyof TState,
88
+ parse?: (_: unknown) => TResult,
89
+ ) => [value: TResult, setValue: (newVal: TResult | ((prevVal: TResult) => TResult)) => void]
90
+
91
+ /**
92
+ * Create reactive queries within a component.
93
+ * @param config.queries A function that returns a map of named reactive queries.
94
+ * @param config.componentKey A function that returns a unique key for this component.
95
+ * @param config.reactDeps A list of React-level dependencies that will refresh the queries.
96
+ */
97
+ export const useLiveStoreComponent = <TComponentState extends ComponentState, TQueries extends QueryDefinitions>({
98
+ stateSchema: stateSchema_,
99
+ queries = () => ({}) as TQueries,
100
+ componentKey: componentKeyConfig,
101
+ reactDeps = [],
102
+ }: UseLiveStoreComponentProps<TQueries, TComponentState>): {
103
+ queryResults: QueryResults<TQueries>
104
+ state: TComponentState
105
+ setState: Setters<TComponentState>
106
+ useLiveStoreJsonState: UseLiveStoreJsonState<TComponentState>
107
+ } => {
108
+ // TODO we should clean up the state schema handling to remove this special handling for the `id` column
109
+ const stateSchema = React.useMemo(
110
+ () => (stateSchema_ ? { ...stateSchema_, columns: omit(stateSchema_.columns, 'id' as any) } : undefined),
111
+ [stateSchema_],
112
+ )
113
+
114
+ // performance.mark('useLiveStoreComponent:start')
115
+ const componentKey = useComponentKey(componentKeyConfig, reactDeps)
116
+ const { store, globalQueries } = useStore()
117
+
118
+ const componentKeyLabel = React.useMemo(() => labelForKey(componentKey), [componentKey])
119
+
120
+ // The following `React.useMemo` and `React.useEffect` calls are used to start and end a span for the lifetime of this component.
121
+ const { span, otelCtx } = React.useMemo(() => {
122
+ const existingSpan = spanAlreadyStartedCache.get(componentKeyLabel)
123
+ if (existingSpan !== undefined) return existingSpan
124
+
125
+ const span = store.otel.tracer.startSpan(
126
+ `LiveStore:useLiveStoreComponent:${componentKeyLabel}`,
127
+ {},
128
+ store.otel.queriesSpanContext,
129
+ )
130
+
131
+ const otelCtx = otel.trace.setSpan(otel.context.active(), span)
132
+
133
+ spanAlreadyStartedCache.set(componentKeyLabel, { span, otelCtx })
134
+
135
+ return { span, otelCtx }
136
+ }, [componentKeyLabel, store.otel.queriesSpanContext, store.otel.tracer])
137
+
138
+ React.useEffect(
139
+ () => () => {
140
+ spanAlreadyStartedCache.delete(componentKeyLabel)
141
+ span.end()
142
+ },
143
+ [componentKeyLabel, span],
144
+ )
145
+
146
+ const generateQueries = React.useCallback(
147
+ ({
148
+ state$,
149
+ otelCtx,
150
+ registerSubscription,
151
+ isTemporaryQuery,
152
+ }: {
153
+ state$: LiveStoreJSQuery<TComponentState>
154
+ otelCtx: otel.Context
155
+ registerSubscription: RegisterSubscription
156
+ isTemporaryQuery: boolean
157
+ }) =>
158
+ queries({
159
+ rxSQL: <T>(genQuery: (get: GetAtom) => string, queriedTables: string[]) =>
160
+ store.querySQL<T>(genQuery, queriedTables, undefined, componentKey, undefined, otelCtx),
161
+ rxGraphQL: <Result extends Record<string, any>, Variables extends Record<string, any>>(
162
+ query: DocumentNode<Result, Variables>,
163
+ genVariableValues: (get: GetAtom) => Variables,
164
+ label?: string,
165
+ ) => store.queryGraphQL(query, genVariableValues, { componentKey, label }, otelCtx),
166
+ globalQueries,
167
+ state$,
168
+ subscribe: registerSubscription,
169
+ isTemporaryQuery,
170
+ }),
171
+
172
+ // NOTE: we don't include the queries function passed in by the user here;
173
+ // the reason is that we don't want to force them to memoize that function.
174
+ // Instead, we just assume that the function always has the same contents.
175
+ // This makes sense for LiveStore because the component config should be static.
176
+ // TODO: document this and consider whether it's the right API surface.
177
+ // eslint-disable-next-line react-hooks/exhaustive-deps
178
+ [store, componentKey, globalQueries],
179
+ )
180
+
181
+ const defaultComponentState = React.useMemo(() => {
182
+ const defaultState = (
183
+ stateSchema === undefined ? {} : mapValues(stateSchema.columns, (c) => c.default)
184
+ ) as TComponentState
185
+
186
+ defaultState.id = componentKeyConfig.id
187
+
188
+ return defaultState
189
+ }, [componentKeyConfig.id, stateSchema])
190
+
191
+ // Step 1:
192
+ // Synchronously create state and queries for initial render pass.
193
+ // We do this in a temporary query context which cleans up after itself, making it idempotent
194
+ // TODO get rid of the temporary query workaround
195
+ const { initialComponentState, initialQueryResults } = React.useMemo(() => {
196
+ return store.otel.tracer.startActiveSpan('LiveStore:useLiveStoreComponent:initial', {}, otelCtx, (span) => {
197
+ const otelCtx = otel.trace.setSpan(otel.context.active(), span)
198
+
199
+ return store.inTempQueryContext(() => {
200
+ try {
201
+ // create state query
202
+ let stateQuery: LiveStoreJSQuery<TComponentState>
203
+ if (stateSchema === undefined) {
204
+ // TODO don't set up a query if there's no state schema (keeps the graph more clean)
205
+ stateQuery = store.queryJS(
206
+ () => ({}),
207
+ componentKey,
208
+ undefined,
209
+ otelCtx,
210
+ ) as unknown as LiveStoreJSQuery<TComponentState>
211
+ } else {
212
+ const componentTableName = tableNameForComponentKey(componentKey)
213
+ const whereClause = componentKey._tag === 'singleton' ? '' : `where id = '${componentKey.id}'`
214
+ stateQuery = store
215
+ .querySQL<TComponentState>(
216
+ () => sql`select * from ${componentTableName} ${whereClause} limit 1`,
217
+ [componentTableName],
218
+ undefined,
219
+ componentKey,
220
+ `localState:query:${componentKeyLabel}`,
221
+ otelCtx,
222
+ )
223
+ .getFirstRow({ defaultValue: defaultComponentState })
224
+ }
225
+ const initialComponentState = stateQuery.results$.result
226
+
227
+ const queries = generateQueries({
228
+ state$: stateQuery,
229
+ otelCtx,
230
+ registerSubscription: () => {},
231
+ isTemporaryQuery: true,
232
+ })
233
+ for (const [name, query] of Object.entries(queries)) {
234
+ query.label = name
235
+ }
236
+ const initialQueryResults = mapValues(queries, (query) => query.results$.result) as QueryResults<TQueries>
237
+
238
+ return { initialComponentState, initialQueryResults }
239
+ } finally {
240
+ span.end()
241
+ }
242
+ })
243
+ })
244
+ }, [store, otelCtx, stateSchema, generateQueries, componentKey, componentKeyLabel, defaultComponentState])
245
+
246
+ // Now that we've computed the initial state synchronously,
247
+ // we can set up our useState calls w/ a default value populated...
248
+ const [componentStateRef, setComponentState_] = useStateRefWithReactiveInput<TComponentState>(initialComponentState)
249
+
250
+ const [queryResultsRef, setQueryResults_] = useStateRefWithReactiveInput<QueryResults<TQueries>>(initialQueryResults)
251
+
252
+ const setState = (
253
+ stateSchema === undefined
254
+ ? {}
255
+ : // TODO: do we have a better type for the values that can go in SQLite?
256
+ mapValues(stateSchema.columns, (_, columnName) => (value: string | number) => {
257
+ // Don't update the state if it's the same as the value already seen in the component
258
+ if (componentStateRef.current[columnName] === value) return
259
+
260
+ if (['componentKey', 'columnNames'].includes(columnName)) {
261
+ shouldNeverHappen(`Can't use reserved column name ${columnName}`)
262
+ }
263
+
264
+ return store.applyEvent('updateComponentState', {
265
+ componentKey,
266
+ columnNames: [columnName],
267
+ [columnName]: value,
268
+ })
269
+ })
270
+ ) as Setters<TComponentState>
271
+
272
+ setState.setMany = (columnValues: Partial<TComponentState>) => {
273
+ // TODO use hashing instead
274
+ // Don't update the state if it's the same as the value already seen in the component
275
+ if (Object.entries(columnValues).every(([columnName, value]) => componentStateRef.current[columnName] === value)) {
276
+ return
277
+ }
278
+
279
+ const columnNames = Object.keys(columnValues)
280
+
281
+ return store.applyEvent('updateComponentState', { componentKey, columnNames, ...columnValues })
282
+ }
283
+
284
+ // OK, now all the synchronous work is done;
285
+ // time to set up our long-running queries in an effect
286
+ React.useEffect(() => {
287
+ return store.otel.tracer.startActiveSpan(
288
+ 'LiveStore:useLiveStoreComponent:long-running',
289
+ { attributes: {} },
290
+ otelCtx,
291
+ (span) => {
292
+ const otelCtx = otel.trace.setSpan(otel.context.active(), span)
293
+ const unsubs: (() => void)[] = []
294
+
295
+ // create state query
296
+ let stateQuery: LiveStoreJSQuery<TComponentState>
297
+ if (stateSchema === undefined) {
298
+ stateQuery = store.queryJS(
299
+ () => ({}),
300
+ componentKey,
301
+ undefined,
302
+ otelCtx,
303
+ ) as unknown as LiveStoreJSQuery<TComponentState>
304
+ } else {
305
+ const componentTableName = tableNameForComponentKey(componentKey)
306
+ insertRowForComponentInstance({ store, componentKey, stateSchema })
307
+
308
+ const whereClause = componentKey._tag === 'singleton' ? '' : `where id = '${componentKey.id}'`
309
+ stateQuery = store
310
+ .querySQL<TComponentState>(
311
+ () => sql`select * from ${componentTableName} ${whereClause} limit 1`,
312
+ [componentTableName],
313
+ undefined,
314
+ componentKey,
315
+ // TODO introduce a refresh "grouping" concept to associate related refreshes in the debugger UI
316
+ `localState:query:${componentKeyLabel}`,
317
+ otelCtx,
318
+ )
319
+ .getFirstRow({ defaultValue: defaultComponentState })
320
+ }
321
+
322
+ unsubs.push(
323
+ store.subscribe(
324
+ stateQuery,
325
+ (results) => {
326
+ if (isEqual(results, componentStateRef.current) === false) {
327
+ setComponentState_(results as TComponentState)
328
+ }
329
+ },
330
+ undefined,
331
+ { label: `useLiveStoreComponent:localState:subscribe:${stateQuery.label}` },
332
+ ),
333
+ )
334
+
335
+ const registerSubscription: RegisterSubscription = (query, callback, onUnsubscribe) => {
336
+ unsubs.push(
337
+ store.subscribe(
338
+ query,
339
+ (results) => {
340
+ callback(results)
341
+ },
342
+ onUnsubscribe,
343
+ { label: `useLiveStoreComponent:query:manual-subscribe:${query.label}` },
344
+ ),
345
+ )
346
+ }
347
+
348
+ const queries = generateQueries({ state$: stateQuery, otelCtx, registerSubscription, isTemporaryQuery: false })
349
+ // Use the name given to this query in the useQueries hook as its label
350
+ for (const [name, query] of Object.entries(queries)) {
351
+ query.label = name
352
+ }
353
+ for (const [key, query] of Object.entries(queries)) {
354
+ unsubs.push(
355
+ store.subscribe(
356
+ query,
357
+ (results) => {
358
+ const newQueryResults = { ...queryResultsRef.current, [key]: results }
359
+ if (isEqual(newQueryResults, queryResultsRef.current) === false) {
360
+ setQueryResults_(newQueryResults)
361
+ }
362
+ },
363
+ undefined,
364
+ { label: `useLiveStoreComponent:query:subscribe:${query.label}` },
365
+ ),
366
+ )
367
+ }
368
+
369
+ return () => {
370
+ for (const unsub of unsubs) {
371
+ unsub()
372
+ }
373
+
374
+ span.end()
375
+ }
376
+ },
377
+ )
378
+ // NOTE excluding `setComponentState_` and `setQueryResults_` from the deps array as it seems to cause an infinite loop
379
+ // This should probably be improved
380
+ // eslint-disable-next-line react-hooks/exhaustive-deps
381
+ }, [
382
+ store,
383
+ componentKey,
384
+ stateSchema,
385
+ defaultComponentState,
386
+ generateQueries,
387
+ otelCtx,
388
+ componentStateRef,
389
+ // setComponentState_,
390
+ // setQueryResults_,
391
+ ])
392
+
393
+ // Very important: remove any queries / other resources associated w/ this component
394
+ React.useEffect(() => () => store.unmountComponent(componentKey), [store, componentKey])
395
+
396
+ // performance.mark('useLiveStoreComponent:end')
397
+ // performance.measure(`useLiveStoreComponent:${componentKey.type}`, 'useLiveStoreComponent:start', 'useLiveStoreComponent:end')
398
+
399
+ const state = componentStateRef.current
400
+
401
+ const useLiveStoreJsonState = <TResult>(
402
+ jsonStringKey: keyof TComponentState,
403
+ parse: (_: unknown) => TResult = (_) => _ as TResult,
404
+ ): [value: TResult, setValue: (newVal: TResult | ((prevVal: TResult) => TResult)) => void] => {
405
+ const value = React.useMemo<TResult>(() => {
406
+ return parse(JSON.parse(state[jsonStringKey] as string))
407
+ // eslint-disable-next-line react-hooks/exhaustive-deps
408
+ }, [state[jsonStringKey], parse])
409
+
410
+ const setValue = React.useCallback(
411
+ (newValOrFn: TResult | ((prev: TResult) => TResult)) => {
412
+ const newVal =
413
+ typeof newValOrFn === 'function'
414
+ ? // NOTE we're using the ref instead of the value because we want to be sure
415
+ // we're using the latest value when the setter is called
416
+ (newValOrFn as any)(parse(JSON.parse(componentStateRef.current[jsonStringKey] as string)))
417
+ : newValOrFn
418
+ setState[jsonStringKey](JSON.stringify(newVal) as any)
419
+ },
420
+ [parse, jsonStringKey],
421
+ )
422
+
423
+ return [value, setValue]
424
+ }
425
+
426
+ return {
427
+ queryResults: queryResultsRef.current,
428
+ state,
429
+ setState,
430
+ useLiveStoreJsonState,
431
+ }
432
+ }
433
+
434
+ export type Setters<TComponentState> = {
435
+ [k in keyof TComponentState]: (newValue: TComponentState[k]) => void
436
+ } & {
437
+ setMany: (newValues: Partial<TComponentState>) => void
438
+ }
439
+
440
+ export const useComponentKey = ({ name, id }: ComponentKeyConfig, deps: DependencyList = []) =>
441
+ React.useMemo<ComponentKey>(() => {
442
+ switch (id) {
443
+ case 'singleton': {
444
+ return { _tag: 'singleton', componentName: name, id: 'singleton' }
445
+ }
446
+ case '__ephemeral__': {
447
+ return { _tag: 'ephemeral', componentName: name, id: uuid() }
448
+ }
449
+ default: {
450
+ return { _tag: 'custom', componentName: name, id }
451
+ }
452
+ }
453
+ // eslint-disable-next-line react-hooks/exhaustive-deps
454
+ }, [...deps, id, name])
455
+
456
+ /**
457
+ * Create a row storing the state for a component instance, if none exists yet.
458
+ * Initialized with default values, and keyed on the component key.
459
+ */
460
+ const insertRowForComponentInstance = <T>({
461
+ store,
462
+ componentKey,
463
+ stateSchema,
464
+ }: {
465
+ store: Store<BaseGraphQLContext>
466
+ componentKey: ComponentKey
467
+ stateSchema: ComponentStateSchema<T>
468
+ }) => {
469
+ const columnNames = ['id', ...Object.keys(stateSchema.columns)]
470
+ const columnValues = columnNames.map((name) => `$${name}`).join(', ')
471
+
472
+ const tableName = tableNameForComponentKey(componentKey)
473
+ const insertQuery = sql`insert into ${tableName} (${columnNames.join(
474
+ ', ',
475
+ )}) select ${columnValues} where not exists(select 1 from ${tableName} where id = '${componentKey.id}')`
476
+
477
+ void store.execute(
478
+ insertQuery,
479
+ {
480
+ id: componentKey.id,
481
+ ...mapValues(stateSchema.columns, (column) => prepareValueForSql(column.default ?? null)),
482
+ },
483
+ [tableName],
484
+ )
485
+ }
486
+
487
+ const prepareValueForSql = (value: string | number | boolean | null) => {
488
+ if (typeof value === 'string' || typeof value === 'number' || value === null) {
489
+ return value
490
+ } else {
491
+ return value ? 1 : 0
492
+ }
493
+ }
@@ -0,0 +1,51 @@
1
+ import React from 'react'
2
+
3
+ /**
4
+ * A variant of `React.useState` which allows the `inputState` to change over time as well.
5
+ * Important: This hook is synchronous / single-render-pass (i.e. doesn't use `useEffect` or `setState` directly).
6
+ *
7
+ * Notes:
8
+ * - 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)
9
+ * - This hook might not work properly with React Suspense
10
+ * - Also see this Tweet for more potential problems: https://twitter.com/schickling/status/1677317711104278528
11
+ *
12
+ */
13
+ export const useStateRefWithReactiveInput = <T>(
14
+ inputState: T,
15
+ ): [React.MutableRefObject<T>, (newState: T | ((prev: T) => T)) => void] => {
16
+ const [_, rerender] = React.useState(0)
17
+
18
+ const lastKnownInputStateRef = React.useRef<T>(inputState)
19
+ const stateRef = React.useRef<T>(inputState)
20
+
21
+ if (lastKnownInputStateRef.current !== inputState) {
22
+ lastKnownInputStateRef.current = inputState
23
+
24
+ // NOTE we don't need to re-render here, because the component is already re-rendering due to the `inputState` change
25
+ stateRef.current = inputState
26
+ }
27
+
28
+ const setStateAndRerender = React.useCallback(
29
+ (newState: ((prev: T) => T) | T) => {
30
+ // @ts-expect-error https://github.com/microsoft/TypeScript/issues/37663
31
+ const val = typeof newState === 'function' ? newState(stateRef.current) : newState
32
+ stateRef.current = val
33
+ rerender((c) => c + 1)
34
+ },
35
+ [rerender],
36
+ )
37
+
38
+ return [stateRef, setStateAndRerender]
39
+ }
40
+
41
+ // Down-side of this implementation: Double render pass due to `setState` call (which forces a re-render)
42
+ // Keeping around for now in case `useStateRefWithReactiveInput` doesn't work out
43
+ // const _useStateWithReactiveInput = <T>(inputState: T): [T, (newState: T | ((prev: T) => T)) => void] => {
44
+ // const [externalState, setExternalState] = React.useState(inputState)
45
+
46
+ // if (externalState !== inputState) {
47
+ // setExternalState(inputState)
48
+ // }
49
+
50
+ // return [externalState, setExternalState]
51
+ // }