Skip to content

Commit

Permalink
feat(nbstore): add nbstore worker
Browse files Browse the repository at this point in the history
  • Loading branch information
EYHN committed Dec 18, 2024
1 parent 3fddf05 commit 8553071
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 61 deletions.
2 changes: 1 addition & 1 deletion packages/common/nbstore/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"sideEffects": false,
"exports": {
".": "./src/index.ts",
"./op": "./src/op/index.ts",
"./worker": "./src/worker/index.ts",
"./idb": "./src/impls/idb/index.ts",
"./idb/v1": "./src/impls/idb/v1/index.ts",
"./cloud": "./src/impls/cloud/index.ts",
Expand Down
14 changes: 12 additions & 2 deletions packages/common/nbstore/src/impls/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import type { Storage } from '../storage';
import { CloudBlobStorage, CloudDocStorage } from './cloud';
import { BroadcastChannelAwarenessStorage } from './broadcast-channel/awareness';
import {
CloudAwarenessStorage,
CloudBlobStorage,
CloudDocStorage,
} from './cloud';
import {
IndexedDBBlobStorage,
IndexedDBDocStorage,
Expand All @@ -13,14 +18,19 @@ const idb: StorageConstructor[] = [
IndexedDBDocStorage,
IndexedDBBlobStorage,
IndexedDBSyncStorage,
BroadcastChannelAwarenessStorage,
];

const idbv1: StorageConstructor[] = [
IndexedDBV1DocStorage,
IndexedDBV1BlobStorage,
];

const cloud: StorageConstructor[] = [CloudDocStorage, CloudBlobStorage];
const cloud: StorageConstructor[] = [
CloudDocStorage,
CloudBlobStorage,
CloudAwarenessStorage,
];

export const storages: StorageConstructor[] = cloud.concat(idbv1, idb);

Expand Down
58 changes: 0 additions & 58 deletions packages/common/nbstore/src/op/ops.ts

This file was deleted.

File renamed without changes.
File renamed without changes.
103 changes: 103 additions & 0 deletions packages/common/nbstore/src/worker/ops.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import type {
BlobRecord,
DocClock,
DocClocks,
DocDiff,
DocRecord,
DocUpdate,
HistoryFilter,
ListedBlobRecord,
ListedHistory,
StorageOptions,
} from '../storage';
import type { AwarenessRecord } from '../storage/awareness';
import type { DocSyncDocState, DocSyncState } from '../sync/doc';

interface GroupedWorkerOps {
init: {
init: [
{
local: { name: string; opts: StorageOptions }[];
remotes: { name: string; opts: StorageOptions }[][];
},
void,
];
};

docStorage: {
getDoc: [string, DocRecord | null];
getDocDiff: [{ docId: string; state?: Uint8Array }, DocDiff | null];
pushDocUpdate: [{ update: DocUpdate; origin?: string }, DocClock];
getDocTimestamps: [Date, DocClocks];
getDocTimestamp: [string, DocClock];
deleteDoc: [string, void];
subscribeDocUpdate: [void, { update: DocRecord; origin?: string }];
};

historyStorage: {
listHistory: [{ docId: string; filter?: HistoryFilter }, ListedHistory[]];
getHistory: [DocClock, DocRecord | null];
deleteHistory: [DocClock, void];
rollbackDoc: [DocClock & { editor?: string }, void];
};

blobStorage: {
getBlob: [string, BlobRecord | null];
setBlob: [BlobRecord, void];
deleteBlob: [{ key: string; permanently: boolean }, void];
releaseBlobs: [void, void];
listBlobs: [void, ListedBlobRecord[]];
};

syncStorage: {
getPeerClocks: [string, DocClocks];
setPeerClock: [{ peer: string } & DocClock, void];
getPeerPushedClocks: [string, DocClocks];
setPeerPushedClock: [{ peer: string } & DocClock, void];
clearClocks: [void, void];
};

awarenessStorage: {
update: [{ awareness: AwarenessRecord; origin?: string }, void];
subscribeUpdate: [void, { awareness: AwarenessRecord; origin?: string }];
subscribeOnCollect: [void, { collectId: string }];
collect: [void, { collectId: string; awareness: AwarenessRecord }];
};

docSync: {
state: [string, DocSyncState];
docState: [string, DocSyncDocState];
addPriority: [string, boolean];
};

blobSync: {
downloadBlob: [string, BlobRecord | null];
uploadBlob: [BlobRecord, void];
};

awarenessSync: {
update: [{ awareness: AwarenessRecord; origin?: string }, void];
subscribeUpdate: [void, { awareness: AwarenessRecord; origin?: string }];
subscribeOnCollect: [void, { collectId: string }];
collect: [void, { collectId: string; awareness: AwarenessRecord }];
};
}

type Values<T> = T extends { [k in keyof T]: any } ? T[keyof T] : never;
type UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends (
x: infer I
) => void
? I
: never;

export type WorkerOps = UnionToIntersection<
Values<
Values<{
[k in keyof GroupedWorkerOps]: {
[k2 in keyof GroupedWorkerOps[k]]: k2 extends string
? Record<`${k}.${k2}`, GroupedWorkerOps[k][k2]>
: never;
};
}>
>
>;
File renamed without changes.

0 comments on commit 8553071

Please sign in to comment.