@livestore/livestore 0.4.0 → 0.5.0-dev.1

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 (119) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/dist/QueryCache.d.ts.map +1 -1
  3. package/dist/SqliteDbWrapper.d.ts +1 -1
  4. package/dist/SqliteDbWrapper.d.ts.map +1 -1
  5. package/dist/SqliteDbWrapper.js +9 -7
  6. package/dist/SqliteDbWrapper.js.map +1 -1
  7. package/dist/SqliteDbWrapper.test.js +22 -5
  8. package/dist/SqliteDbWrapper.test.js.map +1 -1
  9. package/dist/effect/LiveStore.d.ts +68 -16
  10. package/dist/effect/LiveStore.d.ts.map +1 -1
  11. package/dist/effect/LiveStore.js +22 -20
  12. package/dist/effect/LiveStore.js.map +1 -1
  13. package/dist/effect/LiveStore.test.js +7 -8
  14. package/dist/effect/LiveStore.test.js.map +1 -1
  15. package/dist/effect/mod.js +2 -2
  16. package/dist/internal/mod.js +2 -2
  17. package/dist/live-queries/base-class.d.ts +20 -3
  18. package/dist/live-queries/base-class.d.ts.map +1 -1
  19. package/dist/live-queries/base-class.js +30 -4
  20. package/dist/live-queries/base-class.js.map +1 -1
  21. package/dist/live-queries/client-document-get-query.d.ts.map +1 -1
  22. package/dist/live-queries/client-document-get-query.js +1 -1
  23. package/dist/live-queries/computed.d.ts +1 -1
  24. package/dist/live-queries/computed.d.ts.map +1 -1
  25. package/dist/live-queries/computed.js +5 -5
  26. package/dist/live-queries/computed.js.map +1 -1
  27. package/dist/live-queries/db-query.d.ts +3 -3
  28. package/dist/live-queries/db-query.d.ts.map +1 -1
  29. package/dist/live-queries/db-query.js +30 -21
  30. package/dist/live-queries/db-query.js.map +1 -1
  31. package/dist/live-queries/db-query.test.js +15 -15
  32. package/dist/live-queries/db-query.test.js.map +1 -1
  33. package/dist/live-queries/mod.js +4 -4
  34. package/dist/live-queries/signal.d.ts +1 -1
  35. package/dist/live-queries/signal.d.ts.map +1 -1
  36. package/dist/live-queries/signal.js +2 -2
  37. package/dist/live-queries/signal.js.map +1 -1
  38. package/dist/live-queries/signal.test.js +5 -5
  39. package/dist/live-queries/signal.test.js.map +1 -1
  40. package/dist/mod.js +8 -8
  41. package/dist/reactive.d.ts +1 -1
  42. package/dist/reactive.d.ts.map +1 -1
  43. package/dist/reactive.js +20 -1
  44. package/dist/reactive.js.map +1 -1
  45. package/dist/reactive.test.js +1 -1
  46. package/dist/store/StoreRegistry.d.ts +12 -12
  47. package/dist/store/StoreRegistry.d.ts.map +1 -1
  48. package/dist/store/StoreRegistry.js +25 -25
  49. package/dist/store/StoreRegistry.js.map +1 -1
  50. package/dist/store/StoreRegistry.test.js +65 -62
  51. package/dist/store/StoreRegistry.test.js.map +1 -1
  52. package/dist/store/create-store.d.ts +27 -16
  53. package/dist/store/create-store.d.ts.map +1 -1
  54. package/dist/store/create-store.js +55 -27
  55. package/dist/store/create-store.js.map +1 -1
  56. package/dist/store/create-store.test.d.ts +2 -0
  57. package/dist/store/create-store.test.d.ts.map +1 -0
  58. package/dist/store/create-store.test.js +28 -0
  59. package/dist/store/create-store.test.js.map +1 -0
  60. package/dist/store/devtools.d.ts +1 -2
  61. package/dist/store/devtools.d.ts.map +1 -1
  62. package/dist/store/devtools.js +6 -5
  63. package/dist/store/devtools.js.map +1 -1
  64. package/dist/store/store-commit.test.d.ts +2 -0
  65. package/dist/store/store-commit.test.d.ts.map +1 -0
  66. package/dist/store/store-commit.test.js +85 -0
  67. package/dist/store/store-commit.test.js.map +1 -0
  68. package/dist/store/store-eventstream.test.js +6 -6
  69. package/dist/store/store-eventstream.test.js.map +1 -1
  70. package/dist/store/store-types.d.ts +10 -28
  71. package/dist/store/store-types.d.ts.map +1 -1
  72. package/dist/store/store-types.js +2 -38
  73. package/dist/store/store-types.js.map +1 -1
  74. package/dist/store/store-types.test.js +4 -4
  75. package/dist/store/store.d.ts +15 -16
  76. package/dist/store/store.d.ts.map +1 -1
  77. package/dist/store/store.js +149 -91
  78. package/dist/store/store.js.map +1 -1
  79. package/dist/utils/data-structures.d.ts.map +1 -1
  80. package/dist/utils/dev.d.ts.map +1 -1
  81. package/dist/utils/dev.js +1 -1
  82. package/dist/utils/dev.js.map +1 -1
  83. package/dist/utils/function-string.d.ts +3 -3
  84. package/dist/utils/function-string.d.ts.map +1 -1
  85. package/dist/utils/stack-info.d.ts.map +1 -1
  86. package/dist/utils/stack-info.js +2 -1
  87. package/dist/utils/stack-info.js.map +1 -1
  88. package/dist/utils/stack-info.test.js +1 -1
  89. package/dist/utils/tests/fixture.d.ts +26 -34
  90. package/dist/utils/tests/fixture.d.ts.map +1 -1
  91. package/dist/utils/tests/fixture.js.map +1 -1
  92. package/dist/utils/tests/mod.js +3 -3
  93. package/dist/utils/tests/otel.d.ts +6 -6
  94. package/dist/utils/tests/otel.d.ts.map +1 -1
  95. package/package.json +40 -50
  96. package/src/SqliteDbWrapper.test.ts +25 -3
  97. package/src/SqliteDbWrapper.ts +13 -10
  98. package/src/effect/LiveStore.test.ts +16 -16
  99. package/src/effect/LiveStore.ts +83 -51
  100. package/src/live-queries/__snapshots__/db-query.test.ts.snap +303 -97
  101. package/src/live-queries/base-class.ts +39 -7
  102. package/src/live-queries/computed.ts +5 -5
  103. package/src/live-queries/db-query.test.ts +9 -9
  104. package/src/live-queries/db-query.ts +42 -30
  105. package/src/live-queries/signal.test.ts +2 -2
  106. package/src/live-queries/signal.ts +3 -3
  107. package/src/reactive.ts +45 -35
  108. package/src/store/StoreRegistry.test.ts +62 -59
  109. package/src/store/StoreRegistry.ts +49 -38
  110. package/src/store/create-store.test.ts +35 -0
  111. package/src/store/create-store.ts +110 -55
  112. package/src/store/devtools.ts +283 -283
  113. package/src/store/store-commit.test.ts +137 -0
  114. package/src/store/store-eventstream.test.ts +16 -7
  115. package/src/store/store-types.test.ts +1 -1
  116. package/src/store/store-types.ts +10 -56
  117. package/src/store/store.ts +184 -110
  118. package/src/utils/dev.ts +1 -1
  119. package/src/utils/stack-info.ts +4 -1
