-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
promise_proxy.js
209 lines (168 loc) · 4.2 KB
/
promise_proxy.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { get } from 'ember-metal/property_get';
import setProperties from 'ember-metal/set_properties';
import { computed } from 'ember-metal/computed';
import { Mixin } from 'ember-metal/mixin';
import EmberError from 'ember-metal/error';
var not = computed.not;
var or = computed.or;
/**
@module ember
@submodule ember-runtime
*/
function tap(proxy, promise) {
setProperties(proxy, {
isFulfilled: false,
isRejected: false
});
return promise.then(function(value) {
setProperties(proxy, {
content: value,
isFulfilled: true
});
return value;
}, function(reason) {
setProperties(proxy, {
reason: reason,
isRejected: true
});
throw reason;
}, 'Ember: PromiseProxy');
}
/**
A low level mixin making ObjectProxy promise-aware.
```javascript
var ObjectPromiseProxy = Ember.ObjectProxy.extend(Ember.PromiseProxyMixin);
var proxy = ObjectPromiseProxy.create({
promise: $.getJSON('/some/remote/data.json')
});
proxy.then(function(json){
// the json
}, function(reason) {
// the reason why you have no json
});
```
the proxy has bindable attributes which
track the promises life cycle
```javascript
proxy.get('isPending') //=> true
proxy.get('isSettled') //=> false
proxy.get('isRejected') //=> false
proxy.get('isFulfilled') //=> false
```
When the the $.getJSON completes, and the promise is fulfilled
with json, the life cycle attributes will update accordingly.
```javascript
proxy.get('isPending') //=> false
proxy.get('isSettled') //=> true
proxy.get('isRejected') //=> false
proxy.get('isFulfilled') //=> true
```
As the proxy is an ObjectProxy, and the json now its content,
all the json properties will be available directly from the proxy.
```javascript
// Assuming the following json:
{
firstName: 'Stefan',
lastName: 'Penner'
}
// both properties will accessible on the proxy
proxy.get('firstName') //=> 'Stefan'
proxy.get('lastName') //=> 'Penner'
```
@class Ember.PromiseProxyMixin
@public
*/
export default Mixin.create({
/**
If the proxied promise is rejected this will contain the reason
provided.
@property reason
@default null
@public
*/
reason: null,
/**
Once the proxied promise has settled this will become `false`.
@property isPending
@default true
@public
*/
isPending: not('isSettled').readOnly(),
/**
Once the proxied promise has settled this will become `true`.
@property isSettled
@default false
@public
*/
isSettled: or('isRejected', 'isFulfilled').readOnly(),
/**
Will become `true` if the proxied promise is rejected.
@property isRejected
@default false
@public
*/
isRejected: false,
/**
Will become `true` if the proxied promise is fulfilled.
@property isFulfilled
@default false
@public
*/
isFulfilled: false,
/**
The promise whose fulfillment value is being proxied by this object.
This property must be specified upon creation, and should not be
changed once created.
Example:
```javascript
Ember.ObjectProxy.extend(Ember.PromiseProxyMixin).create({
promise: <thenable>
});
```
@property promise
@public
*/
promise: computed({
get() {
throw new EmberError('PromiseProxy\'s promise must be set');
},
set(key, promise) {
return tap(this, promise);
}
}),
/**
An alias to the proxied promise's `then`.
See RSVP.Promise.then.
@method then
@param {Function} callback
@return {RSVP.Promise}
@public
*/
then: promiseAlias('then'),
/**
An alias to the proxied promise's `catch`.
See RSVP.Promise.catch.
@method catch
@param {Function} callback
@return {RSVP.Promise}
@since 1.3.0
@public
*/
'catch': promiseAlias('catch'),
/**
An alias to the proxied promise's `finally`.
See RSVP.Promise.finally.
@method finally
@param {Function} callback
@return {RSVP.Promise}
@since 1.3.0
@public
*/
'finally': promiseAlias('finally')
});
function promiseAlias(name) {
return function () {
var promise = get(this, 'promise');
return promise[name](...arguments);
};
}