-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseContext.js
30 lines (27 loc) · 949 Bytes
/
BaseContext.js
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
export default class BaseContext {
constructor(initialValue) {
this._currentValue = initialValue;
this._subscribers = [];
}
updateValue(newValue) {
// console.log('Updating context:', newValue);
this._currentValue = newValue;
this._subscribers.forEach(subscriber => {
subscriber();
// console.log('Subscriber notified:', subscriber);
});
}
subscribe(subscriber) {
// console.log('Subscribing to context:', subscriber);
this._subscribers.push(subscriber);
// console.log('Current Subscribers:', this._subscribers);
return () => {
this._subscribers = this._subscribers.filter(sub => sub !== subscriber);
// console.log('Unsubscribing from context:', subscriber);
// console.log('Remaining Subscribers:', this._subscribers);
};
}
get value() {
return this._currentValue;
}
}