-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathdeprecated-promise-proxy.ts
76 lines (63 loc) · 2.33 KB
/
deprecated-promise-proxy.ts
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
import { deprecate } from '@ember/debug';
import { get } from '@ember/object';
import { DEBUG } from '@glimmer/env';
import { resolve } from 'rsvp';
import { PromiseObject } from './promise-proxy-base';
function promiseObject<T>(promise: Promise<T>): PromiseObject<T> {
return PromiseObject.create({
promise: resolve(promise),
}) as PromiseObject<T>;
}
// constructor is accessed in some internals but not including it in the copyright for the deprecation
const ALLOWABLE_METHODS = ['constructor', 'then', 'catch', 'finally'];
const ALLOWABLE_PROPS = ['__ec_yieldable__', '__ec_cancel__'];
const PROXIED_OBJECT_PROPS = ['content', 'isPending', 'isSettled', 'isRejected', 'isFulfilled', 'promise', 'reason'];
const ProxySymbolString = String(Symbol.for('PROXY_CONTENT'));
export function deprecatedPromiseObject<T>(promise: Promise<T>): PromiseObject<T> {
const promiseObjectProxy: PromiseObject<T> = promiseObject(promise);
if (!DEBUG) {
return promiseObjectProxy;
}
const handler = {
get(target: object, prop: string, receiver: object): unknown {
if (typeof prop === 'symbol') {
if (String(prop) === ProxySymbolString) {
return;
}
return Reflect.get(target, prop, receiver);
}
if (prop === 'constructor') {
return target.constructor;
}
if (ALLOWABLE_PROPS.includes(prop)) {
return target[prop];
}
if (!ALLOWABLE_METHODS.includes(prop)) {
deprecate(
`Accessing ${prop} is deprecated. The return type is being changed from PromiseObjectProxy to a Promise. The only available methods to access on this promise are .then, .catch and .finally`,
false,
{
id: 'ember-data:model-save-promise',
until: '5.0',
for: '@ember-data/store',
since: {
available: '4.4',
enabled: '4.4',
},
}
);
} else {
return (target[prop] as () => unknown).bind(target);
}
if (PROXIED_OBJECT_PROPS.includes(prop)) {
return target[prop];
}
const value: unknown = get(target, prop);
if (value && typeof value === 'function' && typeof value.bind === 'function') {
return value.bind(receiver);
}
return undefined;
},
};
return new Proxy(promiseObjectProxy, handler);
}