-
Notifications
You must be signed in to change notification settings - Fork 382
/
observed-fields.ts
43 lines (40 loc) · 1.4 KB
/
observed-fields.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* Copyright (c) 2018, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import { ArrayReduce, isFalse } from '@lwc/shared';
import { ComponentInterface } from './component';
import { getAssociatedVM } from './vm';
import { valueMutated, valueObserved } from '../libs/mutation-tracker';
export function createObservedFieldsDescriptorMap(fields: PropertyKey[]): PropertyDescriptorMap {
return ArrayReduce.call(
fields,
(acc: PropertyDescriptorMap, field) => {
acc[field] = createObservedFieldPropertyDescriptor(field);
return acc;
},
{}
) as PropertyDescriptorMap;
}
function createObservedFieldPropertyDescriptor(key: PropertyKey): PropertyDescriptor {
return {
get(this: ComponentInterface): any {
const vm = getAssociatedVM(this);
valueObserved(this, key);
return vm.cmpTrack[key];
},
set(this: ComponentInterface, newValue: any) {
const vm = getAssociatedVM(this);
if (newValue !== vm.cmpTrack[key]) {
vm.cmpTrack[key] = newValue;
if (isFalse(vm.isDirty)) {
valueMutated(this, key);
}
}
},
enumerable: true,
configurable: true,
};
}