Ver Fonte

ts: convert `EventParser.parseLogs` to a generator function (#2018)

Matthew Callens há 3 anos atrás
pai
commit
e67c50f914
4 ficheiros alterados com 22 adições e 16 exclusões
  1. 1 0
      CHANGELOG.md
  2. 7 4
      ts/src/program/event.ts
  3. 2 2
      ts/src/program/namespace/simulate.ts
  4. 12 10
      ts/tests/events.spec.ts

+ 1 - 0
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
 

+ 7 - 4
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);

+ 2 - 2
ts/src/program/namespace/simulate.ts

@@ -53,9 +53,9 @@ export default class SimulateFactory {
       const events: Event<IdlEvent, IdlTypes<IDL>>[] = [];
       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 };
     };

+ 12 - 10
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");
-    });
+    }
   });
 });