Skip to content

Commit

Permalink
Merge 26a9574 into a22429a
Browse files Browse the repository at this point in the history
  • Loading branch information
Himenon authored Oct 21, 2018
2 parents a22429a + 26a9574 commit 8c89113
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 1 deletion.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@
<summary>@babel/standalone</summary>
https://github.com/babel/babel/tree/master/packages/babel-standalone
</details>
<details>
<summary>EventEmitter</summary>
<p>https://nodejs.org/api/events.html#events_emitter_removelistener_eventname_listener</p>
<p>EventEmitterのテスト</p>
</details>
<details>
<summary>express-session</summary>
<p>https://github.com/expressjs/session</p>
<p>Debug `yarn run express:session`</p>
<p>Debug: yarn run express:session</p>
</details>
<details>
<summary>gray-matter</summary>
Expand Down
105 changes: 105 additions & 0 deletions src/event-emitter/__tests__/oneshot.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { CustomEventEmitter, CustomPayload, HogeClass } from '../oneshot';

describe('独自のEventEmitterのテスト', () => {
let dispatcher: CustomEventEmitter;
beforeAll(() => {
dispatcher = new CustomEventEmitter()
dispatcher.removeAllListeners();
})

test('dispatchされることを確認する', () => {
const fn = jest.fn();
dispatcher.addListener('nya', () => {
fn();
})
expect(fn.mock.calls.length).toBe(0);
dispatcher.emit('nya', {});
expect(fn.mock.calls.length).toBe(1);
dispatcher.emit('nya', {});
expect(fn.mock.calls.length).toBe(2);
})

test('引数がある場合', () => {
const fn = jest.fn((x: number, y: number, z: number) => x + y + z);
dispatcher.addListener('add', (payload: CustomPayload['add']) => {
fn(payload.a, payload.b, payload.c)
});
dispatcher.emit('add', { a: 1, b: 2, c: 3});
expect(fn.mock.calls[0][0]).toBe(1);
expect(fn.mock.calls[0][1]).toBe(2);
expect(fn.mock.calls[0][2]).toBe(3);
expect(fn.mock.results[0].value).toBe(6);
})

test('特定のイベントリスナーの着脱を確認する', () => {
const fn = jest.fn(() => undefined);
// 1回目
dispatcher.addListener('nya', fn);
dispatcher.emit('nya', {});
expect(fn.mock.calls.length).toBe(1);
// 外す
dispatcher.removeListener('nya', fn);
dispatcher.emit('nya', {});
expect(fn.mock.calls.length).toBe(1);
// 2回目
dispatcher.addListener('nya', fn);
dispatcher.emit('nya', {});
expect(fn.mock.calls.length).toBe(2);
})

test('HogeClassのセットしたときとセットしていないときの挙動を確認', () => {
const hoge = new HogeClass();
const hello = jest.fn((payload: CustomPayload['hoge']) => `hello ${payload.name}`);;
expect(hello.mock.calls.length).toBe(0);
dispatcher.emit('hoge', {
name: 'first'
})
hoge.hello = hello;
dispatcher.setHogeClassEvent(hoge);
dispatcher.emit('hoge', {
name: 'second'
})
expect(hello.mock.calls.length).toBe(1);
expect(hello.mock.results[0].value).toBe('hello second');
})

test('HogeClassの状態変化', () => {
const hoge = new HogeClass();
expect(hoge.result).toBeUndefined();
dispatcher.emit('hoge', {
name: 'hogeee'
});
expect(hoge.result).not.toBe('Hello hogeee !');
dispatcher.addListener('hoge', hoge.hello)
dispatcher.emit('hoge', {
name: 'ふえええ'
});
expect(hoge.result).not.toBe('Hello ふえええ !');
})

test('HogeClassのリセット', () => {
const dispatcher2 = new CustomEventEmitter();
const hoge = new HogeClass();
const hello = jest.fn((payload: CustomPayload['hoge']) => `hello ${payload.name}`);;
hoge.hello = hello;

expect((dispatcher2 as any ).hoge).toBeUndefined();
dispatcher2.emit('hoge', { name: 'foo' });
expect(hello.mock.calls.length).toBe(0);

dispatcher2.setHogeClassEvent(hoge);
expect((dispatcher2 as any ).hoge).toEqual(hoge);
expect((dispatcher2 as any ).hoge instanceof HogeClass).toBeTruthy();

dispatcher2.emit('hoge', { name: 'foo' });
expect(hello.mock.calls.length).toBe(1);
expect(hello.mock.results[0].value).toBe('hello foo');

dispatcher2.emit('reset', undefined);
expect((dispatcher2 as any ).hoge).toBeUndefined();
dispatcher2.emit('hoge', { name: 'foo' });
expect(hello.mock.calls.length).toBe(1);
expect(hello.mock.results[1]).toBeUndefined();
})
});

59 changes: 59 additions & 0 deletions src/event-emitter/oneshot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { EventEmitter } from 'events';

export class HogeClass {
public result: string | undefined;
public hello(payload: {name: string}) {
this.result = `Hello ${payload.name} !`
}
}

export interface CustomPayload {
hoge: {
name: string
};
foo: {
foo: string;
};
add: {
a: number;
b: number;
c: number;
};
nya: {};
reset: undefined;
}

export interface CustomListener {
hoge: (payload: CustomPayload['hoge']) => void;
foo: (payload: CustomPayload['foo']) => void;
add: (payload: CustomPayload['add']) => void;
nya: (payload: CustomPayload['nya']) => void;
reset: (payload: CustomPayload['reset']) => void;
}

export class CustomEventEmitter extends EventEmitter {

private hoge: HogeClass | undefined;

public setHogeClassEvent(hoge: HogeClass) {
this.hoge = hoge;
this.addListener('hoge', (payload: CustomPayload['hoge'])=> {
if (this.hoge) { this.hoge.hello(payload) };
});
this.addListener('reset', () => {
this.hoge = undefined;
})
}

public addListener<K extends keyof CustomListener>(event: K, listener : CustomListener[K]): this {
return super.addListener(event as string, listener);
}

public emit<K extends keyof CustomListener>(event: K, payload: CustomPayload[K]): boolean {
return super.emit(event, payload);
}

public removeListener<K extends keyof CustomListener>(event: K, listener: (...args: any[]) => void): this {
return super.removeListener(event, listener);
}
}
Empty file added src/event-emitter/types.ts
Empty file.

0 comments on commit 8c89113

Please sign in to comment.