|
1 | 1 | import { WorkerRunner } from "@effect/platform"; |
2 | 2 | import { BrowserWorkerRunner } from "@effect/platform-browser"; |
3 | 3 | import { Effect, Layer } from "effect"; |
| 4 | +import { ApiClient } from "../lib/api-client"; |
| 5 | +import { Dexie } from "../lib/dexie"; |
| 6 | +import { RuntimeClient } from "../lib/runtime-client"; |
| 7 | +import { TempWorkspace } from "../lib/services/temp-workspace"; |
| 8 | +import { WorkspaceManager } from "../lib/services/workspace-manager"; |
4 | 9 | import { WorkerMessage } from "./schema"; |
5 | 10 |
|
6 | 11 | const WorkerLive = WorkerRunner.layerSerialized(WorkerMessage, { |
7 | | - Bootstrap: ({ workspaceId }) => |
| 12 | + Bootstrap: (params) => |
8 | 13 | Effect.gen(function* () { |
9 | | - yield* Effect.log(`Bootstrapping workspace ${workspaceId}`); |
| 14 | + const { client } = yield* ApiClient; |
| 15 | + const { initClient } = yield* Dexie; |
| 16 | + |
| 17 | + const manager = yield* WorkspaceManager; |
| 18 | + const temp = yield* TempWorkspace; |
| 19 | + |
| 20 | + yield* Effect.log(`Running workspace '${params.workspaceId}'`); |
| 21 | + |
| 22 | + const workspace = yield* manager |
| 23 | + .getById({ workspaceId: params.workspaceId }) |
| 24 | + .pipe(Effect.mapError(() => "Get workspace error")); |
| 25 | + |
| 26 | + const clientId = yield* initClient.pipe( |
| 27 | + Effect.mapError(() => "Init client error") |
| 28 | + ); |
| 29 | + |
| 30 | + const tempUpdates = yield* temp |
| 31 | + .get({ workspaceId: workspace.workspaceId }) |
| 32 | + .pipe(Effect.mapError(() => "Get temp workspace error")); |
| 33 | + |
| 34 | + if (tempUpdates !== null) { |
| 35 | + const response = yield* Effect.fromNullable(workspace.token).pipe( |
| 36 | + Effect.flatMap((token) => |
| 37 | + client.syncData |
| 38 | + .push({ |
| 39 | + headers: { Authorization: `Bearer ${token}` }, |
| 40 | + path: { workspaceId: workspace.workspaceId }, |
| 41 | + payload: { clientId, snapshot: tempUpdates.snapshot }, |
| 42 | + }) |
| 43 | + .pipe( |
| 44 | + Effect.map((response) => ({ ...response, token })), |
| 45 | + Effect.mapError(() => "Sync push error") |
| 46 | + ) |
| 47 | + ), |
| 48 | + Effect.orElse(() => |
| 49 | + client.syncAuth |
| 50 | + .generateToken({ |
| 51 | + payload: { |
| 52 | + clientId, |
| 53 | + snapshot: tempUpdates.snapshot, |
| 54 | + workspaceId: params.workspaceId, |
| 55 | + }, |
| 56 | + }) |
| 57 | + .pipe( |
| 58 | + Effect.tap(({ token }) => |
| 59 | + manager.setToken({ |
| 60 | + workspaceId: workspace.workspaceId, |
| 61 | + token, |
| 62 | + }) |
| 63 | + ), |
| 64 | + Effect.mapError(() => "Generate token error") |
| 65 | + ) |
| 66 | + ) |
| 67 | + ); |
| 68 | + |
| 69 | + yield* manager |
| 70 | + .put({ |
| 71 | + workspaceId: response.workspaceId, |
| 72 | + snapshot: response.snapshot, |
| 73 | + token: response.token, |
| 74 | + version: new Uint8Array(), // TODO |
| 75 | + }) |
| 76 | + .pipe(Effect.mapError(() => "Put workspace error")); |
| 77 | + |
| 78 | + yield* Effect.log("Sync completed"); |
| 79 | + } else { |
| 80 | + yield* Effect.log("No sync updates"); |
| 81 | + } |
| 82 | + |
10 | 83 | return true; |
11 | 84 | }), |
12 | 85 | }).pipe(Layer.provide(BrowserWorkerRunner.layer)); |
13 | 86 |
|
14 | | -Effect.runFork(WorkerRunner.launch(WorkerLive)); |
| 87 | +RuntimeClient.runFork(WorkerRunner.launch(WorkerLive)); |
0 commit comments