@@ -0,0 +1,137 @@
1
+ import { expect } from 'vitest'
2
+
3
+ import { Vitest } from '@livestore/utils-dev/node-vitest'
4
+ import { Effect } from '@livestore/utils/effect'
5
+
6
+ import { events, makeTodoMvc, tables } from '../utils/tests/fixture.ts'
7
+
8
+ Vitest.describe('Store commit API', () => {
9
+ Vitest.live('should materialize events returned by the commit callback', () =>
10
+ Effect.gen(function* () {
11
+ const store = yield* makeTodoMvc()
12
+ const firstTodo = { id: '1', text: 'Make coffee', completed: false }
13
+ const secondTodo = { id: '2', text: 'Buy groceries', completed: false }
14
+
15
+ // Regression for https://github.com/livestorejs/livestore/issues/1611.
16
+ store.commit(() => {
17
+ expect(store.query(tables.todos)).toEqual([])
18
+ return [events.todoCreated(firstTodo), events.todoCreated(secondTodo)]
19
+ })
20
+
21
+ expect(store.query(tables.todos.orderBy('id', 'asc'))).toEqual([firstTodo, secondTodo])
22
+ }).pipe(Effect.scoped),
23
+ )
24
+
25
+ Vitest.live('should materialize a single callback event', () =>
26
+ Effect.gen(function* () {
27
+ const store = yield* makeTodoMvc()
28
+ const todo = { id: '1', text: 'Make coffee', completed: false }
29
+ const batch = [events.todoCreated(todo)] as const
30
+
31
+ store.commit(() => batch)
32
+
33
+ expect(store.query(tables.todos)).toEqual([todo])
34
+ }).pipe(Effect.scoped),
35
+ )
36
+
37
+ Vitest.live('should accept an empty callback', () =>
38
+ Effect.gen(function* () {
39
+ const store = yield* makeTodoMvc()
40
+
41
+ store.commit(() => [])
42
+
43
+ expect(store.query(tables.todos)).toEqual([])
44
+ }).pipe(Effect.scoped),
45
+ )
46
+
47
+ Vitest.live('should apply no events when the callback throws', () =>
48
+ Effect.gen(function* () {
49
+ const store = yield* makeTodoMvc()
50
+ const todo = { id: '1', text: 'Make coffee', completed: false }
51
+ const error = new Error('Callback failed')
52
+
53
+ expect(() =>
54
+ store.commit(() => {
55
+ throw error
56
+ }),
57
+ ).toThrow(error)
58
+
59
+ expect(store.query(tables.todos)).toEqual([])
60
+ store.commit(events.todoCreated(todo))
61
+ expect(store.query(tables.todos)).toEqual([todo])
62
+ }).pipe(Effect.scoped),
63
+ )
64
+
65
+ Vitest.live('should commit multiple returned event types in order', () =>
66
+ Effect.gen(function* () {
67
+ const store = yield* makeTodoMvc()
68
+ const todo = { id: '1', text: 'Make coffee', completed: false }
69
+
70
+ store.commit(() => [events.todoCreated(todo), events.todoCompleted({ id: todo.id })])
71
+
72
+ expect(store.query(tables.todos)).toEqual([{ ...todo, completed: true }])
73
+ }).pipe(Effect.scoped),
74
+ )
75
+
76
+ for (const options of [{}, { label: 'Create todo' }]) {
77
+ Vitest.live(`should accept callback options ${JSON.stringify(options)}`, () =>
78
+ Effect.gen(function* () {
79
+ const store = yield* makeTodoMvc()
80
+ const todo = { id: '1', text: 'Make coffee', completed: false }
81
+
82
+ store.commit(options, () => [events.todoCreated(todo)])
83
+
84
+ expect(store.query(tables.todos)).toEqual([todo])
85
+ }).pipe(Effect.scoped),
86
+ )
87
+ }
88
+
89
+ Vitest.live('should reject callback values that are not event arrays', () =>
90
+ Effect.gen(function* () {
91
+ const store = yield* makeTodoMvc()
92
+
93
+ expect(() => Reflect.apply(store.commit, store, [() => undefined])).toThrow(
94
+ 'store.commit callback must synchronously return an array of events',
95
+ )
96
+ expect(() => Reflect.apply(store.commit, store, [() => Promise.resolve([])])).toThrow(
97
+ 'store.commit callback must synchronously return an array of events',
98
+ )
99
+
100
+ expect(store.query(tables.todos)).toEqual([])
101
+ }).pipe(Effect.scoped),
102
+ )
103
+
104
+ Vitest.live('should reject non-array and asynchronous callbacks at the type level', () =>
105
+ Effect.gen(function* () {
106
+ const store = yield* makeTodoMvc()
107
+
108
+ // Keep invalid calls uninvoked so TypeScript checks the public overloads without executing them.
109
+ const assertRejectedCallbackTypes = () => {
110
+ // @ts-expect-error Commit callbacks must return an event array.
111
+ store.commit(() => {})
112
+ // @ts-expect-error Commit callbacks do not receive an event emitter.
113
+ store.commit((emit: () => void) => {
114
+ emit()
115
+ return []
116
+ })
117
+ // @ts-expect-error Commit callbacks must be synchronous.
118
+ store.commit(async () => [])
119
+ }
120
+
121
+ expect(assertRejectedCallbackTypes).toBeTypeOf('function')
122
+ }).pipe(Effect.scoped),
123
+ )
124
+
125
+ Vitest.live('should still accept direct events with options and no events', () =>
126
+ Effect.gen(function* () {
127
+ const store = yield* makeTodoMvc()
128
+ const todo = { id: '1', text: 'Make coffee', completed: false }
129
+
130
+ store.commit()
131
+ store.commit({ label: 'Empty commit' })
132
+ store.commit({ label: 'Create and complete' }, events.todoCreated(todo), events.todoCompleted({ id: todo.id }))
133
+
134
+ expect(store.query(tables.todos)).toEqual([{ ...todo, completed: true }])
135
+ }).pipe(Effect.scoped),
136
+ )
137
+ })
@@ -9,8 +9,17 @@ import type { ShutdownDeferred, Store } from '@livestore/livestore'
9
9
  import { createStore, makeShutdownDeferred } from '@livestore/livestore'
