-
Notifications
You must be signed in to change notification settings - Fork 327
/
Copy pathPublisherMethods.js
181 lines (157 loc) · 5.69 KB
/
PublisherMethods.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
var _ = require('./utils');
/**
* A module of methods for object that you want to be able to listen to.
* This module is consumed by `createStore` and `createAction`
*/
module.exports = {
/**
* Hook used by the publisher that is invoked before emitting
* and before `shouldEmit`. The arguments are the ones that the action
* is invoked with. If this function returns something other than
* undefined, that will be passed on as arguments for shouldEmit and
* emission.
*/
preEmit: function() {},
/**
* Hook used by the publisher after `preEmit` to determine if the
* event should be emitted with given arguments. This may be overridden
* in your application, default implementation always returns true.
*
* @returns {Boolean} true if event should be emitted
*/
shouldEmit: function() { return true; },
/**
* Subscribes the given callback for action triggered
*
* @param {Function} callback The callback to register as event handler
* @param {Mixed} [optional] bindContext The context to bind the callback with
* @returns {Function} Callback that unsubscribes the registered event handler
*/
listen: function(callback, bindContext) {
bindContext = bindContext || this;
var eventHandler = function(args) {
if (aborted){
return;
}
callback.apply(bindContext, args);
}, me = this, aborted = false;
this.emitter.addListener(this.eventLabel, eventHandler);
return function() {
aborted = true;
me.emitter.removeListener(me.eventLabel, eventHandler);
};
},
/**
* Attach handlers to promise that trigger the completed and failed
* child publishers, if available.
*
* @param {Object} The promise to attach to
*/
promise: function(promise) {
var me = this;
var canHandlePromise =
this.children.indexOf('completed') >= 0 &&
this.children.indexOf('failed') >= 0;
if (!canHandlePromise){
throw new Error('Publisher must have "completed" and "failed" child publishers');
}
promise.then(function(response) {
return me.completed(response);
}, function(error) {
return me.failed(error);
});
},
/**
* Subscribes the given callback for action triggered, which should
* return a promise that in turn is passed to `this.promise`
*
* @param {Function} callback The callback to register as event handler
*/
listenAndPromise: function(callback, bindContext) {
var me = this;
bindContext = bindContext || this;
this.willCallPromise = (this.willCallPromise || 0) + 1;
var removeListen = this.listen(function() {
if (!callback) {
throw new Error('Expected a function returning a promise but got ' + callback);
}
var args = arguments,
promise = callback.apply(bindContext, args);
return me.promise.call(me, promise);
}, bindContext);
return function () {
me.willCallPromise--;
removeListen.call(me);
};
},
/**
* Publishes an event using `this.emitter` (if `shouldEmit` agrees)
*/
trigger: function() {
var args = arguments,
pre = this.preEmit.apply(this, args);
args = pre === undefined ? args : _.isArguments(pre) ? pre : [].concat(pre);
if (this.shouldEmit.apply(this, args)) {
this.emitter.emit(this.eventLabel, args);
}
},
/**
* Tries to publish the event on the next tick
*/
triggerAsync: function(){
var args = arguments,me = this;
_.nextTick(function() {
me.trigger.apply(me, args);
});
},
/**
* Returns a Promise for the triggered action
*
* @return {Promise}
* Resolved by completed child action.
* Rejected by failed child action.
* If listenAndPromise'd, then promise associated to this trigger.
* Otherwise, the promise is for next child action completion.
*/
triggerPromise: function(){
var me = this;
var args = arguments;
var canHandlePromise =
this.children.indexOf('completed') >= 0 &&
this.children.indexOf('failed') >= 0;
var promise = _.createPromise(function(resolve, reject) {
// If `listenAndPromise` is listening
// patch `promise` w/ context-loaded resolve/reject
if (me.willCallPromise) {
_.nextTick(function() {
var old_promise_method = me.promise;
me.promise = function (promise) {
promise.then(resolve, reject);
// Back to your regularly schedule programming.
me.promise = old_promise_method;
return me.promise.apply(me, arguments);
};
me.trigger.apply(me, args);
});
return;
}
if (canHandlePromise) {
var removeSuccess = me.completed.listen(function(args) {
removeSuccess();
removeFailed();
resolve(args);
});
var removeFailed = me.failed.listen(function(args) {
removeSuccess();
removeFailed();
reject(args);
});
}
me.triggerAsync.apply(me, args);
if (!canHandlePromise) {
resolve();
}
});
return promise;
}
};