Skip to content

Commit

Permalink
fix: do not enable the eventManager by default + add close method (#331)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexishecf authored Dec 19, 2024
1 parent f7fdc93 commit 5673830
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 10 deletions.
6 changes: 6 additions & 0 deletions apps/angular-wallet/src/app/services/wallet.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,19 @@ export class WalletService {
walletAbstraction: new WalletApiClient('mainnet', core, {
apiURL,
}),
eventManagerParams: {
pullInterval: 5000,
},
}),
testnet: new Wallet({
chainType: 'testnet',
auth: { mnemonic },
walletAbstraction: new WalletApiClient('testnet', core, {
apiURL,
}),
eventManagerParams: {
pullInterval: 5000,
},
}),
};

Expand Down
3 changes: 3 additions & 0 deletions apps/arianee-react-wallet/src/app/utils/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ export const getWallet = (chainType: ChainType, walletApiUrl: string) => {
chainType,
arianeeAccessToken,
arianeeAccessTokenPrefix: prefix,
eventManagerParams: {
pullInterval: 5000,
},
});

return wallet;
Expand Down
6 changes: 5 additions & 1 deletion packages/wallet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ wallet.authenticate(); // force generate a wallet scoped arianee access token, u

The wallet emits events that can be listened to. It uses a pull mechanism under the hood, the pull interval can be passed to the constructor under the `eventManagerParams.pullInterval` key. Events will be pulled every `pullInterval` milliseconds if and only if there is at least one listener.

Default pull interval is 5 seconds.
Default pull interval is -1 (disabled), to enable it, pass any value above 0 (e.g. 5000 for 5 seconds).
If you use this feature, make sure to call `wallet.close()` when you are done with the wallet to stop the event manager and associated interval / listeners.

```typescript
// pull events every 2 seconds
Expand All @@ -197,6 +198,9 @@ wallet.smartAsset.received.removeListener(listener);

// remove all listeners
wallet.smartAsset.received.removeAllListeners();

// once you are done with the wallet, make sure to close it to stop the event manager (if you enabled it / set pull interval > 0)
wallet.close();
```

### Proofs
Expand Down
18 changes: 13 additions & 5 deletions packages/wallet/src/lib/services/eventManager/eventManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,12 @@ describe('EventManager', () => {
caseName: 'parameterized interval',
interval: 2000,
},
{
caseName: 'default interval (5000)',
interval: undefined,
},
])(
'should call setInterval with the pull method (interval: $caseName)',
({ interval }) => {
jest.clearAllMocks();

const expectedInterval = interval ?? 5000;
const expectedInterval = interval ?? -1;

expect(setIntervalSpy).not.toHaveBeenCalled();

Expand All @@ -70,6 +66,18 @@ describe('EventManager', () => {
);
}
);

it('should not call setInterval with the pull method (interval: default interval (-1))', () => {
jest.clearAllMocks();

expect(setIntervalSpy).not.toHaveBeenCalled();

new EventManager(chainType, walletApiClient, userAddress, jest.fn(), {
pullInterval: -1,
});

expect(setIntervalSpy).not.toHaveBeenCalled();
});
});

describe('pull', () => {
Expand Down
22 changes: 18 additions & 4 deletions packages/wallet/src/lib/services/eventManager/eventManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ type WrappedEventEmitters = {

export type EventManagerParams = {
/**
* Interval between each pull in ms
* @default 5000
* Interval between each pull in ms, -1 to disable (default), any value > 0 to enable
* @default -1
*/
pullInterval?: number;
arianeeApiUrl?: string;
Expand All @@ -34,6 +34,8 @@ export default class EventManager<T extends ChainType>
private pullAfter!: Date;
private arianeeApiClient: ArianeeApiClient;

private timer: ReturnType<typeof setInterval> | null = null;

private userTokenIds: SmartAsset['certificateId'][] = [];
private userTokenIssuers: BrandIdentity['address'][] = [];

Expand All @@ -46,7 +48,10 @@ export default class EventManager<T extends ChainType>
private fetchLike: typeof fetch,
params?: EventManagerParams
) {
this.pullInterval = params?.pullInterval ?? 5000;
this.pullInterval = params?.pullInterval ?? -1;

if (this.pullInterval === 0)
throw new Error('Pull interval must be greater than 0, or -1 to disable');

this.arianeeApiClient = new ArianeeApiClient(
params?.arianeeApiUrl ?? 'https://api.arianee.com',
Expand All @@ -56,7 +61,16 @@ export default class EventManager<T extends ChainType>
this.updatePullAfter();

if (this.pullInterval > 0)
setInterval(this.pull.bind(this), this.pullInterval);
this.timer = setInterval(this.pull.bind(this), this.pullInterval);
}

/** Remove all listeners and interval */
public kill() {
if (this.timer) clearInterval(this.timer);

this.arianee.removeAllListeners();

this.timer = null;
}

private readonly arianee: EventEmitter = new EventEmitter();
Expand Down
7 changes: 7 additions & 0 deletions packages/wallet/src/lib/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,13 @@ export default class Wallet<
return this.core.getAddress();
}

/**
* Close all connections and stop all listeners should be called when the wallet instance won't be used anymore
*/
public close() {
this.eventManager.kill();
}

/**
* Use this to force trigger an authentication signature if
* you use a wallet provider such as Metamask or WalletConnect
Expand Down

0 comments on commit 5673830

Please sign in to comment.