Skip to content
This repository has been archived by the owner on Dec 1, 2023. It is now read-only.

Commit

Permalink
v2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Steffan committed Mar 15, 2018
1 parent eab9c31 commit 6cd9fab
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 74 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ $ npm install vue-event-manager
```

### CDN
Available on [jsdelivr](https://cdn.jsdelivr.net/npm/vue-event-manager@1.0.6) or [unpkg](https://unpkg.com/vue-event-manager@1.0.6).
Available on [jsdelivr](https://cdn.jsdelivr.net/npm/vue-event-manager@2.0.0) or [unpkg](https://unpkg.com/vue-event-manager@2.0.0).
```html
<script src="https://cdn.jsdelivr.net/npm/vue-event-manager@1.0.6"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-event-manager@2.0.0"></script>
```

## Example
Expand Down
75 changes: 52 additions & 23 deletions dist/vue-event-manager.common.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* vue-event-manager v1.0.6
* vue-event-manager v2.0.0
* https://github.com/pagekit/vue-event-manager
* Released under the MIT License.
*/
Expand All @@ -16,6 +16,10 @@ function isObject(obj) {
return obj !== null && typeof obj === 'object';
}

function isUndefined(obj) {
return typeof obj === 'undefined';
}

function forEach(collection, callback) {
Object.keys(collection || {}).forEach(function (key) {
callback.call(null, collection[key], key);
Expand Down Expand Up @@ -43,6 +47,7 @@ if (!Array.prototype.findIndex) {
var o = Object(this);
var len = o.length >>> 0;
var thisArg = arguments[1];

var k = 0;

while (k < len) {
Expand Down Expand Up @@ -75,9 +80,8 @@ EventManager.prototype.on = function on (event, callback, priority) {
if ( priority === void 0 ) priority = 0;


var listeners = this.listeners[event] || [], index;

index = listeners.findIndex(function (listener) { return listener.priority < priority; });
var listeners = this.listeners[event] || [];
var index = listeners.findIndex(function (listener) { return listener.priority < priority; });

if (~index) {
listeners.splice(index, 0, {callback: callback, priority: priority});
Expand All @@ -96,11 +100,11 @@ EventManager.prototype.off = function off (event, callback) {
delete this.listeners[event];
}

var listeners = this.listeners[event], index;
var listeners = this.listeners[event];

if (listeners && callback) {

index = listeners.findIndex(function (listener) { return listener.callback === callback; });
var index = listeners.findIndex(function (listener) { return listener.callback === callback; });

if (~index) {
listeners.splice(index, 1);
Expand All @@ -109,36 +113,61 @@ EventManager.prototype.off = function off (event, callback) {
};

EventManager.prototype.trigger = function trigger (event, params, asynch) {
if ( params === void 0 ) params = [];
if ( asynch === void 0 ) asynch = false;


if (!isArray(params)) {
params = [params];
}
var $event = new Event(event, params);
var reject = function (result) { return Promise.reject(result); };
var resolve = function (result) { return !isUndefined(result) ? result : $event.result; };
var reducer = function (result, ref) {
var callback = ref.callback;

return ((this.listeners[event] || []).concat()).reduce(function (result, listener) {

var callback = function (result) {
var next = function (result) {

if (!isUndefined(result)) {
$event.result = result;
}

if (result === false) {
return result;
$event.stopPropagation();
}

if (isArray(result)) {
params = result;
if ($event.isPropagationStopped()) {
return $event.result;
}

return listener.callback.apply(listener.callback, params);
return callback.apply(callback, [$event].concat($event.params));
};

if (asynch) {
return result.then(callback);
}
return asynch ? result.then(next, reject) : next(result);
};

var listeners = (this.listeners[event] || []).concat();
var result = listeners.reduce(reducer, asynch ? Promise.resolve() : undefined);

return asynch ? result.then(resolve, reject) : resolve(result);
};

return callback(result);
var Event = function Event(name, params) {
if ( params === void 0 ) params = [];

}, asynch ? Promise.resolve() : undefined);

if (!isArray(params)) {
params = [params];
}

this.name = name;
this.params = params;
this.result = undefined;
};

Event.prototype.stopPropagation = function stopPropagation () {
this.stop = true;
};

Event.prototype.isPropagationStopped = function isPropagationStopped () {
return this.stop === true;
};

/**
Expand All @@ -163,9 +192,9 @@ function initEvents() {
var this$1 = this;


var _events = [];
var ref = this.$options;
var events = ref.events;
var _events = [];

if (events) {

Expand All @@ -192,7 +221,7 @@ function bindListener(fn, vm) {
if (typeof fn === 'string') {
return function () {
return vm[fn].apply(vm, arguments);
}
};
}

return fn.bind(vm);
Expand Down
75 changes: 52 additions & 23 deletions dist/vue-event-manager.es2015.js → dist/vue-event-manager.esm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* vue-event-manager v1.0.6
* vue-event-manager v2.0.0
* https://github.com/pagekit/vue-event-manager
* Released under the MIT License.
*/
Expand All @@ -14,6 +14,10 @@ function isObject(obj) {
return obj !== null && typeof obj === 'object';
}

function isUndefined(obj) {
return typeof obj === 'undefined';
}

function forEach(collection, callback) {
Object.keys(collection || {}).forEach(function (key) {
callback.call(null, collection[key], key);
Expand Down Expand Up @@ -41,6 +45,7 @@ if (!Array.prototype.findIndex) {
var o = Object(this);
var len = o.length >>> 0;
var thisArg = arguments[1];

var k = 0;

while (k < len) {
Expand Down Expand Up @@ -73,9 +78,8 @@ EventManager.prototype.on = function on (event, callback, priority) {
if ( priority === void 0 ) priority = 0;


var listeners = this.listeners[event] || [], index;

index = listeners.findIndex(function (listener) { return listener.priority < priority; });
var listeners = this.listeners[event] || [];
var index = listeners.findIndex(function (listener) { return listener.priority < priority; });

if (~index) {
listeners.splice(index, 0, {callback: callback, priority: priority});
Expand All @@ -94,11 +98,11 @@ EventManager.prototype.off = function off (event, callback) {
delete this.listeners[event];
}

var listeners = this.listeners[event], index;
var listeners = this.listeners[event];

if (listeners && callback) {

index = listeners.findIndex(function (listener) { return listener.callback === callback; });
var index = listeners.findIndex(function (listener) { return listener.callback === callback; });

if (~index) {
listeners.splice(index, 1);
Expand All @@ -107,36 +111,61 @@ EventManager.prototype.off = function off (event, callback) {
};

EventManager.prototype.trigger = function trigger (event, params, asynch) {
if ( params === void 0 ) params = [];
if ( asynch === void 0 ) asynch = false;


if (!isArray(params)) {
params = [params];
}
var $event = new Event(event, params);
var reject = function (result) { return Promise.reject(result); };
var resolve = function (result) { return !isUndefined(result) ? result : $event.result; };
var reducer = function (result, ref) {
var callback = ref.callback;

return ((this.listeners[event] || []).concat()).reduce(function (result, listener) {

var callback = function (result) {
var next = function (result) {

if (!isUndefined(result)) {
$event.result = result;
}

if (result === false) {
return result;
$event.stopPropagation();
}

if (isArray(result)) {
params = result;
if ($event.isPropagationStopped()) {
return $event.result;
}

return listener.callback.apply(listener.callback, params);
return callback.apply(callback, [$event].concat($event.params));
};

if (asynch) {
return result.then(callback);
}
return asynch ? result.then(next, reject) : next(result);
};

var listeners = (this.listeners[event] || []).concat();
var result = listeners.reduce(reducer, asynch ? Promise.resolve() : undefined);

return asynch ? result.then(resolve, reject) : resolve(result);
};

return callback(result);
var Event = function Event(name, params) {
if ( params === void 0 ) params = [];

}, asynch ? Promise.resolve() : undefined);

if (!isArray(params)) {
params = [params];
}

this.name = name;
this.params = params;
this.result = undefined;
};

Event.prototype.stopPropagation = function stopPropagation () {
this.stop = true;
};

Event.prototype.isPropagationStopped = function isPropagationStopped () {
return this.stop === true;
};

/**
Expand All @@ -161,9 +190,9 @@ function initEvents() {
var this$1 = this;


var _events = [];
var ref = this.$options;
var events = ref.events;
var _events = [];

if (events) {

Expand All @@ -190,7 +219,7 @@ function bindListener(fn, vm) {
if (typeof fn === 'string') {
return function () {
return vm[fn].apply(vm, arguments);
}
};
}

return fn.bind(vm);
Expand Down
Loading

0 comments on commit 6cd9fab

Please sign in to comment.