Commit 3bd6485b authored by Xunnamius (Luna)'s avatar Xunnamius (Luna)

a few more updates

parent 16dd06f7
......@@ -66,7 +66,7 @@ export default class EventFrameEmitter extends EventEmitter {
}
async _emit(eventName: string, args: Array<any>, ignoreGlobalListeners: boolean) {
const eventFrame = args[0] instanceof EventFrame ? args[0] : new EventFrame();
const eventFrame = args[0] instanceof EventFrame ? args.shift() : new EventFrame();
const listeners = this.listeners(eventName);
if(!listeners && (ignoreGlobalListeners || !this._globalListeners.size))
......@@ -132,7 +132,8 @@ export default class EventFrameEmitter extends EventEmitter {
/**
* Adds a listener that will interrupt the event loop if the EventFrame is
* `stop()`-ed. Note that calls to `removeListener()` should pass in the
* return value of this function as the listener to be removed.
* return value of this function as the listener to be removed unless
* `eventName == 'error'`.
*
* Note that the first argument passed to the listener is an EventFrame
* instance followed by any additional arguments.
......
......@@ -32,15 +32,13 @@ describe('::addListener', () => {
expect(worked).toBe(true);
});
test(`::addListener(${EVENT_NAME}, listener) works with synchronous listeners`, async () => {
test(`::addListener(${EVENT_NAME}, listener) works with synchronous listeners`, () => {
const oracle = oracleFactory();
let worked = false;
let promise = null;
oracle.addListener(EVENT_NAME, () => worked = true);
promise = oracle.emit(EVENT_NAME);
oracle.emit(EVENT_NAME);
await promise;
expect(worked).toBe(true);
});
......@@ -59,6 +57,46 @@ describe('::addListener', () => {
});
});
describe('::addGlobalListener', () => {
test(`::addGlobalListener(listener) works with synchronous listeners that return a Promise`, async () => {
const oracle = oracleFactory();
let worked = false;
let promise = null;
oracle.addGlobalListener(listenerFactory.returnsPromise(() => worked = true));
promise = oracle.emit(EVENT_NAME);
expect(worked).toBe(false);
await promise;
expect(worked).toBe(true);
});
test(`::addGlobalListener(listener) works with synchronous listeners`, () => {
const oracle = oracleFactory();
let worked = false;
oracle.addGlobalListener(() => worked = true);
oracle.emit(EVENT_NAME);
expect(worked).toBe(true);
});
test(`::addGlobalListener(listener) works with asynchronous listeners`, async () => {
const oracle = oracleFactory();
let worked = false;
let promise = null;
oracle.addGlobalListener(listenerFactory.async(() => worked = true));
promise = oracle.emit(EVENT_NAME);
expect(worked).toBe(false);
await promise;
expect(worked).toBe(true);
});
});
describe('::emit', () => {
test(`::emit(${EVENT_NAME}) listeners emit error event on throw`, async () => {
const oracle = oracleFactory();
......@@ -84,6 +122,16 @@ describe('::emit', () => {
expect(worked).toBe(true);
});
test(`::emit(${EVENT_NAME}) eventFrame.name set to "${EVENT_NAME}"`, () => {
const oracle = oracleFactory();
let E = {};
oracle.addListener(EVENT_NAME, e => E = e);
oracle.emit(EVENT_NAME);
expect(E.name).toBe(EVENT_NAME);
});
test(`::emit(${EVENT_NAME}, ...args) calls listener(EventFrame, ...args)`, () => {
const oracle = oracleFactory();
let worked = false;
......@@ -99,6 +147,31 @@ describe('::emit', () => {
expect(worked).toBe(true);
});
test(`::emit(${EVENT_NAME}, eventFrame) calls listener(EventFrame)`, async () => {
const oracle = oracleFactory();
const eventFrame = new EventFrame();
let worked = false;
oracle.addListener(EVENT_NAME, e => {
expect(e).toBe(eventFrame);
worked = true;
});
await oracle.emit(EVENT_NAME, eventFrame);
expect(worked).toBe(true);
});
test(`::emit(${EVENT_NAME}, eventFrame) eventFrame.name is not mutated`, () => {
const oracle = oracleFactory();
const eventFrame = new EventFrame();
eventFrame.name = 'test';
oracle.addListener(EVENT_NAME, () => {});
oracle.emit(EVENT_NAME, eventFrame);
expect(eventFrame.name).toBe('test');
});
test(`::emit(${EVENT_NAME}, eventFrame, ...args) calls listener(EventFrame, ...args)`, async () => {
const oracle = oracleFactory();
const eventFrame = new EventFrame();
......@@ -114,9 +187,36 @@ describe('::emit', () => {
await oracle.emit(EVENT_NAME, eventFrame, 1, 2);
expect(worked).toBe(true);
});
test.todo(`::emit() is a no-op if there are no listeners`);
});
describe('::emitIgnoreGlobalListeners', () => {
test.todo(`(all the same tests as emit go here)`);
});
describe('::removeListener', () => {
test.todo(`::removeListener() removes addListener`);
test.todo(`::removeListener() removes once`);
});
describe('::removeGlobalListener', () => {
test.todo(`::removeGlobalListener() removes addGlobalListener`);
});
describe('::once', () => {
test.todo(`::once() listener() is only executed once`);
});
describe('::on', () => {
test.todo(`::on() works identical to ::addListener()`);
});
describe('EventFrame', () => {
test(`::constructor() initializes with ::name = '<unknown>'`, () => {
expect((new EventFrame()).name).toBe('<unknown>');
});
test(`::stop() interrupts event loop`, () => {
const oracle = oracleFactory();
let worked = false;
......@@ -133,11 +233,14 @@ describe('EventFrame', () => {
const oracle = oracleFactory();
let worked = false;
oracle.addListener(EVENT_NAME, () => worked = true);
oracle.addListener(EVENT_NAME, e => e.stop());
oracle.addListener(EVENT_NAME, () => worked = false);
oracle.addGlobalListener(() => worked = true);
oracle.addGlobalListener(e => e.stop());
oracle.addGlobalListener(() => worked = false);
oracle.emit(EVENT_NAME);
expect(worked).toBe(true);
});
test.todo(`::continue() is called after each listener fires (including global)`);
test.todo(`::finish() is called once after all listeners have fired (including global)`);
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment