browserify / events Goto Github PK
View Code? Open in Web Editor NEWNode's event emitter for all engines.
License: MIT License
Node's event emitter for all engines.
License: MIT License
Hi the vulnerability CVE-2018-25076 is found in the version 3.3.0 with the grype vulnerability scanner. There is prediction to fix this this?
https://nvd.nist.gov/vuln/detail/CVE-2018-25076
This happened when on of my Store in my Flux application emit a 'CHANGE' event to its listeners.
Is there a good reason not to handle the exceptions here?
EventEmitter.prototype.emit = function(type) {
var er, handler, len, args, i, listeners;
if (!this._events)
this._events = {};
// If there is no 'error' event listener then throw.
if (type === 'error') {
if (!this._events.error ||
(isObject(this._events.error) && !this._events.error.length)) {
er = arguments[1];
if (er instanceof Error) {
throw er; // Unhandled 'error' event
}
throw TypeError('Uncaught, unspecified "error" event.');
}
}
handler = this._events[type];
if (isUndefined(handler))
return false;
if (isFunction(handler)) {
switch (arguments.length) {
// fast cases
case 1:
handler.call(this);
break;
case 2:
handler.call(this, arguments[1]);
break;
case 3:
handler.call(this, arguments[1], arguments[2]);
break;
// slower
default:
args = Array.prototype.slice.call(arguments, 1);
handler.apply(this, args);
}
} else if (isObject(handler)) {
args = Array.prototype.slice.call(arguments, 1);
listeners = handler.slice();
len = listeners.length;
for (i = 0; i < len; i++)
listeners[i].apply(this, args);
}
return true;
};
ee.on('message', function (text) {
console.log(gg) // gg is undefined
})
in the case above, it'should throw an error, but there is nothing
Just want to see if there is a possibility to include bower support for frontend development.
off is not a function
When this module is run through UglifyJS, the license comment is stripped. You can solve this by adding a comment like the following:
/*! events | Copyright Joyent, Inc. and other Node contributors | MIT License */
(At least some UglifyJS wrappers are configured to preserve comments beginning with a bang. A @license
prefix may also work.)
An AbortSignal
can be used to cancel waiting on events. See: https://nodejs.org/api/events.html#events_events_on_emitter_eventname_options
Why you don't use standard JS "Array.prototype.splice" for it?
(Performance reasons https://jsperf.com/splice-vs-events-spliceone)
function spliceOne(list, index) {
for (; index + 1 < list.length; index++)
list[index] = list[index + 1];
list.pop();
}
https://github.com/Gozala/events/blob/master/events.js#L434-L438
Could you update the module to the latest Node api ?
I am trying to use the matrix-js-sdk
package in sveltekit and it requires events (but need to run in the browser).
Is there a way I can import this module using vite (the svelte build tool) so that it exposes events api to browsers?
npm ERR! Unsupported
npm ERR! Not compatible with your version of node/npm: [email protected]
npm ERR! Required: {"node":"0.4.x","teleport":">=0.2.0"}
npm ERR! Actual: {"npm":"1.0.10","node":"v0.5.0-pre"}
npm ERR!
npm ERR! System Linux 2.6.38-8-generic
npm ERR! command "node" "/usr/local/bin/npm" "install" "events"
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /home/nb/npm-debug.log
npm not ok
parent.js
spawn('npm',['run','server'])
event.on('server-start-success', function(){
console.log('server start success')
})
child.js
event.emit('server-start-success');
Is there any desire to support engines without console
?
I am currently writing code for a JavaScript engine bundled with Qt 4.8 that doesn't have it, so when I reached this line it crashed.
Therefore I replaced this module with this one for the time being however if there's inclination, I am happy to submit a PR.
Hi there. It would be awesome from a distribution packaging point of view if you could include the full text of the MIT license in your software, usually in a file called LICENSE.
Thanks!
Node.js have a native events
implementation, so it would be useful to note in the README in what cases there is a benefit or requirement to use this module over the native events
module.
Hi,
our js error tracker is telling us that Function.prototype.apply
in this line
https://github.com/Gozala/events/blob/f6f6a376252519dfc27740b5b7fb7d69fcde44f1/events.js#L28 can be undefined and then calling call
on it raises an error. It mainly happens on ios with safari but also sometimes on chrome (ios).
Do you have any idea why this can happens and how to fix it?
Thanks!
Hi,
I have a question about best practices.
I'm trying to implement my own event emitter, and I was using the following code:
const length = listeners.length;
for (let i = 0, i < length; i++) {
listeners[i].call(this, event);
}
But this throws an error if some listener removes itself because it'd change the array length while looping through the listeners.
So I decide to change to a forEach, something like:
listeners.forEach((listener) => listener.call(this, event));
And worked like a charm! But I did some research on some EventEmitter polyfills, and no one uses this pattern. Everyone uses an array clone pattern, and iterate with a for
.
There's a specific reason to choose this pattern over another?
Thanks!
https://nodejs.org/api/events.html#events_events_on_emitter_eventname
Introduced in Node.js 13.6.0, require('events').on
creates an Async Iterator over all the emits of an event.
With just a slight modification from the original Node source, this could be made browser compatible, will attach pull request.
I personally don't have time to invest into this project, and I believe so does @defunctzombie
If you want to help maintaining this project please let us know.
Regards
I know there are methods to remove events, such as removeListener
and removeAllListeners
. But I think there should be a better method of removing events that don't correlate to the listener itself. Perhaps .on
could return an object that keeps chainability, but also has special methods to kill that singular event.
let bar = foo.on(...);
bar.unset();
foo.on(...).on(...)
Minimalist version i coded for scratch coders like me lmao.
class Emitter {
constructor() {
this.eventsNames = {}
this.on = (callName, callback_f) => {
if(typeof callback_f != 'function') throw new Error("Callback must must type of 'function'.")
if(this.eventsNames[callName] == undefined) this.eventsNames[callName] = []
this.eventsNames[callName].push(callback_f)
}
this.emit = (callName, datas) => {
if(this.eventsNames[callName] == undefined) return;
for(let i in this.eventsNames[callName]) {
try { this.eventsNames[callName][i](datas) } catch(e) { console.log(e) }
}
}
this.removeListeners = (callName) => (this.eventsNames[callName] = [])
this.removeAllListeners = (callName) => (this.eventsNames = {})
this.countListeners = (callName) => (this.eventsNames[callName] != undefined ? this.eventsNames[callName].length : 0)
}
}
let emitter = new Emitter()
emitter .on("hello", () => { console.log("hello world") })
emitter.emit("hello")
Is there a plan to fix this open issue? https://nvd.nist.gov/vuln/detail/CVE-2018-25076
I have a typescript application running on node.
I am using 'EventEmitter' class to emit a change in variable value.
This is my piece of code,
import events from 'events';
public async updateStream(streamContext: string, state: boolean): Promise<string> {
const eventEmitter = new events.EventEmitter();
if (state === true) {
return StreamManagement.instance.activeStreams.get(streamContext).streamState = 'Paused';
} else {
const streamState = StreamManagement.instance.activeStreams.get(streamContext).streamState = 'Active';
eventEmitter.emit('resume');
return streamState;
}
}
public async waitForStreamActive(stream: Stream) {
const eventEmitter = new events.EventEmitter();
// tslint:disable-next-line:no-unused-expression
return new Promise(( resolve ) => {
eventEmitter.on('resume', resolve );
});
}
This piece of code builds fine. But when i run the code, as in execute the operation, I am getting the following error,
error: errorHandler - Apply - Hit Unhandled exception {"timestamp":"2019-04-29T12:33:49.209Z"}
error: errorHandler - Apply - Cannot read property 'EventEmitter' of undefined - TypeError: Cannot read property 'EventEmitter' of undefined
at StreamResource.updateStream (C:\Vertigo\core\reference_platform\dist\index.js:10695:51)
at StreamService.patchStream (C:\Vertigo\core\reference_platform\dist\index.js:22524:40)
at process._tickCallback (internal/process/next_tick.js:68:7) {"timestamp":"2019-04-29T12:33:49.215Z"}
What am I doing wrong?
Node.js 14.5.0 added a DOM-like EventTarget
API.
https://nodejs.org/api/events.html#events_eventtarget_and_event_api
Node.js 11.13 added an EventEmitter.once
method: https://nodejs.org/api/events.html#events_events_once_emitter_name
It returns a Promise that resolves when the requested event is fired. If an 'error' event is fired first, the Promise rejects with the error.
The path to implementing this looks a bit like:
lib/events.js
in the nodejs/node repository)test-modulename-functionname.js
so you can find them by searching for events-once
via the Github UI https://github.com/nodejs/node/find/mastertests/
and add its require call to tests/index.js
. Since this test will require Promise support, it should only be executed if Promises are available. See the require('./symbols.js')
call for one approach. (We run browser tests in old IE etc, so Promise support isn't guaranteed.)events.js
at the correct location (it should be in the same order in the file as in Node.js, to make things easy to cross-reference).events
features in environments that do not support Promises, though.EventEmitter.once
API to work in README.md
.We can then release this as a minor version.
When EventEmitter.emit('error', error) is called in node, and goes unhandled, node will throw the error on the next tick, where it can't be handled by the current call stack.
This library throws the error during the emit call itself, which makes the error end up being thrown into a call stack that is not expecting it. This is particularly odd, because the caller is attempting to report the error, and it gets thrown back. I believe this behavior is incorrect.
I think the correct behavior, to mimic node, would be report the error using setTimeout or setImmediate, to clear the call stack before throwing. That way, the error will actually make it into the browser console output, which is generally benign.
Before NPM version 3.0.0, I could use modules that require()
'd this module, and then go ahead and use the Node core events
module all I wanted. But now in NPM@3, the entire dependency tree is installed as flat as possible in the node_modules
folder. This means that if I use a module that uses this module, I can no longer use the native events
module anymore, because this one overwrites it. I did not put this module in my package.json
as a dependency, an yet I am forced to use it. This should NOT be possible. If it's important to provide mirrors of the Node core modules in NPM, they should be under a different name. This wouldn't cause any problems, but it would solve this one.
Not covered by event built-in module of the node
EventEmitter.listenerCount is now (node v4) deprecated in favor of EventEmitter.prototype.listenerCount
https://github.com/nodejs/node/wiki/API-changes-between-v0.10-and-v4#events
Behavior broke in node 0.10, see nodejs/node-v0.x-archive#7157 (fixed in node.js master).
Example:
var A = function() {};
A.prototype = new events.EventEmitter();
var a1 = new A();
var a2 = new A();
a1.on('test', function() {
console.log('test');
});
a2.emit('test');
This logs 'test' where it shouldn't.
what should i do to unbind the event?
As a continuation of #58, I'd like to submit a request to backport off()
to v2. This is because browserify does not want to upgrade to v3 due to the breaking changes that come with it.
I don't believe this should be difficult to implement, it just needs to be an alias of removeListener()
which would (probably) just be a one line addition.
events.js:292
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::8081
My webserver can no longer start due to events.js throwing a fatal error that prevents me from listening on any ports. I am using Repl.it and events is used for Discord.js.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.