Skip to content

Commit

Permalink
Merge pull request #11 from CAWorks-ChrisA/main
Browse files Browse the repository at this point in the history
Make snapshotTopic configurable from wksp.create()
  • Loading branch information
zjkmxy authored Nov 16, 2024
2 parents 30e43dc + 85e8155 commit 681580e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/adaptors/yjs-ndn-adaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export class NdnSvsAdaptor {
// Verify this part if there's a change in naming convention.
// NOTE: Currently naming convention is hard-coded. May need organizing.
// WARNING: It does not support multiple Yjs documents in the same app.
const snapshotPrefix = this.syncAgent.appPrefix.append('32=snapshot');
const snapshotPrefix = this.syncAgent.appPrefix.append('32=' + this.snapshotTopic);
// New SVS encodings
const snapshotName = snapshotPrefix.append(new Component(Version.type, encodedSV));

Expand Down Expand Up @@ -156,7 +156,7 @@ export class NdnSvsAdaptor {

// NOTE: The following code depend on snapshot naming convention to work.
// Verify this part if there's a change in naming convention.
const snapshotPrefix = this.syncAgent.appPrefix.append('32=snapshot');
const snapshotPrefix = this.syncAgent.appPrefix.append('32=' + this.snapshotTopic);

// NOTE: The following code depend on snapshot naming convention to work.
// Verify this part if there's a change in naming convention.
Expand Down
5 changes: 4 additions & 1 deletion src/sync-agent/sync-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class SyncAgent implements AsyncDisposable {
readonly latestOnly: LatestOnlyDelivery,
readonly onReset?: () => void,
readonly groupKey?: CryptoKey,
readonly snapshotTopic: string = 'snapshot',
) {
atLeastOnce.onReset = () => this.onResetTriggered();
latestOnly.onReset = () => this.onResetTriggered();
Expand Down Expand Up @@ -356,7 +357,7 @@ export class SyncAgent implements AsyncDisposable {

// NOTE: The following code depend on snapshot naming convention to work.
// Verify this part if there's a change in naming convention.
if (intName.get(this.appPrefix.length)?.equals(Component.from('32=snapshot'))) {
if (intName.get(this.appPrefix.length)?.equals(Component.from('32=' + this.snapshotTopic))) {
const wire = await this.persistStorage.get(intName.toString());
if (wire === undefined || wire.length === 0) {
// console.warn(`A remote peer is fetching a non-existing object: ${intName.toString()}`);
Expand Down Expand Up @@ -460,6 +461,7 @@ export class SyncAgent implements AsyncDisposable {
verifier: Verifier,
onReset?: () => void,
groupKeyBits?: Uint8Array,
snapshotTopic?: string,
) {
const tempStorage = new InMemoryStorage();
// Note: we need the signer name to be /[appPrefix]/<nodeId>/KEY/<keyID>
Expand Down Expand Up @@ -510,6 +512,7 @@ export class SyncAgent implements AsyncDisposable {
latestOnly,
onReset,
groupKey,
snapshotTopic,
);
resolver!((content, id) => ret.onUpdate(content, id));
return ret;
Expand Down
3 changes: 3 additions & 0 deletions src/workspace/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class Workspace implements AsyncDisposable {
useBundler?: boolean;
groupKeyBits?: Uint8Array;
snapshotInterval?: number;
snapshotTopic?: string;
}) {
// Always init a new one, and then load.
if (opts.createNewDoc) {
Expand All @@ -47,6 +48,7 @@ export class Workspace implements AsyncDisposable {
opts.verifier,
opts.onReset,
opts.groupKeyBits,
opts.snapshotTopic ?? 'snapshot',
);

// Root doc using CRDT and Sync
Expand All @@ -56,6 +58,7 @@ export class Workspace implements AsyncDisposable {
'doc',
opts.useBundler ?? false,
opts.snapshotInterval ?? 100,
opts.snapshotTopic ?? 'snapshot',
);
const yjsSnapshotMgr = new YjsStateManager(
() => encodeSyncState(syncAgent!.getUpdateSyncSV()),
Expand Down

0 comments on commit 681580e

Please sign in to comment.