Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jul 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
});
}
});
});