-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathconnect-base.ts
141 lines (117 loc) · 3.47 KB
/
connect-base.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { AfterContentInit, Input, OnDestroy } from '@angular/core';
import {
AbstractControl,
FormArray,
FormControl,
FormGroup,
NgControl,
} from '@angular/forms';
import { Subscription } from 'rxjs';
import { Unsubscribe } from 'redux';
import { debounceTime } from 'rxjs/operators';
import { FormStore } from '../form-store';
import { State } from '../state';
export interface ControlPair {
path: string[];
control: AbstractControl;
}
export class ConnectBase implements OnDestroy, AfterContentInit {
get path(): string[] {
const path =
typeof this.connect === 'function' ? this.connect() : this.connect;
switch (typeof path) {
case 'object':
if (State.empty(path)) {
return [];
}
if (Array.isArray(path)) {
return path as string[];
}
case 'string':
return (path as string).split(/\./g);
default:
// fallthrough above (no break)
throw new Error(
`Cannot determine path to object: ${JSON.stringify(path)}`,
);
}
}
@Input() connect?: () => (string | number) | (string | number)[];
protected store?: FormStore;
protected form: any;
private stateSubscription?: Unsubscribe;
private formSubscription?: Subscription;
ngOnDestroy() {
if (this.formSubscription) {
this.formSubscription.unsubscribe();
}
if (typeof this.stateSubscription === 'function') {
this.stateSubscription(); // unsubscribe
}
}
ngAfterContentInit() {
Promise.resolve().then(() => {
this.resetState();
if (this.store) {
this.stateSubscription = this.store.subscribe(() => this.resetState());
}
Promise.resolve().then(() => {
this.formSubscription = (this.form.valueChanges as any)
.pipe(debounceTime(0))
.subscribe((values: any) => this.publish(values));
});
});
}
private descendants(path: string[], formElement: any): ControlPair[] {
const pairs = new Array<ControlPair>();
if (formElement instanceof FormArray) {
formElement.controls.forEach((c, index) => {
for (const d of this.descendants((path as any).concat([index]), c)) {
pairs.push(d);
}
});
} else if (formElement instanceof FormGroup) {
for (const k of Object.keys(formElement.controls)) {
pairs.push({
path: path.concat([k]),
control: formElement.controls[k],
});
}
} else if (
formElement instanceof NgControl ||
formElement instanceof FormControl
) {
return [{ path, control: formElement as any }];
} else {
throw new Error(
`Unknown type of form element: ${formElement.constructor.name}`,
);
}
return pairs.filter(p => {
const parent = (p.control as any)._parent;
return parent === this.form.control || parent === this.form;
});
}
private resetState() {
const formElement =
this.form.control === undefined ? this.form : this.form.control;
const children = this.descendants([], formElement);
children.forEach(c => {
const { path, control } = c;
const value = State.get(this.getState(), this.path.concat(path));
if (control.value !== value) {
control.setValue(value);
}
});
}
private publish(value: any) {
if (this.store) {
this.store.valueChanged(this.path, this.form, value);
}
}
private getState() {
if (this.store) {
return this.store.getState();
}
}
}