Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

uevent

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

uevent - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0

browser.js

37

package.json
{
"name": "uevent",
"version": "1.0.0",
"version": "2.0.0",
"description": "Event emitter micro library",
"main": "uevent.js",
"author": [
{
"name": "Jerome Etienne",
"homepage": "http://jetienne.com"
},
{
"name": "Damien \"Mistic\" Sorel",
"email": "contact@git.strangeplanet.fr",
"homepage": "http://www.strangeplanet.fr"
}
],
"main": "index.js",
"browser": "browser.js",
"types": "types.d.ts",
"author": {
"name": "Damien \"Mistic\" Sorel",
"email": "contact@git.strangeplanet.fr",
"homepage": "https://www.strangeplanet.fr"
},
"keywords": [

@@ -31,12 +27,13 @@ "events",

"devDependencies": {
"grunt": "^0.4.5",
"grunt-contrib-jshint": "^1.0.0",
"grunt-contrib-uglify": "^1.0.0",
"grunt-mocha-test": "^0.12.7",
"jit-grunt": "^0.10.0",
"time-grunt": "^1.3.0"
"@babel/core": "^7.2.2",
"@babel/preset-env": "^7.2.3",
"mocha": "^5.2.0",
"rollup": "^1.0.0",
"rollup-plugin-babel": "^4.2.0",
"rollup-plugin-commonjs": "^9.2.0"
},
"scripts": {
"test": "grunt test"
"test": "mocha tests/*",
"compile": "rollup --config rollup.config.js"
}
}
# uEvent
[![Bower version](https://img.shields.io/bower/v/uevent.svg?style=flat-square)](https://github.com/mistic100/uEvent)
[![NPM version](https://img.shields.io/npm/v/uevent.svg?style=flat-square)](https://www.npmjs.com/package/uevent)
[![Build Status](https://travis-ci.org/mistic100/uEvent.svg?branch=master)](https://travis-ci.org/mistic100/uEvent)
[![npm version](https://img.shields.io/npm/v/uevent.svg?style=flat-square)](https://www.npmjs.com/package/uevent)
[![jsDelivr CDN](https://data.jsdelivr.com/v1/package/npm/uevent/badge)](https://www.jsdelivr.com/package/npm/uevent)
[![Build Status](https://img.shields.io/travis/mistic100/uEvent/master.svg?style=flat-square)](https://travis-ci.org/mistic100/uEvent)
[![Dependencies Status](https://david-dm.org/mistic100/uEvent/status.svg?style=flat-square)](https://david-dm.org/mistic100/uEvent)

@@ -12,51 +13,74 @@ _uEvent_ is a event emitter library which provides the [observer pattern](http://en.wikipedia.org/wiki/Observer_pattern) to javascript objects.

### Changes from original
* AMD support
* renamed `bind`/`unbind` into `on`/`off`
* `on` and `off` can be called with a space separated list of events like jQuery
* `on` and `off` can be called with a hashmap like jQuery
* add `once` method [alinz](https://github.com/alinz/microevent.js/commit/a8293fe9571ea4e609d51ec906d627e64dfb8eba)
* add `change` method
* allow to rename methods in the mixin
* make `on`, `off` and `once` methods chainable [feross](https://github.com/PeerCDN/microevent.js/commit/617c9a26ed861b812c61eb836b22c0f313292a20)
* fix error in `off` [#27](https://github.com/jeromeetienne/microevent.js/pull/27)
* fix callback skips when `off` is called from another callback [#10](https://github.com/jeromeetienne/microevent.js/issues/10)
* integrate **prevent default** and **stop propagation** patterns
* listeners can be objects with an [handleEvent](https://developer.mozilla.org/en/docs/Web/API/EventListener#handleEvent()) method
## Features
## How to Use It
* jQuery-like API (`on`, `off`, `trigger`)
* Once support
* Value modifier support
* **prevent default** and **stop propagation** patterns
* [handleEvent](https://developer.mozilla.org/en/docs/Web/API/EventListener#handleEvent()) support
You need a single file [uevent.js](https://github.com/mistic100/uEvent/blob/master/uevent.min.js).
Include it in a webpage via the usual script tag.
```html
<script src="uevent.min.js"></script>
## Installation
```
$ npm install uevent
```
To include it in a nodejs code isn't much harder
## Usage
### Create an emitter
**Direct**
```js
var uEvent = require('uevent')
const obj = new uevent.EventEmitter();
```
Now suppose you got a class `Foobar`, and you wish it to support the observer pattern:
**Class extend**
```js
uEvent.mixin(Foobar)
class Manager extends uevent.EventEmitter {
}
class obj = new Manager();
```
If needed you can rename the name of some or all methods:
**Mixin**
```js
uEvent.mixin(Foobar, {
'trigger': 'emit',
'change': 'modify'
})
const obj = {};
uevent.mixin(obj);
```
After applying the mixin the following methods are added to your class (all methods are chainable):
### Callback signature
### on
The first parameter of the callback is always an `Event` object having the following properties :
- `type` the name of the event
- `target` the source object of the event
- `args` additional parameters
When additional parameters are provided they are passed to the callback :
```js
const callback = function(event, param1, param2) {};
```
When using the `handleEvent` feature you only get the event object :
```js
const listener = {
handleEvent: function(event) {}
};
```
## API
### `on`
Add one or many event handlers.

@@ -69,3 +93,3 @@

// bind 'callback' to 'event1' and 'event2'
obj.on('event1 event2', callback)
obj.on('event1 event2', callback);

@@ -76,6 +100,6 @@ // bind 'callback1' to 'event1' and 'callback2' to 'event2'

event2: callback2
})
});
```
### off
### `off`

@@ -86,9 +110,9 @@ Remove one or many or all event handlers.

// remove all callbacks for 'event'
obj.off('event')
obj.off('event');
// remove 'callback' if attached to 'event'
obj.off('event', callback)
obj.off('event', callback);
// remove all callbacks for 'event1' and 'event2'
obj.off('event1 event2')
obj.off('event1 event2');

@@ -99,9 +123,9 @@ // remove 'callback1' if attached to 'event1' and 'callback2' if attached to 'event2'

event2: callback2
})
});
// remove all callbacks
obj.off()
obj.off();
```
### once
### `once`

@@ -112,3 +136,3 @@ Same as `on` but the callbacks will be removed after the first invocation.

### trigger
### `trigger`

@@ -119,9 +143,9 @@ Trigger all handlers for an event. Accept optional arguments transmitted to the callbacks.

// trigger 'event'
obj.trigger('event')
obj.trigger('event');
// trigger 'event' with arguments
obj.trigger('event', true, 42)
obj.trigger('event', true, 42);
```
### change
### `change`

@@ -138,12 +162,13 @@ Works like `trigger` but returns a value. This is used to filter a value before display for example. All callbacks **must** accept at least on input value and return the modified (or not) value.

The `Event` object has an additional `value` property which holds the current value.
## Advanced
uEvent integrates two concepts from jQuery : prevent default and stop propagation. This is done via an additional argument transmitted to each `trigger` and/or `change` callback.
### Prevent default
Call `preventDefault()` on this additional object to "mark" the event. After calling `trigger` you get a reference to the Event object and test `isDefaultPrevented()`.
Call `preventDefault()` on this `Event` object to "mark" the event. After calling `trigger` you get a reference to this `Event` object and can test `isDefaultPrevented()`.
```js
obj.on('event', function(id, e) {
obj.on('event', function(e, id) {
if (id == 0) {

@@ -154,3 +179,3 @@ e.preventDefault();

var e = obj.trigger('event', id);
const e = obj.trigger('event', id);

@@ -164,6 +189,6 @@ if (!e.isDefaultPrevented()) {

Call `stopPropagation()` on the Event object to prevent any further callbacks to be called. Works for `trigger` and `change`.
Call `stopPropagation()` on the `Event` object to prevent any further callbacks to be called. Works for `trigger` and `change`.
```js
obj.on('event', function(val, e) {
obj.on('event', function(e, val) {
e.stopPropagation();

@@ -173,74 +198,12 @@ return val;

obj.on('event', function(val, e) {
obj.on('event', function(e, val) {
return 'azerty';
});
var newVal = obj.change('event', '1234');
const newVal = obj.change('event', '1234');
// newVal is still '1234'
```
### Event listener
When using `on`, `off` and `once` you can provide an object instead of the callback. The `handleEvent` method of this object will be called with the event object as first argument.
```js
var listener = {
handleEvent: function(e) {
// e.type : name of the event
// e.args : array of 'trigger' arguments
}
};
obj.on('event', listener)
```
## Example
First we define the class which gonna use uEvent. This is a ticker, it is triggering 'tick' event every second, and add the current date as parameter
```js
var Ticker = function() {
var self = this;
var i = 0;
setInterval(function() {
self.trigger('tick', new Date());
i = self.change('index', i);
}, 1000);
};
```
We mixin _uEvent_ into _Ticker_ and we are all set.
```
uEvent.mixin(Ticker);
```
Now lets actually use the _Ticker_ class. First, create the object.
```js
var ticker = new Ticker();
```
and bind our _tick_ event with its data parameter
```js
ticker.on('tick', function(date) {
console.log('notified date', date);
});
ticker.on('hello', function(index) {
console.log('index', index);
return index + 1;
});
```
And you will see this output:
```
notified date Tue, 22 Mar 2011 14:43:41 GMT
index 0
notified date Tue, 22 Mar 2011 14:43:42 GMT
index 1
...
```
## License
This library is available under the MIT license.

Sorry, the diff of this file is not supported yet

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