Skip to content

Commit

Permalink
fix: observable-fields
Browse files Browse the repository at this point in the history
  • Loading branch information
jodarove committed Aug 30, 2019
1 parent a901a64 commit 5d5f7af
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
12 changes: 6 additions & 6 deletions packages/@lwc/engine/src/framework/decorators/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { ComponentConstructor } from '../component';
import { internalWireFieldDecorator } from './wire';
import { internalTrackDecorator } from './track';
import { createPublicPropertyDescriptor, createPublicAccessorDescriptor } from './api';
import { createObservedFieldPropertyDescriptor } from '../observed-fields';
import {
WireAdapterConstructor,
storeWiredMethodMeta,
Expand All @@ -45,7 +44,7 @@ interface PropCompilerDef {
interface WireCompilerDef {
method?: number;
adapter: WireAdapterConstructor;
configCallback: ConfigCallback;
config: ConfigCallback;
}
interface RegisterDecoratorMeta {
readonly publicMethods?: MethodCompilerMeta;
Expand Down Expand Up @@ -185,7 +184,7 @@ export function registerDecorators(
if (!isUndefined(wire)) {
for (const fieldOrMethodName in wire) {
const { adapter, method } = wire[fieldOrMethodName];
const { configCallback } = wire[fieldOrMethodName];
const configCallback = wire[fieldOrMethodName].config;
if (method === 1) {
if (process.env.NODE_ENV !== 'production') {
validateMethodDecoratedWithWire(Ctor, fieldOrMethodName);
Expand Down Expand Up @@ -221,18 +220,18 @@ export function registerDecorators(
}
}
if (!isUndefined(fields)) {
for (const fieldName in fields) {
for (let i = 0, n = fields.length; i < n; i++) {
if (process.env.NODE_ENV !== 'production') {
validateObservedField(Ctor, fieldName);
validateObservedField(Ctor, fields[i]);
}
defineProperty(proto, fieldName, createObservedFieldPropertyDescriptor(fieldName));
}
}
setDecoratorsMeta(Ctor, {
apiMethods,
apiFields,
wiredMethods,
wiredFields,
fields,
});
return Ctor;
}
Expand All @@ -244,6 +243,7 @@ interface DecoratorMeta {
readonly apiFields: string[];
readonly wiredMethods: string[];
readonly wiredFields: string[];
readonly fields?: string[];
}

function setDecoratorsMeta(Ctor: ComponentConstructor, meta: DecoratorMeta) {
Expand Down
8 changes: 7 additions & 1 deletion packages/@lwc/engine/src/framework/def.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import {
isUndefined,
isFunction,
ArrayConcat,
defineProperties,
} from '../shared/language';
import { createObservedFieldsDescriptorMap } from './observed-fields';
import { getInternalField } from '../shared/fields';
import {
resolveCircularModuleDependency,
Expand Down Expand Up @@ -99,7 +101,7 @@ function createComponentDef(
const { name } = meta;
let { template } = meta;
const decoratorsMeta = getDecoratorsMeta(Ctor);
const { apiFields, apiMethods, wiredFields, wiredMethods } = decoratorsMeta;
const { apiFields, apiMethods, wiredFields, wiredMethods, fields } = decoratorsMeta;
const proto = Ctor.prototype;

let {
Expand All @@ -125,6 +127,10 @@ function createComponentDef(
render = render || superDef.render;
template = template || superDef.template;

if (!isUndefined(fields)) {
defineProperties(proto, createObservedFieldsDescriptorMap(fields));
}

const def: ComponentDef = {
ctor: Ctor,
name,
Expand Down
16 changes: 14 additions & 2 deletions packages/@lwc/engine/src/framework/observed-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,21 @@ import { getComponentVM } from './vm';
import assert from '../shared/assert';
import { valueMutated, valueObserved } from '../libs/mutation-tracker';
import { isRendering, vmBeingRendered } from './invoker';
import { isFalse } from '../shared/language';
import { isFalse, ArrayReduce } from '../shared/language';

export function createObservedFieldPropertyDescriptor(key: string): PropertyDescriptor {
export function createObservedFieldsDescriptorMap(fields: PropertyKey[]): PropertyDescriptorMap {
return ArrayReduce.call(
fields,
(acc: PropertyDescriptorMap, field) => {
acc[field] = createObservedFieldPropertyDescriptor(field);

return acc;
},
{}
) as PropertyDescriptorMap;
}

function createObservedFieldPropertyDescriptor(key: string): PropertyDescriptor {
return {
get(this: ComponentInterface): any {
const vm = getComponentVM(this);
Expand Down

0 comments on commit 5d5f7af

Please sign in to comment.