@braintree/event-emitter
Advanced tools
Comparing version 0.4.1 to 1.0.0
@@ -0,1 +1,21 @@ | ||
# 1.0.0 | ||
- in Typescript, `on` can specify event payload type as a Generic | ||
```typescript | ||
type EventPayload = { | ||
foo: string; | ||
}; | ||
emitter.on<EventPayload>("event-name", (payload) => { | ||
// payload is of type EventPayload | ||
}); | ||
``` | ||
_Breaking Changes_ | ||
- remove `createChild` helper | ||
- `_emit` has been renamed to `emit` | ||
- `emit` now only takes one event payload argument instead of any number of arguments | ||
# 0.4.1 | ||
@@ -2,0 +22,0 @@ |
@@ -0,10 +1,10 @@ | ||
declare type CallbackFunction<T = unknown> = (eventPayload: T) => void; | ||
declare class EventEmitter { | ||
_events: Record<string, Function[]>; | ||
_events: Record<string, CallbackFunction[]>; | ||
constructor(); | ||
on(event: string, callback: Function): void; | ||
off(event: string, callback: Function): void; | ||
_emit(event: string, ...args: any[]): void; | ||
on<T>(event: string, callback: CallbackFunction<T>): void; | ||
off(event: string, callback: CallbackFunction): void; | ||
emit(event: string, eventPayload?: unknown): void; | ||
hasListener(event: string): boolean; | ||
static createChild(ChildObject: any): void; | ||
} | ||
export = EventEmitter; |
@@ -22,7 +22,3 @@ "use strict"; | ||
}; | ||
EventEmitter.prototype._emit = function (event) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
EventEmitter.prototype.emit = function (event, eventPayload) { | ||
var eventCallbacks = this._events[event]; | ||
@@ -33,3 +29,3 @@ if (!eventCallbacks) { | ||
eventCallbacks.forEach(function (callback) { | ||
callback.apply(void 0, args); | ||
callback(eventPayload); | ||
}); | ||
@@ -44,9 +40,4 @@ }; | ||
}; | ||
EventEmitter.createChild = function (ChildObject) { | ||
ChildObject.prototype = Object.create(EventEmitter.prototype, { | ||
constructor: ChildObject, | ||
}); | ||
}; | ||
return EventEmitter; | ||
}()); | ||
module.exports = EventEmitter; |
{ | ||
"name": "@braintree/event-emitter", | ||
"version": "0.4.1", | ||
"version": "1.0.0", | ||
"description": "A simple event emitter.", | ||
@@ -31,9 +31,9 @@ "main": "dist/event-emitter.js", | ||
"devDependencies": { | ||
"@types/jest": "^26.0.9", | ||
"eslint": "^7.6.0", | ||
"eslint-config-braintree": "^5.0.0-typescript-prep-rc.17", | ||
"jest": "^26.3.0", | ||
"prettier": "^2.0.5", | ||
"ts-jest": "^26.1.4", | ||
"typescript": "^3.9.7" | ||
"@types/jest": "^26.0.24", | ||
"eslint": "^7.31.0", | ||
"eslint-config-braintree": "^5.0.0-typescript-prep-rc.18", | ||
"jest": "^27.0.6", | ||
"prettier": "^2.3.2", | ||
"ts-jest": "^27.0.3", | ||
"typescript": "^4.3.5" | ||
}, | ||
@@ -40,0 +40,0 @@ "jest": { |
@@ -20,9 +20,9 @@ # event-emitter | ||
```js | ||
var EventEmitter = require("@braintree/event-emitter"); | ||
import EventEmitter from "@braintree/event-emitter"; | ||
function MyClass() { | ||
EventEmitter.call(this); | ||
class MyClass extends EventEmitter() { | ||
// my class definition | ||
} | ||
MyClass.prototype = EventEmitter.createChild(MyClass); | ||
const emitter = new MyClass(); | ||
``` | ||
@@ -33,9 +33,7 @@ | ||
```js | ||
var obj = new MyClass(); | ||
obj.on("event-name", function (data) { | ||
emitter.on("event-name", function (data) { | ||
console.log("called with", data.payload, "!"); | ||
}); | ||
obj._emit("event-name", { payload: "foo" }); // logs "called with foo!" | ||
emitter.emit("event-name", { payload: "foo" }); // logs "called with foo!" | ||
``` | ||
@@ -46,9 +44,8 @@ | ||
```js | ||
var obj = new MyClass(); | ||
var cb = function () {}; | ||
const cb = function () {}; | ||
obj.on("event-name", cb); | ||
obj.off("event-name", cb); | ||
emitter.on("event-name", cb); | ||
emitter.off("event-name", cb); | ||
obj._emit("event-name", { payload: "foo" }); // cb is not called | ||
emitter.emit("event-name", { payload: "foo" }); // cb is not called | ||
``` | ||
@@ -55,0 +52,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
5462
0
50
55