Skip to content

Commit

Permalink
fix: Removed dependency for Node.EventEmitter
Browse files Browse the repository at this point in the history
  • Loading branch information
kyasbal committed Jul 19, 2017
1 parent ba6fbdc commit e792d45
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 22 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
"grimoire"
],
"dependencies": {
"@types/node": "^8.0.14",
"events": "^1.1.1"
"eventemitter3": "^2.0.3"
},
"devDependencies": {
"ava": "^0.21.0",
Expand Down
56 changes: 40 additions & 16 deletions src/Base/EEObject.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,50 @@
/// <reference types="node"/>
import {EventEmitter} from "events";
import { EventEmitter, ListenerFn } from "eventemitter3";
import IDObject from "./IDObject";

/**
* EventEmitterをmixinしたIDObject
*/
class EEObject extends IDObject implements NodeJS.EventEmitter {
public prependListener: (event: string, listener: Function) => this;
public prependOnceListener: (event: string, listener: Function) => this;
public eventNames: () => string[];
public addListener: (event: string, listener: Function) => this;
public on: (event: string, listener: Function) => this;
public once: (event: string, listener: Function) => this;
public removeListener: (event: string, listener: Function) => this;
public removeAllListeners: (event?: string) => this;
public setMaxListeners: (n: number) => this;
public getMaxListeners: () => number;
public listeners: (event: string) => Function[];
public listenerCount: (type: string) => number;
public emit: (event: string, ...args: any[]) => boolean;
class EEObject extends IDObject implements EventEmitter {
/**
* Return an array listing the events for which the emitter has registered
* listeners.
*/
public eventNames: () => Array<string | symbol>;

/**
* Return the listeners registered for a given event.
*/
public listeners: ((event: string | symbol, exists: boolean) => Array<ListenerFn> | boolean) & ((event: string | symbol) => Array<ListenerFn>);

/**
* Calls each of the listeners registered for a given event.
*/
public emit: (event: string | symbol, ...args: Array<any>) => boolean;

/**
* Add a listener for a given event.
*/
public on: (event: string | symbol, fn: ListenerFn, context?: any) => this;
public addListener: (event: string | symbol, fn: ListenerFn, context?: any) => this;

/**
* Add a one-time listener for a given event.
*/
public once: (event: string | symbol, fn: ListenerFn, context?: any) => this;

/**
* Remove the listeners of a given event.
*/
public removeListener: (event: string | symbol, fn?: ListenerFn, context?: any, once?: boolean) => this;
public off: (event: string | symbol, fn?: ListenerFn, context?: any, once?: boolean) => this;

/**
* Remove all listeners, or those of the specified event.
*/
public removeAllListeners: (event?: string | symbol) => this;
constructor() {
super();
EventEmitter.call(this);
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/Base/ListenerFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {ListenerFn} from "eventemitter3";
export interface Fake {}
export default ListenerFn;
9 changes: 5 additions & 4 deletions src/Interface/NodeInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import GomlParser from "../Node/GomlParser";
import Attribute from "../Node/Attribute";
import GomlNode from "../Node/GomlNode";
import {Name, Nullable} from "../Base/Types";
import ListenerFunction from "../Base/ListenerFunction";


/**
Expand Down Expand Up @@ -104,9 +105,9 @@ export default class NodeInterface {
/**
* 対象ノードにイベントリスナを追加します。
* @param {string} eventName [description]
* @param {Function} listener [description]
* @param {ListenerFunction} listener [description]
*/
public on(eventName: string, listener: Function): NodeInterface {
public on(eventName: string, listener: ListenerFunction): NodeInterface {
this.forEach(node => {
node.on(eventName, listener);
});
Expand All @@ -116,9 +117,9 @@ export default class NodeInterface {
/**
* 対象ノードに指定したイベントリスナが登録されていれば削除します
* @param {string} eventName [description]
* @param {Function} listener [description]
* @param {ListenerFunction} listener [description]
*/
public off(eventName: string, listener: Function): NodeInterface {
public off(eventName: string, listener: ListenerFunction): NodeInterface {
this.forEach(node => {
node.removeListener(eventName, listener);
});
Expand Down

0 comments on commit e792d45

Please sign in to comment.