Skip to content

Commit

Permalink
ts: convert EventParser.parseLogs to a generator function (#2018)
Browse files Browse the repository at this point in the history
  • Loading branch information
callensm authored Jul 2, 2022
1 parent 2ad0067 commit e67c50f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 7 additions & 4 deletions ts/src/program/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
}
});
}
}
);

Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions ts/src/program/namespace/simulate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
};
Expand Down
22 changes: 12 additions & 10 deletions ts/tests/events.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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 = [
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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");
});
}
});
});

1 comment on commit e67c50f

@vercel
Copy link

@vercel vercel bot commented on e67c50f Jul 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

anchor-docs – ./

anchor-docs-200ms.vercel.app
www.anchor-lang.com
anchor-docs-git-master-200ms.vercel.app
anchor-lang.com

Please sign in to comment.