This repository has been archived by the owner on Oct 2, 2024. It is now read-only.
-
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.
- Loading branch information
0 parents
commit 9787ff1
Showing
10 changed files
with
2,912 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"presets": [ | ||
[ | ||
"env", | ||
{ | ||
"targets": { | ||
"node": 4, | ||
"browsers": "last 2 versions" | ||
} | ||
} | ||
] | ||
], | ||
"plugins": [ | ||
"add-module-exports" | ||
], | ||
"sourceMaps": true | ||
} |
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,13 @@ | ||
# Editor configuration, see http://editorconfig.org | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
indent_style = space | ||
indent_size = 2 | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
|
||
[*.md] | ||
max_line_length = off | ||
trim_trailing_whitespace = false |
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 @@ | ||
extends: semistandard |
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,27 @@ | ||
# Created by .ignore support plugin (hsz.mobi) | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
pids | ||
*.pid | ||
*.seed | ||
*.pid.lock | ||
lib-cov | ||
coverage | ||
.nyc_output | ||
.grunt | ||
bower_components | ||
.lock-wscript | ||
build/Release | ||
node_modules/ | ||
jspm_packages/ | ||
typings/ | ||
.npm | ||
.eslintcache | ||
.node_repl_history | ||
*.tgz | ||
.yarn-integrity | ||
.env | ||
dist/ |
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,13 @@ | ||
Copyright 2017 Christopher Hiller <boneskull@boneskull.com> (https://boneskull.com/) | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. |
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,49 @@ | ||
# eventemittable | ||
|
||
> A composable `EventEmitter` for Stampit v3 | ||
## Install | ||
|
||
```bash | ||
$ npm install stampit eventemittable --save | ||
``` | ||
|
||
[Stampit](https://npm.im/stampit) v3 or greater is a peer dependency of this module. | ||
|
||
## Usage | ||
|
||
```js | ||
import EventEmittable from 'eventemittable'; | ||
import stampit from 'stampit'; | ||
|
||
// some stamp | ||
const User = stampit.init((opts, {instance}) => { | ||
if (opts.name) { | ||
instance.name = opts.name; | ||
} | ||
}) | ||
.props({ | ||
name: { | ||
first: "(unnamed)", | ||
last: "(unnamed)" | ||
} | ||
}); | ||
|
||
// an emittable version of some stamp | ||
const EmittableUser = User.compose(EventEmittable); | ||
|
||
// elsewhere... | ||
const user = EmittableUser({name: {first: 'Guy', last: 'Fieri'}}); | ||
user.on('name', name => { | ||
console.log(`${name.first} ${name.last}`); | ||
}); | ||
user.emit('name', user.name); // 'Guy Fieri' | ||
``` | ||
|
||
## Notes | ||
|
||
Apologies to [koresar](https://github.com/koresar). :D | ||
|
||
## License | ||
|
||
© 2017 [Christopher Hiller](https://boneskull.com). Licensed Apache-2.0. |
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,56 @@ | ||
{ | ||
"name": "eventemittable", | ||
"version": "0.0.0", | ||
"main": "dist/eventemittable.js", | ||
"module": "src/eventemittable.js", | ||
"author": "Christopher Hiller <boneskull@boneskull.com> (https://boneskull.com/)", | ||
"license": "Apache-2.0", | ||
"description": "A composable EventEmitter for Stampit v3", | ||
"homepage": "https://github.com/boneskull/eventemittable", | ||
"files": [ | ||
"dist", | ||
"src" | ||
], | ||
"keywords": [ | ||
"stampit", | ||
"events", | ||
"eventemitter", | ||
"eventemittable", | ||
"stamp", | ||
"compose", | ||
"composable", | ||
"emit", | ||
"emitter" | ||
], | ||
"devDependencies": { | ||
"babel-cli": "^6.23.0", | ||
"babel-plugin-add-module-exports": "^0.2.1", | ||
"babel-preset-env": "^1.2.0", | ||
"babel-register": "^6.23.0", | ||
"eslint": "^3.19.0", | ||
"eslint-config-semistandard": "^11.0.0", | ||
"eslint-config-standard": "^10.2.1", | ||
"eslint-plugin-import": "^2.2.0", | ||
"eslint-plugin-node": "^4.2.2", | ||
"eslint-plugin-promise": "^3.5.0", | ||
"eslint-plugin-standard": "^3.0.1", | ||
"mocha": "^3.3.0", | ||
"sinon": "^2.1.0", | ||
"stampit": "^3.1.2", | ||
"unexpected": "^10.27.0", | ||
"unexpected-sinon": "^10.7.1" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"peerDependencies": { | ||
"stampit": "^3.0.0" | ||
}, | ||
"scripts": { | ||
"pretest": "eslint '*.js' src test --fix", | ||
"build": "babel --source-maps --out-dir dist src", | ||
"build:watch": "babel --source-maps --watch --out-dir dist src", | ||
"test": "mocha --require babel-register" | ||
}, | ||
"dependencies": {} | ||
} |
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,9 @@ | ||
import stampit from 'stampit'; | ||
import {EventEmitter} from 'events'; | ||
|
||
// https://github.com/stampit-org/stampit/blob/master/advanced-examples/event-emitter.js | ||
export default stampit() | ||
.compose(stampit.composers(({stamp}) => { | ||
stamp.compose.methods = stamp.compose.methods || Object.create(null); | ||
Object.setPrototypeOf(stamp.compose.methods, EventEmitter.prototype); | ||
})); |
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,204 @@ | ||
/* eslint-env mocha */ | ||
|
||
import {EventEmitter} from 'events'; | ||
import EventEmittable from '../src/eventemittable'; | ||
import unexpected from 'unexpected'; | ||
import usinon from 'unexpected-sinon'; | ||
import sinon from 'sinon'; | ||
import stampit from 'stampit'; | ||
|
||
const expect = unexpected.clone() | ||
.use(usinon); | ||
|
||
describe('EventEmittable', function () { | ||
let sbx; | ||
|
||
beforeEach(function () { | ||
sbx = sinon.sandbox.create(); | ||
}); | ||
|
||
afterEach(function () { | ||
sbx.restore(); | ||
}); | ||
|
||
describe('method', function () { | ||
let ee; | ||
|
||
beforeEach(function () { | ||
ee = EventEmittable(); | ||
}); | ||
|
||
describe('once()', function () { | ||
let listener; | ||
|
||
beforeEach(function () { | ||
listener = sbx.stub(); | ||
}); | ||
|
||
it('should call listener once', function () { | ||
ee.once('foo', listener); | ||
ee.emit('foo'); | ||
ee.emit('foo'); | ||
expect(listener, 'was called once'); | ||
}); | ||
}); | ||
|
||
describe('on()', function () { | ||
let listener; | ||
|
||
beforeEach(function () { | ||
listener = sbx.stub(); | ||
}); | ||
|
||
it('should call listener', function () { | ||
ee.on('foo', listener); | ||
ee.emit('foo'); | ||
ee.emit('foo'); | ||
expect(listener, 'was called twice'); | ||
}); | ||
}); | ||
|
||
describe('emit()', function () { | ||
let listeners; | ||
|
||
beforeEach(function () { | ||
listeners = [ | ||
sbx.stub(), | ||
sbx.stub(), | ||
sbx.stub() | ||
]; | ||
listeners.forEach(listener => { | ||
ee.once('foo', listener); | ||
}); | ||
}); | ||
|
||
it('should emit to multiple listeners', function () { | ||
ee.emit('foo'); | ||
|
||
listeners.forEach(listener => { | ||
expect(listener, 'was called once'); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('removeListener()', function () { | ||
let listeners; | ||
|
||
beforeEach(function () { | ||
listeners = [ | ||
sbx.stub(), | ||
sbx.stub() | ||
]; | ||
listeners.forEach(listener => { | ||
ee.once('foo', listener); | ||
}); | ||
}); | ||
|
||
it('should remove a listener from the event', function () { | ||
ee.removeListener('foo', listeners[1]); | ||
ee.emit('foo'); | ||
expect(listeners[0], 'was called once'); | ||
expect(listeners[1], 'was not called'); | ||
}); | ||
}); | ||
|
||
describe('removeAllListeners()', function () { | ||
let listeners; | ||
|
||
beforeEach(function () { | ||
listeners = [ | ||
sbx.stub(), | ||
sbx.stub() | ||
]; | ||
listeners.forEach(listener => { | ||
ee.once('foo', listener); | ||
ee.once('bar', listener); | ||
}); | ||
}); | ||
|
||
describe('when provided an event', function () { | ||
it('should remove all listeners from a single event', function () { | ||
ee.removeAllListeners('foo'); | ||
ee.emit('foo'); | ||
listeners.forEach(listener => { | ||
expect(listener, 'was not called'); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when not provided an event', function () { | ||
it('should remove all listeners from all events', function () { | ||
ee.removeAllListeners(); | ||
ee.emit('foo'); | ||
ee.emit('bar'); | ||
listeners.forEach(listener => { | ||
expect(listener, 'was not called'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('getMaxListeners()', function () { | ||
it('should return the (default) maximum number of listeners', | ||
function () { | ||
expect(ee.getMaxListeners(), | ||
'to equal', | ||
EventEmitter.defaultMaxListeners); | ||
}); | ||
}); | ||
|
||
describe('setMaxListeners()', function () { | ||
it('should set the maximum number of listeners', function () { | ||
ee.setMaxListeners(2); | ||
expect(ee.getMaxListeners(), 'to equal', 2); | ||
}); | ||
}); | ||
|
||
describe('listeners()', function () { | ||
let listeners; | ||
|
||
beforeEach(function () { | ||
listeners = [ | ||
sbx.stub(), | ||
sbx.stub() | ||
]; | ||
listeners.forEach(listener => { | ||
ee.once('foo', listener); | ||
}); | ||
}); | ||
|
||
it('should return an array of listeners for an event', function () { | ||
expect(ee.listeners('foo'), 'to satisfy', listeners); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should throw if max listeners exceeded', function () { | ||
const ee = EventEmittable(); | ||
sbx.stub(process, 'emitWarning'); | ||
ee.setMaxListeners(2); | ||
ee.on('foo', sbx.stub()); | ||
ee.on('foo', sbx.stub()); | ||
ee.on('foo', sbx.stub()); | ||
expect(process.emitWarning, 'was called once'); | ||
}); | ||
|
||
it('should allow composition', function () { | ||
const MyStamp = stampit({ | ||
methods: { | ||
foo () { | ||
this.emit('foo'); | ||
} | ||
} | ||
}) | ||
.compose(EventEmittable); | ||
|
||
const stamp = MyStamp(); | ||
const listener = sbx.stub(); | ||
stamp.on('foo', listener); | ||
stamp.foo(); | ||
expect(listener, 'was called once'); | ||
}); | ||
|
||
// guess I'm convinced. | ||
}); |
Oops, something went wrong.