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

This commit is contained in:
Matthew Callens 2022-07-01 23:29:19 -04:00 committed by GitHub
parent 2ad00678b6
commit e67c50f914
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 16 deletions

View File

@ -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

View File

@ -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);

View File

@ -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 };
};

View File

@ -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");
});
}
});
});