Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: PromiseProxy Deprecation for Save #8025

Merged
merged 2 commits into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/-ember-data/tests/integration/records/save-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,17 @@ module('integration/records/save - Save Record', function (hooks) {
assert.ok(true, 'save operation was resolved');
if (DEPRECATE_SAVE_PROMISE_ACCESS) {
assert.strictEqual(saved.get('id'), '123');
assert.strictEqual(saved.id, undefined);
} else {
assert.strictEqual(saved.id, undefined);
}
assert.strictEqual(model, post, 'resolves with the model');
if (DEPRECATE_SAVE_PROMISE_ACCESS) {
assert.expectDeprecation({ id: 'ember-data:model-save-promise', count: 2 });
// We don't care about the exact value of the property, but accessing it
// should not throw an error and only show a deprecation.
assert.strictEqual(saved.__ec_cancel__, undefined);

assert.expectDeprecation({ id: 'ember-data:model-save-promise', count: 4 });
}
});

Expand Down
2 changes: 1 addition & 1 deletion packages/model/addon/-private/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ class Model extends EmberObject {
save(options) {
const promise = this._internalModel.save(options).then(() => this);
if (DEPRECATE_SAVE_PROMISE_ACCESS) {
return deprecatedPromiseObject(PromiseObject.create({ promise }));
return deprecatedPromiseObject(promise);
}

return promise;
Expand Down
12 changes: 9 additions & 3 deletions packages/store/addon/-private/system/promise-proxies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ export function promiseArray<I, T extends EmberArrayLike<I>>(promise: Promise<T>
// constructor is accessed in some internals but not including it in the copyright for the deprecation
const ALLOWABLE_METHODS = ['constructor', 'then', 'catch', 'finally'];

export function deprecatedPromiseObject<T>(promise: PromiseObjectProxy<T>): PromiseObjectProxy<T> {
export function deprecatedPromiseObject<T>(promise: Promise<T>): PromiseObjectProxy<T> {
const promiseObjectProxy: PromiseObjectProxy<T> = promiseObject(promise);
const handler = {
get(target: object, prop: string, receiver?: object): unknown {
if (!ALLOWABLE_METHODS.includes(prop)) {
Expand All @@ -124,9 +125,14 @@ export function deprecatedPromiseObject<T>(promise: PromiseObjectProxy<T>): Prom
);
}

return (Reflect.get(target, prop, receiver) as Function).bind(target);
const value: unknown = Reflect.get(target, prop, receiver);
if (value && typeof value === 'function' && typeof value.bind === 'function') {
return value.bind(target);
}

return value;
},
};

return new Proxy(promise, handler) as PromiseObjectProxy<T>;
return new Proxy(promiseObjectProxy, handler) as PromiseObjectProxy<T>;
}