Skip to content

Commit

Permalink
Fix: PromiseProxy Deprecation for Save (#8025)
Browse files Browse the repository at this point in the history
* promise-proxies: Fix "Cannot read properties of undefined (reading 'bind')" error

* fix count

Co-authored-by: Tobias Bieniek <tobias@bieniek.cloud>
  • Loading branch information
runspired and Turbo87 authored Jul 15, 2022
1 parent 7a2fd39 commit ae20278
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
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>;
}

0 comments on commit ae20278

Please sign in to comment.