Skip to content

Commit

Permalink
Merge branch 'caridy/wire-reform-2' of github.com:salesforce/lwc into…
Browse files Browse the repository at this point in the history
… caridy/wire-reform-2
  • Loading branch information
caridy committed Aug 29, 2019
2 parents d02243b + 5d5f7af commit 8b6c978
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 23 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
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@
* 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)
.toString(16)
.substring(1);
}

register(Provider, eventTarget => {
const adapterEventTargetCallback = eventTarget => {
let unsubscribeCallback;

function callback(data, unsubscribe) {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -37,7 +37,8 @@ function createContextPayload(value) {
return value;
}

register(Provider, eventTarget => {
// register(Provider, eventTarget => {
const adapterEventTargetCallback = eventTarget => {
let unsubscribeCallback;

function callback(value, unsubscribe) {
Expand All @@ -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);
Expand Down

0 comments on commit 8b6c978

Please sign in to comment.