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

[Implement] Naive basic EventEmitter class #18

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
104 changes: 104 additions & 0 deletions assembly/events/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { BLOCK_OVERHEAD, BLOCK } from "rt/common";

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): 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);
ClassEvents.set(event, <u64>idof<U>() | (<u64>lengthof<U>() << 32));
}

private _events: Map<string, u32[]> = new Map<string, u32[]>();

public on<T>(event: string, callback: T): EventEmitter {
if (!isFunction<T>()) ERROR("EventEmitter#on can only be called with a callback of type T where T is a static function.");
let rtId = changetype<BLOCK>(changetype<usize>(this) - BLOCK_OVERHEAD).rtId;
let EventMap = EventEmitter.EventMap;
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 classEventSignature = ClassEvents.get(event);
let classEventCallbackID = <u32>(classEventSignature & 0xFFFFFFFF);
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));
return this;
}

public emit<A = string | null, B = string | null, C = string | null, D = string | null, E = string | null, F = string | null, G = string | null, H = string | null, I = string | null, J = string | null>
(event: string, a: A = null, b: B = null, c: C = null, d: D = null, e: E = null, f: F = null, g: G = null, h: H = null, i: I = null, j: J = null): EventEmitter {
if (this._events.has(event)) {
let rtId = changetype<BLOCK>(changetype<usize>(this) - BLOCK_OVERHEAD).rtId;
let ClassEvents = EventEmitter.EventMap.get(rtId);
assert(ClassEvents.has(event), "No event definition.");
let classEventSignature = ClassEvents.get(event);
let classEventArgLength = <u32>(classEventSignature >> 32) & 0xFFFFFFFF;
let classEventCallbackID = <u32>(classEventSignature & 0xFFFFFFFF);
let list = this._events.get(event);
let length = list.length;
switch (classEventArgLength) {
case 0: {
assert(classEventCallbackID == idof<() => void>());
for (let z = 0; z < length; z++) call_indirect(unchecked(list[z]));
break;
}
case 1: {
assert(classEventCallbackID == idof<(a: A) => void>());
for (let z = 0; z < length; z++) call_indirect(unchecked(list[z]), a);
break;
}
case 2: {
assert(classEventCallbackID == idof<(a: A, b: B) => void>());
for (let z = 0; z < length; z++) call_indirect(unchecked(list[z]), a, b);
break;
}
case 3: {
assert(classEventCallbackID == idof<(a: A, b: B, c: C) => void>());
for (let z = 0; z < length; z++) call_indirect(unchecked(list[z]), a, b, c);
break;
}
case 4: {
assert(classEventCallbackID == idof<(a: A, b: B, c: C, d: D) => void>());
for (let z = 0; z < length; z++) call_indirect(unchecked(list[z]), a, b, c, d);
break;
}
case 5: {
assert(classEventCallbackID == idof<(a: A, b: B, c: C, d: D, e: E) => void>());
for (let z = 0; z < length; z++) call_indirect(unchecked(list[z]), a, b, c, d, e);
break;
}
case 6: {
assert(classEventCallbackID == idof<(a: A, b: B, c: C, d: D, e: E, f: F) => void>());
for (let z = 0; z < length; z++) call_indirect(unchecked(list[z]), a, b, c, d, e, f);
break;
}
case 7: {
assert(classEventCallbackID == idof<(a: A, b: B, c: C, d: D, e: E, f: F, g: G) => void>());
for (let z = 0; z < length; z++) call_indirect(unchecked(list[z]), a, b, c, d, e, f, g);
break;
}
case 8: {
assert(classEventCallbackID == idof<(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H) => void>());
for (let z = 0; z < length; z++) call_indirect(unchecked(list[z]), a, b, c, d, e, f, g, h);
break;
}
case 9: {
assert(classEventCallbackID == idof<(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I) => void>());
for (let z = 0; z < length; z++) call_indirect(unchecked(list[z]), a, b, c, d, e, f, g, h, i);
break;
}
case 10: {
assert(classEventCallbackID == idof<(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j: J) => void>());
for (let z = 0; z < length; z++) call_indirect(unchecked(list[z]), a, b, c, d, e, f, g, h, i, j);
break;
}
default: assert(false);
}
}
return this;
}
}
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;
}
10 changes: 5 additions & 5 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"devDependencies": {
"@as-pect/core": "^2.3.1",
"assemblyscript": "github:assemblyscript/assemblyscript",
"assemblyscript": "github:jtenner/assemblyscript#builtins-dist",
"glob": "^7.1.4",
"wasi": "github:devsnek/node-wasi"
},
Expand Down
39 changes: 39 additions & 0 deletions tests/events.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { EventEmitter } from "events";

let calls: i32 = 0;

type i32Callback = (a: i32) => void;
type i64Callback = (a: i64, b: i64) => void;
class CustomEventEmitter extends EventEmitter {}

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

describe("events", () => {
test("events", () => {
let t = new CustomEventEmitter();
calls = 0;
t.on<i32Callback>("data", (wrongName: i32) => {
calls += 1;
expect<i32>(wrongName).toBe(42);
});
t.emit<i32>("data", 42,);
t.emit<i32>("data", 42);
t.emit<i32>("data", 42);
expect<i32>(calls).toBe(3);
});

test("events2", () => {
let t = new CustomEventEmitter();
calls = 0;
t.on<i64Callback>("data2", (a: i64, b: i64) => {
calls += 1;
expect<i64>(a).toBe(42);
expect<i64>(b).toBe(42);
});
t.emit<i64, i64>("data2", <i64>42, <i64>42);
t.emit<i64, i64>("data2", <i64>42, <i64>42);
t.emit<i64, i64>("data2", <i64>42, <i64>42);
expect<i32>(calls).toBe(3);
});
});