Async.js
17.2 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
/***
MochiKit.Async 0.80
See <http://mochikit.com/> for documentation, downloads, license, etc.
(c) 2005 Bob Ippolito. All rights Reserved.
***/
if (typeof(dojo) != 'undefined') {
dojo.provide("MochiKit.Async");
dojo.require("MochiKit.Base");
}
if (typeof(JSAN) != 'undefined') {
JSAN.use("MochiKit.Base", []);
}
try {
if (typeof(MochiKit.Base) == 'undefined') {
throw "";
}
} catch (e) {
throw "MochiKit.Async depends on MochiKit.Base!";
}
if (typeof(MochiKit.Async) == 'undefined') {
MochiKit.Async = {};
}
MochiKit.Async.NAME = "MochiKit.Async";
MochiKit.Async.VERSION = "0.80";
MochiKit.Async.__repr__ = function () {
return "[" + this.NAME + " " + this.VERSION + "]";
}
MochiKit.Async.toString = function () {
return this.__repr__();
}
MochiKit.Async.AlreadyCalledError = function (deferred) {
/***
Raised by the Deferred if callback or errback happens
after it was already fired.
***/
this.deferred = deferred;
};
MochiKit.Async.AlreadyCalledError.prototype = new MochiKit.Base.NamedError("MochiKit.Async.AlreadyCalledError");
MochiKit.Async.CancelledError = function (deferred) {
/***
Raised by the Deferred cancellation mechanism.
***/
this.deferred = deferred;
};
MochiKit.Async.CancelledError.prototype = new MochiKit.Base.NamedError("MochiKit.Async.CancelledError");
MochiKit.Async.BrowserComplianceError = function (msg) {
/***
Raised when the JavaScript runtime is not capable of performing
the given function. Technically, this should really never be
raised because a non-conforming JavaScript runtime probably
isn't going to support exceptions in the first place.
***/
this.message = msg;
};
MochiKit.Async.BrowserComplianceError.prototype = new MochiKit.Base.NamedError("MochiKit.Async.BrowserComplianceError");
MochiKit.Async.GenericError = function (msg) {
this.message = msg;
};
MochiKit.Async.GenericError.prototype = new MochiKit.Base.NamedError("MochiKit.Async.GenericError");
MochiKit.Async.XMLHttpRequestError = function (req, msg) {
/***
Raised when an XMLHttpRequest does not complete for any reason.
***/
this.req = req;
this.message = msg;
try {
// Strange but true that this can raise in some cases.
this.number = req.status;
} catch (e) {
// pass
}
};
MochiKit.Async.XMLHttpRequestError.prototype = new MochiKit.Base.NamedError("MochiKit.Async.XMLHttpRequestError");
MochiKit.Async.Deferred = function (/* optional */ canceller) {
/***
Encapsulates a sequence of callbacks in response to a value that
may not yet be available. This is modeled after the Deferred class
from Twisted <http://twistedmatrix.com>.
Why do we want this? JavaScript has no threads, and even if it did,
threads are hard. Deferreds are a way of abstracting non-blocking
events, such as the final response to an XMLHttpRequest.
The sequence of callbacks is internally represented as a list
of 2-tuples containing the callback/errback pair. For example,
the following call sequence::
var d = new Deferred();
d.addCallback(myCallback);
d.addErrback(myErrback);
d.addBoth(myBoth);
d.addCallbacks(myCallback, myErrback);
is translated into a Deferred with the following internal
representation::
[
[myCallback, null],
[null, myErrback],
[myBoth, myBoth],
[myCallback, myErrback]
]
The Deferred also keeps track of its current status (fired).
Its status may be one of three things:
-1: no value yet (initial condition)
0: success
1: error
A Deferred will be in the error state if one of the following
three conditions are met:
1. The result given to callback or errback is "instanceof" Error
2. The previous callback or errback raised an exception while executing
3. The previous callback or errback returned a value "instanceof" Error
Otherwise, the Deferred will be in the success state. The state of the
Deferred determines the next element in the callback sequence to run.
When a callback or errback occurs with the example deferred chain, something
equivalent to the following will happen (imagine that exceptions are caught
and returned)::
// d.callback(result) or d.errback(result)
if (!(result instanceof Error)) {
result = myCallback(result);
}
if (result instanceof Error) {
result = myErrback(result);
}
result = myBoth(result);
if (result instanceof Error) {
result = myErrback(result);
} else {
result = myCallback(result);
}
The result is then stored away in case another step is added to the
callback sequence. Since the Deferred already has a value available,
any new callbacks added will be called immediately.
There are two other "advanced" details about this implementation that are
useful:
Callbacks are allowed to return Deferred instances themselves, so
you can build complicated sequences of events with ease.
The creator of the Deferred may specify a canceller. The canceller
is a function that will be called if Deferred.cancel is called before
the Deferred fires. You can use this to implement clean aborting of an
XMLHttpRequest, etc. Note that cancel will fire the deferred with a
CancelledError (unless your canceller returns another kind of error),
so the errbacks should be prepared to handle that error for cancellable
Deferreds.
***/
this.chain = [];
this.id = this._nextId();
this.fired = -1;
this.paused = 0;
this.results = [null, null];
this.canceller = canceller;
this.silentlyCancelled = false;
};
MochiKit.Async.Deferred.prototype.repr = function () {
var state;
if (this.fired == -1) {
state = 'unfired';
} else if (this.fired == 0) {
state = 'success';
} else {
state = 'error';
}
return 'Deferred(' + this.id + ', ' + state + ')';
};
MochiKit.Async.Deferred.prototype.toString = MochiKit.Base.forward("repr");
MochiKit.Async.Deferred.prototype._nextId = (function () {
var x = 0;
return function () {
return ++x;
}
})();
MochiKit.Async.Deferred.prototype.cancel = function () {
/***
Cancels a Deferred that has not yet received a value,
or is waiting on another Deferred as its value.
If a canceller is defined, the canceller is called.
If the canceller did not return an error, or there
was no canceller, then the errback chain is started
with CancelledError.
***/
if (this.fired == -1) {
if (this.canceller) {
this.canceller(this);
} else {
this.silentlyCancelled = true;
}
if (this.fired == -1) {
this.errback(new MochiKit.Async.CancelledError(this));
}
} else if ((this.fired == 0) && (this.results[0] instanceof MochiKit.Async.Deferred)) {
this.results[0].cancel();
}
};
MochiKit.Async.Deferred.prototype._pause = function () {
/***
Used internally to signal that it's waiting on another Deferred
***/
this.paused++;
};
MochiKit.Async.Deferred.prototype._unpause = function () {
/***
Used internally to signal that it's no longer waiting on another
Deferred.
***/
this.paused--;
if ((this.paused == 0) && (this.fired >= 0)) {
this._fire();
}
};
MochiKit.Async.Deferred.prototype._continue = function (res) {
/***
Used internally when a dependent deferred fires.
***/
this._resback(res);
this._unpause();
};
MochiKit.Async.Deferred.prototype._resback = function (res) {
/***
The primitive that means either callback or errback
***/
this.fired = ((res instanceof Error) ? 1 : 0);
this.results[this.fired] = res;
this._fire();
};
MochiKit.Async.Deferred.prototype._check = function () {
if (this.fired != -1) {
if (!this.silentlyCancelled) {
throw new MochiKit.Async.AlreadyCalledError(this);
}
this.silentlyCancelled = false;
return;
}
};
MochiKit.Async.Deferred.prototype.callback = function (res) {
/***
Begin the callback sequence with a non-error value.
callback or errback should only be called once
on a given Deferred.
***/
this._check();
this._resback(res);
};
MochiKit.Async.Deferred.prototype.errback = function (res) {
/***
Begin the callback sequence with an error result.
callback or errback should only be called once
on a given Deferred.
***/
this._check();
if (!(res instanceof Error)) {
res = new MochiKit.Async.GenericError(res);
}
this._resback(res);
};
MochiKit.Async.Deferred.prototype.addBoth = function (fn) {
/***
Add the same function as both a callback and an errback as the
next element on the callback sequence. This is useful for code
that you want to guarantee to run, e.g. a finalizer.
***/
return this.addCallbacks(fn, fn);
};
MochiKit.Async.Deferred.prototype.addCallback = function (fn) {
/***
Add a single callback to the end of the callback sequence.
***/
return this.addCallbacks(fn, null);
};
MochiKit.Async.Deferred.prototype.addErrback = function (fn) {
/***
Add a single errback to the end of the callback sequence.
***/
return this.addCallbacks(null, fn);
};
MochiKit.Async.Deferred.prototype.addCallbacks = function (cb, eb) {
/***
Add separate callback and errback to the end of the callback
sequence.
***/
this.chain.push([cb, eb])
if (this.fired >= 0) {
this._fire();
}
return this;
};
MochiKit.Async.Deferred.prototype._fire = function () {
/***
Used internally to exhaust the callback sequence when a result
is available.
***/
var chain = this.chain;
var fired = this.fired;
var res = this.results[fired];
var self = this;
var cb = null;
while (chain.length > 0 && this.paused == 0) {
// Array
var pair = chain.shift();
var f = pair[fired];
if (f == null) {
continue;
}
try {
res = f(res);
fired = ((res instanceof Error) ? 1 : 0);
if (res instanceof MochiKit.Async.Deferred) {
cb = function (res) {
self._continue(res);
}
this._pause();
}
} catch (err) {
fired = 1;
res = err;
}
}
this.fired = fired;
this.results[fired] = res;
if (cb && this.paused) {
// this is for "tail recursion" in case the dependent deferred
// is already fired
res.addBoth(cb);
}
};
MochiKit.Async.evalJSONRequest = function (req) {
/***
Evaluate a JSON (JavaScript Object Notation) XMLHttpRequest
@param req: The request whose responseText is to be evaluated
@rtype: L{Object}
***/
return eval('(' + req.responseText + ')');
};
MochiKit.Async.succeed = function (/* optional */result) {
/***
Return a Deferred that has already had '.callback(result)' called.
This is useful when you're writing synchronous code to an asynchronous
interface: i.e., some code is calling you expecting a Deferred result,
but you don't actually need to do anything asynchronous. Just return
succeed(theResult).
See L{fail} for a version of this function that uses a failing Deferred
rather than a successful one.
@param result: The result to give to the Deferred's 'callback' method.
@rtype: L{Deferred}
***/
var d = new MochiKit.Async.Deferred();
d.callback.apply(d, arguments);
return d;
};
MochiKit.Async.fail = function (/* optional */result) {
/***
Return a Deferred that has already had '.errback(result)' called.
See L{succeed}'s docstring for rationale.
@param result: The same argument that L{Deferred.errback} takes.
@rtype: L{Deferred}
***/
var d = new MochiKit.Async.Deferred();
d.errback.apply(d, arguments);
return d;
};
MochiKit.Async.getXMLHttpRequest = function () {
var self = arguments.callee;
if (!self.XMLHttpRequest) {
var tryThese = [
function () { return new XMLHttpRequest(); },
function () { return new ActiveXObject('Msxml2.XMLHTTP'); },
function () { return new ActiveXObject('Microsoft.XMLHTTP'); },
function () { return new ActiveXObject('Msxml2.XMLHTTP.4.0'); },
function () {
throw new MochiKit.Async.BrowserComplianceError("Browser does not support XMLHttpRequest");
}
];
for (var i = 0; i < tryThese.length; i++) {
var func = tryThese[i];
try {
self.XMLHttpRequest = func;
return func();
} catch (e) {
// pass
}
}
}
return self.XMLHttpRequest();
};
MochiKit.Async.sendXMLHttpRequest = function (req, /* optional */ sendContent) {
if (typeof(sendContent) == 'undefined') {
send = null;
}
var canceller = function () {
// IE SUCKS
try {
req.onreadystatechange = null;
} catch (e) {
try {
req.onreadystatechange = function () {};
} catch (e) {
}
}
req.abort();
}
var d = new MochiKit.Async.Deferred(canceller);
var onreadystatechange = function () {
// MochiKit.Logging.logDebug('req.readyState', req.readyState);
if (req.readyState == 4) {
// IE SUCKS
try {
req.onreadystatechange = null;
} catch (e) {
try {
req.onreadystatechange = function () {};
} catch (e) {
}
}
var status = null;
try {
status = req.status;
if (!status && MochiKit.Base.isNotEmpty(req.responseText)) {
// 0 or undefined seems to mean cached or local
status = 304;
}
} catch (e) {
// pass
// MochiKit.Logging.logDebug('error getting status?', repr(items(e)));
}
// 200 is OK, 304 is NOT_MODIFIED
if (status == 200 || status == 304) { // OK
d.callback(req);
} else {
var err = new MochiKit.Async.XMLHttpRequestError(req, "Request failed");
if (err.number) {
// XXX: This seems to happen on page change
d.errback(err);
} else {
// MochiKit.Logging.logDebug("Ignoring XMLHttpRequest, undefined status");
}
}
}
}
try {
req.onreadystatechange = onreadystatechange;
req.send(sendContent);
} catch (e) {
try {
req.onreadystatechange = null;
} catch (ignore) {
// pass
}
d.errback(e);
}
return d;
}
MochiKit.Async.doSimpleXMLHttpRequest = function (url) {
var req = MochiKit.Async.getXMLHttpRequest();
req.open("GET", url, true);
return MochiKit.Async.sendXMLHttpRequest(req);
};
MochiKit.Async.loadJSONDoc = function (url) {
/***
Do a simple XMLHttpRequest to a URL and get the response
as a JSON document.
@param url: The URL to GET
@rtype: L{Deferred} returning the evaluated JSON response
***/
var d = MochiKit.Async.doSimpleXMLHttpRequest(url);
d = d.addCallback(MochiKit.Async.evalJSONRequest);
return d;
};
MochiKit.Async.wait = function (seconds, /* optional */value) {
var d = new MochiKit.Async.Deferred();
var bind = MochiKit.Base.bind;
var partial = MochiKit.Base.partial;
if (typeof(value) != 'undefined') {
d.addCallback(function () { return value; });
}
var timeout = setTimeout(bind(d.callback, d), Math.floor(seconds * 1000));
d.canceller = partial(clearTimeout, timeout);
return d;
};
MochiKit.Async.callLater = function (seconds, func) {
var m = MochiKit.Base;
var func = m.partial.apply(null, m.extend(null, arguments, 1));
return MochiKit.Async.wait(seconds).addCallback(
function (res) { return func(); }
);
};
MochiKit.Async.EXPORT = [
"AlreadyCalledError",
"CancelledError",
"BrowserComplianceError",
"GenericError",
"XMLHttpRequestError",
"Deferred",
"succeed",
"fail",
"getXMLHttpRequest",
"doSimpleXMLHttpRequest",
"loadJSONDoc",
"wait",
"callLater",
"sendXMLHttpRequest"
];
MochiKit.Async.EXPORT_OK = [
"evalJSONRequest"
];
MochiKit.Async.__new__ = function () {
this.EXPORT_TAGS = {
":common": this.EXPORT,
":all": MochiKit.Base.concat(this.EXPORT, this.EXPORT_OK)
};
MochiKit.Base.nameFunctions(this);
};
MochiKit.Async.__new__();
if ((typeof(JSAN) == 'undefined' && typeof(dojo) == 'undefined')
|| (typeof(MochiKit.__compat__) == 'boolean' && MochiKit.__compat__)) {
(function (self) {
var all = self.EXPORT_TAGS[":all"];
for (var i = 0; i < all.length; i++) {
this[all[i]] = self[all[i]];
}
})(MochiKit.Async);
}