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

Commit 6cd9fab

Browse files
author
Steffan
committed
v2.0.0
1 parent eab9c31 commit 6cd9fab

File tree

6 files changed

+161
-74
lines changed

6 files changed

+161
-74
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ $ npm install vue-event-manager
1717
```
1818

1919
### CDN
20-
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).
20+
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).
2121
```html
22-
<script src="https://cdn.jsdelivr.net/npm/vue-event-manager@1.0.6"></script>
22+
<script src="https://cdn.jsdelivr.net/npm/vue-event-manager@2.0.0"></script>
2323
```
2424

2525
## Example

dist/vue-event-manager.common.js

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* vue-event-manager v1.0.6
2+
* vue-event-manager v2.0.0
33
* https://github.com/pagekit/vue-event-manager
44
* Released under the MIT License.
55
*/
@@ -16,6 +16,10 @@ function isObject(obj) {
1616
return obj !== null && typeof obj === 'object';
1717
}
1818

19+
function isUndefined(obj) {
20+
return typeof obj === 'undefined';
21+
}
22+
1923
function forEach(collection, callback) {
2024
Object.keys(collection || {}).forEach(function (key) {
2125
callback.call(null, collection[key], key);
@@ -43,6 +47,7 @@ if (!Array.prototype.findIndex) {
4347
var o = Object(this);
4448
var len = o.length >>> 0;
4549
var thisArg = arguments[1];
50+
4651
var k = 0;
4752

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

7782

78-
var listeners = this.listeners[event] || [], index;
79-
80-
index = listeners.findIndex(function (listener) { return listener.priority < priority; });
83+
var listeners = this.listeners[event] || [];
84+
var index = listeners.findIndex(function (listener) { return listener.priority < priority; });
8185

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

99-
var listeners = this.listeners[event], index;
103+
var listeners = this.listeners[event];
100104

101105
if (listeners && callback) {
102106

103-
index = listeners.findIndex(function (listener) { return listener.callback === callback; });
107+
var index = listeners.findIndex(function (listener) { return listener.callback === callback; });
104108

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

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

115118

116-
if (!isArray(params)) {
117-
params = [params];
118-
}
119+
var $event = new Event(event, params);
120+
var reject = function (result) { return Promise.reject(result); };
121+
var resolve = function (result) { return !isUndefined(result) ? result : $event.result; };
122+
var reducer = function (result, ref) {
123+
var callback = ref.callback;
119124

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

122-
var callback = function (result) {
126+
var next = function (result) {
127+
128+
if (!isUndefined(result)) {
129+
$event.result = result;
130+
}
123131

124132
if (result === false) {
125-
return result;
133+
$event.stopPropagation();
126134
}
127135

128-
if (isArray(result)) {
129-
params = result;
136+
if ($event.isPropagationStopped()) {
137+
return $event.result;
130138
}
131139

132-
return listener.callback.apply(listener.callback, params);
140+
return callback.apply(callback, [$event].concat($event.params));
133141
};
134142

135-
if (asynch) {
136-
return result.then(callback);
137-
}
143+
return asynch ? result.then(next, reject) : next(result);
144+
};
145+
146+
var listeners = (this.listeners[event] || []).concat();
147+
var result = listeners.reduce(reducer, asynch ? Promise.resolve() : undefined);
148+
149+
return asynch ? result.then(resolve, reject) : resolve(result);
150+
};
138151

139-
return callback(result);
152+
var Event = function Event(name, params) {
153+
if ( params === void 0 ) params = [];
140154

141-
}, asynch ? Promise.resolve() : undefined);
155+
156+
if (!isArray(params)) {
157+
params = [params];
158+
}
159+
160+
this.name = name;
161+
this.params = params;
162+
this.result = undefined;
163+
};
164+
165+
Event.prototype.stopPropagation = function stopPropagation () {
166+
this.stop = true;
167+
};
168+
169+
Event.prototype.isPropagationStopped = function isPropagationStopped () {
170+
return this.stop === true;
142171
};
143172

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

165194

195+
var _events = [];
166196
var ref = this.$options;
167197
var events = ref.events;
168-
var _events = [];
169198

170199
if (events) {
171200

@@ -192,7 +221,7 @@ function bindListener(fn, vm) {
192221
if (typeof fn === 'string') {
193222
return function () {
194223
return vm[fn].apply(vm, arguments);
195-
}
224+
};
196225
}
197226

198227
return fn.bind(vm);

dist/vue-event-manager.es2015.js renamed to dist/vue-event-manager.esm.js

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* vue-event-manager v1.0.6
2+
* vue-event-manager v2.0.0
33
* https://github.com/pagekit/vue-event-manager
44
* Released under the MIT License.
55
*/
@@ -14,6 +14,10 @@ function isObject(obj) {
1414
return obj !== null && typeof obj === 'object';
1515
}
1616

17+
function isUndefined(obj) {
18+
return typeof obj === 'undefined';
19+
}
20+
1721
function forEach(collection, callback) {
1822
Object.keys(collection || {}).forEach(function (key) {
1923
callback.call(null, collection[key], key);
@@ -41,6 +45,7 @@ if (!Array.prototype.findIndex) {
4145
var o = Object(this);
4246
var len = o.length >>> 0;
4347
var thisArg = arguments[1];
48+
4449
var k = 0;
4550

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

7580

76-
var listeners = this.listeners[event] || [], index;
77-
78-
index = listeners.findIndex(function (listener) { return listener.priority < priority; });
81+
var listeners = this.listeners[event] || [];
82+
var index = listeners.findIndex(function (listener) { return listener.priority < priority; });
7983

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

97-
var listeners = this.listeners[event], index;
101+
var listeners = this.listeners[event];
98102

99103
if (listeners && callback) {
100104

101-
index = listeners.findIndex(function (listener) { return listener.callback === callback; });
105+
var index = listeners.findIndex(function (listener) { return listener.callback === callback; });
102106

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

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

113116

114-
if (!isArray(params)) {
115-
params = [params];
116-
}
117+
var $event = new Event(event, params);
118+
var reject = function (result) { return Promise.reject(result); };
119+
var resolve = function (result) { return !isUndefined(result) ? result : $event.result; };
120+
var reducer = function (result, ref) {
121+
var callback = ref.callback;
117122

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

120-
var callback = function (result) {
124+
var next = function (result) {
125+
126+
if (!isUndefined(result)) {
127+
$event.result = result;
128+
}
121129

122130
if (result === false) {
123-
return result;
131+
$event.stopPropagation();
124132
}
125133

126-
if (isArray(result)) {
127-
params = result;
134+
if ($event.isPropagationStopped()) {
135+
return $event.result;
128136
}
129137

130-
return listener.callback.apply(listener.callback, params);
138+
return callback.apply(callback, [$event].concat($event.params));
131139
};
132140

133-
if (asynch) {
134-
return result.then(callback);
135-
}
141+
return asynch ? result.then(next, reject) : next(result);
142+
};
143+
144+
var listeners = (this.listeners[event] || []).concat();
145+
var result = listeners.reduce(reducer, asynch ? Promise.resolve() : undefined);
146+
147+
return asynch ? result.then(resolve, reject) : resolve(result);
148+
};
136149

137-
return callback(result);
150+
var Event = function Event(name, params) {
151+
if ( params === void 0 ) params = [];
138152

139-
}, asynch ? Promise.resolve() : undefined);
153+
154+
if (!isArray(params)) {
155+
params = [params];
156+
}
157+
158+
this.name = name;
159+
this.params = params;
160+
this.result = undefined;
161+
};
162+
163+
Event.prototype.stopPropagation = function stopPropagation () {
164+
this.stop = true;
165+
};
166+
167+
Event.prototype.isPropagationStopped = function isPropagationStopped () {
168+
return this.stop === true;
140169
};
141170

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

163192

193+
var _events = [];
164194
var ref = this.$options;
165195
var events = ref.events;
166-
var _events = [];
167196

168197
if (events) {
169198

@@ -190,7 +219,7 @@ function bindListener(fn, vm) {
190219
if (typeof fn === 'string') {
191220
return function () {
192221
return vm[fn].apply(vm, arguments);
193-
}
222+
};
194223
}
195224

196225
return fn.bind(vm);

0 commit comments

Comments
 (0)