Subversion Repositories Applications.papyrus

Rev

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