-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from botorjs/create_channel
create channel
- Loading branch information
Showing
11 changed files
with
275 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,63 @@ | ||
# event-bus | ||
Library that core of Botorjs is a event bus | ||
# Event Bus | ||
[![Build Status](https://travis-ci.org/botorjs/event-bus.svg?branch=master)](https://travis-ci.org/botorjs/event-bus) | ||
[![Coverage Status](https://coveralls.io/repos/github/botorjs/event-bus/badge.svg?branch=master)](https://coveralls.io/github/botorjs/event-bus?branch=master) | ||
|
||
### Library that core of Botorjs is a event bus | ||
|
||
# Installation | ||
``` | ||
npm install @botorjs/event-bus --save | ||
``` | ||
|
||
# Setup and Example | ||
|
||
|
||
```js | ||
|
||
import { EventBus } from "../src/EventBus"; | ||
var bus: EventBus = new EventBus(); | ||
|
||
// | ||
bus.on("test", function(data) { | ||
// process | ||
}) | ||
bus.emit("test", "test"); | ||
|
||
// channel point-to-point | ||
bus.registerChannel("test", "test", (data: ContextChannel) => { | ||
// process | ||
}); | ||
bus.emit("test","t"); | ||
bus.emit("test","t"); | ||
bus.emit("test","t"); | ||
``` | ||
|
||
# API | ||
|
||
## EventBus | ||
|
||
| Property | Description | | ||
|---------- |:-------------| | ||
| on(name, callback) | listen event | | ||
| once(name, callback) | listen event one time | | ||
| off(name, callback) | remove listen event | | ||
| registerChannel(name, event_name, callback, limit = 0) | register a channel with callback handle | | ||
| getChannel(name) | get channel have register | | ||
| removeChannel(name) | remove channel have register | | ||
|
||
|
||
## ContextChannel | ||
* data connext of Channel | ||
|
||
| Property | Description | | ||
|---------- |:-------------| | ||
| data | get value data | | ||
| channel | set channel event | | ||
|
||
## Channel | ||
|
||
| Property | Description | | ||
|---------- |:-------------| | ||
| name | name channel | | ||
| listen | listen event | | ||
| remove | remove listen event | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Channel } from './src/Channel'; | ||
import { EventBus } from './src/EventBus'; | ||
import { ContextChannel } from './src/ContextChannel'; | ||
|
||
export { Channel, EventBus, ContextChannel } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,8 @@ | |
"event-bus", | ||
"event", | ||
"bus", | ||
"chanel", | ||
"channel", | ||
"point-to-point", | ||
"node", | ||
"typescript" | ||
], | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { EventEmitter } from 'events'; | ||
import { ContextChannel } from './ContextChannel'; | ||
import { EventBus } from './EventBus'; | ||
import Debug from 'debug'; | ||
|
||
var debug = Debug("channel"); | ||
|
||
export enum StateChannel { | ||
Process, | ||
Waiting, | ||
} | ||
|
||
export class Channel { | ||
|
||
private _name: string; | ||
private _state: StateChannel; | ||
private _limist_queue: number; | ||
private _queue: any[]; | ||
private _bus: EventEmitter; | ||
private _callback: Function; | ||
private _context: ContextChannel; | ||
private _event_name: string; | ||
private _event_bus: EventBus; | ||
|
||
constructor(event_bus: EventBus, name: string,event_name: string, callback: Function, limit: number = 0) { | ||
this._event_bus = event_bus; | ||
this._event_name = event_name; | ||
this._name = name; | ||
this._callback = callback; | ||
this._limist_queue = limit; | ||
this._bus = new EventEmitter(); | ||
this._queue = new Array(); | ||
this._bus.on("next", this._next.bind(this)); | ||
this._bus.on("handler", this.handler.bind(this)); | ||
this._context = new ContextChannel(this); | ||
|
||
// | ||
this._state = StateChannel.Waiting; | ||
} | ||
|
||
public get name(): string { | ||
return this._name; | ||
} | ||
|
||
private handler(data: any) { | ||
this._state = StateChannel.Process; | ||
this._context.data = data; | ||
try { | ||
this._callback(this._context); | ||
} finally { | ||
this._state = StateChannel.Waiting; | ||
} | ||
this.callEventNext(); | ||
} | ||
|
||
private callEventNext() { | ||
debug("call next event"); | ||
this._bus.emit("next"); | ||
} | ||
|
||
public listen() { | ||
this._event_bus.on(this._event_name, this._listen.bind(this)); | ||
} | ||
|
||
public remove() { | ||
this._event_bus.off(this._event_name, this._listen.bind(this)); | ||
} | ||
|
||
private _listen(data: any) { | ||
debug("listen new event channel"); | ||
if(this._limist_queue != 0 && this._limist_queue >= this._queue.length) { | ||
return; | ||
} | ||
this._queue.push(data); | ||
this.callEventNext(); | ||
} | ||
|
||
private _next() { | ||
debug("next process"); | ||
if(this._state == StateChannel.Waiting) { | ||
debug("ext process StateChannel Waiting"); | ||
if(this._queue.length > 0) { | ||
var data = this._queue.shift(); | ||
this._bus.emit("handler", data) | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Channel } from "./Channel"; | ||
|
||
|
||
export class ContextChannel { | ||
public data: any; | ||
private _channel: Channel; | ||
|
||
constructor(channel: Channel) { | ||
this._channel = channel; | ||
} | ||
|
||
public get channel():Channel{ | ||
return this._channel; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { expect } from "chai"; | ||
import { EventBus } from "../src/EventBus"; | ||
import { ContextChannel } from "../src/ContextChannel"; | ||
|
||
|
||
var bus: EventBus = null; | ||
|
||
describe('Channel test', function() { | ||
|
||
before(function () { | ||
bus = new EventBus(); | ||
}) | ||
|
||
after(function(){ | ||
bus = null; | ||
}) | ||
|
||
it('register', function() { | ||
bus.registerChannel("test", "test", (data: ContextChannel) => { | ||
expect(data.channel.name).to.eql("test"); | ||
}); | ||
var channel = bus.getChannel("test"); | ||
expect(channel.name).to.eql("test"); | ||
bus.emit("test","t"); | ||
bus.emit("test","t"); | ||
bus.emit("test","t"); | ||
}); | ||
|
||
it('remove channel', function() { | ||
var channel = bus.getChannel("test"); | ||
bus.removeChannel(channel); | ||
channel = bus.getChannel("test"); | ||
expect(channel).to.eql(undefined); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,25 @@ | ||
{ | ||
"compileOnSave": false, | ||
"compilerOptions": { | ||
"declaration": true, | ||
"emitDecoratorMetadata": true, | ||
"experimentalDecorators": true, | ||
"module": "commonjs", | ||
"moduleResolution": "node", | ||
"noEmitOnError": true, | ||
"noImplicitAny": false, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"rootDir": ".", | ||
"inlineSourceMap": true, | ||
"target": "es6", | ||
"inlineSources": true, | ||
"typeRoots": ["node_modules/@types"], | ||
"pretty": true, | ||
"outDir": "lib" | ||
}, | ||
"exclude": ["node_modules"], | ||
"files": [ | ||
"./index.ts" | ||
] | ||
} | ||
"compileOnSave": false, | ||
"compilerOptions": { | ||
"declaration": true, | ||
"emitDecoratorMetadata": true, | ||
"experimentalDecorators": true, | ||
"module": "commonjs", | ||
"moduleResolution": "node", | ||
"noEmitOnError": true, | ||
"noImplicitAny": false, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"rootDir": ".", | ||
"inlineSourceMap": true, | ||
"target": "es6", | ||
"inlineSources": true, | ||
"typeRoots": ["node_modules/@types"], | ||
"pretty": true, | ||
"outDir": "lib" | ||
}, | ||
"exclude": ["node_modules"], | ||
"files": [ | ||
"./index.ts" | ||
] | ||
} |