This repository has been archived on 2022-08-19. You can view files and clone it, but cannot push or open issues or pull requests.
hydrogen-web/src/observable/BaseObservableCollection.js
Bruno Windels 1f15ca6498 more WIP
2020-04-18 19:16:16 +02:00

80 lines
1.8 KiB
JavaScript

export default class BaseObservableCollection {
constructor() {
this._handlers = new Set();
}
onSubscribeFirst() {
}
onUnsubscribeLast() {
}
subscribe(handler) {
this._handlers.add(handler);
if (this._handlers.size === 1) {
this.onSubscribeFirst();
}
return () => {
if (handler) {
this._handlers.delete(handler);
if (this._handlers.size === 0) {
this.onUnsubscribeLast();
}
handler = null;
}
return null;
};
}
// Add iterator over handlers here
}
// like an EventEmitter, but doesn't have an event type
export class BaseObservableValue extends BaseObservableCollection {
emit(argument) {
for (const h of this._handlers) {
h(argument);
}
}
}
export class ObservableValue extends BaseObservableValue {
constructor(initialValue) {
super();
this._value = initialValue;
}
get() {
return this._value;
}
set(value) {
this._value = value;
this.emit(this._value);
}
}
export function tests() {
class Collection extends BaseObservableCollection {
constructor() {
super();
this.firstSubscribeCalls = 0;
this.firstUnsubscribeCalls = 0;
}
onSubscribeFirst() { this.firstSubscribeCalls += 1; }
onUnsubscribeLast() { this.firstUnsubscribeCalls += 1; }
}
return {
test_unsubscribe(assert) {
const c = new Collection();
const unsubscribe = c.subscribe({});
unsubscribe();
assert.equal(c.firstSubscribeCalls, 1);
assert.equal(c.firstUnsubscribeCalls, 1);
}
}
}