Skip to content

Commit

Permalink
Add unsubscribeEventListeners method to cloud wallet.
Browse files Browse the repository at this point in the history
  • Loading branch information
gasher committed Feb 12, 2025
1 parent 3d9dd10 commit b573874
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
17 changes: 16 additions & 1 deletion integration-tests/cloud-wallet.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {IWallet} from '@docknetwork/wallet-sdk-core/lib/types';
import {closeWallet, createNewWallet} from './helpers/wallet-helpers';
import {DataStore} from '@docknetwork/wallet-sdk-data-store/src/types';
import {DataStore, DataStoreEvents} from '@docknetwork/wallet-sdk-data-store/src/types';
import {
SYNC_MARKER_TYPE,
initializeCloudWallet,
Expand All @@ -20,6 +20,7 @@ describe('Cloud wallet', () => {
let pullDocuments: any;
let getSyncMarkerDiff: any;
let clearEdvDocuments: any;
let unsubscribeEventListeners: any;
let wallet: IWallet;

beforeAll(async () => {
Expand All @@ -40,6 +41,7 @@ describe('Cloud wallet', () => {
pullDocuments,
getSyncMarkerDiff,
clearEdvDocuments,
unsubscribeEventListeners,
} = await initializeCloudWallet({
dataStore,
edvUrl: EDV_URL,
Expand Down Expand Up @@ -148,5 +150,18 @@ describe('Cloud wallet', () => {
expect(syncMarkerDiff > 0).toBeTruthy();
});

// This test should be run last as it unsubscribes from all event listeners
it('should unsubscribe from all events listeners', async () => {
expect(dataStore.events.listeners(DataStoreEvents.DocumentCreated)).toHaveLength(1);
expect(dataStore.events.listeners(DataStoreEvents.DocumentDeleted)).toHaveLength(1);
expect(dataStore.events.listeners(DataStoreEvents.DocumentUpdated)).toHaveLength(1);

unsubscribeEventListeners();

expect(dataStore.events.listeners(DataStoreEvents.DocumentCreated)).toHaveLength(0);
expect(dataStore.events.listeners(DataStoreEvents.DocumentDeleted)).toHaveLength(0);
expect(dataStore.events.listeners(DataStoreEvents.DocumentUpdated)).toHaveLength(0);
});

afterAll(() => closeWallet());
});
8 changes: 8 additions & 0 deletions packages/core/src/cloud-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ export async function initializeCloudWallet({
dataStore.events.on(DataStoreEvents.DocumentDeleted, removeDocumentHandler);
dataStore.events.on(DataStoreEvents.DocumentUpdated, updateDocumentHandler);


function unsubscribeEventListeners() {
dataStore.events.off(DataStoreEvents.DocumentCreated, addDocumentHandler);
dataStore.events.off(DataStoreEvents.DocumentDeleted, removeDocumentHandler);
dataStore.events.off(DataStoreEvents.DocumentUpdated, updateDocumentHandler);
}

async function getSyncMarkerDiff() {
const edvSyncMaker = await findDocumentByContentId(SYNC_MARKER_TYPE);
const localSyncMarker = await dataStore.documents.getDocumentById(
Expand Down Expand Up @@ -181,5 +188,6 @@ export async function initializeCloudWallet({
updateDocumentByContentId,
waitForEdvIdle,
pullDocuments,
unsubscribeEventListeners,
};
}

0 comments on commit b573874

Please sign in to comment.