Subversion Repositories Applications.papyrus

Rev

Rev 1371 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
11
dojo.provide("dojo.Deferred");
12
dojo.require("dojo.lang.func");
13
dojo.Deferred = function (canceller) {
14
	this.chain = [];
15
	this.id = this._nextId();
16
	this.fired = -1;
17
	this.paused = 0;
18
	this.results = [null, null];
19
	this.canceller = canceller;
20
	this.silentlyCancelled = false;
21
};
22
dojo.lang.extend(dojo.Deferred, {getFunctionFromArgs:function () {
23
	var a = arguments;
24
	if ((a[0]) && (!a[1])) {
25
		if (dojo.lang.isFunction(a[0])) {
26
			return a[0];
27
		} else {
28
			if (dojo.lang.isString(a[0])) {
29
				return dj_global[a[0]];
30
			}
31
		}
32
	} else {
33
		if ((a[0]) && (a[1])) {
34
			return dojo.lang.hitch(a[0], a[1]);
35
		}
36
	}
37
	return null;
38
}, makeCalled:function () {
39
	var deferred = new dojo.Deferred();
40
	deferred.callback();
41
	return deferred;
42
}, repr:function () {
43
	var state;
44
	if (this.fired == -1) {
45
		state = "unfired";
46
	} else {
47
		if (this.fired == 0) {
48
			state = "success";
49
		} else {
50
			state = "error";
51
		}
52
	}
53
	return "Deferred(" + this.id + ", " + state + ")";
54
}, toString:dojo.lang.forward("repr"), _nextId:(function () {
55
	var n = 1;
56
	return function () {
57
		return n++;
58
	};
59
})(), cancel:function () {
60
	if (this.fired == -1) {
61
		if (this.canceller) {
62
			this.canceller(this);
63
		} else {
64
			this.silentlyCancelled = true;
65
		}
66
		if (this.fired == -1) {
67
			this.errback(new Error(this.repr()));
68
		}
69
	} else {
70
		if ((this.fired == 0) && (this.results[0] instanceof dojo.Deferred)) {
71
			this.results[0].cancel();
72
		}
73
	}
74
}, _pause:function () {
75
	this.paused++;
76
}, _unpause:function () {
77
	this.paused--;
78
	if ((this.paused == 0) && (this.fired >= 0)) {
79
		this._fire();
80
	}
81
}, _continue:function (res) {
82
	this._resback(res);
83
	this._unpause();
84
}, _resback:function (res) {
85
	this.fired = ((res instanceof Error) ? 1 : 0);
86
	this.results[this.fired] = res;
87
	this._fire();
88
}, _check:function () {
89
	if (this.fired != -1) {
90
		if (!this.silentlyCancelled) {
91
			dojo.raise("already called!");
92
		}
93
		this.silentlyCancelled = false;
94
		return;
95
	}
96
}, callback:function (res) {
97
	this._check();
98
	this._resback(res);
99
}, errback:function (res) {
100
	this._check();
101
	if (!(res instanceof Error)) {
102
		res = new Error(res);
103
	}
104
	this._resback(res);
105
}, addBoth:function (cb, cbfn) {
106
	var enclosed = this.getFunctionFromArgs(cb, cbfn);
107
	if (arguments.length > 2) {
108
		enclosed = dojo.lang.curryArguments(null, enclosed, arguments, 2);
109
	}
110
	return this.addCallbacks(enclosed, enclosed);
111
}, addCallback:function (cb, cbfn) {
112
	var enclosed = this.getFunctionFromArgs(cb, cbfn);
113
	if (arguments.length > 2) {
114
		enclosed = dojo.lang.curryArguments(null, enclosed, arguments, 2);
115
	}
116
	return this.addCallbacks(enclosed, null);
117
}, addErrback:function (cb, cbfn) {
118
	var enclosed = this.getFunctionFromArgs(cb, cbfn);
119
	if (arguments.length > 2) {
120
		enclosed = dojo.lang.curryArguments(null, enclosed, arguments, 2);
121
	}
122
	return this.addCallbacks(null, enclosed);
123
	return this.addCallbacks(null, cbfn);
124
}, addCallbacks:function (cb, eb) {
125
	this.chain.push([cb, eb]);
126
	if (this.fired >= 0) {
127
		this._fire();
128
	}
129
	return this;
130
}, _fire:function () {
131
	var chain = this.chain;
132
	var fired = this.fired;
133
	var res = this.results[fired];
134
	var self = this;
135
	var cb = null;
136
	while (chain.length > 0 && this.paused == 0) {
137
		var pair = chain.shift();
138
		var f = pair[fired];
139
		if (f == null) {
140
			continue;
141
		}
142
		try {
143
			res = f(res);
144
			fired = ((res instanceof Error) ? 1 : 0);
145
			if (res instanceof dojo.Deferred) {
146
				cb = function (res) {
147
					self._continue(res);
148
				};
149
				this._pause();
150
			}
151
		}
152
		catch (err) {
153
			fired = 1;
154
			res = err;
155
		}
156
	}
157
	this.fired = fired;
158
	this.results[fired] = res;
159
	if ((cb) && (this.paused)) {
160
		res.addBoth(cb);
161
	}
162
}});
163