10
10
  import { omitUndefineds } from '@livestore/utils'
11
11
  import { Vitest } from '@livestore/utils-dev/node-vitest'
12
- import type { OtelTracer, Scope } from '@livestore/utils/effect'
13
- import { Context, Effect, FetchHttpClient, Layer, Logger, LogLevel, Queue, Stream } from '@livestore/utils/effect'
12
+ import {
13
+ type OtelTracer,
14
+ type Scope,
15
+ Context,
16
+ Effect,
17
+ FetchHttpClient,
18
+ Layer,
19
+ Queue,
20
+ References,
21
+ Stream,
22
+ } from '@livestore/utils/effect'
14
23
  import { nanoid } from '@livestore/utils/nanoid'
15
24
  import { PlatformNode } from '@livestore/utils/node'
16
25
 
@@ -22,7 +31,7 @@ const withTestCtx = Vitest.makeWithTestCtx({
22
31
  TestContextLive,
23
32
  PlatformNode.NodeFileSystem.layer,
24
33
  FetchHttpClient.layer,
25
- Logger.minimumLogLevel(LogLevel.Debug),
34
+ Layer.succeed(References.MinimumLogLevel, 'Debug'),
26
35
  ),
27
36
  })
28
37
 
@@ -32,7 +41,7 @@ const withTestCtx = Vitest.makeWithTestCtx({
32
41
  * tests/package-common/src/leader-thread/stream-events.test.ts
33
42
  */
34
43
  Vitest.describe('Store events API', () => {
35
- Vitest.scopedLive('should resume when reconnected to sync backend', (test) =>
44
+ Vitest.live('should resume when reconnected to sync backend', (test) =>
36
45
  Effect.gen(function* () {
37
46
  const { makeStore, mockSyncBackend } = yield* TestContext
38
47
  const store = yield* makeStore()
@@ -69,7 +78,7 @@ Vitest.describe('Store events API', () => {
69
78
  )
70
79
  })
71
80
 
72
- class TestContext extends Context.Tag('TestContext')<
81
+ class TestContext extends Context.Service<
73
82
  TestContext,
74
83
  {
75
84
  makeStore: (args?: {
@@ -87,9 +96,9 @@ class TestContext extends Context.Tag('TestContext')<
87
96
  mockSyncBackend: MockSyncBackend
88
97
  shutdownDeferred: ShutdownDeferred
89
98
  }
90
- >() {}
99
+ >()('TestContext') {}
91
100
 
92
- const TestContextLive = Layer.scoped(
101
+ const TestContextLive = Layer.effect(
93
102
  TestContext,
94
103
  Effect.gen(function* () {
95
104
  const mockSyncBackend = yield* makeMockSyncBackend()
@@ -20,7 +20,7 @@ describe('isQueryable', () => {
20
20
  it('identifies live query definitions', () => {
21
21
  const def = queryDb({
22
22
  query: 'select 1 as value',
23
- schema: Schema.Array(Schema.Struct({ value: Schema.Number })),
23
+ schema: Schema.Array(Schema.Struct({ value: Schema.Finite })),
24
24
  })
25
25
 
26
26
  expect(isQueryable(def)).toBe(true)
@@ -1,21 +1,19 @@
1
1
  import type * as otel from '@opentelemetry/api'
2
2
 
3
3
  import {
4
+ type BackendIdMismatchError,
4
5
  type ClientSession,
5
6
  type ClientSessionSyncProcessor,
6
- type ClientSessionSyncProcessorSimulationParams,
7
7
  type IntentionalShutdownCause,
8
8
  isQueryBuilder,
9
9
  type MaterializeError,
10
10
  type QueryBuilder,
11
11
  type StoreInterrupted,
12
- type BackendIdMismatchError,
13
12
  type UnknownError,
14
13
  } from '@livestore/common'
15
14
  import type { StreamEventsOptions } from '@livestore/common/leader-thread'
16
15
  import type { LiveStoreEvent, LiveStoreSchema } from '@livestore/common/schema'
17
- import type { Effect, Runtime, Schema, Scope } from '@livestore/utils/effect'
18
- import { Deferred, Predicate } from '@livestore/utils/effect'
16
+ import { type Context, Deferred, type Effect, Predicate, type Schema, type Scope } from '@livestore/utils/effect'
19
17
 
20
18
  import type {
21
19
  LiveQuery,
@@ -24,7 +22,7 @@ import type {
24
22
  ReactivityGraphContext,
25
23
  SignalDef,
26
24
  } from '../live-queries/base-class.ts'
27
- import { TypeId } from '../live-queries/base-class.ts'
25
+ import { isLiveQueryDef, TypeId } from '../live-queries/base-class.ts'
28
26
  import type { DebugRefreshReasonBase, Ref } from '../reactive.ts'
29
27
  import type { SqliteDbWrapper } from '../SqliteDbWrapper.ts'
30
28
  import type { ReferenceCountedSet } from '../utils/data-structures.ts'
@@ -98,7 +96,7 @@ export type StoreInternals = {
98
96
  * Exposed primarily for Devtools (e.g. databrowser) to validate ad‑hoc
99
97
  * event payloads. Application code should not depend on it directly.
100
98
  */
101
- readonly eventSchema: Schema.Schema<LiveStoreEvent.Client.Decoded, LiveStoreEvent.Client.Encoded>
99
+ readonly eventSchema: Schema.Codec<LiveStoreEvent.Client.Decoded, LiveStoreEvent.Client.Encoded>
102
100
 
103
101
  /**
104
102
  * The active client session backing this Store. Provides access to the
@@ -115,14 +113,14 @@ export type StoreInternals = {
115
113
  readonly sqliteDbWrapper: SqliteDbWrapper
116
114
 
117
115
  /**
118
- * Effect runtime and scope used to fork background fibers for the Store.
116
+ * Effect context and scope used to fork background fibers for the Store.
119
117
  *
120
- * - `runtime` executes effects from imperative Store APIs.
118
+ * - `services` provides services when executing effects from imperative Store APIs.
121
119
  * - `lifetimeScope` owns forked fibers; closed during Store shutdown.
122
120
  */
123
121
  readonly effectContext: {
124
- /** Effect runtime to run Store effects with proper environment. */
125
- readonly runtime: Runtime.Runtime<Scope.Scope>
122
+ /** Effect context used to run Store effects with proper services. */
123
+ readonly services: Context.Context<Scope.Scope>
126
124
  /** Scope that owns all long‑lived fibers spawned by the Store. */
127
125
  readonly lifetimeScope: Scope.Scope
128
126
  }
@@ -189,7 +187,7 @@ export type StoreConstructorParams<TSchema extends LiveStoreSchema = LiveStoreSc
189
187
  context: TContext
190
188
  otelOptions: OtelOptions
191
189
  effectContext: {
192
- runtime: Runtime.Runtime<Scope.Scope>
190
+ services: Context.Context<Scope.Scope>
193
191
  lifetimeScope: Scope.Scope
194
192
  }
195
193
  confirmUnsavedChanges: boolean
@@ -197,9 +195,6 @@ export type StoreConstructorParams<TSchema extends LiveStoreSchema = LiveStoreSc
197
195
  params: {
198
196
  leaderPushBatchSize: number
199
197
  eventQueryBatchSize?: number
200
- simulation?: {
201
- clientSessionSyncProcessor: typeof ClientSessionSyncProcessorSimulationParams.Type
202
- }
203
198
  }
204
199
  __runningInDevtools: boolean
205
200
  }
@@ -335,48 +330,7 @@ export namespace Queryable {
335
330
  export type Result<TQueryable extends Queryable<any>> = TQueryable extends Queryable<infer TResult> ? TResult : never
336
331
  }
337
332
 
338
- /**
339
- * Type guard that checks if a value is a query or signal definition.
340
- *
341
- * Use this to distinguish between definitions (blueprints) and instances (live queries).
342
- * Definitions are created by `queryDb()`, `computed()`, and `signal()`.
343
- *
344
- * @example
345
- * ```ts
346
- * const todos$ = queryDb(tables.todos.all())
347
- *
348
- * if (isLiveQueryDef(todos$)) {
349
- * console.log('This is a definition:', todos$.label)
350
- * }
351
- * ```
352
- */
353
- export const isLiveQueryDef = (value: unknown): value is LiveQueryDef<any> | SignalDef<any> => {
354
- if (typeof value !== 'object' || value === null) {
355
- return false
356
- }
357
-
358
- if (!('_tag' in value)) {
359
- return false
360
- }
361
-
362
- const tag = (value as LiveQueryDef<any> | SignalDef<any>)._tag
363
- if (tag !== 'def' && tag !== 'signal-def') {
364
- return false
365
- }
366
-
367
- const candidate = value as LiveQueryDef<any>
368
- if (typeof candidate.make !== 'function') {
369
- // The store calls make() to turn the definition into a live query instance.
370
- return false
371
- }
372
-
373
- if (typeof candidate.hash !== 'string' || typeof candidate.label !== 'string') {
374
- // Both identifiers must be present so the store can cache and log the query.
375
- return false
376
- }
377
-
378
- return true
379
- }
333
+ export { isLiveQueryDef }
380
334
 
381
335
  /**
382
336
  * Type guard that checks if a value is a live query instance.