Skip to content

Commit 244e4cb

Browse files
committed
Code optimization
1 parent 5c7287a commit 244e4cb

1 file changed

Lines changed: 10 additions & 23 deletions

File tree

src/app/utils/databinding.ts

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,6 @@ export const Notifications = (function () {
100100
observers: [],
101101
propName
102102
});
103-
state.subscribe(value => {
104-
Notifications.next({
105-
state,
106-
value,
107-
});
108-
});
109103
},
110104
attach(state: {}, obj: unknown) {
111105
assert.instanceOf(state, DynamicProperty, `Wrong parameter ${state}. Should be of ${DynamicProperty.name}`);
@@ -131,28 +125,21 @@ export const Notifications = (function () {
131125
})();
132126

133127
class DynamicProperty<T> {
134-
private subscribers: Array<(v: T) => void> = [];
135-
136-
constructor(public value: T, public get = () => this.value, public set = (val: T) => this.value = val) {
137-
138-
}
128+
constructor(
129+
public value: T,
130+
public get = () => this.value,
131+
public set = (val: T) => this.value = val
132+
) { }
139133

140134
getValue() {
141135
return this.get();
142136
}
143137

144-
subscribe(a: (v: unknown) => void) {
145-
this.subscribers.push(a);
146-
}
147-
148138
next(v: T) {
149139
this.set(v);
150-
this.subscribers.forEach(s => {
151-
try {
152-
s(v);
153-
} catch (ex) {
154-
console.error('Unexpected error', ex);
155-
}
140+
Notifications.next({
141+
state: this,
142+
value: v,
156143
});
157144
}
158145
}
@@ -185,8 +172,8 @@ export function State<T>(target: T, propName: string, descriptor?: PropertyDescr
185172
}
186173

187174
const opts = {
188-
get: initState,
189-
set: initState,
175+
get: function (this: T) { return initState.call(this).get(); },
176+
set: function (this: T, v: unknown) { initState.call(this).next(v); },
190177
enumerable: true,
191178
configurable: true
192179
};

0 commit comments

Comments
 (0)