New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

tiny-emitter

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tiny-emitter - npm Package Compare versions

Comparing version 0.1.1 to 0.1.2

88

index.js

@@ -1,47 +0,59 @@

var Emitter = function () {
function Emitter () {
this.e = {};
};
}
Emitter.prototype.on = function (name, callback, ctx) {
var e = this.e;
e[name] = e[name] || [];
e[name].push({
fn: callback,
ctx: ctx || null
});
return this;
};
Emitter.prototype = {
on: function (name, callback, ctx) {
var e = this.e;
e[name] = e[name] || [];
e[name].push({
fn: callback,
ctx: ctx || null
});
return this;
},
Emitter.prototype.emit = function (name) {
var data = [].slice.call(arguments, 1);
var evtArr = this.e[name] || [];
for (var i in evtArr) {
evtArr[i].fn.apply(evtArr[i].ctx, data);
}
return this;
};
once: function (name, callback, ctx) {
var self = this;
var fn = function () {
self.off(name, fn);
callback.apply(ctx, arguments);
};
return this.on(name, fn, ctx);
},
Emitter.prototype.off = function (name, callback) {
var e = this.e;
var evts = e[name];
if (!callback) {
e[name] = [];
}
else {
for (var idx in evts) {
if (!evts || !evts[idx]) continue;
if (evts[idx].fn !== callback) continue;
evts.splice(idx, 1);
emit: function (name) {
var data = [].slice.call(arguments, 1);
var evtArr = this.e[name] || [];
for (var i in evtArr) {
evtArr[i].fn.apply(evtArr[i].ctx, data);
}
return this;
},
off: function (name, callback) {
var e = this.e;
var evts = e[name];
if (!callback) {
e[name] = [];
}
else {
for (var idx in evts) {
if (!evts || !evts[idx]) continue;
if (evts[idx].fn !== callback) continue;
evts.splice(idx, 1);
}
}
return this;
}
return this;
};
module.exports = Emitter;
{
"name": "tiny-emitter",
"version": "0.1.1",
"version": "0.1.2",
"description": "A tiny (less than 500 bytes) event emitter library",

@@ -5,0 +5,0 @@ "main": "index.js",

# tiny-emitter
A tiny (less than 500 bytes) event emitter library
A tiny (less than 500 bytes) event emitter library. Works with [Browserify](http://browserify.org).

@@ -34,2 +34,10 @@ ## Install

### once(event, callback[, context])
Subscribe to an event only **once**
* `event` - the name of the event to subscribe to
* `callback` - the function to call when event is emitted
* `context` - (OPTIONAL) - the context to bind the event callback to
### off(event[, callback])

@@ -36,0 +44,0 @@

@@ -26,2 +26,28 @@ var Emitter = require('../index.js');

test('subscibes only once to an event', function (t) {
var emitter = new Emitter();
emitter.once('test', function () {
t.equal(emitter.e.test.length, 0, 'removed event from list');
t.end();
});
emitter.emit('test');
});
test('keeps context when subscribed only once', function (t) {
var emitter = new Emitter();
var context = {
contextValue: true
};
emitter.once('test', function () {
t.ok(this.contextValue, 'is in context');
t.equal(emitter.e.test.length, 0, 'not subscribed anymore');
t.end();
}, context);
emitter.emit('test');
});
test('emits an event', function (t) {

@@ -78,1 +104,15 @@ var emitter = new Emitter();

test('removes an event inside another event', function (t) {
var emitter = new Emitter();
emitter.on('test', function () {
t.equal(emitter.e.test.length, 1, 'event is still in list');
emitter.off('test');
t.equal(emitter.e.test.length, 0, 'event is gone from list');
t.end();
});
emitter.emit('test');
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc