@livestore/adapter-expo 0.3.0-dev.19 → 0.3.0-dev.22

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,22 +0,0 @@
1
- import type { ClientSession, ConnectDevtoolsToStore } from '@livestore/common';
2
- import { IntentionalShutdownCause, UnexpectedError } from '@livestore/common';
3
- import type { PullQueueItem } from '@livestore/common/leader-thread';
4
- import type { LiveStoreSchema, MutationEvent } from '@livestore/common/schema';
5
- import type { ParseResult, Scope } from '@livestore/utils/effect';
6
- import { Cause, Effect, Queue } from '@livestore/utils/effect';
7
- import type { DbPairRef } from './common.js';
8
- export type BootedDevtools = {
9
- onMutation: ({ mutationEventEncoded, }: {
10
- mutationEventEncoded: MutationEvent.AnyEncoded;
11
- }) => Effect.Effect<void, UnexpectedError, never>;
12
- };
13
- export declare const bootDevtools: ({ connectDevtoolsToStore, clientSession, schema, shutdown, dbRef, dbMutationLogRef, incomingSyncMutationsQueue, }: {
14
- connectDevtoolsToStore: ConnectDevtoolsToStore;
15
- clientSession: ClientSession;
16
- schema: LiveStoreSchema;
17
- dbRef: DbPairRef;
18
- dbMutationLogRef: DbPairRef;
19
- shutdown: (cause: Cause.Cause<UnexpectedError | IntentionalShutdownCause>) => void;
20
- incomingSyncMutationsQueue: Queue.Queue<PullQueueItem>;
21
- }) => Effect.Effect<BootedDevtools, UnexpectedError | ParseResult.ParseError, Scope.Scope>;
22
- //# sourceMappingURL=devtools.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"devtools.d.ts","sourceRoot":"","sources":["../src/devtools.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAA;AAC9E,OAAO,EAEL,wBAAwB,EAKxB,eAAe,EAChB,MAAM,mBAAmB,CAAA;AAC1B,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAA;AACpE,OAAO,KAAK,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAA;AAE9E,OAAO,KAAK,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAA;AACjE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAA+C,MAAM,yBAAyB,CAAA;AAG3G,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAG5C,MAAM,MAAM,cAAc,GAAG;IAC3B,UAAU,EAAE,CAAC,EACX,oBAAoB,GACrB,EAAE;QACD,oBAAoB,EAAE,aAAa,CAAC,UAAU,CAAA;KAC/C,KAAK,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,eAAe,EAAE,KAAK,CAAC,CAAA;CAClD,CAAA;AAED,eAAO,MAAM,YAAY,GAAI,mHAQ1B;IACD,sBAAsB,EAAE,sBAAsB,CAAA;IAC9C,aAAa,EAAE,aAAa,CAAA;IAC5B,MAAM,EAAE,eAAe,CAAA;IACvB,KAAK,EAAE,SAAS,CAAA;IAChB,gBAAgB,EAAE,SAAS,CAAA;IAC3B,QAAQ,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,eAAe,GAAG,wBAAwB,CAAC,KAAK,IAAI,CAAA;IAClF,0BAA0B,EAAE,KAAK,CAAC,KAAK,CAAC,aAAa,CAAC,CAAA;CACvD,KAAG,MAAM,CAAC,MAAM,CAAC,cAAc,EAAE,eAAe,GAAG,WAAW,CAAC,UAAU,EAAE,KAAK,CAAC,KAAK,CAsPrB,CAAA"}
package/dist/devtools.js DELETED
@@ -1,177 +0,0 @@
1
- import { Devtools, IntentionalShutdownCause, liveStoreVersion, MUTATION_LOG_META_TABLE, SCHEMA_META_TABLE, SCHEMA_MUTATIONS_META_TABLE, UnexpectedError, } from '@livestore/common';
2
- import { makeExpoDevtoolsChannel } from '@livestore/devtools-expo-common/web-channel';
3
- import { Cause, Effect, Queue, Schema, Stream, SubscriptionRef, WebChannel } from '@livestore/utils/effect';
4
- import * as SQLite from 'expo-sqlite';
5
- import { makeSqliteDb, overwriteDbFile } from './common.js';
6
- export const bootDevtools = ({ connectDevtoolsToStore, clientSession, schema, shutdown, dbRef, dbMutationLogRef, incomingSyncMutationsQueue, }) => Effect.gen(function* () {
7
- const appHostId = 'expo';
8
- const isLeader = true;
9
- const expoDevtoolsChannel = yield* makeExpoDevtoolsChannel({
10
- listenSchema: Schema.Union(Devtools.MessageToApp, Devtools.MessageToApp),
11
- sendSchema: Schema.Union(Devtools.MessageFromApp, Devtools.MessageFromApp),
12
- });
13
- const isConnected = yield* SubscriptionRef.make(false);
14
- /**
15
- * Used to forward messages from `expoDevtoolsChannel` to a "filtered" `storeDevtoolsChannel`
16
- * which is expected by the `connectDevtoolsToStore` function.
17
- */
18
- const storeDevtoolsChannelProxy = yield* WebChannel.queueChannelProxy({
19
- schema: { listen: Devtools.MessageToApp, send: Devtools.MessageFromApp },
20
- });
21
- yield* storeDevtoolsChannelProxy.sendQueue.pipe(Stream.fromQueue, Stream.tap((msg) => expoDevtoolsChannel.send(msg)), Stream.runDrain, Effect.forkScoped);
22
- const getDatabaseName = (db) => db.current.db.databasePath.slice(db.current.db.databasePath.lastIndexOf('/') + 1);
23
- yield* expoDevtoolsChannel.listen.pipe(Stream.flatten(), Stream.tap((decodedEvent) => Effect.gen(function* () {
24
- if (Schema.is(Devtools.MessageToApp)(decodedEvent)) {
25
- yield* storeDevtoolsChannelProxy.listenQueue.pipe(Queue.offer(decodedEvent));
26
- return;
27
- }
28
- // if (decodedEvent._tag === 'LSD.DevtoolsReady') {
29
- // if ((yield* isConnected.get) === false) {
30
- // // yield* expoDevtoolsChannel.send(Devtools.AppHostReady.make({ appHostId, liveStoreVersion, isLeader }))
31
- // }
32
- // return
33
- // }
34
- // if (decodedEvent._tag === 'LSD.DevtoolsConnected') {
35
- // if (yield* isConnected.get) {
36
- // console.warn('devtools already connected')
37
- // return
38
- // }
39
- // yield* connectDevtoolsToStore(storeDevtoolsChannelProxy.webChannel).pipe(
40
- // Effect.tapCauseLogPretty,
41
- // Effect.forkScoped,
42
- // )
43
- // yield* SubscriptionRef.set(isConnected, true)
44
- // return
45
- // }
46
- // if (decodedEvent._tag === 'LSD.Disconnect') {
47
- // yield* SubscriptionRef.set(isConnected, false)
48
- // // yield* disconnect
49
- // // TODO is there a better place for this?
50
- // yield* expoDevtoolsChannel.send(Devtools.AppHostReady.make({ appHostId, liveStoreVersion, isLeader }))
51
- // return
52
- // }
53
- const { requestId } = decodedEvent;
54
- const reqPayload = { requestId, appHostId, liveStoreVersion };
55
- switch (decodedEvent._tag) {
56
- case 'LSD.Ping': {
57
- yield* expoDevtoolsChannel.send(Devtools.Pong.make({ ...reqPayload }));
58
- return;
59
- }
60
- case 'LSD.Leader.SnapshotReq': {
61
- const data = yield* clientSession.leaderThread.export;
62
- yield* expoDevtoolsChannel.send(Devtools.SnapshotRes.make({ snapshot: data, ...reqPayload }));
63
- return;
64
- }
65
- case 'LSD.Leader.LoadDatabaseFileReq': {
66
- const { data } = decodedEvent;
67
- let tableNames;
68
- try {
69
- const tmpExpoDb = SQLite.deserializeDatabaseSync(data);
70
- const tmpDb = makeSqliteDb(tmpExpoDb);
71
- const tableNameResults = tmpDb.select(`select name from sqlite_master where type = 'table'`);
72
- tableNames = new Set(tableNameResults.map((_) => _.name));
73
- tmpExpoDb.closeSync();
74
- }
75
- catch (e) {
76
- yield* expoDevtoolsChannel.send(Devtools.LoadDatabaseFileRes.make({ ...reqPayload, status: 'unsupported-file' }));
77
- console.error(e);
78
- return;
79
- }
80
- if (tableNames.has(MUTATION_LOG_META_TABLE)) {
81
- // yield* SubscriptionRef.set(shutdownStateSubRef, 'shutting-down')
82
- dbMutationLogRef.current.db.closeSync();
83
- yield* overwriteDbFile(getDatabaseName(dbMutationLogRef), data);
84
- dbMutationLogRef.current = undefined;
85
- dbRef.current.db.closeSync();
86
- SQLite.deleteDatabaseSync(getDatabaseName(dbRef));
87
- }
88
- else if (tableNames.has(SCHEMA_META_TABLE) && tableNames.has(SCHEMA_MUTATIONS_META_TABLE)) {
89
- // yield* SubscriptionRef.set(shutdownStateSubRef, 'shutting-down')
90
- // yield* db.import(data)
91
- dbRef.current.db.closeSync();
92
- yield* overwriteDbFile(getDatabaseName(dbRef), data);
93
- }
94
- else {
95
- yield* expoDevtoolsChannel.send(Devtools.LoadDatabaseFileRes.make({ ...reqPayload, status: 'unsupported-database' }));
96
- return;
97
- }
98
- yield* expoDevtoolsChannel.send(Devtools.LoadDatabaseFileRes.make({ ...reqPayload, status: 'ok' }));
99
- yield* shutdown(Cause.fail(IntentionalShutdownCause.make({ reason: 'devtools-import' })));
100
- return;
101
- }
102
- case 'LSD.Leader.ResetAllDataReq': {
103
- const { mode } = decodedEvent;
104
- dbRef.current.db.closeSync();
105
- SQLite.deleteDatabaseSync(getDatabaseName(dbRef));
106
- if (mode === 'all-data') {
107
- dbMutationLogRef.current.db.closeSync();
108
- SQLite.deleteDatabaseSync(getDatabaseName(dbMutationLogRef));
109
- }
110
- yield* expoDevtoolsChannel.send(Devtools.ResetAllDataRes.make({ ...reqPayload }));
111
- yield* shutdown(Cause.fail(IntentionalShutdownCause.make({ reason: 'devtools-reset' })));
112
- return;
113
- }
114
- case 'LSD.Leader.DatabaseFileInfoReq': {
115
- const dbSizeQuery = `SELECT page_count * page_size as size FROM pragma_page_count(), pragma_page_size();`;
116
- const dbFileSize = dbRef.current.db.prepareSync(dbSizeQuery).executeSync().getFirstSync()
117
- .size;
118
- const mutationLogFileSize = dbMutationLogRef
119
- .current.db.prepareSync(dbSizeQuery)
120
- .executeSync()
121
- .getFirstSync().size;
122
- yield* expoDevtoolsChannel.send(Devtools.DatabaseFileInfoRes.make({
123
- readModel: { fileSize: dbFileSize, persistenceInfo: { fileName: 'livestore.db' } },
124
- mutationLog: {
125
- fileSize: mutationLogFileSize,
126
- persistenceInfo: { fileName: 'livestore-mutationlog.db' },
127
- },
128
- ...reqPayload,
129
- }));
130
- return;
131
- }
132
- case 'LSD.Leader.MutationLogReq': {
133
- const mutationLog = yield* clientSession.leaderThread.getMutationLogData;
134
- yield* expoDevtoolsChannel.send(Devtools.MutationLogRes.make({ mutationLog, ...reqPayload }));
135
- return;
136
- }
137
- case 'LSD.Leader.RunMutationReq': {
138
- const { mutationEventEncoded: mutationEventEncoded_ } = decodedEvent;
139
- const mutationDef = schema.mutations.get(mutationEventEncoded_.mutation);
140
- // const nextMutationEventIdPair = clientSession.mutations.nextMutationEventIdPair({
141
- // clientOnly: mutationDef.options.clientOnly,
142
- // })
143
- // const mutationEventEncoded = new MutationEvent.EncodedWithMeta({
144
- // ...mutationEventEncoded_,
145
- // // ...nextMutationEventIdPair,
146
- // })
147
- // const mutationEventDecoded = yield* Schema.decode(mutationEventSchema)(mutationEventEncoded)
148
- // yield* Queue.offer(incomingSyncMutationsQueue, {
149
- // payload: { _tag: 'upstream-advance', newEvents: [mutationEventEncoded] },
150
- // remaining: 0,
151
- // })
152
- // const mutationDef =
153
- // schema.mutations.get(mutationEventEncoded.mutation) ??
154
- // shouldNeverHappen(`Unknown mutation: ${mutationEventEncoded.mutation}`)
155
- // yield* clientSession.mutations.push([mutationEventEncoded])
156
- yield* expoDevtoolsChannel.send(Devtools.RunMutationRes.make({ ...reqPayload }));
157
- return;
158
- }
159
- case 'LSD.Leader.SyncingInfoReq': {
160
- const syncingInfo = Devtools.SyncingInfo.make({
161
- enabled: false,
162
- metadata: {},
163
- });
164
- yield* expoDevtoolsChannel.send(Devtools.SyncingInfoRes.make({ syncingInfo, ...reqPayload }));
165
- return;
166
- }
167
- }
168
- })), Stream.runDrain, Effect.tapCauseLogPretty, Effect.forkScoped);
169
- // yield* expoDevtoolsChannel.send(Devtools.AppHostReady.make({ appHostId, isLeader, liveStoreVersion }))
170
- const onMutation = ({ mutationEventEncoded }) => expoDevtoolsChannel
171
- .send(Devtools.MutationBroadcast.make({ mutationEventEncoded, liveStoreVersion }))
172
- .pipe(UnexpectedError.mapToUnexpectedError);
173
- return {
174
- onMutation,
175
- };
176
- }).pipe(Effect.withSpan('@livestore/adapter-expo:bootDevtools'));
177
- //# sourceMappingURL=devtools.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"devtools.js","sourceRoot":"","sources":["../src/devtools.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,QAAQ,EACR,wBAAwB,EACxB,gBAAgB,EAChB,uBAAuB,EACvB,iBAAiB,EACjB,2BAA2B,EAC3B,eAAe,GAChB,MAAM,mBAAmB,CAAA;AAG1B,OAAO,EAAE,uBAAuB,EAAE,MAAM,6CAA6C,CAAA;AAErF,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAA;AAC3G,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AAGrC,OAAO,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAU3D,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,EAC3B,sBAAsB,EACtB,aAAa,EACb,MAAM,EACN,QAAQ,EACR,KAAK,EACL,gBAAgB,EAChB,0BAA0B,GAS3B,EAAwF,EAAE,CACzF,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC;IAClB,MAAM,SAAS,GAAG,MAAM,CAAA;IACxB,MAAM,QAAQ,GAAG,IAAI,CAAA;IAErB,MAAM,mBAAmB,GAAG,KAAK,CAAC,CAAC,uBAAuB,CAAC;QACzD,YAAY,EAAE,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,YAAY,EAAE,QAAQ,CAAC,YAAY,CAAC;QACxE,UAAU,EAAE,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,cAAc,EAAE,QAAQ,CAAC,cAAc,CAAC;KAC3E,CAAC,CAAA;IAEF,MAAM,WAAW,GAAG,KAAK,CAAC,CAAC,eAAe,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;IAEtD;;;OAGG;IACH,MAAM,yBAAyB,GAAG,KAAK,CA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9E,kBAAkB;gBAClB,KAAK;gBAEL,sBAAsB;gBACtB,2DAA2D;gBAC3D,4EAA4E;gBAE5E,8DAA8D;gBAE9D,KAAK,CAAC,CAAC,mBAAmB,CAAC,IAAI,CAAC,QAAQ,CAAC,cAAc,CAAC,IAAI,CAAC,EAAE,GAAG,UAAU,EAAE,CAAC,CAAC,CAAA;gBAEhF,OAAM;YACR,CAAC;YACD,KAAK,2BAA2B,CAAC,CAAC,CAAC;gBACjC,MAAM,WAAW,GAAG,QAAQ,CAAC,WAAW,CAAC,IAAI,CAAC;oBAC5C,OAAO,EAAE,KAAK;oBACd,QAAQ,EAAE,EAAE;iBACb,CAAC,CAAA;gBAEF,KAAK,CAAC,CAAC,mBAAmB,CAAC,IAAI,CAAC,QAAQ,CAAC,cAAc,CAAC,IAAI,CAAC,EAAE,WAAW,EAAE,GAAG,UAAU,EAAE,CAAC,CAAC,CAAA;gBAE7F,OAAM;YACR,CAAC;QACH,CAAC;IACH,CAAC,CAAC,CACH,EACD,MAAM,CAAC,QAAQ,EACf,MAAM,CAAC,iBAAiB,EACxB,MAAM,CAAC,UAAU,CAClB,CAAA;IACD,yGAAyG;IAEzG,MAAM,UAAU,GAAG,CAAC,EAAE,oBAAoB,EAAsD,EAAE,EAAE,CAClG,mBAAmB;SAChB,IAAI,CAAC,QAAQ,CAAC,iBAAiB,CAAC,IAAI,CAAC,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,CAAC,CAAC;SACjF,IAAI,CAAC,eAAe,CAAC,oBAAoB,CAAC,CAAA;IAE/C,OAAO;QACL,UAAU;KACX,CAAA;AACH,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,sCAAsC,CAAC,CAAC,CAAA"}
package/src/common.ts DELETED
@@ -1,29 +0,0 @@
1
- import type { SqliteDb } from '@livestore/common'
2
- import { base64 } from '@livestore/utils'
3
- import { Effect } from '@livestore/utils/effect'
4
- import * as ExpoFS from 'expo-file-system'
5
- import type * as SQLite from 'expo-sqlite'
6
-
7
- export type DbPairRef = {
8
- current:
9
- | {
10
- db: SQLite.SQLiteDatabase
11
- sqliteDb: SqliteDb
12
- }
13
- | undefined
14
- }
15
-
16
- export const getDbFilePath = (dbName: string) => {
17
- return `${ExpoFS.documentDirectory}SQLite/${dbName}`
18
- }
19
-
20
- export const overwriteDbFile = (dbName: string, data: Uint8Array) =>
21
- Effect.gen(function* () {
22
- const path = getDbFilePath(dbName)
23
-
24
- yield* Effect.promise(() => ExpoFS.deleteAsync(path, { idempotent: true }))
25
-
26
- // TODO avoid converting to string once the ExpoFS API supports binary data
27
- const b64String = base64.encode(data)
28
- yield* Effect.promise(() => ExpoFS.writeAsStringAsync(path, b64String, { encoding: ExpoFS.EncodingType.Base64 }))
29
- })
package/src/devtools.ts DELETED
@@ -1,292 +0,0 @@
1
- // @ts-nocheck
2
- import type { ClientSession, ConnectDevtoolsToStore } from '@livestore/common'
3
- import {
4
- Devtools,
5
- IntentionalShutdownCause,
6
- liveStoreVersion,
7
- MUTATION_LOG_META_TABLE,
8
- SCHEMA_META_TABLE,
9
- SCHEMA_MUTATIONS_META_TABLE,
10
- UnexpectedError,
11
- } from '@livestore/common'
12
- import type { PullQueueItem } from '@livestore/common/leader-thread'
13
- import type { LiveStoreSchema, MutationEvent } from '@livestore/common/schema'
14
- import { makeExpoDevtoolsChannel } from '@livestore/devtools-expo-common/web-channel'
15
- import type { ParseResult, Scope } from '@livestore/utils/effect'
16
- import { Cause, Effect, Queue, Schema, Stream, SubscriptionRef, WebChannel } from '@livestore/utils/effect'
17
- import * as SQLite from 'expo-sqlite'
18
-
19
- import type { DbPairRef } from './common.js'
20
- import { makeSqliteDb, overwriteDbFile } from './common.js'
21
-
22
- export type BootedDevtools = {
23
- onMutation: ({
24
- mutationEventEncoded,
25
- }: {
26
- mutationEventEncoded: MutationEvent.AnyEncoded
27
- }) => Effect.Effect<void, UnexpectedError, never>
28
- }
29
-
30
- export const bootDevtools = ({
31
- connectDevtoolsToStore,
32
- clientSession,
33
- schema,
34
- shutdown,
35
- dbRef,
36
- dbMutationLogRef,
37
- incomingSyncMutationsQueue,
38
- }: {
39
- connectDevtoolsToStore: ConnectDevtoolsToStore
40
- clientSession: ClientSession
41
- schema: LiveStoreSchema
42
- dbRef: DbPairRef
43
- dbMutationLogRef: DbPairRef
44
- shutdown: (cause: Cause.Cause<UnexpectedError | IntentionalShutdownCause>) => void
45
- incomingSyncMutationsQueue: Queue.Queue<PullQueueItem>
46
- }): Effect.Effect<BootedDevtools, UnexpectedError | ParseResult.ParseError, Scope.Scope> =>
47
- Effect.gen(function* () {
48
- const appHostId = 'expo'
49
- const isLeader = true
50
-
51
- const expoDevtoolsChannel = yield* makeExpoDevtoolsChannel({
52
- listenSchema: Schema.Union(Devtools.MessageToApp, Devtools.MessageToApp),
53
- sendSchema: Schema.Union(Devtools.MessageFromApp, Devtools.MessageFromApp),
54
- })
55
-
56
- const isConnected = yield* SubscriptionRef.make(false)
57
-
58
- /**
59
- * Used to forward messages from `expoDevtoolsChannel` to a "filtered" `storeDevtoolsChannel`
60
- * which is expected by the `connectDevtoolsToStore` function.
61
- */
62
- const storeDevtoolsChannelProxy = yield* WebChannel.queueChannelProxy({
63
- schema: { listen: Devtools.MessageToApp, send: Devtools.MessageFromApp },
64
- })
65
-
66
- yield* storeDevtoolsChannelProxy.sendQueue.pipe(
67
- Stream.fromQueue,
68
- Stream.tap((msg) => expoDevtoolsChannel.send(msg)),
69
- Stream.runDrain,
70
- Effect.forkScoped,
71
- )
72
-
73
- const getDatabaseName = (db: DbPairRef) =>
74
- db.current!.db.databasePath.slice(db.current!.db.databasePath.lastIndexOf('/') + 1)
75
-
76
- yield* expoDevtoolsChannel.listen.pipe(
77
- Stream.flatten(),
78
- Stream.tap((decodedEvent) =>
79
- Effect.gen(function* () {
80
- if (Schema.is(Devtools.MessageToApp)(decodedEvent)) {
81
- yield* storeDevtoolsChannelProxy.listenQueue.pipe(Queue.offer(decodedEvent))
82
- return
83
- }
84
-
85
- // if (decodedEvent._tag === 'LSD.DevtoolsReady') {
86
- // if ((yield* isConnected.get) === false) {
87
- // // yield* expoDevtoolsChannel.send(Devtools.AppHostReady.make({ appHostId, liveStoreVersion, isLeader }))
88
- // }
89
-
90
- // return
91
- // }
92
-
93
- // if (decodedEvent._tag === 'LSD.DevtoolsConnected') {
94
- // if (yield* isConnected.get) {
95
- // console.warn('devtools already connected')
96
- // return
97
- // }
98
-
99
- // yield* connectDevtoolsToStore(storeDevtoolsChannelProxy.webChannel).pipe(
100
- // Effect.tapCauseLogPretty,
101
- // Effect.forkScoped,
102
- // )
103
-
104
- // yield* SubscriptionRef.set(isConnected, true)
105
- // return
106
- // }
107
-
108
- // if (decodedEvent._tag === 'LSD.Disconnect') {
109
- // yield* SubscriptionRef.set(isConnected, false)
110
-
111
- // // yield* disconnect
112
-
113
- // // TODO is there a better place for this?
114
- // yield* expoDevtoolsChannel.send(Devtools.AppHostReady.make({ appHostId, liveStoreVersion, isLeader }))
115
-
116
- // return
117
- // }
118
-
119
- const { requestId } = decodedEvent
120
- const reqPayload = { requestId, appHostId, liveStoreVersion }
121
-
122
- switch (decodedEvent._tag) {
123
- case 'LSD.Ping': {
124
- yield* expoDevtoolsChannel.send(Devtools.Pong.make({ ...reqPayload }))
125
- return
126
- }
127
- case 'LSD.Leader.SnapshotReq': {
128
- const data = yield* clientSession.leaderThread.export
129
-
130
- yield* expoDevtoolsChannel.send(Devtools.SnapshotRes.make({ snapshot: data!, ...reqPayload }))
131
-
132
- return
133
- }
134
- case 'LSD.Leader.LoadDatabaseFileReq': {
135
- const { data } = decodedEvent
136
-
137
- let tableNames: Set<string>
138
-
139
- try {
140
- const tmpExpoDb = SQLite.deserializeDatabaseSync(data)
141
- const tmpDb = makeSqliteDb(tmpExpoDb)
142
- const tableNameResults = tmpDb.select<{ name: string }>(
143
- `select name from sqlite_master where type = 'table'`,
144
- )
145
-
146
- tableNames = new Set(tableNameResults.map((_) => _.name))
147
-
148
- tmpExpoDb.closeSync()
149
- } catch (e) {
150
- yield* expoDevtoolsChannel.send(
151
- Devtools.LoadDatabaseFileRes.make({ ...reqPayload, status: 'unsupported-file' }),
152
- )
153
-
154
- console.error(e)
155
-
156
- return
157
- }
158
-
159
- if (tableNames.has(MUTATION_LOG_META_TABLE)) {
160
- // yield* SubscriptionRef.set(shutdownStateSubRef, 'shutting-down')
161
-
162
- dbMutationLogRef.current!.db.closeSync()
163
-
164
- yield* overwriteDbFile(getDatabaseName(dbMutationLogRef), data)
165
-
166
- dbMutationLogRef.current = undefined
167
-
168
- dbRef.current!.db.closeSync()
169
- SQLite.deleteDatabaseSync(getDatabaseName(dbRef))
170
- } else if (tableNames.has(SCHEMA_META_TABLE) && tableNames.has(SCHEMA_MUTATIONS_META_TABLE)) {
171
- // yield* SubscriptionRef.set(shutdownStateSubRef, 'shutting-down')
172
-
173
- // yield* db.import(data)
174
-
175
- dbRef.current!.db.closeSync()
176
-
177
- yield* overwriteDbFile(getDatabaseName(dbRef), data)
178
- } else {
179
- yield* expoDevtoolsChannel.send(
180
- Devtools.LoadDatabaseFileRes.make({ ...reqPayload, status: 'unsupported-database' }),
181
- )
182
- return
183
- }
184
-
185
- yield* expoDevtoolsChannel.send(Devtools.LoadDatabaseFileRes.make({ ...reqPayload, status: 'ok' }))
186
-
187
- yield* shutdown(Cause.fail(IntentionalShutdownCause.make({ reason: 'devtools-import' })))
188
-
189
- return
190
- }
191
- case 'LSD.Leader.ResetAllDataReq': {
192
- const { mode } = decodedEvent
193
-
194
- dbRef.current!.db.closeSync()
195
- SQLite.deleteDatabaseSync(getDatabaseName(dbRef))
196
-
197
- if (mode === 'all-data') {
198
- dbMutationLogRef.current!.db.closeSync()
199
- SQLite.deleteDatabaseSync(getDatabaseName(dbMutationLogRef))
200
- }
201
-
202
- yield* expoDevtoolsChannel.send(Devtools.ResetAllDataRes.make({ ...reqPayload }))
203
-
204
- yield* shutdown(Cause.fail(IntentionalShutdownCause.make({ reason: 'devtools-reset' })))
205
-
206
- return
207
- }
208
- case 'LSD.Leader.DatabaseFileInfoReq': {
209
- const dbSizeQuery = `SELECT page_count * page_size as size FROM pragma_page_count(), pragma_page_size();`
210
- const dbFileSize = dbRef.current!.db.prepareSync(dbSizeQuery).executeSync<any>().getFirstSync()!
211
- .size as number
212
- const mutationLogFileSize = dbMutationLogRef
213
- .current!.db.prepareSync(dbSizeQuery)
214
- .executeSync<any>()
215
- .getFirstSync()!.size as number
216
-
217
- yield* expoDevtoolsChannel.send(
218
- Devtools.DatabaseFileInfoRes.make({
219
- readModel: { fileSize: dbFileSize, persistenceInfo: { fileName: 'livestore.db' } },
220
- mutationLog: {
221
- fileSize: mutationLogFileSize,
222
- persistenceInfo: { fileName: 'livestore-mutationlog.db' },
223
- },
224
- ...reqPayload,
225
- }),
226
- )
227
-
228
- return
229
- }
230
- case 'LSD.Leader.MutationLogReq': {
231
- const mutationLog = yield* clientSession.leaderThread.getMutationLogData
232
-
233
- yield* expoDevtoolsChannel.send(Devtools.MutationLogRes.make({ mutationLog, ...reqPayload }))
234
-
235
- return
236
- }
237
- case 'LSD.Leader.RunMutationReq': {
238
- const { mutationEventEncoded: mutationEventEncoded_ } = decodedEvent
239
- const mutationDef = schema.mutations.get(mutationEventEncoded_.mutation)!
240
- // const nextMutationEventIdPair = clientSession.mutations.nextMutationEventIdPair({
241
- // clientOnly: mutationDef.options.clientOnly,
242
- // })
243
-
244
- // const mutationEventEncoded = new MutationEvent.EncodedWithMeta({
245
- // ...mutationEventEncoded_,
246
- // // ...nextMutationEventIdPair,
247
- // })
248
-
249
- // const mutationEventDecoded = yield* Schema.decode(mutationEventSchema)(mutationEventEncoded)
250
- // yield* Queue.offer(incomingSyncMutationsQueue, {
251
- // payload: { _tag: 'upstream-advance', newEvents: [mutationEventEncoded] },
252
- // remaining: 0,
253
- // })
254
-
255
- // const mutationDef =
256
- // schema.mutations.get(mutationEventEncoded.mutation) ??
257
- // shouldNeverHappen(`Unknown mutation: ${mutationEventEncoded.mutation}`)
258
-
259
- // yield* clientSession.mutations.push([mutationEventEncoded])
260
-
261
- yield* expoDevtoolsChannel.send(Devtools.RunMutationRes.make({ ...reqPayload }))
262
-
263
- return
264
- }
265
- case 'LSD.Leader.SyncingInfoReq': {
266
- const syncingInfo = Devtools.SyncingInfo.make({
267
- enabled: false,
268
- metadata: {},
269
- })
270
-
271
- yield* expoDevtoolsChannel.send(Devtools.SyncingInfoRes.make({ syncingInfo, ...reqPayload }))
272
-
273
- return
274
- }
275
- }
276
- }),
277
- ),
278
- Stream.runDrain,
279
- Effect.tapCauseLogPretty,
280
- Effect.forkScoped,
281
- )
282
- // yield* expoDevtoolsChannel.send(Devtools.AppHostReady.make({ appHostId, isLeader, liveStoreVersion }))
283
-
284
- const onMutation = ({ mutationEventEncoded }: { mutationEventEncoded: MutationEvent.AnyEncoded }) =>
285
- expoDevtoolsChannel
286
- .send(Devtools.MutationBroadcast.make({ mutationEventEncoded, liveStoreVersion }))
287
- .pipe(UnexpectedError.mapToUnexpectedError)
288
-
289
- return {
290
- onMutation,
291
- }
292
- }).pipe(Effect.withSpan('@livestore/adapter-expo:bootDevtools'))