UNPKG

4.55 kBTypeScriptView Raw
1declare module '@ember/object/promise-proxy-mixin' {
2 import Mixin from '@ember/object/mixin';
3 /**
4 A low level mixin making ObjectProxy promise-aware.
5
6 ```javascript
7 import { resolve } from 'rsvp';
8 import $ from 'jquery';
9 import ObjectProxy from '@ember/object/proxy';
10 import PromiseProxyMixin from '@ember/object/promise-proxy-mixin';
11
12 let ObjectPromiseProxy = ObjectProxy.extend(PromiseProxyMixin);
13
14 let proxy = ObjectPromiseProxy.create({
15 promise: resolve($.getJSON('/some/remote/data.json'))
16 });
17
18 proxy.then(function(json){
19 // the json
20 }, function(reason) {
21 // the reason why you have no json
22 });
23 ```
24
25 the proxy has bindable attributes which
26 track the promises life cycle
27
28 ```javascript
29 proxy.get('isPending') //=> true
30 proxy.get('isSettled') //=> false
31 proxy.get('isRejected') //=> false
32 proxy.get('isFulfilled') //=> false
33 ```
34
35 When the $.getJSON completes, and the promise is fulfilled
36 with json, the life cycle attributes will update accordingly.
37 Note that $.getJSON doesn't return an ECMA specified promise,
38 it is useful to wrap this with an `RSVP.resolve` so that it behaves
39 as a spec compliant promise.
40
41 ```javascript
42 proxy.get('isPending') //=> false
43 proxy.get('isSettled') //=> true
44 proxy.get('isRejected') //=> false
45 proxy.get('isFulfilled') //=> true
46 ```
47
48 As the proxy is an ObjectProxy, and the json now its content,
49 all the json properties will be available directly from the proxy.
50
51 ```javascript
52 // Assuming the following json:
53 {
54 firstName: 'Stefan',
55 lastName: 'Penner'
56 }
57
58 // both properties will accessible on the proxy
59 proxy.get('firstName') //=> 'Stefan'
60 proxy.get('lastName') //=> 'Penner'
61 ```
62
63 @class PromiseProxyMixin
64 @public
65 */
66 interface PromiseProxyMixin<T> {
67 /**
68 If the proxied promise is rejected this will contain the reason
69 provided.
70
71 @property reason
72 @default null
73 @public
74 */
75 reason: unknown;
76 /**
77 Once the proxied promise has settled this will become `false`.
78
79 @property isPending
80 @default true
81 @public
82 */
83 readonly isPending: boolean;
84 /**
85 Once the proxied promise has settled this will become `true`.
86
87 @property isSettled
88 @default false
89 @public
90 */
91 readonly isSettled: boolean;
92 /**
93 Will become `true` if the proxied promise is rejected.
94
95 @property isRejected
96 @default false
97 @public
98 */
99 isRejected: boolean;
100 /**
101 Will become `true` if the proxied promise is fulfilled.
102
103 @property isFulfilled
104 @default false
105 @public
106 */
107 isFulfilled: boolean;
108 /**
109 The promise whose fulfillment value is being proxied by this object.
110
111 This property must be specified upon creation, and should not be
112 changed once created.
113
114 Example:
115
116 ```javascript
117 import ObjectProxy from '@ember/object/proxy';
118 import PromiseProxyMixin from '@ember/object/promise-proxy-mixin';
119
120 ObjectProxy.extend(PromiseProxyMixin).create({
121 promise: <thenable>
122 });
123 ```
124
125 @property promise
126 @public
127 */
128 promise: Promise<T>;
129 /**
130 An alias to the proxied promise's `then`.
131
132 See RSVP.Promise.then.
133
134 @method then
135 @param {Function} callback
136 @return {RSVP.Promise}
137 @public
138 */
139 then: this['promise']['then'];
140 /**
141 An alias to the proxied promise's `catch`.
142
143 See RSVP.Promise.catch.
144
145 @method catch
146 @param {Function} callback
147 @return {RSVP.Promise}
148 @since 1.3.0
149 @public
150 */
151 catch: this['promise']['catch'];
152 /**
153 An alias to the proxied promise's `finally`.
154
155 See RSVP.Promise.finally.
156
157 @method finally
158 @param {Function} callback
159 @return {RSVP.Promise}
160 @since 1.3.0
161 @public
162 */
163 finally: this['promise']['finally'];
164 }
165 const PromiseProxyMixin: Mixin;
166 export default PromiseProxyMixin;
167}