refactor: take heat off of TraitProxy

This commit is contained in:
cha0s 2019-04-12 14:11:26 -05:00
parent 037e130006
commit f27e303f5a
2 changed files with 55 additions and 12 deletions

View File

@ -6,11 +6,56 @@ import {Resource} from '@avocado/resource';
import {Traits} from './traits';
// Cache builtins to avoid HasProperty
const entityBuiltins = [
'$$events',
'$$namespaces',
'_instanceUuid',
'_traits',
'_uri',
'_uuid',
'addListener',
'addTrait',
'addTraits',
'allTraitInstances',
'allTraitTypes',
'emit',
'fromJSON',
'is',
'hydrate',
'instanceUuid',
'invokeHook',
'invokeHookFlat',
'lookupEmitListeners',
'off',
'offSingleEvent',
'on',
'_on',
'once',
'onSingleEvent',
'patchState',
'regenerateUuid',
'removeAllTraits',
'removeEventListenersFor',
'removeListener',
'removeTrait',
'removeTraits',
'state',
'tick',
'toJSON',
'uuid',
'uri',
];
const entityBuiltinsMap = new Map();
for (const entityBuiltin of entityBuiltins) {
entityBuiltinsMap.set(entityBuiltin, true);
}
class TraitProxy {
has(entity, property, receiver) {
if (property in entity) {
return Reflect.has(entity, property, receiver);
const isBuiltin = entityBuiltinsMap.get(property);
if (isBuiltin) {
return true;
}
else {
return entity._traits.hasProperty(property);
@ -18,8 +63,9 @@ class TraitProxy {
}
get(entity, property, receiver) {
if (property in entity) {
return Reflect.get(entity, property, receiver);
const isBuiltin = entityBuiltinsMap.get(property);
if (isBuiltin) {
return entity[property];
}
else {
return entity._traits.getProperty(property);
@ -27,15 +73,12 @@ class TraitProxy {
}
set(entity, property, value, receiver) {
if (
(property in entity)
|| !entity._traits.setProperty(property, value, receiver)
) {
return Reflect.set(entity, property, value, receiver);
}
else {
const isBuiltin = entityBuiltinsMap.get(property);
if (isBuiltin) {
entity[property] = value;
return true;
}
return entity._traits.setProperty(property, value, receiver);
}
}

View File

@ -41,9 +41,9 @@ export class Resource {
}
constructor() {
this._instanceUuid = uuid();
this._uri = undefined;
this._uuid = undefined;
this._instanceUuid = uuid();
}
fromJSON({uri, uuid}) {