init project
This commit is contained in:
+8
@@ -0,0 +1,8 @@
|
||||
# Changelog
|
||||
|
||||
## [1.1.1](https://github.com/protobufjs/protobuf.js/compare/eventemitter-v1.1.0...eventemitter-v1.1.1) (2026-05-22)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* Backport misc utility hardening ([#2280](https://github.com/protobufjs/protobuf.js/issues/2280)) ([8a45c13](https://github.com/protobufjs/protobuf.js/commit/8a45c13d22ec2d05ab1b7935fcb5331ea59a9cd0))
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
Copyright (c) 2016, Daniel Wirtz All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of its author, nor the names of its contributors
|
||||
may be used to endorse or promote products derived from this software
|
||||
without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
@protobufjs/eventemitter
|
||||
========================
|
||||
[](https://www.npmjs.com/package/@protobufjs/eventemitter)
|
||||
|
||||
A minimal event emitter.
|
||||
|
||||
API
|
||||
---
|
||||
|
||||
* **new EventEmitter()**<br />
|
||||
Constructs a new event emitter instance.
|
||||
|
||||
* **EventEmitter#on(evt: `string`, fn: `function`, [ctx: `Object`]): `EventEmitter`**<br />
|
||||
Registers an event listener.
|
||||
|
||||
* **EventEmitter#off([evt: `string`], [fn: `function`]): `EventEmitter`**<br />
|
||||
Removes an event listener or any matching listeners if arguments are omitted.
|
||||
|
||||
* **EventEmitter#emit(evt: `string`, ...args: `*`): `EventEmitter`**<br />
|
||||
Emits an event by calling its listeners with the specified arguments.
|
||||
|
||||
**License:** [BSD 3-Clause License](https://opensource.org/licenses/BSD-3-Clause)
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
export = EventEmitter;
|
||||
|
||||
type EventEmitterListener = (...args: any[]) => {};
|
||||
|
||||
/**
|
||||
* Constructs a new event emitter instance.
|
||||
* @classdesc A minimal event emitter.
|
||||
* @memberof util
|
||||
* @constructor
|
||||
*/
|
||||
declare class EventEmitter {
|
||||
|
||||
/**
|
||||
* Constructs a new event emitter instance.
|
||||
* @classdesc A minimal event emitter.
|
||||
* @memberof util
|
||||
* @constructor
|
||||
*/
|
||||
constructor();
|
||||
|
||||
/**
|
||||
* Registers an event listener.
|
||||
* @param {string} evt Event name
|
||||
* @param {EventEmitterListener} fn Listener
|
||||
* @param {*} [ctx] Listener context
|
||||
* @returns {this} `this`
|
||||
*/
|
||||
public on(evt: string, fn: EventEmitterListener, ctx?: any): EventEmitter;
|
||||
|
||||
/**
|
||||
* Removes an event listener or any matching listeners if arguments are omitted.
|
||||
* @param {string} [evt] Event name. Removes all listeners if omitted.
|
||||
* @param {EventEmitterListener} [fn] Listener to remove. Removes all listeners of `evt` if omitted.
|
||||
* @returns {this} `this`
|
||||
*/
|
||||
public off(evt?: string, fn?: EventEmitterListener): EventEmitter;
|
||||
|
||||
/**
|
||||
* Emits an event by calling its listeners with the specified arguments.
|
||||
* @param {string} evt Event name
|
||||
* @param {...*} args Arguments
|
||||
* @returns {this} `this`
|
||||
*/
|
||||
public emit(evt: string, ...args: any[]): EventEmitter;
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
"use strict";
|
||||
module.exports = EventEmitter;
|
||||
|
||||
/**
|
||||
* Constructs a new event emitter instance.
|
||||
* @classdesc A minimal event emitter.
|
||||
* @memberof util
|
||||
* @constructor
|
||||
*/
|
||||
function EventEmitter() {
|
||||
|
||||
/**
|
||||
* Registered listeners.
|
||||
* @type {Object.<string,*>}
|
||||
* @private
|
||||
*/
|
||||
this._listeners = Object.create(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Event listener as used by {@link util.EventEmitter}.
|
||||
* @typedef EventEmitterListener
|
||||
* @type {function}
|
||||
* @param {...*} args Arguments
|
||||
* @returns {undefined}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Registers an event listener.
|
||||
* @param {string} evt Event name
|
||||
* @param {EventEmitterListener} fn Listener
|
||||
* @param {*} [ctx] Listener context
|
||||
* @returns {this} `this`
|
||||
*/
|
||||
EventEmitter.prototype.on = function on(evt, fn, ctx) {
|
||||
(this._listeners[evt] || (this._listeners[evt] = [])).push({
|
||||
fn : fn,
|
||||
ctx : ctx || this
|
||||
});
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Removes an event listener or any matching listeners if arguments are omitted.
|
||||
* @param {string} [evt] Event name. Removes all listeners if omitted.
|
||||
* @param {EventEmitterListener} [fn] Listener to remove. Removes all listeners of `evt` if omitted.
|
||||
* @returns {this} `this`
|
||||
*/
|
||||
EventEmitter.prototype.off = function off(evt, fn) {
|
||||
if (evt === undefined)
|
||||
this._listeners = Object.create(null);
|
||||
else {
|
||||
if (fn === undefined)
|
||||
this._listeners[evt] = [];
|
||||
else {
|
||||
var listeners = this._listeners[evt];
|
||||
if (!listeners)
|
||||
return this;
|
||||
for (var i = 0; i < listeners.length;)
|
||||
if (listeners[i].fn === fn)
|
||||
listeners.splice(i, 1);
|
||||
else
|
||||
++i;
|
||||
}
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Emits an event by calling its listeners with the specified arguments.
|
||||
* @param {string} evt Event name
|
||||
* @param {...*} args Arguments
|
||||
* @returns {this} `this`
|
||||
*/
|
||||
EventEmitter.prototype.emit = function emit(evt) {
|
||||
var listeners = this._listeners[evt];
|
||||
if (listeners) {
|
||||
var args = [],
|
||||
i = 1;
|
||||
for (; i < arguments.length;)
|
||||
args.push(arguments[i++]);
|
||||
for (i = 0; i < listeners.length;)
|
||||
listeners[i].fn.apply(listeners[i++].ctx, args);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "@protobufjs/eventemitter",
|
||||
"description": "A minimal event emitter.",
|
||||
"version": "1.1.1",
|
||||
"author": "Daniel Wirtz <dcode+protobufjs@dcode.io>",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/dcodeIO/protobuf.js.git"
|
||||
},
|
||||
"license": "BSD-3-Clause",
|
||||
"main": "index.js",
|
||||
"types": "index.d.ts",
|
||||
"devDependencies": {
|
||||
"istanbul": "^0.4.5",
|
||||
"tape": "^5.0.0"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "tape tests/*.js",
|
||||
"coverage": "istanbul cover node_modules/tape/bin/tape tests/*.js"
|
||||
}
|
||||
}
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
var tape = require("tape");
|
||||
|
||||
var EventEmitter = require("..");
|
||||
|
||||
tape.test("eventemitter", function(test) {
|
||||
|
||||
var ee = new EventEmitter();
|
||||
var fn;
|
||||
var ctx = {};
|
||||
|
||||
test.equal(Object.getPrototypeOf(ee._listeners), null, "should not inherit listener lookup keys");
|
||||
|
||||
test.doesNotThrow(function() {
|
||||
ee.emit("a", 1);
|
||||
ee.off();
|
||||
ee.off("a");
|
||||
ee.off("a", function() {});
|
||||
}, "should not throw if no listeners are registered");
|
||||
|
||||
test.equal(ee.on("a", function(arg1) {
|
||||
test.equal(this, ctx, "should be called with this = ctx");
|
||||
test.equal(arg1, 1, "should be called with arg1 = 1");
|
||||
}, ctx), ee, "should return itself when registering events");
|
||||
ee.emit("a", 1);
|
||||
|
||||
ee.off("a");
|
||||
test.same(Object.keys(ee._listeners), [ "a" ], "should keep the event key when calling off(evt)");
|
||||
test.same(ee._listeners.a, [], "should remove all listeners of the respective event when calling off(evt)");
|
||||
|
||||
ee.off();
|
||||
test.equal(Object.getPrototypeOf(ee._listeners), null, "should keep the listener table isolated when just calling off()");
|
||||
test.same(Object.keys(ee._listeners), [], "should remove all listeners when just calling off()");
|
||||
|
||||
ee.on("a", fn = function(arg1) {
|
||||
test.equal(this, ctx, "should be called with this = ctx");
|
||||
test.equal(arg1, 1, "should be called with arg1 = 1");
|
||||
}, ctx).emit("a", 1);
|
||||
|
||||
ee.off("a", fn);
|
||||
test.same(Object.keys(ee._listeners), [ "a" ], "should keep the event key when calling off(evt, fn)");
|
||||
test.same(ee._listeners.a, [], "should remove the exact listener when calling off(evt, fn)");
|
||||
|
||||
ee.on("a", function() {
|
||||
test.equal(this, ee, "should be called with this = ee");
|
||||
}).emit("a");
|
||||
|
||||
test.doesNotThrow(function() {
|
||||
ee.off("a", fn);
|
||||
}, "should not throw if no such listener is found");
|
||||
|
||||
test.test(test.name + " - special event names", function(test) {
|
||||
var ee = new EventEmitter();
|
||||
var calls = 0;
|
||||
|
||||
test.doesNotThrow(function() {
|
||||
ee.off("__proto__", function() {});
|
||||
}, "should not throw when removing an absent special event listener");
|
||||
|
||||
ee.on("__proto__", function(arg) {
|
||||
++calls;
|
||||
test.equal(arg, 1, "should pass arguments for __proto__ events");
|
||||
});
|
||||
ee.on("constructor", function(arg) {
|
||||
++calls;
|
||||
test.equal(arg, 2, "should pass arguments for constructor events");
|
||||
});
|
||||
ee.emit("__proto__", 1);
|
||||
ee.emit("constructor", 2);
|
||||
|
||||
test.equal(calls, 2, "should dispatch special event names");
|
||||
test.equal(Object.getPrototypeOf(ee._listeners), null, "should keep the listener table isolated");
|
||||
test.ok(Object.prototype.hasOwnProperty.call(ee._listeners, "__proto__"), "should store __proto__ as an own event key");
|
||||
test.ok(Object.prototype.hasOwnProperty.call(ee._listeners, "constructor"), "should store constructor as an own event key");
|
||||
|
||||
ee.off("__proto__");
|
||||
ee.off("constructor");
|
||||
test.same(ee._listeners.__proto__, [], "should clear __proto__ listeners");
|
||||
test.same(ee._listeners.constructor, [], "should clear constructor listeners");
|
||||
test.end();
|
||||
});
|
||||
|
||||
test.end();
|
||||
});
|
||||
Reference in New Issue
Block a user