-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.process.js
189 lines (173 loc) · 6.92 KB
/
jquery.process.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
182
183
184
185
186
187
188
189
/**
* jQueryProcess
* https://github.com/nothrem/jQueryProcess
* Improved jQuery Deferred that will always remember scope given into the promise.
* Code based on jQuery code of $.Deferred function
* Code improved for better performance and extended by Nothrem Sinsky: https://github.com/nothrem
* (c) 2015
*/
(function($) {
if (!$) { return; }
$.Process = function( init ) {
if (!(this instanceof $.Process)) {
throw "Cannnot use Process constructor as a function!";
}
$.ProcessFactory.call(this, {}, init);
};
$.ProcessFactory = function( source, init ) {
var process;
if (this instanceof $.Process) {
process = this;
}
else {
process = new $.Process( init );
process.promise(source);
return process;
}
var tuples = [
// action, add listener, listener list, final state, listeners for process
// must be created every time because of the $.Callbacks()
[ "resolve", "done", $.Callbacks("once memory"), "resolved", $.Callbacks("once memory") ],
[ "reject", "fail", $.Callbacks("once memory"), "rejected", $.Callbacks("once memory") ],
[ "notify", "progress", $.Callbacks("memory"), undefined, $.Callbacks("memory") ]
],
events = ['on', 'one', 'off', 'trigger', 'triggerHandler'],
state = "pending",
promise = {
state: function() {
return state;
},
always: function() {
process.done( arguments ).fail( arguments );
return this;
},
sync: function(process) {
promise
.done(process.resolve)
.fail(process.reject)
.progress(process.notify);
return source;
},
promise: function( obj ) {
source = promise.promiseWith(obj);
return source;
},
promiseWith: function( obj ) {
return obj ? $.extend( obj, promise ) : source;
},
ajax: function( obj ) {
promise.promise(obj);
return promise.ajaxWith(obj);
},
ajaxWith: function( obj ) {
var jqXHR = promise.promiseWith(obj);
jqXHR.success = jqXHR.done;
jqXHR.error = jqXHR.fail;
return jqXHR;
},
//ECMAScript 2015 (ES6) compatible methods
all: function() {
var promises = [];
$.each(arguments, function() {
var me = this;
if (!me || 'function' !== typeof me.then) { return; }
promises.push(me);
me.then(function() {
var pos = promises.indexOf(me);
(pos < 0 || promises.splice(pos,1));
(promises.length || process.resolve());
}, process.reject);
});
return this;
},
race: function() {
$.each(arguments, function() {
(!this || 'function' != typeof this.then
|| this.then(process.resolve, process.reject));
});
return this;
},
then: function(done, fail, progress) {
(!done || this.done(done));
(!fail || this.fail(fail));
(!progress || this.progress(progress));
return this;
},
'catch': function(onRejected) {
this.fail(onRejected);
return this;
}
};
//var
// Add event-specific methods
$.each( events, function(i, method) {
promise[method] = function() {
var me = (this === process ? process : source);
$.fn[method].apply($(me), arguments);
return this;
};
} );
// Add list-specific methods
$.each( tuples, function( i, tuple ) {
var list = tuple[ 2 ],
listProc = tuple[ 4 ],
stateString = tuple[ 3 ];
// promise[ done | fail | progress ] = list.add
promise[tuple[1]] = function() {
if (this === process) {
listProc.add.apply(listProc, arguments);
}
else {
list.add.apply(list, arguments);
}
return this;
};
// Handle state
if ( stateString ) {
list.add(function() {
// state = [ resolved | rejected ]
state = stateString;
// [ reject_list | resolve_list ].disable; progress_list.lock
}, tuples[ i ^ 1 ][ 2 ].disable, tuples[ 2 ][ 2 ].lock );
}
// process[ resolve | reject | notify ]
process[ tuple[0] ] = function() {
process[ tuple[0] + "With" ]( source , arguments );
return this;
};
process[ tuple[0] + "Ajax" ] = function() {
var args = arguments, jqXHR = promise.ajax();
if ('resolve' === tuple[0]) { args = [args[0], args[1], jqXHR]; }
else if ('reject' === tuple[0]) { args = [jqXHR, args[0], args[1]]; }
window.setTimeout(function() { process[ tuple[0] + "With" ]( jqXHR , args ); }, 1);
return this;
};
process[tuple[0] + "With"] = function(obj, args){
if (state !== "pending") { return; } //already resolved, do not fire another event
if ('notify' === tuple[0] && 'string' === typeof args[0]) {
args[0] = args[0].replace(' ', '_');
$.fn.trigger.apply($(process), args);
$.fn.trigger.apply($(obj), args);
}
listProc.fireWith(process, args);
list.fireWith(obj, args);
return this;
};
});
//allow to register callback for specific event
process.onCallback = function(event) {
return function() {
process.notify(event, arguments);
};
};
// Make the process a promise
promise.promiseWith( process );
promise.promise( source || {});
// Call given func if any
if ( init ) {
init.call( process, process );
}
// All done!
return process;
};
})(window.jQuery);