Skip to content

Commit 5b0c393

Browse files
bootstrap on page load
1 parent e657cca commit 5b0c393

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

apps/client/src/routes/$workspaceId/index.tsx

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,30 @@ import { WorkspaceManager } from "../../lib/services/workspace-manager";
1010
import { useActionEffect } from "../../lib/use-action-effect";
1111
import { Bootstrap, LiveQuery } from "../../workers/schema";
1212

13+
const bootstrap = ({ workspaceId }: { workspaceId: string }) =>
14+
Effect.gen(function* () {
15+
const pool = yield* Worker.makePoolSerialized({ size: 1 });
16+
return yield* pool.broadcast(new Bootstrap({ workspaceId }));
17+
}).pipe(
18+
Effect.scoped,
19+
Effect.provide(
20+
BrowserWorker.layer(
21+
() =>
22+
new globalThis.Worker(
23+
new URL("./src/workers/sync.ts", globalThis.origin),
24+
{ type: "module" }
25+
)
26+
)
27+
)
28+
);
29+
1330
export const Route = createFileRoute("/$workspaceId/")({
1431
component: RouteComponent,
1532
loader: ({ params: { workspaceId } }) =>
1633
RuntimeClient.runPromise(
1734
WorkspaceManager.getById({ workspaceId }).pipe(
18-
Effect.flatMap(Effect.fromNullable)
35+
Effect.flatMap(Effect.fromNullable),
36+
Effect.tap(({ workspaceId }) => bootstrap({ workspaceId }))
1937
)
2038
),
2139
});
@@ -27,25 +45,7 @@ function RouteComponent() {
2745
workspaceId: workspace.workspaceId,
2846
});
2947

30-
const [, bootstrap] = useActionEffect(
31-
({ workspaceId }: { workspaceId: string }) =>
32-
Effect.gen(function* () {
33-
const pool = yield* Worker.makePoolSerialized({ size: 1 });
34-
return yield* pool.broadcast(new Bootstrap({ workspaceId }));
35-
}).pipe(
36-
Effect.scoped,
37-
Effect.provide(
38-
BrowserWorker.layer(
39-
() =>
40-
new globalThis.Worker(
41-
new URL("./src/workers/sync.ts", globalThis.origin),
42-
{ type: "module" }
43-
)
44-
)
45-
)
46-
)
47-
);
48-
48+
const [, onBootstrap, bootstrapping] = useActionEffect(bootstrap);
4949
const [, onAdd] = useActionEffect((formData: FormData) =>
5050
Effect.gen(function* () {
5151
const loroStorage = yield* LoroStorage;
@@ -98,13 +98,14 @@ function RouteComponent() {
9898

9999
<p>{workspace.workspaceId}</p>
100100
<button
101+
disabled={bootstrapping}
101102
onClick={() =>
102103
startTransition(() =>
103-
bootstrap({ workspaceId: workspace.workspaceId })
104+
onBootstrap({ workspaceId: workspace.workspaceId })
104105
)
105106
}
106107
>
107-
Bootstrap
108+
{bootstrapping ? "Bootstrapping..." : "Bootstrap"}
108109
</button>
109110

110111
<form action={onAdd}>

0 commit comments

Comments
 (0)