@livestore/livestore 0.3.0-dev.26 → 0.3.0-dev.27

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.
@@ -1,6 +1,6 @@
1
1
  import type { ClientSession, IntentionalShutdownCause, StoreInterrupted, UnexpectedError } from '@livestore/common'
2
- import type { EventId, LiveStoreSchema, MutationEvent } from '@livestore/common/schema'
3
- import type { Effect, MutableHashMap, Runtime, Scope } from '@livestore/utils/effect'
2
+ import type { LiveStoreSchema, MutationEvent } from '@livestore/common/schema'
3
+ import type { Effect, Runtime, Scope } from '@livestore/utils/effect'
4
4
  import { Deferred } from '@livestore/utils/effect'
5
5
  import type * as otel from '@opentelemetry/api'
6
6
 
@@ -41,16 +41,16 @@ export type StoreOptions<TSchema extends LiveStoreSchema = LiveStoreSchema, TCon
41
41
  storeId: string
42
42
  context: TContext
43
43
  otelOptions: OtelOptions
44
- disableDevtools?: boolean
45
- lifetimeScope: Scope.Scope
46
- runtime: Runtime.Runtime<Scope.Scope>
44
+ effectContext: {
45
+ runtime: Runtime.Runtime<Scope.Scope>
46
+ lifetimeScope: Scope.Scope
47
+ }
47
48
  confirmUnsavedChanges: boolean
48
49
  batchUpdates: (runUpdates: () => void) => void
49
- // TODO validate whether we still need this
50
- unsyncedMutationEvents: MutableHashMap.MutableHashMap<EventId.EventId, MutationEvent.ForSchema<TSchema>>
51
50
  params: {
52
51
  leaderPushBatchSize: number
53
52
  }
53
+ __runningInDevtools: boolean
54
54
  }
55
55
 
56
56
  export type RefreshReason =
