Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
[Fix] Veryify function type is void, obtain function parameter length
  • Loading branch information
jtenner committed Jul 30, 2019
commit 1ff2d68539b4cc6c5290bda7883705d428c6deb2
14 changes: 7 additions & 7 deletions assembly/events/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { BLOCK_OVERHEAD, BLOCK } from "rt/common";


export class EventEmitter {
export abstract class EventEmitter {
public static EventMap: Map<i32, Map<string, u64>> = new Map<i32, Map<string, u64>>();
public static registerEventCallback<T, U>(event: string, length: u32 = 1): void {
public static registerEventCallback<T, U>(event: string): void {
if (!isFunction<U>()) {
ERROR("Cannot register event callback of type U where U is not a function.");
}
if (!isVoid<ReturnType<U>>()) {
ERROR("Cannot register event callback of type U where ReturnType<U> is not void.");
}
if (!EventEmitter.EventMap.has(idof<T>())) {
EventEmitter.EventMap.set(idof<T>(), new Map<string, u64>());
}
let ClassEvents = EventEmitter.EventMap.get(idof<T>());
if (ClassEvents.has(event)) {
throw new Error("EventMap already contains a definition for event: " + event);
}
let definition: u64 = <u64>idof<U>() | (<u64>length << 32);
let definition: u64 = <u64>idof<U>() | (<u64>ParameterCount<U>() << 32);
ClassEvents.set(event, definition);
log<u64>(definition);
}

private _events: Map<string, u32[]> = new Map<string, u32[]>();
Expand All @@ -30,10 +31,9 @@ export class EventEmitter {
if (!EventMap.has(rtId)) throw new Error("Cannot attach events to an EventEmitter with no EventMap definitions.");
let ClassEvents = EventMap.get(rtId);
if (!ClassEvents.has(event)) throw new Error("Event does not exist: " + event);
let cbId = idof<T>();
let classEventSignature = ClassEvents.get(event);
let classEventCallbackID = <u32>(classEventSignature & 0xFFFFFFFF);
assert(cbId == classEventCallbackID);
assert(idof<T>() == classEventCallbackID);
if (!this._events.has(event)) this._events.set(event, new Array<u32>());
let eventList = this._events.get(event);
eventList.push(changetype<u32>(callback));
Expand Down
9 changes: 9 additions & 0 deletions assembly/node.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,12 @@ declare class Buffer extends Uint8Array {
/** Reads a signed integer at the designated offset. */
readInt8(offset?: i32): i8;
}

export abstract class EventEmitter {
/** Call this function globally to setup a callback on your EventEmitter type `T`, with callback type `U`, and eventName `event`. */
public static registerEventCallback<T, U>(event: string): void;
/** Register a callback event of type `T` with eventName `event`. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times. */
public on<T>(event: string, callback: T): this;
/** Emit a callback event with the given parameters (up to 10.) */
public emit<T extends any[]>(...args: T): this;
}
47 changes: 14 additions & 33 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
},
"devDependencies": {
"@as-pect/core": "^2.3.1",
"assemblyscript": "github:jtenner/assemblyscript#idof-dist",
"glob": "^7.1.4",
"wasi": "github:devsnek/node-wasi"
},
"scripts": {
"test": "node tests/node"
},
"dependencies": {}
"dependencies": {
"assemblyscript": "github:jtenner/assemblyscript#builtins-dist"
}
}
6 changes: 3 additions & 3 deletions tests/events.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ type i32Callback = (a: i32) => void;
type i64Callback = (a: i64, b: i64) => void;
class CustomEventEmitter extends EventEmitter {}

EventEmitter.registerEventCallback<CustomEventEmitter, i32Callback>("data", 1);
EventEmitter.registerEventCallback<CustomEventEmitter, i64Callback>("data2", 2);
EventEmitter.registerEventCallback<CustomEventEmitter, i32Callback>("data");
EventEmitter.registerEventCallback<CustomEventEmitter, i64Callback>("data2");

describe("events", () => {
test("events", () => {
Expand All @@ -17,7 +17,7 @@ describe("events", () => {
calls += 1;
expect<i32>(wrongName).toBe(42);
});
t.emit<i32>("data", 42);
t.emit<i32>("data", 42,);
t.emit<i32>("data", 42);
t.emit<i32>("data", 42);
expect<i32>(calls).toBe(3);
Expand Down