Skip to content

Commit

Permalink
Fix destroy of workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
zjkmxy committed Jan 27, 2024
1 parent ae078d9 commit 0aeef29
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 35 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ucla-irl/ndnts-aux",
"version": "1.0.9",
"version": "1.0.10",
"description": "NDNts Auxiliary Package for Web and Deno",
"scripts": {
"test": "deno test --no-check",
Expand Down
46 changes: 23 additions & 23 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions src/adaptors/yjs-state-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const SnapshotKey = 'localSnapshot';
* YjsStateManager manages updates transported by SyncAgent.
* It receives and emits updates, encodes and loads local state as updates.
*/
export class YjsStateManager {
export class YjsStateManager implements AsyncDisposable {
private readonly callback = this.docUpdateHandler.bind(this);
private counter = 0;

Expand All @@ -21,11 +21,15 @@ export class YjsStateManager {
doc.on('update', this.callback);
}

public destroy() {
this.saveLocalSnapshot(this.getState());
public async destroy() {
await this.saveLocalSnapshot(this.getState());
this.doc.off('update', this.callback);
}

async [Symbol.asyncDispose]() {
return await this.destroy();
}

private docUpdateHandler(_update: Uint8Array, origin: undefined) {
if (origin !== this) { // This condition must be true
this.counter += 1;
Expand Down
12 changes: 8 additions & 4 deletions src/sync-agent/sync-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { panic } from '../utils/panic.ts';
export type ChannelType = 'update' | 'blob' | 'status' | 'blobUpdate';
const AllChannelValues = ['update', 'blob', 'status', 'blobUpdate'];

export class SyncAgent {
export class SyncAgent implements AsyncDisposable {
private _ready = false;
readonly listeners: { [key: string]: (content: Uint8Array, id: Name) => void } = {};
readonly producer;
Expand Down Expand Up @@ -56,13 +56,17 @@ export class SyncAgent {
});
}

public destroy() {
this.atLeastOnce.destroy();
this.latestOnly.destroy();
public async destroy() {
await this.atLeastOnce.destroy();
await this.latestOnly.destroy();
this.trafficAttractor.close();
this.producer.close();
}

async [Symbol.asyncDispose]() {
return await this.destroy();
}

public reset() {
this.atLeastOnce.reset();
this.latestOnly.reset();
Expand Down
8 changes: 8 additions & 0 deletions src/utils/name-lit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ import { Encoder, NNI } from '@ndn/tlv';
/**
* NDN Name generator for test. Allows to use tag literals to generate NDN name from boilerplate.
* Designed for unit tests only. Do not use in production, as it is slow and not guaranteed to reflect the latest spec.
*
* @remarks
*
* 1. Alternative URI only works for templates, e.g. `` name`/aa/seg=${1}` ``.
* 2. Do not try to fool this function on purpuse. It is designed for test use only and
* does not handle corner cases at all.
* For example, `` name`/8=seg=${0}` ``.
*
* @param templates Name boilerplate
* @param values Values
*/
Expand Down
14 changes: 10 additions & 4 deletions src/workspace/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { encodeSyncState, parseSyncState, SyncAgent } from '../sync-agent/mod.ts
import { NdnSvsAdaptor, YjsStateManager } from '../adaptors/mod.ts';
import * as Y from 'yjs';

export class Workspace {
export class Workspace implements AsyncDisposable {
private constructor(
public readonly nodeId: Name,
public readonly persistStore: Storage,
Expand Down Expand Up @@ -79,10 +79,16 @@ export class Workspace {
this.syncAgent.fire();
}

public destroy() {
public async destroy() {
this.syncAgent.ready = false;
this.yjsSnapshotMgr.destroy();
this.syncAgent.destroy();
await Promise.all([
this.yjsSnapshotMgr.destroy(),
this.syncAgent.destroy(),
]);
// persistStore is not created by workspace
}

async [Symbol.asyncDispose]() {
return await this.destroy();
}
}

0 comments on commit 0aeef29

Please sign in to comment.