From d1c000f00d6ad53f80373ce943e1330b252c75cd Mon Sep 17 00:00:00 2001 From: Matthew Callens Date: Thu, 30 Jun 2022 12:03:06 -0400 Subject: [PATCH 1/2] convert parseLogs to a generator function --- ts/src/program/event.ts | 11 +++++++---- ts/src/program/namespace/simulate.ts | 4 ++-- ts/tests/events.spec.ts | 22 ++++++++++++---------- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/ts/src/program/event.ts b/ts/src/program/event.ts index b1fe316dac..872251b39c 100644 --- a/ts/src/program/event.ts +++ b/ts/src/program/event.ts @@ -100,18 +100,21 @@ export class EventManager { if (logs.err) { return; } - this._eventParser.parseLogs(logs.logs, (event) => { + + for (const event of this._eventParser.parseLogs(logs.logs)) { const allListeners = this._eventListeners.get(event.name); + if (allListeners) { allListeners.forEach((listener) => { const listenerCb = this._eventCallbacks.get(listener); + if (listenerCb) { const [, callback] = listenerCb; callback(event.data, ctx.slot, logs.signature); } }); } - }); + } } ); @@ -172,14 +175,14 @@ export class EventParser { // its emission, thereby allowing us to know if a given log event was // emitted by *this* program. If it was, then we parse the raw string and // emit the event if the string matches the event being subscribed to. - public parseLogs(logs: string[], callback: (log: Event) => void) { + public *parseLogs(logs: string[]) { const logScanner = new LogScanner(logs); const execution = new ExecutionContext(); let log = logScanner.next(); while (log !== null) { let [event, newProgram, didPop] = this.handleLog(execution, log); if (event) { - callback(event); + yield event; } if (newProgram) { execution.push(newProgram); diff --git a/ts/src/program/namespace/simulate.ts b/ts/src/program/namespace/simulate.ts index 1c653f50e7..f73a821511 100644 --- a/ts/src/program/namespace/simulate.ts +++ b/ts/src/program/namespace/simulate.ts @@ -53,9 +53,9 @@ export default class SimulateFactory { const events: Event>[] = []; if (idl.events) { let parser = new EventParser(programId, coder); - parser.parseLogs(logs, (event) => { + for (const event of parser.parseLogs(logs)) { events.push(event); - }); + } } return { events, raw: logs }; }; diff --git a/ts/tests/events.spec.ts b/ts/tests/events.spec.ts index a909f22de5..f02fc00ad4 100644 --- a/ts/tests/events.spec.ts +++ b/ts/tests/events.spec.ts @@ -26,9 +26,9 @@ describe("Events", () => { const programId = PublicKey.default; const eventParser = new EventParser(programId, coder); - eventParser.parseLogs(logs, () => { + if (Array.from(eventParser.parseLogs(logs)).length > 0) { throw new Error("Should never find logs"); - }); + } }); it("Upgrade event check", () => { const logs = [ @@ -54,9 +54,9 @@ describe("Events", () => { const programId = PublicKey.default; const eventParser = new EventParser(programId, coder); - eventParser.parseLogs(logs, () => { + if (Array.from(eventParser.parseLogs(logs)).length > 0) { throw new Error("Should never find logs"); - }); + } }); it("Find event with different start log.", (done) => { const logs = [ @@ -118,10 +118,11 @@ describe("Events", () => { ); const eventParser = new EventParser(programId, coder); - eventParser.parseLogs(logs, (event) => { + const gen = eventParser.parseLogs(logs); + for (const event of gen) { expect(event.name).toEqual("NftSold"); done(); - }); + } }); it("Find event from logs", (done) => { const logs = [ @@ -213,10 +214,11 @@ describe("Events", () => { ); const eventParser = new EventParser(programId, coder); - eventParser.parseLogs(logs, (event) => { + const gen = eventParser.parseLogs(logs); + for (const event of gen) { expect(event.name).toEqual("ListingClosed"); done(); - }); + } }); it("Listen to different program and send other program logs with same name", () => { const logs = [ @@ -271,8 +273,8 @@ describe("Events", () => { ); const eventParser = new EventParser(programId, coder); - eventParser.parseLogs(logs, () => { + if (Array.from(eventParser.parseLogs(logs)).length > 0) { throw new Error("Should never find logs"); - }); + } }); }); From 417f88a3d414ebf952a07e01d0f6803b7509cc59 Mon Sep 17 00:00:00 2001 From: Matthew Callens Date: Fri, 1 Jul 2022 20:36:09 -0400 Subject: [PATCH 2/2] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5725ae6a77..40c0588397 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ com/project-serum/anchor/pull/1841)). * ts: Implement a coder for SPL associated token program ([#1939](https://github.com/coral-xyz/anchor/pull/1939)). * ts: verbose error for missing `ANCHOR_WALLET` variable when using `NodeWallet.local()` ([#1958](https://github.com/coral-xyz/anchor/pull/1958)). * ts: Add `MethodsBuilder#accountsStrict` for strict typing on ix account input ([#2019](https://github.com/coral-xyz/anchor/pull/2019)). +* ts: Change `EventParser#parseLogs` implementation to be a generator instead of callback function ([#2018](https://github.com/coral-xyz/anchor/pull/2018)). ### Fixes