diff options
| author | ruki <waruqi@gmail.com> | 2018-11-08 00:38:48 +0800 |
|---|---|---|
| committer | ruki <waruqi@gmail.com> | 2018-11-07 21:53:09 +0800 |
| commit | 26105034da4fcce7ac883c899d781f016559310d (patch) | |
| tree | c459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/nanobus | |
| parent | 2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff) | |
| download | xmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip | |
switch to vuepress
Diffstat (limited to 'node_modules/nanobus')
| -rw-r--r-- | node_modules/nanobus/.travis.yml | 17 | ||||
| -rw-r--r-- | node_modules/nanobus/LICENSE | 21 | ||||
| -rw-r--r-- | node_modules/nanobus/README.md | 105 | ||||
| -rw-r--r-- | node_modules/nanobus/bench.js | 70 | ||||
| -rw-r--r-- | node_modules/nanobus/index.d.ts | 20 | ||||
| -rw-r--r-- | node_modules/nanobus/index.js | 162 | ||||
| -rw-r--r-- | node_modules/nanobus/package.json | 38 | ||||
| -rw-r--r-- | node_modules/nanobus/test.js | 328 |
8 files changed, 761 insertions, 0 deletions
diff --git a/node_modules/nanobus/.travis.yml b/node_modules/nanobus/.travis.yml new file mode 100644 index 00000000..061f08ef --- /dev/null +++ b/node_modules/nanobus/.travis.yml @@ -0,0 +1,17 @@ +node_js: +- "4" +- "5" +- "6" +- "7" +sudo: false +language: node_js +script: "npm run test" +after_success: "npm i -g codecov && npm run coverage && codecov" +addons: + apt: + packages: + - xvfb +install: + - export DISPLAY=':99.0' + - Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 & + - npm install diff --git a/node_modules/nanobus/LICENSE b/node_modules/nanobus/LICENSE new file mode 100644 index 00000000..c03ab533 --- /dev/null +++ b/node_modules/nanobus/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 Yoshua Wuyts + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/node_modules/nanobus/README.md b/node_modules/nanobus/README.md new file mode 100644 index 00000000..d57f697a --- /dev/null +++ b/node_modules/nanobus/README.md @@ -0,0 +1,105 @@ +# nanobus [![stability][0]][1] +[![npm version][2]][3] [![build status][4]][5] [![test coverage][6]][7] +[![downloads][8]][9] [![js-standard-style][10]][11] + +Tiny message bus. + +## Usage +```js +var nanobus = require('nanobus') +var bus = nanobus() + +bus.on('foo', function (color) { + console.log('color is', color) +}) + +bus.emit('foo', 'blue') +``` + +## FAQ +### Why not use the Node API? +We had the requirement for a `*` event to catch all calls, and figured we could +improve the file size at the same time. This library is about 1/3rd the size of +Node's version. And it was easy to build, so yeah good enough of an excuse hah. + +### How do I listen for replies? +You can do this by using the `.once()` listener and establishing a convention +around naming schemas. + +```js +bus.on('foo', function (color) { + console.log('foo called') + bus.emit('foo:res') +}) + +bus.once('foo:res', function () { + console.log('response received') +}) +bus.emit('foo') +``` + +### When shouldn't I use this package? +If you're only writing code that runs inside Node and don't need a `'*'` +listener, consider using the built-in event emitter API instead. + +### Are the emitters asynchronous? +No. If you're interested in doing that, use something like +[nanotick](https://github.com/yoshuawuyts/nanotick) to batch events and ensure +they run asynchronously. + +## API +### `bus = nanobus([name])` +Create a new `nanobus` instance. Optionally takes a name that will be used for +tracing in the browser using the `performance.mark` / `performance.measure` +API. + +### `bus.emit(eventName, [data])` +Emit an event. Arbitrary data can optionally be passed as an argument. `'*'` +listeners run after named listeners. + +### `bus.on(eventName, listener([data]))` +### `bus.addListener(eventName, listener([data]))` +Listen to an event. If the event name is `'*'` the listener signature is +`listener(eventName, [data], [performanceTimingId])`. + +### `bus.prependListener(eventName, listener([data]))` +Listen to an event, but make sure it's pushed to the start of the listener +queue. If the event name is `'*'` the listener signature is +`listener(eventName, [data])`. + +### `bus.once(eventName, listener([data]))` +Listen to an event, and clear it after it's been called once. If the event +name is `'*'` the listener signature is +`listener(eventName, [data], [performanceTimingId])`. + +### `bus.prependOnceListener(eventName, listener([data]))` +Listen to an event, and clear it after it's been called once. If the event +name is `'*'` the listener signature is `listener(eventName, [data])`. + +### `bus.removeListener(eventName, listener)` +Remove a specific listener to an event. + +### `listeners = bus.listeners(eventName)` +Return all listeners for a given event. `'*'` listeners are not included in +this list. Use `bus.listeners('*')` to get a list of `'*'` listeners. + +### `bus.removeAllListeners([eventName])` +Remove all listeners to an event. If no event name is passed, removes all +listeners on the message bus. `'*'` listeners are not removed unless +`eventName` is `*` or no event name is passed. + +## License +[MIT](https://tldrlegal.com/license/mit-license) + +[0]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square +[1]: https://nodejs.org/api/documentation.html#documentation_stability_index +[2]: https://img.shields.io/npm/v/nanobus.svg?style=flat-square +[3]: https://npmjs.org/package/nanobus +[4]: https://img.shields.io/travis/choojs/nanobus/master.svg?style=flat-square +[5]: https://travis-ci.org/choojs/nanobus +[6]: https://img.shields.io/codecov/c/github/choojs/nanobus/master.svg?style=flat-square +[7]: https://codecov.io/github/choojs/nanobus +[8]: http://img.shields.io/npm/dm/nanobus.svg?style=flat-square +[9]: https://npmjs.org/package/nanobus +[10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square +[11]: https://github.com/feross/standard diff --git a/node_modules/nanobus/bench.js b/node_modules/nanobus/bench.js new file mode 100644 index 00000000..d1ff0949 --- /dev/null +++ b/node_modules/nanobus/bench.js @@ -0,0 +1,70 @@ +var bench = require('nanobench') +var nanobus = require('./') +var assert = require('assert') + +bench('emit 200.000 times', function (b) { + var bus = nanobus() + var obj = { bin: 'baz' } + bus.on('foo:bar', function (data) { + assert.equal(data, obj, 'data was same') + }) + bus.on('beep:boop', function (data) { + assert.equal(data, undefined) + }) + + b.start() + for (var i = 0; i < 200000; i++) { + bus.emit('foo:bar', obj) + bus.emit('beep:boop') + } + b.end() +}) + +bench('emit once 200.000 times', function (b) { + var bus = nanobus() + bus.once('foo:bar', function (data) { + assert(true) + }) + + b.start() + for (var i = 0; i < 200000; i++) { + bus.emit('foo:bar') + bus.emit('foo:bar') + } + b.end() +}) + +bench('run multiple * tests 200.000 times', function (b) { + var bus = nanobus() + var i = 0 + + bus.on('foo:bar', handler) + bus.on('bin:baz', handler) + bus.on('*', handler) + + b.start() + for (var j = 0; j < 200000; j++) { + bus.emit('foo:bar') + bus.emit('bin:baz') + bus.removeAllListeners('bin:baz') + bus.emit('bin:baz') + bus.removeListener('*', handler) + bus.emit('foo:bar') + bus.on('*', handler) + bus.emit('foo:bar') + bus.removeAllListeners('*') + bus.emit('foo:bar') + bus.on('*', handler) + bus.emit('foo:bar') + bus.removeAllListeners() + bus.once('*', handler) + bus.emit('foo:bar') + bus.emit('foo:bar') + bus.removeAllListeners() + } + b.end() + + function handler (data) { + assert(++i) + } +}) diff --git a/node_modules/nanobus/index.d.ts b/node_modules/nanobus/index.d.ts new file mode 100644 index 00000000..15aa0b5b --- /dev/null +++ b/node_modules/nanobus/index.d.ts @@ -0,0 +1,20 @@ +type NanobusListener = (...args: any[]) => void +type AttachListener = (eventName:string, listener:NanobusListener) => Nanobus + +declare class Nanobus { + private _name:string + private _startListeners:Array<any> + private _listeners:{[key:string]:any} + constructor(name?:string) + emit:(eventName:string, ...args:any[]) => Nanobus + on:AttachListener + addListener:AttachListener + prependListener:AttachListener + once:AttachListener + prependOnceListener:AttachListener + removeListener:AttachListener + removeAllListeners(eventName:string):Nanobus + listeners(eventName:string):NanobusListener[] +} + +export = Nanobus
\ No newline at end of file diff --git a/node_modules/nanobus/index.js b/node_modules/nanobus/index.js new file mode 100644 index 00000000..ccd01295 --- /dev/null +++ b/node_modules/nanobus/index.js @@ -0,0 +1,162 @@ +var splice = require('remove-array-items') +var nanotiming = require('nanotiming') +var assert = require('assert') + +module.exports = Nanobus + +function Nanobus (name) { + if (!(this instanceof Nanobus)) return new Nanobus(name) + + this._name = name || 'nanobus' + this._starListeners = [] + this._listeners = {} +} + +Nanobus.prototype.emit = function (eventName) { + assert.equal(typeof eventName, 'string', 'nanobus.emit: eventName should be type string') + + var data = [] + for (var i = 1, len = arguments.length; i < len; i++) { + data.push(arguments[i]) + } + + var emitTiming = nanotiming(this._name + "('" + eventName + "')") + var listeners = this._listeners[eventName] + if (listeners && listeners.length > 0) { + this._emit(this._listeners[eventName], data) + } + + if (this._starListeners.length > 0) { + this._emit(this._starListeners, eventName, data, emitTiming.uuid) + } + emitTiming() + + return this +} + +Nanobus.prototype.on = Nanobus.prototype.addListener = function (eventName, listener) { + assert.equal(typeof eventName, 'string', 'nanobus.on: eventName should be type string') + assert.equal(typeof listener, 'function', 'nanobus.on: listener should be type function') + + if (eventName === '*') { + this._starListeners.push(listener) + } else { + if (!this._listeners[eventName]) this._listeners[eventName] = [] + this._listeners[eventName].push(listener) + } + return this +} + +Nanobus.prototype.prependListener = function (eventName, listener) { + assert.equal(typeof eventName, 'string', 'nanobus.prependListener: eventName should be type string') + assert.equal(typeof listener, 'function', 'nanobus.prependListener: listener should be type function') + + if (eventName === '*') { + this._starListeners.unshift(listener) + } else { + if (!this._listeners[eventName]) this._listeners[eventName] = [] + this._listeners[eventName].unshift(listener) + } + return this +} + +Nanobus.prototype.once = function (eventName, listener) { + assert.equal(typeof eventName, 'string', 'nanobus.once: eventName should be type string') + assert.equal(typeof listener, 'function', 'nanobus.once: listener should be type function') + + var self = this + this.on(eventName, once) + function once () { + listener.apply(self, arguments) + self.removeListener(eventName, once) + } + return this +} + +Nanobus.prototype.prependOnceListener = function (eventName, listener) { + assert.equal(typeof eventName, 'string', 'nanobus.prependOnceListener: eventName should be type string') + assert.equal(typeof listener, 'function', 'nanobus.prependOnceListener: listener should be type function') + + var self = this + this.prependListener(eventName, once) + function once () { + listener.apply(self, arguments) + self.removeListener(eventName, once) + } + return this +} + +Nanobus.prototype.removeListener = function (eventName, listener) { + assert.equal(typeof eventName, 'string', 'nanobus.removeListener: eventName should be type string') + assert.equal(typeof listener, 'function', 'nanobus.removeListener: listener should be type function') + + if (eventName === '*') { + this._starListeners = this._starListeners.slice() + return remove(this._starListeners, listener) + } else { + if (typeof this._listeners[eventName] !== 'undefined') { + this._listeners[eventName] = this._listeners[eventName].slice() + } + + return remove(this._listeners[eventName], listener) + } + + function remove (arr, listener) { + if (!arr) return + var index = arr.indexOf(listener) + if (index !== -1) { + splice(arr, index, 1) + return true + } + } +} + +Nanobus.prototype.removeAllListeners = function (eventName) { + if (eventName) { + if (eventName === '*') { + this._starListeners = [] + } else { + this._listeners[eventName] = [] + } + } else { + this._starListeners = [] + this._listeners = {} + } + return this +} + +Nanobus.prototype.listeners = function (eventName) { + var listeners = eventName !== '*' + ? this._listeners[eventName] + : this._starListeners + + var ret = [] + if (listeners) { + var ilength = listeners.length + for (var i = 0; i < ilength; i++) ret.push(listeners[i]) + } + return ret +} + +Nanobus.prototype._emit = function (arr, eventName, data, uuid) { + if (typeof arr === 'undefined') return + if (arr.length === 0) return + if (data === undefined) { + data = eventName + eventName = null + } + + if (eventName) { + if (uuid !== undefined) { + data = [eventName].concat(data, uuid) + } else { + data = [eventName].concat(data) + } + } + + var length = arr.length + for (var i = 0; i < length; i++) { + var listener = arr[i] + listener.apply(listener, data) + } +} diff --git a/node_modules/nanobus/package.json b/node_modules/nanobus/package.json new file mode 100644 index 00000000..2feef517 --- /dev/null +++ b/node_modules/nanobus/package.json @@ -0,0 +1,38 @@ +{ + "name": "nanobus", + "description": "Tiny message bus", + "version": "4.3.5", + "browser": { + "assert": "nanoassert" + }, + "dependencies": { + "nanoassert": "^1.1.0", + "nanotiming": "^7.2.0", + "remove-array-items": "^1.0.0" + }, + "devDependencies": { + "browserify": "^15.2.0", + "dependency-check": "^3.0.0", + "nanobench": "^2.1.0", + "nyc": "^11.4.1", + "standard": "^10.0.3", + "tap-spec": "^4.1.1", + "tape": "^4.6.3", + "tape-run": "^3.0.1" + }, + "keywords": [ + "emit", + "event" + ], + "license": "MIT", + "repository": "choojs/nanobus", + "scripts": { + "bench": "node bench.js", + "coverage": "nyc report --reporter=text-lcov > coverage.lcov", + "deps": "dependency-check . && dependency-check . --extra --no-dev -i nanoassert", + "start": "node .", + "test": "npm run test:node && npm run test:browser", + "test:browser": "browserify test.js | tape-run | tap-spec", + "test:node": "standard && npm run deps && nyc node test.js | tap-spec" + } +} diff --git a/node_modules/nanobus/test.js b/node_modules/nanobus/test.js new file mode 100644 index 00000000..b411e842 --- /dev/null +++ b/node_modules/nanobus/test.js @@ -0,0 +1,328 @@ +var tape = require('tape') +var nanobus = require('./') + +tape('nanobus', function (t) { + t.test('should assert input types', function (t) { + t.plan(7) + var bus = nanobus() + t.throws(bus.emit.bind(bus), /string/) + t.throws(bus.on.bind(bus), /string/) + t.throws(bus.on.bind(bus, 'foo'), /function/) + t.throws(bus.once.bind(bus), /string/) + t.throws(bus.once.bind(bus, 'foo'), /function/) + t.throws(bus.removeListener.bind(bus), /string/) + t.throws(bus.removeListener.bind(bus, 'foo'), /function/) + }) + + t.test('should emit messages', function (t) { + t.plan(4) + var bus = nanobus() + var obj = { bin: 'baz' } + bus.on('foo:bar', function (data) { + t.equal(data, obj, 'data was same') + }) + + bus.emit('foo:bar', obj) + + bus.on('beep:boop', function (data) { + t.equal(data, undefined) + }) + + bus.emit('beep:boop') + + bus.on('baz:floop', function (arg1, arg2) { + t.equal(arg1, 'arg1', 'data was same') + t.equal(arg2, 'arg2', 'data was same') + }) + + bus.emit('baz:floop', 'arg1', 'arg2') + }) + + t.test('should prepend listeners', function (t) { + t.plan(4) + var i = 0 + var bus = nanobus() + bus.on('foo:bar', function (data) { + t.equal(i, 1) + }) + + bus.prependListener('foo:bar', function (data) { + t.equal(i, 0) + i++ + }) + + bus.emit('foo:bar') + + bus.on('*', function (data) { + t.equal(i, 1) + }) + + bus.prependListener('*', function (eventName, data) { + t.equal(i, data) + i++ + }) + + i = 0 + bus.emit('bar:baz', i) + }) + + t.test('should prepend once listeners', function (t) { + t.plan(3) + var i = 0 + var bus = nanobus() + bus.on('foo:bar', function (data) { + t.equal(i, 1) + }) + + bus.prependOnceListener('foo:bar', function (data) { + t.equal(i, 0) + i++ + }) + + bus.emit('foo:bar') + bus.emit('foo:bar') + }) + + t.test('should emit messages once', function (t) { + t.plan(1) + var bus = nanobus() + bus.once('foo:bar', function (data) { + t.pass('called') + }) + + bus.emit('foo:bar') + bus.emit('foo:bar') + }) + + t.test('should properly emit messages when using both once() and on() ', function (t) { + t.plan(2) + var bus = nanobus() + var i = 0 + + bus.once('foo:bar', function onceIncrement () { + i++ + }) + + bus.on('foo:bar', function onIncrement () { + i++ + }) + + bus.once('*', function wildcardOnceIncrement () { + i++ + }) + + bus.on('*', function wildcardOnIncrement () { + i++ + }) + + bus.emit('foo:bar') + t.equal(i, 4, 'incremented by once() and on()') + + bus.emit('foo:bar') + t.equal(i, 6, 'incremented by on() only') + }) + + t.test('should trigger wildcard once', function (t) { + t.plan(3) + var bus = nanobus() + bus.once('*', function (data) { + t.pass('called') + }) + + bus.on('foo:bar', function (data) { + t.pass('called foo:bar') + }) + + bus.on('foo:baz', function (data) { + t.pass('called foo:baz') + }) + + bus.emit('foo:bar') + bus.emit('foo:baz') + }) + + t.test('should be able to remove listeners', function (t) { + t.plan(3) + var bus = nanobus() + bus.on('foo:bar', goodHandler) + bus.on('foo:bar', badHandler) + bus.removeListener('foo:bar', badHandler) + bus.emit('foo:bar') + + bus.once('foo:bar', goodHandler) + bus.removeListener('foo:bar', onceHandler) + bus.emit('foo:bar') + + function goodHandler (data) { + t.pass('called') + } + + function onceHandler (data) { + t.pass('called') + } + + function badHandler (data) { + t.fail('oh no!') + } + }) + + t.test('should be able to remove all listeners', function (t) { + t.plan(1) + var bus = nanobus() + var i = 0 + + bus.on('foo:bar', handler) + bus.on('bin:baz', handler) + bus.removeAllListeners() + bus.emit('foo:bar') + bus.emit('bin:baz') + + t.equal(i, 0, 'no events called') + + function handler (data) { + i++ + } + }) + + t.test('should be able to remove all listeners for an event', function (t) { + t.plan(1) + var bus = nanobus() + var i = 0 + + bus.on('foo:bar', handler) + bus.on('bin:baz', handler) + bus.removeAllListeners('bin:baz') + bus.emit('foo:bar') + bus.emit('bin:baz') + + t.equal(i, 1, '1 event called') + + function handler (data) { + i++ + } + }) + + t.test('should be able to have * listeners', function (t) { + t.plan(12) + var bus = nanobus() + var i = 0 + + bus.on('foo:bar', handler) + bus.on('bin:baz', handler) + bus.on('*', handler) + + bus.emit('foo:bar') + t.equal(i, 2, 'count 2') + + bus.emit('bin:baz') + t.equal(i, 4, 'count 4') + + bus.removeAllListeners('bin:baz') + bus.emit('bin:baz') + t.equal(i, 5, 'count 5') + + bus.removeListener('*', handler) + bus.emit('foo:bar') + t.equal(i, 6, 'count 6') + + bus.on('*', handler) + bus.emit('foo:bar') + t.equal(i, 8, 'count 8') + + bus.removeAllListeners('*') + bus.emit('foo:bar') + t.equal(i, 9, 'count 9') + + bus.on('*', handler) + bus.emit('foo:bar') + t.equal(i, 11, 'count 11') + + bus.removeAllListeners() + t.equal(i, 11, 'count 11') + + bus.once('*', handler) + bus.emit('foo:bar') + t.equal(i, 12, 'count 12') + bus.emit('foo:bar') + t.equal(i, 12, 'count 12') + + bus.removeAllListeners() + t.equal(i, 12, 'count 12') + + bus.on('*', starHandler) + bus.emit('star:event', i) + bus.removeAllListeners() + + function handler (data) { + i++ + } + + function starHandler (eventName, data) { + t.equal(data, i, 'data was same') + } + }) + + t.test('should be able to remove listeners that have not been attached', function (t) { + var bus = nanobus() + + t.doesNotThrow(function () { + bus.removeListener('yay', handler) + }, 'removes unattched "yay" event') + t.doesNotThrow(function () { + bus.removeListener('*', handler) + }, 'removes unattached "*" event') + t.end() + + function handler () {} + }) + + t.test('should be able to get an array of listeners', function (t) { + t.plan(2) + var bus = nanobus() + + bus.on('foo', bar) + bus.on('foo', baz) + + t.deepEqual(bus.listeners('foo'), [bar, baz]) + + bus.on('*', bar) + bus.on('*', baz) + + t.deepEqual(bus.listeners('foo'), [bar, baz]) + + function bar (data) {} + function baz (data) {} + }) + + t.test('should be able to trigger multiple listeners with same args', function (t) { + t.plan(6) + var obj = {foo: 'bar'} + var bus = nanobus() + + bus.on('foo', function (data) { + t.deepEqual(data, obj) + }) + + bus.on('foo', function (data) { + t.deepEqual(data, obj) + }) + + bus.on('foo', function (data) { + t.deepEqual(data, obj) + }) + + bus.on('*', function (name, data) { + t.deepEqual(data, obj) + }) + + bus.on('*', function (name, data) { + t.deepEqual(data, obj) + }) + + bus.on('*', function (name, data) { + t.deepEqual(data, obj) + }) + + bus.emit('foo', obj) + }) +}) |
