Skip to content

Commit

Permalink
Merge pull request #757 from wagenet/no-evaluation-of-getters
Browse files Browse the repository at this point in the history
Don't evaluate getters when looking for handlers
  • Loading branch information
NullVoxPopuli authored Aug 23, 2024
2 parents 594dcc8 + 8b9a841 commit 77bdb1e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
31 changes: 23 additions & 8 deletions addon/src/decorators/key-responder.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
import { inject as service } from '@ember/service';
import { registerDestructor } from '@ember/destroyable';

function findPropertyDescriptor(obj, name) {
let descriptor;
do {
(descriptor = Object.getOwnPropertyDescriptor(obj, name)) ||
(obj = Object.getPrototypeOf(obj));
} while (!descriptor && obj);
return descriptor;
}

function populateKeyboardHandlers(responder) {
responder.keyboardHandlers = responder.keyboardHandlers || {};
if (!responder.keyboardHandlerNames) {
responder.keyboardHandlerNames = {};
for (let propertyName in responder) {
let propertyValue = responder[propertyName];
if (
typeof propertyValue === 'function' &&
propertyValue._emberKeyboardOnKeyDecoratorData
) {
for (let listenerName of propertyValue._emberKeyboardOnKeyDecoratorData
.listenerNames || []) {
responder.keyboardHandlerNames[listenerName] = propertyName;
const descriptor = findPropertyDescriptor(responder, propertyName);
// If it has a getter it's not going to be a function. We also don't want to force getters to
// evaluate as they may be expensive. This has been observed specifically with classic classes
// and computed properties.
if (!descriptor?.get) {
let propertyValue = responder[propertyName];
if (
typeof propertyValue === 'function' &&
propertyValue._emberKeyboardOnKeyDecoratorData
) {
for (let listenerName of propertyValue
._emberKeyboardOnKeyDecoratorData.listenerNames || []) {
responder.keyboardHandlerNames[listenerName] = propertyName;
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions test-app/app/components/decorator-example3/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/* eslint-disable ember/no-classic-classes */
/* eslint-disable ember/no-classic-components */
import Component from '@ember/component';
import { computed } from '@ember/object';
import { keyResponder, onKey } from 'ember-keyboard';

export default keyResponder(
Expand All @@ -26,5 +27,10 @@ export default keyResponder(
}
})
),

// This exists to ensure that we don't call getters when looking for handlers
doNotCall: computed(function () {
throw new Error('This should not be called');
}),
})
);

0 comments on commit 77bdb1e

Please sign in to comment.