From a901a6403cdb671a359af600febc4f19925080e9 Mon Sep 17 00:00:00 2001 From: Jose David Rodriguez Date: Thu, 29 Aug 2019 22:41:50 -0700 Subject: [PATCH 1/2] wip: wire register is broken it was making karma fail --- .../test/context/advanced-context.spec.js | 6 +++--- .../test/context/simple-context.spec.js | 6 +++--- .../x/advancedProvider/advancedProvider.js | 15 +++++++++++---- .../context/x/simpleProvider/simpleProvider.js | 16 ++++++++++++---- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/packages/integration-karma/test/context/advanced-context.spec.js b/packages/integration-karma/test/context/advanced-context.spec.js index ac883638ec..674b90fd12 100644 --- a/packages/integration-karma/test/context/advanced-context.spec.js +++ b/packages/integration-karma/test/context/advanced-context.spec.js @@ -1,6 +1,6 @@ -import { createElement, register } from 'lwc'; -import { registerWireService } from 'wire-service'; -registerWireService(register); +import { createElement } from 'lwc'; +// import { registerWireService } from 'wire-service'; +// registerWireService(register); import { installCustomContext, getValueForIdentity } from 'x/advancedProvider'; import Consumer from 'x/advancedConsumer'; import { setValueForIdentity } from './x/advancedProvider/advancedProvider'; diff --git a/packages/integration-karma/test/context/simple-context.spec.js b/packages/integration-karma/test/context/simple-context.spec.js index f3f08cb008..93a9205a6d 100644 --- a/packages/integration-karma/test/context/simple-context.spec.js +++ b/packages/integration-karma/test/context/simple-context.spec.js @@ -1,6 +1,6 @@ -import { createElement, register } from 'lwc'; -import { registerWireService } from 'wire-service'; -registerWireService(register); +import { createElement } from 'lwc'; +// import { registerWireService } from 'wire-service'; +// registerWireService(register); import { installCustomContext, setCustomContext } from 'x/simpleProvider'; import Consumer from 'x/simpleConsumer'; diff --git a/packages/integration-karma/test/context/x/advancedProvider/advancedProvider.js b/packages/integration-karma/test/context/x/advancedProvider/advancedProvider.js index 5cbd8608a1..21757b27b8 100644 --- a/packages/integration-karma/test/context/x/advancedProvider/advancedProvider.js +++ b/packages/integration-karma/test/context/x/advancedProvider/advancedProvider.js @@ -7,13 +7,13 @@ * identity of the consumer is not tracked. */ -import { register, ValueChangedEvent, LinkContextEvent } from 'wire-service'; +import { WireAdapter, ValueChangedEvent, LinkContextEvent } from 'wire-service'; const { addEventListener } = Document.prototype; const IdentityMetaMap = new WeakMap(); const UniqueEventName = `advanced_context_event_${guid()}`; -const Provider = Symbol('SimpleContextProvider'); +// const Provider = Symbol('SimpleContextProvider'); function guid() { return Math.floor((1 + Math.random()) * 0x10000) @@ -21,7 +21,7 @@ function guid() { .substring(1); } -register(Provider, eventTarget => { +const adapterEventTargetCallback = eventTarget => { let unsubscribeCallback; function callback(data, unsubscribe) { @@ -47,7 +47,14 @@ register(Provider, eventTarget => { unsubscribeCallback = undefined; // resetting it to support reinsertion } }); -}); +}; + +const Provider = class extends WireAdapter { + constructor(dataCallback) { + super(dataCallback); + adapterEventTargetCallback(this.eventTarget); + } +}; function createNewConsumerMeta(provider, callback) { // identity must be an object that can't be proxified otherwise we diff --git a/packages/integration-karma/test/context/x/simpleProvider/simpleProvider.js b/packages/integration-karma/test/context/x/simpleProvider/simpleProvider.js index 785bb1472c..d626b42f7d 100644 --- a/packages/integration-karma/test/context/x/simpleProvider/simpleProvider.js +++ b/packages/integration-karma/test/context/x/simpleProvider/simpleProvider.js @@ -8,13 +8,13 @@ */ // Per Context Component Instance, track the current context data -import { register, ValueChangedEvent, LinkContextEvent } from 'wire-service'; +import { WireAdapter, ValueChangedEvent, LinkContextEvent } from 'wire-service'; const { addEventListener } = Document.prototype; const ContextValueMap = new WeakMap(); const UniqueEventName = `simple_context_event_${guid()}`; -const Provider = Symbol('SimpleContextProvider'); +// const Provider = Symbol('SimpleContextProvider'); function guid() { return Math.floor((1 + Math.random()) * 0x10000) @@ -37,7 +37,8 @@ function createContextPayload(value) { return value; } -register(Provider, eventTarget => { +// register(Provider, eventTarget => { +const adapterEventTargetCallback = eventTarget => { let unsubscribeCallback; function callback(value, unsubscribe) { @@ -62,7 +63,14 @@ register(Provider, eventTarget => { unsubscribeCallback = undefined; // resetting it to support reinsertion } }); -}); +}; + +const Provider = class extends WireAdapter { + constructor(dataCallback) { + super(dataCallback); + adapterEventTargetCallback(this.eventTarget); + } +}; function getContextData(eventTarget) { let contextData = ContextValueMap.get(eventTarget); From 5d5f7afd3097a279c75c61ca064477099af475af Mon Sep 17 00:00:00 2001 From: Jose David Rodriguez Date: Fri, 30 Aug 2019 13:30:34 -0700 Subject: [PATCH 2/2] fix: observable-fields --- .../engine/src/framework/decorators/register.ts | 12 ++++++------ packages/@lwc/engine/src/framework/def.ts | 8 +++++++- .../@lwc/engine/src/framework/observed-fields.ts | 16 ++++++++++++++-- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/packages/@lwc/engine/src/framework/decorators/register.ts b/packages/@lwc/engine/src/framework/decorators/register.ts index 3cfa170d57..5587c6b316 100644 --- a/packages/@lwc/engine/src/framework/decorators/register.ts +++ b/packages/@lwc/engine/src/framework/decorators/register.ts @@ -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, @@ -45,7 +44,7 @@ interface PropCompilerDef { interface WireCompilerDef { method?: number; adapter: WireAdapterConstructor; - configCallback: ConfigCallback; + config: ConfigCallback; } interface RegisterDecoratorMeta { readonly publicMethods?: MethodCompilerMeta; @@ -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); @@ -221,11 +220,10 @@ 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, { @@ -233,6 +231,7 @@ export function registerDecorators( apiFields, wiredMethods, wiredFields, + fields, }); return Ctor; } @@ -244,6 +243,7 @@ interface DecoratorMeta { readonly apiFields: string[]; readonly wiredMethods: string[]; readonly wiredFields: string[]; + readonly fields?: string[]; } function setDecoratorsMeta(Ctor: ComponentConstructor, meta: DecoratorMeta) { diff --git a/packages/@lwc/engine/src/framework/def.ts b/packages/@lwc/engine/src/framework/def.ts index 90d217cb9b..735c3c4252 100644 --- a/packages/@lwc/engine/src/framework/def.ts +++ b/packages/@lwc/engine/src/framework/def.ts @@ -23,7 +23,9 @@ import { isUndefined, isFunction, ArrayConcat, + defineProperties, } from '../shared/language'; +import { createObservedFieldsDescriptorMap } from './observed-fields'; import { getInternalField } from '../shared/fields'; import { resolveCircularModuleDependency, @@ -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 { @@ -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, diff --git a/packages/@lwc/engine/src/framework/observed-fields.ts b/packages/@lwc/engine/src/framework/observed-fields.ts index 2acd23b721..c703a465ca 100644 --- a/packages/@lwc/engine/src/framework/observed-fields.ts +++ b/packages/@lwc/engine/src/framework/observed-fields.ts @@ -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);