@livestore/adapter-expo 0.3.0-dev.17 → 0.3.0-dev.19
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.
- package/dist/.tsbuildinfo +1 -1
- package/dist/common.d.ts +0 -1
- package/dist/common.d.ts.map +1 -1
- package/dist/common.js +1 -87
- package/dist/common.js.map +1 -1
- package/dist/index.d.ts +15 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +125 -151
- package/dist/index.js.map +1 -1
- package/dist/make-sqlite-db.d.ts +20 -0
- package/dist/make-sqlite-db.d.ts.map +1 -0
- package/dist/make-sqlite-db.js +138 -0
- package/dist/make-sqlite-db.js.map +1 -0
- package/dist/shutdown-channel.d.ts +3 -0
- package/dist/shutdown-channel.d.ts.map +1 -0
- package/dist/shutdown-channel.js +8 -0
- package/dist/shutdown-channel.js.map +1 -0
- package/package.json +11 -6
- package/src/common.ts +2 -88
- package/src/index.ts +223 -219
- package/src/make-sqlite-db.ts +167 -0
- package/src/shutdown-channel.ts +10 -0
- package/tmp/pack.tgz +0 -0
- package/tsconfig.json +6 -1
package/dist/common.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { SqliteDb } from '@livestore/common';
|
|
2
2
|
import { Effect } from '@livestore/utils/effect';
|
|
3
3
|
import type * as SQLite from 'expo-sqlite';
|
|
4
|
-
export declare const makeSqliteDb: (db: SQLite.SQLiteDatabase) => SqliteDb;
|
|
5
4
|
export type DbPairRef = {
|
|
6
5
|
current: {
|
|
7
6
|
db: SQLite.SQLiteDatabase;
|
package/dist/common.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../src/common.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../src/common.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAEjD,OAAO,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAA;AAEhD,OAAO,KAAK,KAAK,MAAM,MAAM,aAAa,CAAA;AAE1C,MAAM,MAAM,SAAS,GAAG;IACtB,OAAO,EACH;QACE,EAAE,EAAE,MAAM,CAAC,cAAc,CAAA;QACzB,QAAQ,EAAE,QAAQ,CAAA;KACnB,GACD,SAAS,CAAA;CACd,CAAA;AAED,eAAO,MAAM,aAAa,GAAI,QAAQ,MAAM,WAE3C,CAAA;AAED,eAAO,MAAM,eAAe,GAAI,QAAQ,MAAM,EAAE,MAAM,UAAU,sCAS5D,CAAA"}
|
package/dist/common.js
CHANGED
|
@@ -1,92 +1,6 @@
|
|
|
1
|
-
import { base64
|
|
1
|
+
import { base64 } from '@livestore/utils';
|
|
2
2
|
import { Effect } from '@livestore/utils/effect';
|
|
3
3
|
import * as ExpoFS from 'expo-file-system';
|
|
4
|
-
export const makeSqliteDb = (db) => {
|
|
5
|
-
const stmts = [];
|
|
6
|
-
const sqliteDb = {
|
|
7
|
-
metadata: { fileName: db.databasePath },
|
|
8
|
-
_tag: 'SqliteDb',
|
|
9
|
-
prepare: (queryStr) => {
|
|
10
|
-
try {
|
|
11
|
-
const dbStmt = db.prepareSync(queryStr);
|
|
12
|
-
const stmt = {
|
|
13
|
-
execute: (bindValues) => {
|
|
14
|
-
// console.log('execute', queryStr, bindValues)
|
|
15
|
-
const res = dbStmt.executeSync(bindValues ?? []);
|
|
16
|
-
res.resetSync();
|
|
17
|
-
return () => res.changes;
|
|
18
|
-
},
|
|
19
|
-
select: (bindValues) => {
|
|
20
|
-
const res = dbStmt.executeSync(bindValues ?? []);
|
|
21
|
-
try {
|
|
22
|
-
return res.getAllSync();
|
|
23
|
-
}
|
|
24
|
-
finally {
|
|
25
|
-
res.resetSync();
|
|
26
|
-
}
|
|
27
|
-
},
|
|
28
|
-
finalize: () => dbStmt.finalizeSync(),
|
|
29
|
-
sql: queryStr,
|
|
30
|
-
};
|
|
31
|
-
stmts.push(stmt);
|
|
32
|
-
return stmt;
|
|
33
|
-
}
|
|
34
|
-
catch (e) {
|
|
35
|
-
console.error(`Error preparing statement: ${queryStr}`, e);
|
|
36
|
-
return shouldNeverHappen(`Error preparing statement: ${queryStr}`);
|
|
37
|
-
}
|
|
38
|
-
},
|
|
39
|
-
execute: (queryStr, bindValues) => {
|
|
40
|
-
const stmt = db.prepareSync(queryStr);
|
|
41
|
-
try {
|
|
42
|
-
const res = stmt.executeSync(bindValues ?? []);
|
|
43
|
-
return () => res.changes;
|
|
44
|
-
}
|
|
45
|
-
finally {
|
|
46
|
-
stmt.finalizeSync();
|
|
47
|
-
}
|
|
48
|
-
},
|
|
49
|
-
export: () => {
|
|
50
|
-
return db.serializeSync();
|
|
51
|
-
},
|
|
52
|
-
select: (queryStr, bindValues) => {
|
|
53
|
-
const stmt = sqliteDb.prepare(queryStr);
|
|
54
|
-
const res = stmt.select(bindValues);
|
|
55
|
-
stmt.finalize();
|
|
56
|
-
return res;
|
|
57
|
-
},
|
|
58
|
-
// TODO
|
|
59
|
-
destroy: () => { },
|
|
60
|
-
close: () => {
|
|
61
|
-
for (const stmt of stmts) {
|
|
62
|
-
stmt.finalize();
|
|
63
|
-
}
|
|
64
|
-
return db.closeSync();
|
|
65
|
-
},
|
|
66
|
-
import: () => {
|
|
67
|
-
throw new Error('Not implemented');
|
|
68
|
-
// TODO properly implement this as it seems to require importing to a temporary in-memory db,
|
|
69
|
-
// save it to a file, and then reopen the DB from that file? (see `overwriteDbFile` below)
|
|
70
|
-
},
|
|
71
|
-
session: () => {
|
|
72
|
-
return {
|
|
73
|
-
changeset: () => new Uint8Array(),
|
|
74
|
-
finish: () => { },
|
|
75
|
-
};
|
|
76
|
-
},
|
|
77
|
-
makeChangeset: (data) => {
|
|
78
|
-
return {
|
|
79
|
-
invert: () => {
|
|
80
|
-
return sqliteDb.makeChangeset(data);
|
|
81
|
-
},
|
|
82
|
-
apply: () => {
|
|
83
|
-
// TODO
|
|
84
|
-
},
|
|
85
|
-
};
|
|
86
|
-
},
|
|
87
|
-
};
|
|
88
|
-
return sqliteDb;
|
|
89
|
-
};
|
|
90
4
|
export const getDbFilePath = (dbName) => {
|
|
91
5
|
return `${ExpoFS.documentDirectory}SQLite/${dbName}`;
|
|
92
6
|
};
|
package/dist/common.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"common.js","sourceRoot":"","sources":["../src/common.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,
|
|
1
|
+
{"version":3,"file":"common.js","sourceRoot":"","sources":["../src/common.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAA;AACzC,OAAO,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAA;AAChD,OAAO,KAAK,MAAM,MAAM,kBAAkB,CAAA;AAY1C,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,MAAc,EAAE,EAAE;IAC9C,OAAO,GAAG,MAAM,CAAC,iBAAiB,UAAU,MAAM,EAAE,CAAA;AACtD,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,MAAc,EAAE,IAAgB,EAAE,EAAE,CAClE,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC;IAClB,MAAM,IAAI,GAAG,aAAa,CAAC,MAAM,CAAC,CAAA;IAElC,KAAK,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,WAAW,CAAC,IAAI,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,CAAC,CAAA;IAE3E,2EAA2E;IAC3E,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;IACrC,KAAK,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,kBAAkB,CAAC,IAAI,EAAE,SAAS,EAAE,EAAE,QAAQ,EAAE,MAAM,CAAC,YAAY,CAAC,MAAM,EAAE,CAAC,CAAC,CAAA;AACnH,CAAC,CAAC,CAAA"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,19 @@
|
|
|
1
|
-
import type { Adapter } from '@livestore/common';
|
|
1
|
+
import type { Adapter, SyncOptions } from '@livestore/common';
|
|
2
2
|
export type MakeDbOptions = {
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
sync?: SyncOptions;
|
|
4
|
+
storage?: {
|
|
5
|
+
/**
|
|
6
|
+
* Relative to expo-sqlite's default directory
|
|
7
|
+
*
|
|
8
|
+
* Example of a resulting path for `subDirectory: 'my-app'`:
|
|
9
|
+
* `/data/Containers/Data/Application/<APP_UUID>/Documents/ExponentExperienceData/@<USERNAME>/<APPNAME>/SQLite/my-app/<STORE_ID>/livestore-mutationlog@3.db`
|
|
10
|
+
*/
|
|
11
|
+
subDirectory?: string;
|
|
12
|
+
};
|
|
13
|
+
/** @default 'expo' */
|
|
14
|
+
clientId?: string;
|
|
15
|
+
/** @default 'expo' */
|
|
16
|
+
sessionId?: string;
|
|
5
17
|
};
|
|
6
18
|
export declare const makeAdapter: (options?: MakeDbOptions) => Adapter;
|
|
7
19
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAA6D,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAmBxH,MAAM,MAAM,aAAa,GAAG;IAC1B,IAAI,CAAC,EAAE,WAAW,CAAA;IAClB,OAAO,CAAC,EAAE;QACR;;;;;WAKG;QACH,YAAY,CAAC,EAAE,MAAM,CAAA;KACtB,CAAA;IAED,sBAAsB;IACtB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,sBAAsB;IACtB,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB,CAAA;AAGD,eAAO,MAAM,WAAW,GACrB,UAAS,aAAkB,KAAG,OAkE5B,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,163 +1,137 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
1
|
+
import { Devtools, liveStoreStorageFormatVersion, UnexpectedError } from '@livestore/common';
|
|
2
|
+
import { getClientHeadFromDb, LeaderThreadCtx, makeLeaderThreadLayer } from '@livestore/common/leader-thread';
|
|
3
|
+
import { MutationEvent } from '@livestore/common/schema';
|
|
4
|
+
import { makeChannelForConnectedMeshNode, makeExpoDevtoolsConnectedMeshNode, } from '@livestore/devtools-expo-common/web-channel';
|
|
5
|
+
import { Cause, Effect, FetchHttpClient, Layer, Stream, SubscriptionRef } from '@livestore/utils/effect';
|
|
6
6
|
import * as SQLite from 'expo-sqlite';
|
|
7
|
-
import { makeSqliteDb } from './
|
|
8
|
-
import {
|
|
7
|
+
import { makeSqliteDb } from './make-sqlite-db.js';
|
|
8
|
+
import { makeShutdownChannel } from './shutdown-channel.js';
|
|
9
9
|
// TODO refactor with leader-thread code from `@livestore/common/leader-thread`
|
|
10
|
-
export const makeAdapter = (options) => ({ schema, connectDevtoolsToStore, shutdown, devtoolsEnabled }) => Effect.gen(function* () {
|
|
11
|
-
const {
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
if (dbWasEmptyWhenOpened) {
|
|
35
|
-
yield* migrateDb({ db: dbRef.current.sqliteDb, schema });
|
|
36
|
-
initializeSingletonTables(schema, dbRef.current.sqliteDb);
|
|
37
|
-
switch (migrationOptions.strategy) {
|
|
38
|
-
case 'from-mutation-log': {
|
|
39
|
-
// TODO bring back
|
|
40
|
-
// yield* rehydrateFromMutationLog({
|
|
41
|
-
// db: dbRef.current.sqliteDb,
|
|
42
|
-
// logDb: dbMutationLogRef.current.sqliteDb,
|
|
43
|
-
// schema,
|
|
44
|
-
// migrationOptions,
|
|
45
|
-
// onProgress: () => Effect.void,
|
|
46
|
-
// })
|
|
47
|
-
break;
|
|
48
|
-
}
|
|
49
|
-
case 'hard-reset': {
|
|
50
|
-
// This is already the case by note doing anything now
|
|
51
|
-
break;
|
|
52
|
-
}
|
|
53
|
-
case 'manual': {
|
|
54
|
-
// const migrateFn = migrationStrategy.migrate
|
|
55
|
-
console.warn('Manual migration strategy not implemented yet');
|
|
56
|
-
// TODO figure out a way to get previous database file to pass to the migration function
|
|
57
|
-
break;
|
|
58
|
-
}
|
|
59
|
-
default: {
|
|
60
|
-
casesHandled(migrationOptions);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
const mutationLogExclude = migrationOptions.strategy === 'from-mutation-log'
|
|
65
|
-
? (migrationOptions.excludeMutations ?? new Set(['livestore.RawSql']))
|
|
66
|
-
: new Set(['livestore.RawSql']);
|
|
67
|
-
const mutationEventSchema = MutationEvent.makeMutationEventSchema(schema);
|
|
68
|
-
const mutationDefSchemaHashMap = new Map([...schema.mutations.entries()].map(([k, v]) => [k, Schema.hash(v.schema)]));
|
|
69
|
-
const newMutationLogStmt = dbMutationLogRef.current.sqliteDb.prepare(insertRowPrepared({ tableName: MUTATION_LOG_META_TABLE, columns: mutationLogMetaTable.sqliteDef.columns }));
|
|
70
|
-
const lockStatus = SubscriptionRef.make('has-lock').pipe(Effect.runSync);
|
|
71
|
-
const incomingSyncMutationsQueue = yield* Queue.unbounded().pipe(Effect.acquireRelease(Queue.shutdown));
|
|
72
|
-
const initialMutationEventIdSchema = mutationLogMetaTable.schema.pipe(Schema.pick('idGlobal', 'idClient'), Schema.transform(EventId.EventId, {
|
|
73
|
-
encode: (_) => ({ idGlobal: _.global, idClient: _.client }),
|
|
74
|
-
decode: (_) => EventId.make({ global: _.idGlobal, client: _.idClient }),
|
|
75
|
-
strict: false,
|
|
76
|
-
}), Schema.Array, Schema.headOrElse(() => EventId.make({ global: 0, client: 0 })));
|
|
77
|
-
const initialMutationEventId = yield* Schema.decode(initialMutationEventIdSchema)(dbMutationLogRef.current.sqliteDb.select(sql `SELECT idGlobal, idClient FROM ${MUTATION_LOG_META_TABLE} ORDER BY idGlobal DESC, idClient DESC LIMIT 1`));
|
|
78
|
-
let devtools;
|
|
10
|
+
export const makeAdapter = (options = {}) => ({ schema, connectDevtoolsToStore, shutdown, devtoolsEnabled, storeId, bootStatusQueue, debugInstanceId }) => Effect.gen(function* () {
|
|
11
|
+
const { storage, clientId = 'expo', sessionId = 'expo', sync: syncOptions } = options;
|
|
12
|
+
const lockStatus = yield* SubscriptionRef.make('has-lock');
|
|
13
|
+
const shutdownChannel = yield* makeShutdownChannel(storeId);
|
|
14
|
+
yield* shutdownChannel.listen.pipe(Stream.flatten(), Stream.tap((error) => Effect.sync(() => shutdown(Cause.fail(error)))), Stream.runDrain, Effect.interruptible, Effect.tapCauseLogPretty, Effect.forkScoped);
|
|
15
|
+
const devtoolsWebmeshNode = devtoolsEnabled
|
|
16
|
+
? yield* makeExpoDevtoolsConnectedMeshNode({
|
|
17
|
+
nodeName: `expo-${storeId}-${clientId}-${sessionId}`,
|
|
18
|
+
target: `devtools-${storeId}-${clientId}-${sessionId}`,
|
|
19
|
+
})
|
|
20
|
+
: undefined;
|
|
21
|
+
const { leaderThread, initialSnapshot } = yield* makeLeaderThread({
|
|
22
|
+
storeId,
|
|
23
|
+
clientId,
|
|
24
|
+
sessionId,
|
|
25
|
+
schema,
|
|
26
|
+
makeSqliteDb,
|
|
27
|
+
syncOptions,
|
|
28
|
+
storage: storage ?? {},
|
|
29
|
+
devtoolsEnabled,
|
|
30
|
+
devtoolsWebmeshNode,
|
|
31
|
+
});
|
|
32
|
+
const sqliteDb = yield* makeSqliteDb({ _tag: 'in-memory' });
|
|
33
|
+
sqliteDb.import(initialSnapshot);
|
|
79
34
|
const clientSession = {
|
|
80
35
|
devtools: { enabled: false },
|
|
81
36
|
lockStatus,
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
leaderThread: {
|
|
86
|
-
mutations: {
|
|
87
|
-
pull: Stream.fromQueue(incomingSyncMutationsQueue),
|
|
88
|
-
push: (batch) => Effect.gen(function* () {
|
|
89
|
-
for (const mutationEventEncoded of batch) {
|
|
90
|
-
if (migrationOptions.strategy !== 'from-mutation-log')
|
|
91
|
-
return;
|
|
92
|
-
const mutation = mutationEventEncoded.mutation;
|
|
93
|
-
const mutationDef = schema.mutations.get(mutation) ?? shouldNeverHappen(`Unknown mutation: ${mutation}`);
|
|
94
|
-
const execArgsArr = getExecArgsFromMutation({
|
|
95
|
-
mutationDef,
|
|
96
|
-
mutationEvent: { decoded: undefined, encoded: mutationEventEncoded },
|
|
97
|
-
});
|
|
98
|
-
// write to mutation_log
|
|
99
|
-
if (mutationLogExclude.has(mutation) === false &&
|
|
100
|
-
execArgsArr.some((_) => _.statementSql.includes('__livestore')) === false) {
|
|
101
|
-
const mutationDefSchemaHash = mutationDefSchemaHashMap.get(mutation) ?? shouldNeverHappen(`Unknown mutation: ${mutation}`);
|
|
102
|
-
const argsJson = JSON.stringify(mutationEventEncoded.args ?? {});
|
|
103
|
-
const mutationLogRowValues = {
|
|
104
|
-
idGlobal: mutationEventEncoded.id.global,
|
|
105
|
-
idClient: mutationEventEncoded.id.client,
|
|
106
|
-
mutation: mutationEventEncoded.mutation,
|
|
107
|
-
argsJson,
|
|
108
|
-
schemaHash: mutationDefSchemaHash,
|
|
109
|
-
syncMetadataJson: Option.none(),
|
|
110
|
-
parentIdGlobal: mutationEventEncoded.parentId.global,
|
|
111
|
-
parentIdClient: mutationEventEncoded.parentId.client,
|
|
112
|
-
clientId: 'expo',
|
|
113
|
-
sessionId: 'expo',
|
|
114
|
-
};
|
|
115
|
-
try {
|
|
116
|
-
newMutationLogStmt.execute(makeBindValues({
|
|
117
|
-
columns: mutationLogMetaTable.sqliteDef.columns,
|
|
118
|
-
values: mutationLogRowValues,
|
|
119
|
-
variablePrefix: '$',
|
|
120
|
-
}));
|
|
121
|
-
}
|
|
122
|
-
catch (e) {
|
|
123
|
-
console.error('Error writing to mutation_log', e, mutationLogRowValues);
|
|
124
|
-
debugger;
|
|
125
|
-
throw e;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
else {
|
|
129
|
-
// console.debug('livestore-webworker: skipping mutation log write', mutation, statementSql, bindValues)
|
|
130
|
-
}
|
|
131
|
-
yield* devtools?.onMutation({ mutationEventEncoded }) ?? Effect.void;
|
|
132
|
-
}
|
|
133
|
-
}),
|
|
134
|
-
},
|
|
135
|
-
initialState: {
|
|
136
|
-
migrationsReport: {
|
|
137
|
-
migrations: [],
|
|
138
|
-
},
|
|
139
|
-
leaderHead: initialMutationEventId,
|
|
140
|
-
},
|
|
141
|
-
export: Effect.sync(() => dbRef.current.sqliteDb.export()),
|
|
142
|
-
getMutationLogData: Effect.sync(() => dbMutationLogRef.current.sqliteDb.export()),
|
|
143
|
-
networkStatus: SubscriptionRef.make({ isConnected: false, timestampMs: Date.now(), latchClosed: false }).pipe(Effect.runSync),
|
|
144
|
-
sendDevtoolsMessage: () => Effect.dieMessage('Not implemented'),
|
|
145
|
-
getSyncState: Effect.dieMessage('Not implemented'),
|
|
146
|
-
},
|
|
37
|
+
clientId,
|
|
38
|
+
sessionId,
|
|
39
|
+
leaderThread,
|
|
147
40
|
shutdown: () => Effect.dieMessage('TODO implement shutdown'),
|
|
148
|
-
sqliteDb
|
|
41
|
+
sqliteDb,
|
|
149
42
|
};
|
|
150
43
|
if (devtoolsEnabled) {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
}).pipe(Effect.tapCauseLogPretty, Effect.
|
|
44
|
+
yield* Effect.gen(function* () {
|
|
45
|
+
const storeDevtoolsChannel = yield* makeChannelForConnectedMeshNode({
|
|
46
|
+
target: `devtools-${storeId}-${clientId}-${sessionId}`,
|
|
47
|
+
node: devtoolsWebmeshNode,
|
|
48
|
+
schema: { listen: Devtools.ClientSession.MessageToApp, send: Devtools.ClientSession.MessageFromApp },
|
|
49
|
+
channelType: 'clientSession',
|
|
50
|
+
});
|
|
51
|
+
yield* connectDevtoolsToStore(storeDevtoolsChannel);
|
|
52
|
+
}).pipe(Effect.tapCauseLogPretty, Effect.forkScoped);
|
|
160
53
|
}
|
|
161
54
|
return clientSession;
|
|
162
55
|
}).pipe(Effect.mapError((cause) => (cause._tag === 'LiveStore.UnexpectedError' ? cause : new UnexpectedError({ cause }))), Effect.tapCauseLogPretty);
|
|
56
|
+
const makeLeaderThread = ({ storeId, clientId, sessionId, schema, makeSqliteDb, syncOptions, storage, devtoolsEnabled, devtoolsWebmeshNode, }) => Effect.gen(function* () {
|
|
57
|
+
const subDirectory = storage.subDirectory ? storage.subDirectory.replace(/\/$/, '') + '/' : '';
|
|
58
|
+
const pathJoin = (...paths) => paths.join('/').replaceAll(/\/+/g, '/');
|
|
59
|
+
const directory = pathJoin(SQLite.defaultDatabaseDirectory, subDirectory, storeId);
|
|
60
|
+
const readModelDatabaseName = `${'livestore-'}${schema.hash}@${liveStoreStorageFormatVersion}.db`;
|
|
61
|
+
const dbMutationLogPath = `${'livestore-'}mutationlog@${liveStoreStorageFormatVersion}.db`;
|
|
62
|
+
const dbReadModel = yield* makeSqliteDb({ _tag: 'expo', databaseName: readModelDatabaseName, directory });
|
|
63
|
+
const dbMutationLog = yield* makeSqliteDb({ _tag: 'expo', databaseName: dbMutationLogPath, directory });
|
|
64
|
+
const devtoolsOptions = yield* makeDevtoolsOptions({
|
|
65
|
+
devtoolsEnabled,
|
|
66
|
+
dbReadModel,
|
|
67
|
+
dbMutationLog,
|
|
68
|
+
storeId,
|
|
69
|
+
clientId,
|
|
70
|
+
sessionId,
|
|
71
|
+
devtoolsWebmeshNode,
|
|
72
|
+
});
|
|
73
|
+
const layer = yield* Layer.memoize(makeLeaderThreadLayer({
|
|
74
|
+
clientId,
|
|
75
|
+
dbReadModel,
|
|
76
|
+
dbMutationLog,
|
|
77
|
+
devtoolsOptions,
|
|
78
|
+
makeSqliteDb,
|
|
79
|
+
schema,
|
|
80
|
+
// NOTE we're creating a separate channel here since you can't listen to your own channel messages
|
|
81
|
+
shutdownChannel: yield* makeShutdownChannel(storeId),
|
|
82
|
+
storeId,
|
|
83
|
+
syncOptions,
|
|
84
|
+
}).pipe(Layer.provideMerge(FetchHttpClient.layer)));
|
|
85
|
+
return yield* Effect.gen(function* () {
|
|
86
|
+
const { dbReadModel: db, dbMutationLog, syncProcessor, connectedClientSessionPullQueues, extraIncomingMessagesQueue, initialState, } = yield* LeaderThreadCtx;
|
|
87
|
+
const initialLeaderHead = getClientHeadFromDb(dbMutationLog);
|
|
88
|
+
const pullQueue = yield* connectedClientSessionPullQueues.makeQueue(initialLeaderHead);
|
|
89
|
+
const leaderThread = {
|
|
90
|
+
mutations: {
|
|
91
|
+
pull: Stream.fromQueue(pullQueue),
|
|
92
|
+
push: (batch) => syncProcessor
|
|
93
|
+
.push(batch.map((item) => new MutationEvent.EncodedWithMeta(item)), { waitForProcessing: true })
|
|
94
|
+
.pipe(Effect.provide(layer), Effect.scoped),
|
|
95
|
+
},
|
|
96
|
+
initialState: { leaderHead: initialLeaderHead, migrationsReport: initialState.migrationsReport },
|
|
97
|
+
export: Effect.sync(() => db.export()),
|
|
98
|
+
getMutationLogData: Effect.sync(() => dbMutationLog.export()),
|
|
99
|
+
// TODO
|
|
100
|
+
networkStatus: SubscriptionRef.make({ isConnected: false, timestampMs: Date.now(), latchClosed: false }).pipe(Effect.runSync),
|
|
101
|
+
getSyncState: syncProcessor.syncState,
|
|
102
|
+
sendDevtoolsMessage: (message) => extraIncomingMessagesQueue.offer(message),
|
|
103
|
+
};
|
|
104
|
+
const initialSnapshot = db.export();
|
|
105
|
+
return { leaderThread, initialSnapshot };
|
|
106
|
+
}).pipe(Effect.provide(layer));
|
|
107
|
+
});
|
|
108
|
+
const makeDevtoolsOptions = ({ devtoolsEnabled, dbReadModel, dbMutationLog, storeId, clientId, sessionId, devtoolsWebmeshNode, }) => Effect.gen(function* () {
|
|
109
|
+
if (devtoolsEnabled === false) {
|
|
110
|
+
return {
|
|
111
|
+
enabled: false,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
return {
|
|
115
|
+
enabled: true,
|
|
116
|
+
makeBootContext: Effect.gen(function* () {
|
|
117
|
+
return {
|
|
118
|
+
// devtoolsWebChannel: yield* makeExpoDevtoolsChannel({
|
|
119
|
+
// nodeName: `leader-${storeId}-${clientId}`,
|
|
120
|
+
devtoolsWebChannel: yield* makeChannelForConnectedMeshNode({
|
|
121
|
+
node: devtoolsWebmeshNode,
|
|
122
|
+
target: `devtools-${storeId}-${clientId}-${sessionId}`,
|
|
123
|
+
schema: {
|
|
124
|
+
listen: Devtools.Leader.MessageToApp,
|
|
125
|
+
send: Devtools.Leader.MessageFromApp,
|
|
126
|
+
},
|
|
127
|
+
channelType: 'leader',
|
|
128
|
+
}),
|
|
129
|
+
persistenceInfo: {
|
|
130
|
+
readModel: dbReadModel.metadata.persistenceInfo,
|
|
131
|
+
mutationLog: dbMutationLog.metadata.persistenceInfo,
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
}),
|
|
135
|
+
};
|
|
136
|
+
});
|
|
163
137
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../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
+
dbPointer: number;
|
|
5
|
+
persistenceInfo: PersistenceInfo;
|
|
6
|
+
input: ExpoDatabaseInput;
|
|
7
|
+
};
|
|
8
|
+
type ExpoDatabaseInput = {
|
|
9
|
+
_tag: 'expo';
|
|
10
|
+
databaseName: string;
|
|
11
|
+
directory: string;
|
|
12
|
+
} | {
|
|
13
|
+
_tag: 'in-memory';
|
|
14
|
+
};
|
|
15
|
+
export type MakeExpoSqliteDb = MakeSqliteDb<Metadata, ExpoDatabaseInput, {
|
|
16
|
+
_tag: 'expo';
|
|
17
|
+
} & Metadata>;
|
|
18
|
+
export declare const makeSqliteDb: MakeExpoSqliteDb;
|
|
19
|
+
export {};
|
|
20
|
+
//# sourceMappingURL=make-sqlite-db.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"make-sqlite-db.d.ts","sourceRoot":"","sources":["../src/make-sqlite-db.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,eAAe,EAA+B,MAAM,mBAAmB,CAAA;AAKnG,KAAK,QAAQ,GAAG;IACd,IAAI,EAAE,MAAM,CAAA;IACZ,SAAS,EAAE,MAAM,CAAA;IACjB,eAAe,EAAE,eAAe,CAAA;IAChC,KAAK,EAAE,iBAAiB,CAAA;CACzB,CAAA;AAED,KAAK,iBAAiB,GAClB;IACE,IAAI,EAAE,MAAM,CAAA;IACZ,YAAY,EAAE,MAAM,CAAA;IACpB,SAAS,EAAE,MAAM,CAAA;CAClB,GACD;IACE,IAAI,EAAE,WAAW,CAAA;CAClB,CAAA;AAEL,MAAM,MAAM,gBAAgB,GAAG,YAAY,CAAC,QAAQ,EAAE,iBAAiB,EAAE;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG,QAAQ,CAAC,CAAA;AAErG,eAAO,MAAM,YAAY,EAAE,gBAgCvB,CAAA"}
|