@@ -22,6 +22,7 @@ import {
22
22
  import type { LiveStoreSchema } from '@livestore/common/schema'
23
23
  import {
24
24
  getMutationDef,
25
+ LEADER_MERGE_COUNTER_TABLE,
25
26
  MutationEvent,
26
27
  SCHEMA_META_TABLE,
27
28
  SCHEMA_MUTATIONS_META_TABLE,
@@ -29,17 +30,7 @@ import {
29
30
  } from '@livestore/common/schema'
30
31
  import { assertNever, isDevEnv } from '@livestore/utils'
31
32
  import type { Scope } from '@livestore/utils/effect'
32
- import {
33
- Cause,
34
- Data,
35
- Effect,
36
- Inspectable,
37
- MutableHashMap,
38
- OtelTracer,
39
- Runtime,
40
- Schema,
41
- Stream,
42
- } from '@livestore/utils/effect'
33
+ import { Cause, Effect, Inspectable, OtelTracer, Predicate, Runtime, Schema, Stream } from '@livestore/utils/effect'
43
34
  import { nanoid } from '@livestore/utils/nanoid'
44
35
  import * as otel from '@opentelemetry/api'
45
36
 
@@ -77,16 +68,17 @@ export class Store<TSchema extends LiveStoreSchema = LiveStoreSchema, TContext =
77
68
  */
78
69
  tableRefs: { [key: string]: Ref<null, ReactivityGraphContext, RefreshReason> }
79
70
 
80
- private runtime: Runtime.Runtime<Scope.Scope>
71
+ private effectContext: {
72
+ runtime: Runtime.Runtime<Scope.Scope>
73
+ lifetimeScope: Scope.Scope
74
+ }
81
75
 
82
76
  /** RC-based set to see which queries are currently subscribed to */
83
77
  activeQueries: ReferenceCountedSet<LiveQuery<any>>
84
78
 
85
79
  // NOTE this is currently exposed for the Devtools databrowser to emit mutation events
86
80
  readonly __mutationEventSchema
87
- private unsyncedMutationEvents
88
81
  readonly syncProcessor: ClientSessionSyncProcessor
89
- readonly lifetimeScope: Scope.Scope
90
82
 
91
83
  readonly boot: Effect.Effect<void, UnexpectedError, Scope.Scope>
92
84
 
@@ -96,27 +88,23 @@ export class Store<TSchema extends LiveStoreSchema = LiveStoreSchema, TContext =
96
88
  schema,
97
89
  otelOptions,
98
90
  context,
99
- disableDevtools,
100
91
  batchUpdates,
101
- unsyncedMutationEvents,
102
92
  storeId,
103
- lifetimeScope,
104
- runtime,
93
+ effectContext,
105
94
  params,
106
95
  confirmUnsavedChanges,
96
+ __runningInDevtools,
107
97
  }: StoreOptions<TSchema, TContext>) {
108
98
  super()
109
99
 
110
100
  this.storeId = storeId
111
- this.unsyncedMutationEvents = unsyncedMutationEvents
112
101
 
113
102
  this.sqliteDbWrapper = new SqliteDbWrapper({ otel: otelOptions, db: clientSession.sqliteDb })
114
103
  this.clientSession = clientSession
115
104
  this.schema = schema
116
105
  this.context = context
117
106
 
118
- this.lifetimeScope = lifetimeScope
119
- this.runtime = runtime
107
+ this.effectContext = effectContext
120
108
 
121
109
  const reactivityGraph = makeReactivityGraph()
122
110
 
@@ -125,7 +113,7 @@ export class Store<TSchema extends LiveStoreSchema = LiveStoreSchema, TContext =
125
113
  this.syncProcessor = makeClientSessionSyncProcessor({
126
114
  schema,
127
115
  clientSession,
128
- runtime,
116
+ runtime: effectContext.runtime,
129
117
  applyMutation: (mutationEventDecoded, { otelContext, withChangeset }) => {
130
118
  const mutationDef = getMutationDef(schema, mutationEventDecoded.mutation)
131
119
 
@@ -149,7 +137,10 @@ export class Store<TSchema extends LiveStoreSchema = LiveStoreSchema, TContext =
149
137
  }
150
138
  }
151
139
 
152
- let sessionChangeset: Uint8Array | undefined
140
+ let sessionChangeset:
141
+ | { _tag: 'sessionChangeset'; data: Uint8Array; debug: any }
142
+ | { _tag: 'no-op' }
143
+ | { _tag: 'unset' } = { _tag: 'unset' }
153
144
  if (withChangeset === true) {
154
145
  sessionChangeset = this.sqliteDbWrapper.withChangeset(exec).changeset
155
146
  } else {
@@ -205,18 +196,18 @@ export class Store<TSchema extends LiveStoreSchema = LiveStoreSchema, TContext =
205
196
  queriesSpanContext: otelQueriesSpanContext,
206
197
  }
207
198
 
208
- // TODO find a better way to detect if we're running LiveStore in the LiveStore devtools
209
- // But for now this is a good enough approximation with little downsides
210
- const isRunningInDevtools = disableDevtools === true
211
-
212
199
  // Need a set here since `schema.tables` might contain duplicates and some componentStateTables
213
200
  const allTableNames = new Set(
214
201
  // NOTE we're excluding the LiveStore schema and mutations tables as they are not user-facing
215
202
  // unless LiveStore is running in the devtools
216
- isRunningInDevtools
203
+ __runningInDevtools
217
204
  ? this.schema.tables.keys()
218
205
  : Array.from(this.schema.tables.keys()).filter(
219
- (_) => _ !== SCHEMA_META_TABLE && _ !== SCHEMA_MUTATIONS_META_TABLE && _ !== SESSION_CHANGESET_META_TABLE,
206
+ (_) =>
207
+ _ !== SCHEMA_META_TABLE &&
208
+ _ !== SCHEMA_MUTATIONS_META_TABLE &&
209
+ _ !== SESSION_CHANGESET_META_TABLE &&
210
+ _ !== LEADER_MERGE_COUNTER_TABLE,
220
211
  ),
221
212
  )
222
213
  const existingTableRefs = new Map(
@@ -671,7 +662,11 @@ export class Store<TSchema extends LiveStoreSchema = LiveStoreSchema, TContext =
671
662
  })
672
663
 
673
664
  private runEffectFork = <A, E>(effect: Effect.Effect<A, E, Scope.Scope>) =>
674
- effect.pipe(Effect.forkIn(this.lifetimeScope), Effect.tapCauseLogPretty, Runtime.runFork(this.runtime))
665
+ effect.pipe(
666
+ Effect.forkIn(this.effectContext.lifetimeScope),
667
+ Effect.tapCauseLogPretty,
668
+ Runtime.runFork(this.effectContext.runtime),
669
+ )
675
670
 
676
671
  private getMutateArgs = (
677
672
  firstMutationOrTxnFnOrOptions: any,
@@ -701,10 +696,7 @@ export class Store<TSchema extends LiveStoreSchema = LiveStoreSchema, TContext =
701
696
  mutationsEvents = [firstMutationOrTxnFnOrOptions, ...restMutations]
702
697
  }
703
698
 
704
- mutationsEvents = mutationsEvents.filter(
705
- // @ts-expect-error TODO
706
- (_) => _.id === undefined || !MutableHashMap.has(this.unsyncedMutationEvents, Data.struct(_.id)),
707
- )
699
+ mutationsEvents = mutationsEvents.filter((_) => Predicate.hasProperty(_, 'id') === false)
708
700
 
709
701
  return { mutationsEvents, options }
710
702
  }