Skip to content

Commit 105689b

Browse files
load loro storage
1 parent af3ba40 commit 105689b

6 files changed

Lines changed: 17 additions & 37 deletions

File tree

apps/client/src/lib/hooks/use-food.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
import { hookQuery, useDexieQuery } from "@local/client-lib";
1+
import { Service, useDexieQuery } from "@local/client-lib";
22
import { SnapshotSchema } from "@local/schema";
33
import { RuntimeClient } from "../runtime-client";
44

55
export const useFood = ({ workspaceId }: { workspaceId: string }) => {
66
return useDexieQuery(
77
() =>
88
RuntimeClient.runPromise(
9-
hookQuery((doc) => doc.getList("food"), { workspaceId })
9+
Service.LoroStorage.use(({ query }) =>
10+
query((doc) => doc.getList("food"), {
11+
workspaceId,
12+
})
13+
)
1014
),
1115
SnapshotSchema.fields.food.value
1216
);

packages/client-lib/src/hook-query.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.

packages/client-lib/src/main.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import { hookQuery } from "./hook-query";
21
import { RuntimeLayer } from "./runtime-layer";
32
import * as Service from "./services";
43
import * as SyncWorker from "./sync-worker";
54
import { useActionEffect } from "./use-action-effect";
65
import { useDexieQuery } from "./use-dexie-query";
76

87
export {
9-
hookQuery,
108
RuntimeLayer,
119
Service,
1210
SyncWorker,

packages/client-lib/src/services/loro-storage.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { VERSION, type LoroSchema } from "@local/schema";
1+
import { SnapshotSchema, type LoroSchema } from "@local/schema";
22
import { Effect } from "effect";
33
import { LoroDoc } from "loro-crdt";
44
import { TempWorkspace } from "./temp-workspace";
@@ -20,8 +20,7 @@ export class LoroStorage extends Effect.Service<LoroStorage>()("LoroStorage", {
2020
{ concurrency: "unbounded" }
2121
).pipe(
2222
Effect.map(({ workspace, tempWorkspace }) => {
23-
const doc = new LoroDoc<LoroSchema>();
24-
doc.getMap("metadata").set("version", VERSION);
23+
const doc = SnapshotSchema.EmptyDoc();
2524

2625
if (workspace !== undefined) {
2726
doc.import(workspace.snapshot);
@@ -35,6 +34,11 @@ export class LoroStorage extends Effect.Service<LoroStorage>()("LoroStorage", {
3534
})
3635
);
3736

38-
return { load };
37+
const query = <A>(
38+
extract: (doc: LoroDoc<LoroSchema>) => A,
39+
{ workspaceId }: { workspaceId: string }
40+
) => load({ workspaceId }).pipe(Effect.map(({ doc }) => extract(doc)));
41+
42+
return { load, query } as const;
3943
}),
4044
}) {}

packages/client-lib/src/services/migration.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export class Migration extends Effect.Service<Migration>()("Migration", {
2424
workspace.snapshot
2525
);
2626

27+
yield* Effect.log(doc.toJSON());
28+
2729
const newDoc = yield* Schema.decode(LoroDocMigration)(doc).pipe(
2830
Effect.catchTag(
2931
"ParseError",

packages/schema/src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const CurrentSchema = VersioningSchema[VERSION];
99
const Metadata = Schema.Struct({ version: Schema.Number });
1010

1111
export type LoroSchema = {
12-
metadata: LoroMap<typeof Metadata.Encoded>;
12+
metadata: LoroMap<Partial<typeof Metadata.Encoded>>;
1313
food: LoroList<LoroMap<typeof CurrentSchema.fields.food.value.Encoded>>;
1414
meal: LoroList<LoroMap<typeof CurrentSchema.fields.meal.value.Encoded>>;
1515
};

0 commit comments

Comments
 (0)