Comparing version 2.0.0-alpha.11 to 2.0.0-alpha.12
@@ -5,2 +5,9 @@ # Changelog | ||
## [2.0.0-alpha.12](https://github.com/snowyu/events-ex.js/compare/v2.0.0-alpha.11...v2.0.0-alpha.12) (2024-03-28) | ||
### Features | ||
* add regexp to match events for on/off ([a6fe84d](https://github.com/snowyu/events-ex.js/commit/a6fe84d582a17f95717359a5bd88d4cfed2ddb22)) | ||
## [2.0.0-alpha.11](https://github.com/snowyu/events-ex.js/compare/v2.0.0-alpha.10...v2.0.0-alpha.11) (2024-03-27) | ||
@@ -7,0 +14,0 @@ |
@@ -29,2 +29,2 @@ [events-ex](../README.md) / [Exports](../modules.md) / [event-emitter](../modules/event_emitter-1.md) / EventEmitter | ||
[src/event-emitter.js:8](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/event-emitter.js#L8) | ||
[src/event-emitter.js:8](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/event-emitter.js#L8) |
@@ -50,3 +50,3 @@ [events-ex](../README.md) / [Exports](../modules.md) / [event-emitter](../modules/event_emitter.md) / EventEmitter | ||
[src/event-emitter.d.ts:7](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/event-emitter.d.ts#L7) | ||
[src/event-emitter.d.ts:7](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/event-emitter.d.ts#L7) | ||
@@ -76,3 +76,3 @@ ## Methods | ||
[src/event-emitter.d.ts:38](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/event-emitter.d.ts#L38) | ||
[src/event-emitter.d.ts:38](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/event-emitter.d.ts#L38) | ||
@@ -102,3 +102,3 @@ ___ | ||
[src/event-emitter.d.ts:44](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/event-emitter.d.ts#L44) | ||
[src/event-emitter.d.ts:44](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/event-emitter.d.ts#L44) | ||
@@ -127,3 +127,3 @@ ___ | ||
[src/event-emitter.d.ts:66](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/event-emitter.d.ts#L66) | ||
[src/event-emitter.d.ts:66](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/event-emitter.d.ts#L66) | ||
@@ -152,3 +152,3 @@ ___ | ||
[src/event-emitter.d.ts:59](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/event-emitter.d.ts#L59) | ||
[src/event-emitter.d.ts:59](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/event-emitter.d.ts#L59) | ||
@@ -182,3 +182,3 @@ ___ | ||
[src/event-emitter.d.ts:32](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/event-emitter.d.ts#L32) | ||
[src/event-emitter.d.ts:32](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/event-emitter.d.ts#L32) | ||
@@ -212,3 +212,3 @@ ___ | ||
[src/event-emitter.d.ts:16](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/event-emitter.d.ts#L16) | ||
[src/event-emitter.d.ts:16](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/event-emitter.d.ts#L16) | ||
@@ -242,3 +242,3 @@ ___ | ||
[src/event-emitter.d.ts:24](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/event-emitter.d.ts#L24) | ||
[src/event-emitter.d.ts:24](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/event-emitter.d.ts#L24) | ||
@@ -267,3 +267,3 @@ ___ | ||
[src/event-emitter.d.ts:51](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/event-emitter.d.ts#L51) | ||
[src/event-emitter.d.ts:51](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/event-emitter.d.ts#L51) | ||
@@ -288,3 +288,3 @@ ___ | ||
[src/event-emitter.d.ts:52](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/event-emitter.d.ts#L52) | ||
[src/event-emitter.d.ts:52](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/event-emitter.d.ts#L52) | ||
@@ -310,2 +310,2 @@ ___ | ||
[src/event-emitter.d.ts:67](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/event-emitter.d.ts#L67) | ||
[src/event-emitter.d.ts:67](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/event-emitter.d.ts#L67) |
@@ -52,3 +52,3 @@ [events-ex](../README.md) / [Exports](../modules.md) / [event](../modules/event.md) / Event | ||
[src/event.js:8](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/event.js#L8) | ||
[src/event.js:8](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/event.js#L8) | ||
@@ -65,3 +65,3 @@ ## Properties | ||
[src/event.js:38](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/event.js#L38) | ||
[src/event.js:38](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/event.js#L38) | ||
@@ -78,3 +78,3 @@ ___ | ||
[src/event.js:32](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/event.js#L32) | ||
[src/event.js:32](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/event.js#L32) | ||
@@ -91,3 +91,3 @@ ___ | ||
[src/event.js:26](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/event.js#L26) | ||
[src/event.js:26](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/event.js#L26) | ||
@@ -104,3 +104,3 @@ ___ | ||
[src/event.js:44](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/event.js#L44) | ||
[src/event.js:44](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/event.js#L44) | ||
@@ -123,3 +123,3 @@ ## Methods | ||
[src/event.js:51](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/event.js#L51) | ||
[src/event.js:51](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/event.js#L51) | ||
@@ -147,2 +147,2 @@ ___ | ||
[src/event.js:20](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/event.js#L20) | ||
[src/event.js:20](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/event.js#L20) |
@@ -44,2 +44,2 @@ [events-ex](../README.md) / [Exports](../modules.md) / all-off | ||
[src/all-off.js:12](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/all-off.js#L12) | ||
[src/all-off.js:12](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/all-off.js#L12) |
@@ -9,2 +9,3 @@ [events-ex](../README.md) / [Exports](../modules.md) / consts | ||
- [RegExpEventSymbol](consts.md#regexpeventsymbol) | ||
- [states](consts.md#states) | ||
@@ -14,2 +15,12 @@ | ||
### RegExpEventSymbol | ||
• `Const` **RegExpEventSymbol**: `symbol` \| ``"@@RegExpEvent"`` | ||
#### Defined in | ||
[src/consts.js:13](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/consts.js#L13) | ||
___ | ||
### states | ||
@@ -30,2 +41,2 @@ | ||
[src/consts.js:6](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/consts.js#L6) | ||
[src/consts.js:6](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/consts.js#L6) |
@@ -41,3 +41,3 @@ [events-ex](../README.md) / [Exports](../modules.md) / default-methods | ||
| `emitAsync` | (...`args`: `any`) => `Promise`\<`any`\> | | ||
| `listenerCount` | (`emitter`: `any`, `type`: `any`) => `any` | | ||
| `listenerCount` | (`emitter`: `any`, `type`: `any`) => `number` | | ||
| `listeners` | (`type`: `any`) => `any` | | ||
@@ -52,2 +52,2 @@ | `off` | (`type`: `string`, `listener`: `Function`) => [`EventEmitter`](../classes/event_emitter.EventEmitter.md) | | ||
[src/default-methods.js:8](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/default-methods.js#L8) | ||
[src/default-methods.js:28](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/default-methods.js#L28) |
@@ -40,2 +40,2 @@ [events-ex](../README.md) / [Exports](../modules.md) / has-listeners | ||
[src/has-listeners.js:10](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/has-listeners.js#L10) | ||
[src/has-listeners.js:10](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/has-listeners.js#L10) |
@@ -11,2 +11,3 @@ [events-ex](../README.md) / [Exports](../modules.md) / index | ||
- [EventEmitter](index.md#eventemitter) | ||
- [RegExpEventSymbol](index.md#regexpeventsymbol) | ||
- [allOff](index.md#alloff) | ||
@@ -37,2 +38,8 @@ - [default](index.md#default) | ||
### RegExpEventSymbol | ||
Re-exports [RegExpEventSymbol](consts.md#regexpeventsymbol) | ||
___ | ||
### allOff | ||
@@ -39,0 +46,0 @@ |
@@ -49,2 +49,2 @@ [events-ex](../README.md) / [Exports](../modules.md) / pipe-async | ||
[src/pipe-async.js:21](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/pipe-async.js#L21) | ||
[src/pipe-async.js:21](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/pipe-async.js#L21) |
@@ -49,2 +49,2 @@ [events-ex](../README.md) / [Exports](../modules.md) / pipe | ||
[src/pipe.js:21](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/pipe.js#L21) | ||
[src/pipe.js:21](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/pipe.js#L21) |
@@ -46,2 +46,2 @@ [events-ex](../README.md) / [Exports](../modules.md) / unify | ||
[src/unify.js:17](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/unify.js#L17) | ||
[src/unify.js:17](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/unify.js#L17) |
@@ -39,2 +39,2 @@ [events-ex](../README.md) / [Exports](../modules.md) / util/array-remove | ||
[src/util/array-remove.js:5](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/util/array-remove.js#L5) | ||
[src/util/array-remove.js:5](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/util/array-remove.js#L5) |
@@ -41,2 +41,2 @@ [events-ex](../README.md) / [Exports](../modules.md) / util/object-for-each | ||
[src/util/object-for-each.js:6](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/util/object-for-each.js#L6) | ||
[src/util/object-for-each.js:6](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/util/object-for-each.js#L6) |
@@ -40,2 +40,2 @@ [events-ex](../README.md) / [Exports](../modules.md) / util/string-pad | ||
[src/util/string-pad.js:8](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/util/string-pad.js#L8) | ||
[src/util/string-pad.js:8](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/util/string-pad.js#L8) |
@@ -39,2 +39,2 @@ [events-ex](../README.md) / [Exports](../modules.md) / util/to-int | ||
[src/util/to-int.js:2](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/util/to-int.js#L2) | ||
[src/util/to-int.js:2](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/util/to-int.js#L2) |
@@ -39,2 +39,2 @@ [events-ex](../README.md) / [Exports](../modules.md) / util/valid-callable | ||
[src/util/valid-callable.js:1](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/util/valid-callable.js#L1) | ||
[src/util/valid-callable.js:1](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/util/valid-callable.js#L1) |
@@ -39,2 +39,2 @@ [events-ex](../README.md) / [Exports](../modules.md) / util/valid-object | ||
[src/util/valid-object.js:3](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/util/valid-object.js#L3) | ||
[src/util/valid-object.js:3](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/util/valid-object.js#L3) |
@@ -33,3 +33,3 @@ [events-ex](../README.md) / [Exports](../modules.md) / wrap-event-emitter | ||
[src/wrap-event-emitter.js:7](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/wrap-event-emitter.js#L7) | ||
[src/wrap-event-emitter.js:7](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/wrap-event-emitter.js#L7) | ||
@@ -58,2 +58,2 @@ ## Functions | ||
[src/wrap-event-emitter.js:34](https://github.com/snowyu/events-ex.js/blob/ccd8835/src/wrap-event-emitter.js#L34) | ||
[src/wrap-event-emitter.js:34](https://github.com/snowyu/events-ex.js/blob/a6fe84d/src/wrap-event-emitter.js#L34) |
@@ -11,4 +11,6 @@ events-ex / [Exports](modules.md) | ||
* keep most compatible with [node events](nodejs.org/api/events.html) and [event-emitter][event-emitter] | ||
* Hookable event system for more control over event handling | ||
* Supports async event emitting via `emitAsync` method. | ||
* Supports bubbling and interruption | ||
* Hook-able event system for more control over event handling | ||
* Supports async event emitting via `emitAsync` method which will wait for all async listeners to complete before returning. | ||
* Subscribe events with regular expression | ||
@@ -23,2 +25,3 @@ ### Differences | ||
* `target`: The `Event Emitter` object, which was originally the `this` object. | ||
* `type`: triggered event type(name). | ||
* **`broken change`**: The `emit` return the result of listeners's callback function instead of the successful state. | ||
@@ -75,3 +78,8 @@ * **`broken change`**: The `this` object of listeners' callback function is the `Event` Object instead of the emitter object. | ||
my.on(/^event/, function() { | ||
console.log('regexp match multi events'); | ||
}); | ||
my.emit('event'); | ||
my.emit('event1'); | ||
``` | ||
@@ -78,0 +86,0 @@ |
@@ -7,2 +7,3 @@ export namespace states { | ||
} | ||
export const RegExpEventSymbol: symbol | "@@RegExpEvent"; | ||
declare const CONTINUE: any; | ||
@@ -9,0 +10,0 @@ declare const DONE: 1; |
@@ -6,3 +6,3 @@ "use strict"; | ||
}); | ||
exports.states = void 0; | ||
exports.states = exports.RegExpEventSymbol = void 0; | ||
const CONTINUE = undefined; | ||
@@ -17,2 +17,3 @@ const DONE = 1; | ||
ABORT | ||
}; | ||
}; | ||
const RegExpEventSymbol = exports.RegExpEventSymbol = typeof Symbol === 'function' ? Symbol('RegExpEvent') : '@@RegExpEvent'; |
@@ -32,3 +32,3 @@ export function getEventableMethods(aClass: any): { | ||
listeners(type: any): any; | ||
listenerCount(emitter: any, type: any): any; | ||
listenerCount(emitter: any, type: any): number; | ||
/** | ||
@@ -35,0 +35,0 @@ * Removes a listener function from the specified event type. |
@@ -9,2 +9,3 @@ "use strict"; | ||
var _utilEx = require("util-ex"); | ||
var _consts = require("./consts"); | ||
var _event = require("./event"); | ||
@@ -14,2 +15,18 @@ const create = Object.create; | ||
const slice = Array.prototype.slice; | ||
function isRegExpStr(value) { | ||
const result = typeof value === 'string' && value.length > 2 && value[0] === '/' && value.lastIndexOf('/') > 0; | ||
return result; | ||
} | ||
function isRegExp(value) { | ||
const result = isRegExpStr(value); | ||
return result || (0, _utilEx.isRegExp)(value); | ||
} | ||
function toRegExp(value) { | ||
if (typeof value === 'string' && value.length > 2 && value[0] === '/') { | ||
const i = value.lastIndexOf('/'); | ||
const source = value.slice(1, i); | ||
const flags = value.slice(i + 1); | ||
return new RegExp(source, flags); | ||
} | ||
} | ||
function getEventableMethods(aClass) { | ||
@@ -40,2 +57,5 @@ return { | ||
} | ||
if (isRegExp(type)) { | ||
data = data[_consts.RegExpEventSymbol] || (data[_consts.RegExpEventSymbol] = create(null)); | ||
} | ||
if (!data[type]) { | ||
@@ -182,10 +202,16 @@ data[type] = listener; | ||
listeners(type) { | ||
const data = this._events; | ||
let result; | ||
if (!(data && data[type])) { | ||
result = []; | ||
} else if ((0, _utilEx.isFunction)(data[type])) { | ||
result = [data[type]]; | ||
} else { | ||
result = data[type].slice(); | ||
let data = this._events; | ||
let result = []; | ||
if (data) { | ||
if (isRegExp(type) && data[_consts.RegExpEventSymbol]) { | ||
data = data[_consts.RegExpEventSymbol]; | ||
} | ||
const listener = data[type]; | ||
if (listener) { | ||
if ((0, _utilEx.isFunction)(listener)) { | ||
result = [listener]; | ||
} else { | ||
result = listener.slice(); | ||
} | ||
} | ||
} | ||
@@ -195,12 +221,18 @@ return result; | ||
listenerCount(emitter, type) { | ||
if (typeof emitter === 'string') type = emitter; | ||
if (typeof emitter === 'string' || (0, _utilEx.isRegExp)(emitter)) type = emitter; | ||
emitter = this; | ||
const data = emitter._events; | ||
let result; | ||
if (!(data && data[type])) { | ||
result = 0; | ||
} else if ((0, _utilEx.isFunction)(data[type])) { | ||
result = 1; | ||
} else { | ||
result = data[type].length; | ||
let data = emitter._events; | ||
let result = 0; | ||
if (data) { | ||
if (isRegExp(type) && data[_consts.RegExpEventSymbol]) { | ||
data = data[_consts.RegExpEventSymbol]; | ||
} | ||
const listener = data[type]; | ||
if (listener) { | ||
if ((0, _utilEx.isFunction)(listener)) { | ||
result = 1; | ||
} else { | ||
result = listener.length; | ||
} | ||
} | ||
} | ||
@@ -223,3 +255,7 @@ return result; | ||
} | ||
const data = this._events; | ||
let data = this._events; | ||
const hasRemover = data.removeListener; | ||
if (isRegExp(type) && data[_consts.RegExpEventSymbol]) { | ||
data = data[_consts.RegExpEventSymbol]; | ||
} | ||
if (!data[type]) { | ||
@@ -231,3 +267,3 @@ return this; | ||
delete data[type]; | ||
if (data.removeListener) { | ||
if (hasRemover) { | ||
this.emit('removeListener', type, listener); | ||
@@ -255,3 +291,3 @@ } | ||
} | ||
if (data.removeListener) { | ||
if (hasRemover) { | ||
this.emit('removeListener', type, listener); | ||
@@ -272,3 +308,4 @@ } | ||
} | ||
const data = this._events; | ||
let data = this._events; | ||
const regExpEvents = data[_consts.RegExpEventSymbol]; | ||
// not listening for removeListener, no need to emit | ||
@@ -279,3 +316,9 @@ if (!data.removeListener) { | ||
Object.keys(data).forEach(key => delete data[key]); | ||
if (regExpEvents) { | ||
delete data[_consts.RegExpEventSymbol]; | ||
} | ||
} else { | ||
if (isRegExp(type) && regExpEvents) { | ||
data = regExpEvents; | ||
} | ||
delete data[type]; | ||
@@ -288,3 +331,3 @@ } | ||
for (const key in data) { | ||
if (key === 'removeListener') { | ||
if (key === 'removeListener' || key === _consts.RegExpEventSymbol) { | ||
continue; | ||
@@ -294,7 +337,18 @@ } | ||
} | ||
if (regExpEvents) { | ||
for (const key in regExpEvents) { | ||
this.removeAllListeners(toRegExp(key)); | ||
} | ||
} | ||
this.removeAllListeners('removeListener'); | ||
// delete this._events | ||
Object.keys(data).forEach(key => delete data[key]); | ||
// Object.keys(data).forEach(key => delete data[key]) | ||
if (regExpEvents) { | ||
delete data[_consts.RegExpEventSymbol]; | ||
} | ||
return this; | ||
} | ||
if (isRegExp(type) && regExpEvents) { | ||
data = regExpEvents; | ||
} | ||
const listeners = data[type]; | ||
@@ -337,2 +391,20 @@ if ((0, _utilEx.isFunction)(listeners)) { | ||
} | ||
const regExpEvents = data[_consts.RegExpEventSymbol]; | ||
if (regExpEvents) { | ||
const matched = []; | ||
for (let key in regExpEvents) { | ||
key = toRegExp(key); | ||
if (key && key.test(type)) { | ||
const listener = regExpEvents[key]; | ||
if ((0, _utilEx.isArray)(listener)) { | ||
matched.push.apply(matched, listener); | ||
} else { | ||
matched.push(listener); | ||
} | ||
} | ||
} | ||
if (matched.length) { | ||
listeners = listeners ? !(0, _utilEx.isObject)(listeners) ? [listeners].concat(matched) : listeners.concat(matched) : matched; | ||
} | ||
} | ||
if (!listeners) { | ||
@@ -339,0 +411,0 @@ return; |
{ | ||
"name": "events-ex", | ||
"version": "2.0.0-alpha.11", | ||
"version": "2.0.0-alpha.12", | ||
"description": "Browser-friendly enhanced events most compatible with standard node.js, it's powerful eventable ability.", | ||
@@ -5,0 +5,0 @@ "contributors": [ |
@@ -20,3 +20,4 @@ ### events-ex [![Build Status](https://img.shields.io/travis/snowyu/events-ex.js/master.png)](http://travis-ci.org/snowyu/events-ex.js) [![npm](https://img.shields.io/npm/v/events-ex.svg)](https://npmjs.org/package/events-ex) [![downloads](https://img.shields.io/npm/dm/events-ex.svg)](https://npmjs.org/package/events-ex) [![license](https://img.shields.io/npm/l/events-ex.svg)](https://npmjs.org/package/events-ex) | ||
* 可挂载的事件系统, 用于更好地控制事件处理 | ||
* 支持异步事件通过 `emitAsync` 方法. | ||
* 支持异步事件通过 `emitAsync` 方法,该方法会等待所有异步`listeners`处理完毕后返回结果 | ||
* 支持正则表达式匹配订阅事件 | ||
@@ -31,2 +32,3 @@ ### 区别 | ||
* `target`属性: 事件发射器对象,原本的`this` | ||
* `type`属性: 触发的事件类型名称 | ||
* **`改变`**: `emit` 方法返回监听器回调函数的结果而不是成功状态。 | ||
@@ -84,3 +86,8 @@ * **`改变`**: 监听器回调函数的 `this` 对象是 `Event Object` 事件对象而不是事件发射器对象。 | ||
my.on(/^event/, function() { | ||
console.log('regexp match multi events'); | ||
}); | ||
my.emit('event'); | ||
my.emit('event1'); | ||
``` | ||
@@ -87,0 +94,0 @@ |
@@ -10,4 +10,6 @@ ### events-ex [![Build Status](https://img.shields.io/travis/snowyu/events-ex.js/master.png)](http://travis-ci.org/snowyu/events-ex.js) [![npm](https://img.shields.io/npm/v/events-ex.svg)](https://npmjs.org/package/events-ex) [![downloads](https://img.shields.io/npm/dm/events-ex.svg)](https://npmjs.org/package/events-ex) [![license](https://img.shields.io/npm/l/events-ex.svg)](https://npmjs.org/package/events-ex) | ||
* keep most compatible with [node events](nodejs.org/api/events.html) and [event-emitter][event-emitter] | ||
* Hookable event system for more control over event handling | ||
* Supports async event emitting via `emitAsync` method. | ||
* Supports bubbling and interruption | ||
* Hook-able event system for more control over event handling | ||
* Supports async event emitting via `emitAsync` method which will wait for all async listeners to complete before returning. | ||
* Subscribe events with regular expression | ||
@@ -22,2 +24,3 @@ ### Differences | ||
* `target`: The `Event Emitter` object, which was originally the `this` object. | ||
* `type`: triggered event type(name). | ||
* **`broken change`**: The `emit` return the result of listeners's callback function instead of the successful state. | ||
@@ -74,3 +77,9 @@ * **`broken change`**: The `this` object of listeners' callback function is the `Event` Object instead of the emitter object. | ||
my.on(/^event/, function() { | ||
console.log('regexp match multi events'); | ||
}); | ||
my.emit('event'); | ||
my.emit('event1'); | ||
``` | ||
@@ -77,0 +86,0 @@ |
@@ -12,1 +12,3 @@ const CONTINUE = undefined | ||
} | ||
export const RegExpEventSymbol = typeof Symbol === 'function' ? Symbol('RegExpEvent') : '@@RegExpEvent' |
@@ -1,2 +0,3 @@ | ||
import {defineProperty, isArray, isFunction, isNumber, isObject, isUndefined} from 'util-ex' | ||
import {defineProperty, isArray, isFunction, isNumber, isObject, isRegExp as _isRegExp, isUndefined} from 'util-ex' | ||
import {RegExpEventSymbol} from './consts' | ||
import {Event} from './event'; | ||
@@ -8,2 +9,21 @@ | ||
function isRegExpStr(value) { | ||
const result = typeof value === 'string' && value.length > 2 && value[0] === '/' && value.lastIndexOf('/') > 0 | ||
return result | ||
} | ||
function isRegExp(value) { | ||
const result = isRegExpStr(value) | ||
return result || _isRegExp(value) | ||
} | ||
function toRegExp(value) { | ||
if (typeof value === 'string' && value.length > 2 && value[0] === '/') { | ||
const i = value.lastIndexOf('/') | ||
const source = value.slice(1, i) | ||
const flags = value.slice(i+1) | ||
return new RegExp(source, flags) | ||
} | ||
} | ||
export function getEventableMethods(aClass) { | ||
@@ -32,2 +52,5 @@ return { | ||
} | ||
if (isRegExp(type)) { | ||
data = data[RegExpEventSymbol] || (data[RegExpEventSymbol] = create(null)) | ||
} | ||
if (!data[type]) { | ||
@@ -166,10 +189,16 @@ data[type] = listener | ||
listeners(type) { | ||
const data = this._events | ||
let result | ||
if (!(data && data[type])){ | ||
result = [] | ||
} else if (isFunction(data[type])) { | ||
result = [data[type]] | ||
} else { | ||
result = data[type].slice() | ||
let data = this._events | ||
let result = [] | ||
if (data) { | ||
if (isRegExp(type) && data[RegExpEventSymbol]) { | ||
data = data[RegExpEventSymbol] | ||
} | ||
const listener = data[type] | ||
if (listener) { | ||
if (isFunction(listener)) { | ||
result = [listener] | ||
} else { | ||
result = listener.slice() | ||
} | ||
} | ||
} | ||
@@ -180,13 +209,19 @@ return result | ||
listenerCount(emitter, type) { | ||
if (typeof emitter === 'string') | ||
if (typeof emitter === 'string' || _isRegExp(emitter)) | ||
type = emitter | ||
emitter = this | ||
const data = emitter._events | ||
let result | ||
if (!(data && data[type])){ | ||
result = 0 | ||
} else if (isFunction(data[type])) { | ||
result = 1 | ||
} else { | ||
result = data[type].length | ||
let data = emitter._events | ||
let result = 0 | ||
if (data) { | ||
if (isRegExp(type) && data[RegExpEventSymbol]) { | ||
data = data[RegExpEventSymbol] | ||
} | ||
const listener = data[type] | ||
if (listener) { | ||
if (isFunction(listener)) { | ||
result = 1 | ||
} else { | ||
result = listener.length | ||
} | ||
} | ||
} | ||
@@ -206,3 +241,7 @@ return result | ||
if (!this.hasOwnProperty('_events')) {return this} | ||
const data = this._events | ||
let data = this._events | ||
const hasRemover = data.removeListener | ||
if (isRegExp(type) && data[RegExpEventSymbol]) { | ||
data = data[RegExpEventSymbol] | ||
} | ||
if (!data[type]) {return this} | ||
@@ -212,3 +251,3 @@ const listeners = data[type] | ||
delete data[type] | ||
if (data.removeListener) {this.emit('removeListener', type, listener)} | ||
if (hasRemover) {this.emit('removeListener', type, listener)} | ||
} else if (isObject(listeners)) { | ||
@@ -230,3 +269,3 @@ let i = listeners.length | ||
} | ||
if (data.removeListener) {this.emit('removeListener', type, listener)} | ||
if (hasRemover) {this.emit('removeListener', type, listener)} | ||
} | ||
@@ -244,3 +283,4 @@ return this | ||
if (!this.hasOwnProperty('_events')) {return this} | ||
const data = this._events | ||
let data = this._events | ||
const regExpEvents = data[RegExpEventSymbol] | ||
// not listening for removeListener, no need to emit | ||
@@ -251,3 +291,7 @@ if (!data.removeListener) { | ||
Object.keys(data).forEach(key => delete data[key]) | ||
if (regExpEvents) {delete data[RegExpEventSymbol]} | ||
} else { | ||
if (isRegExp(type) && regExpEvents) { | ||
data = regExpEvents | ||
} | ||
delete data[type] | ||
@@ -260,10 +304,19 @@ } | ||
for (const key in data) { | ||
if (key === 'removeListener') {continue} | ||
if (key === 'removeListener' || key === RegExpEventSymbol) {continue} | ||
this.removeAllListeners(key) | ||
} | ||
if (regExpEvents) { | ||
for (const key in regExpEvents) { | ||
this.removeAllListeners(toRegExp(key)) | ||
} | ||
} | ||
this.removeAllListeners('removeListener') | ||
// delete this._events | ||
Object.keys(data).forEach(key => delete data[key]) | ||
// Object.keys(data).forEach(key => delete data[key]) | ||
if (regExpEvents) {delete data[RegExpEventSymbol]} | ||
return this | ||
} | ||
if ( isRegExp(type) && regExpEvents) { | ||
data = regExpEvents | ||
} | ||
const listeners = data[type] | ||
@@ -304,2 +357,20 @@ if (isFunction(listeners)){ | ||
} | ||
const regExpEvents = data[RegExpEventSymbol] | ||
if (regExpEvents) { | ||
const matched = [] | ||
for (let key in regExpEvents) { | ||
key = toRegExp(key) | ||
if (key && key.test(type)) { | ||
const listener = regExpEvents[key] | ||
if (isArray(listener)) { | ||
matched.push.apply(matched, listener) | ||
} else { | ||
matched.push(listener) | ||
} | ||
} | ||
} | ||
if (matched.length) { | ||
listeners = listeners ? !isObject(listeners) ? [listeners].concat(matched) : listeners.concat(matched) : matched | ||
} | ||
} | ||
if (!listeners) {return} | ||
@@ -306,0 +377,0 @@ if (!isObject(listeners)) { |
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
145924
2322
252