-
-
Notifications
You must be signed in to change notification settings - Fork 147
/
element-property-accessor.ts
65 lines (54 loc) · 1.57 KB
/
element-property-accessor.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { IIndexable } from '@aurelia/kernel';
import {
IAccessor,
ILifecycle,
LifecycleFlags,
Priority,
} from '@aurelia/runtime';
export class ElementPropertyAccessor implements IAccessor<unknown> {
public readonly lifecycle: ILifecycle;
public readonly obj: IIndexable;
public readonly propertyKey: string;
public currentValue: unknown;
public oldValue: unknown;
public hasChanges: boolean;
public priority: Priority;
constructor(
lifecycle: ILifecycle,
obj: IIndexable,
propertyKey: string,
) {
this.lifecycle = lifecycle;
this.obj = obj;
this.propertyKey = propertyKey;
this.currentValue = void 0;
this.oldValue = void 0;
this.hasChanges = false;
this.priority = Priority.propagate;
}
public getValue(): unknown {
return this.currentValue;
}
public setValue(newValue: string | null, flags: LifecycleFlags): void {
this.currentValue = newValue;
this.hasChanges = newValue !== this.oldValue;
if ((flags & LifecycleFlags.fromBind) > 0) {
this.flushRAF(flags);
}
}
public flushRAF(flags: LifecycleFlags): void {
if (this.hasChanges) {
this.hasChanges = false;
const { currentValue } = this;
this.oldValue = currentValue;
this.obj[this.propertyKey] = currentValue;
}
}
public bind(flags: LifecycleFlags): void {
this.lifecycle.enqueueRAF(this.flushRAF, this, this.priority);
this.currentValue = this.oldValue = this.obj[this.propertyKey];
}
public unbind(flags: LifecycleFlags): void {
this.lifecycle.dequeueRAF(this.flushRAF, this);
}
}