Skip to content

Commit cfe6ce6

Browse files
committed
refactor: make SQLiteSupervisor use new state machine setup.
1 parent 8f1650a commit cfe6ce6

2 files changed

Lines changed: 26 additions & 54 deletions

File tree

packages/app/src/lib/workers/peer/impl.ts

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,32 +1085,30 @@ export class Peer {
10851085
}
10861086

10871087
class SqliteSupervisor {
1088-
#state: SqliteState;
1088+
#state: StateMachine<SqliteState>;
10891089
liveQueries: Map<string, { port: MessagePort; statement: SqlStatement }>;
10901090

10911091
constructor() {
1092-
this.#state = { state: "pending", readyPromise: new Deferred() };
1092+
this.#state = stateMachine({ state: "pending" });
10931093
this.liveQueries = new Map();
10941094
}
10951095

10961096
get untilReady() {
1097-
if (this.#state.state === "pending")
1098-
return this.#state.readyPromise.promise;
1099-
else return undefined;
1097+
return this.#state.transitionedTo("ready");
11001098
}
11011099

11021100
get ready() {
1103-
return this.#state.state === "ready";
1101+
return this.#state.current.state === "ready";
11041102
}
11051103

11061104
get sqliteWorker() {
1107-
if (this.#state.state !== "ready")
1105+
if (this.#state.current.state !== "ready")
11081106
throw new Error("Sqlite worker not initialised");
1109-
return this.#state.sqliteWorker;
1107+
return this.#state.current.sqliteWorker;
11101108
}
11111109

11121110
async setReady(workerInterface: SqliteWorkerInterface) {
1113-
if (this.#state.state === "pending") {
1111+
if (this.#state.current.state === "pending") {
11141112
const previousSchemaVersion = await prevStream.getSchemaVersion();
11151113
console.debug("(init.2) SQLite Supervisor ready.", {
11161114
previousSchemaVersion,
@@ -1143,37 +1141,27 @@ class SqliteSupervisor {
11431141
const channel = this.createLiveQueryChannel(port);
11441142
this.createLiveQuery(id, channel.port2, statement);
11451143
}
1146-
1147-
this.#state.readyPromise.resolve();
11481144
}
1149-
this.#state = {
1145+
this.#state.current = {
11501146
state: "ready",
11511147
sqliteWorker: workerInterface,
11521148
};
11531149
}
11541150

11551151
async materializeBatch(events: Batch.Events, priority: TaskPriority) {
1156-
await this.untilReady;
1157-
if (this.#state.state !== "ready")
1158-
throw new Error("Sqlite worker not initialized.");
1159-
return this.#state.sqliteWorker.materializeBatch(events, priority);
1152+
const { sqliteWorker } = await this.#state.transitionedTo("ready");
1153+
return sqliteWorker.materializeBatch(events, priority);
11601154
}
11611155

11621156
// Type assertion for convenience. Todo: use Zod/Arktype for sql output validation?
11631157
async runQuery<T = unknown>(statement: SqlStatement) {
1164-
await this.untilReady;
1165-
if (this.#state.state !== "ready")
1166-
throw new Error("Sqlite worker not initialized.");
1167-
return this.#state.sqliteWorker.runQuery(statement) as Promise<
1168-
QueryResult<T>
1169-
>;
1158+
const { sqliteWorker } = await this.#state.transitionedTo("ready");
1159+
return sqliteWorker.runQuery(statement) as Promise<QueryResult<T>>;
11701160
}
11711161

11721162
async runSavepoint(savepoint: Savepoint) {
1173-
await this.untilReady;
1174-
if (this.#state.state !== "ready")
1175-
throw new Error("Sqlite worker not initialized.");
1176-
return this.#state.sqliteWorker.runSavepoint(savepoint);
1163+
const { sqliteWorker } = await this.#state.transitionedTo("ready");
1164+
return sqliteWorker.runSavepoint(savepoint);
11771165
}
11781166

11791167
createLiveQueryChannel(port: MessagePort) {
@@ -1189,18 +1177,14 @@ class SqliteSupervisor {
11891177
port: MessagePort,
11901178
statement: SqlStatement,
11911179
) {
1192-
await this.untilReady;
1193-
if (this.#state.state !== "ready")
1194-
throw new Error("Sqlite worker not initialized.");
1180+
const { sqliteWorker } = await this.#state.transitionedTo("ready");
11951181
this.liveQueries.set(id, { port, statement });
1196-
await this.#state.sqliteWorker.createLiveQuery(id, port, statement);
1182+
await sqliteWorker.createLiveQuery(id, port, statement);
11971183
}
11981184

11991185
async deleteLiveQuery(id: string) {
1200-
await this.untilReady;
1201-
if (this.#state.state !== "ready")
1202-
throw new Error("Sqlite worker not initialized.");
1186+
const { sqliteWorker } = await this.#state.transitionedTo("ready");
12031187
this.liveQueries.delete(id);
1204-
await this.#state.sqliteWorker.deleteLiveQuery(id);
1188+
await sqliteWorker.deleteLiveQuery(id);
12051189
}
12061190
}

packages/app/src/lib/workers/peer/types.ts

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -458,23 +458,11 @@ export namespace ConnectionStates {
458458
}
459459
}
460460

461-
type SqlitePending = {
462-
state: "pending";
463-
readyPromise: Deferred<void>;
464-
};
465-
466-
type SqliteReady = {
467-
state: "ready";
468-
sqliteWorker: SqliteWorkerInterface;
469-
};
470-
471-
export type SqliteState = SqlitePending | SqliteReady;
472-
473-
// export type SqliteState =
474-
// | {
475-
// state: "pending";
476-
// }
477-
// | {
478-
// state: "ready";
479-
// sqliteWorker: SqliteWorkerInterface;
480-
// };
461+
export type SqliteState =
462+
| {
463+
state: "pending";
464+
}
465+
| {
466+
state: "ready";
467+
sqliteWorker: SqliteWorkerInterface;
468+
};

0 commit comments

Comments
 (0)