Skip to content

Commit

Permalink
refactor: isDescriptor() logic & syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Martinos committed Oct 11, 2023
1 parent 68adad4 commit 6533ee4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 30 deletions.
27 changes: 15 additions & 12 deletions addon/-private/ember-internals.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import __EMBER_METAL__ from '@ember/-internals/metal/index';
import * as EMBER_METAL from '@ember/-internals/metal/index';

const POSSIBLE_DECORATORS = ['AliasDecoratorImpl', 'ComputedDecoratorImpl'];

export function getDependentKeys(descriptorOrDecorator) {
if (__EMBER_METAL__ && __EMBER_METAL__.descriptorForDecorator) {
let descriptor = __EMBER_METAL__.descriptorForDecorator(
descriptorOrDecorator,
);
if (EMBER_METAL && EMBER_METAL.descriptorForDecorator) {
let descriptor = EMBER_METAL.descriptorForDecorator(descriptorOrDecorator);
return descriptor._dependentKeys || [descriptor.altKey];
} else {
return descriptorOrDecorator._dependentKeys;
}
}

export function isDescriptor(o) {
if (__EMBER_METAL__ && __EMBER_METAL__.isClassicDecorator) {
return __EMBER_METAL__.isClassicDecorator(o);
} else {
return (
o && (typeof o === 'object' || typeof o === 'function') && o.isDescriptor
);
}
const isClassicDecorator =
EMBER_METAL &&
EMBER_METAL.isClassicDecorator &&
EMBER_METAL.isClassicDecorator(o);
const _isDescriptor =
o && (typeof o === 'object' || typeof o === 'function') && o.isDescriptor;
const isOtherDecoratorImpl = POSSIBLE_DECORATORS.includes(
o?.constructor?.name,
);
return isClassicDecorator || _isDescriptor || isOtherDecoratorImpl;
}
11 changes: 3 additions & 8 deletions addon/-private/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { isDescriptor } from '../utils/utils';
const { keys } = Object;
const OPTION_KEYS = '__option_keys__';

const POSSIBLE_DECORATORS = ['AliasDecoratorImpl', 'ComputedDecoratorImpl'];

const OptionsObject = EmberObject.extend({
toObject() {
return this[OPTION_KEYS].reduce((obj, key) => {
Expand All @@ -19,15 +17,12 @@ export default class Options {
constructor({ model, attribute, options = {} }) {
const optionKeys = keys(options);
const createParams = { [OPTION_KEYS]: optionKeys, model, attribute };
const someOptionsAreCPs = optionKeys.some((key) => {
return (
isDescriptor(options[key]) ||
POSSIBLE_DECORATORS.includes(options[key]?.constructor?.name)
);
const someOptionsAreDescriptors = optionKeys.some((key) => {
return isDescriptor(options[key]);
});

// If any of the options is a CP, we need to create a custom class for it
if (someOptionsAreCPs) {
if (someOptionsAreDescriptors) {
return OptionsObject.extend(options).create(createParams);
}

Expand Down
11 changes: 1 addition & 10 deletions addon/utils/deep-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,6 @@ import { isDescriptor } from './utils';
import { isNone } from '@ember/utils';
import EmberObject, { defineProperty, set, get } from '@ember/object';

const POSSIBLE_DECORATORS = ['AliasDecoratorImpl', 'ComputedDecoratorImpl'];

function isCP(value) {
return (
isDescriptor(value) ||
POSSIBLE_DECORATORS.includes(value?.constructor?.name)
);
}

export default function deepSet(
obj,
path,
Expand All @@ -38,7 +29,7 @@ export default function deepSet(
currObj = get(currObj, key);
}

if (isCP(value)) {
if (isDescriptor(value)) {
defineProperty(currObj, keyPath[lastKeyIndex], value);
} else {
set(currObj, keyPath[lastKeyIndex], value);
Expand Down

0 comments on commit 6533ee4

Please sign in to comment.