mirror of
https://github.com/vector-im/hydrogen-web.git
synced 2024-12-23 19:45:05 +01:00
Add type annotations to BaseObservable
This commit is contained in:
parent
d73dea797a
commit
319027e2e3
@ -14,20 +14,18 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export class BaseObservable {
|
export abstract class BaseObservable<T> {
|
||||||
constructor() {
|
protected _handlers: Set<T> = new Set<T>();
|
||||||
this._handlers = new Set();
|
|
||||||
}
|
|
||||||
|
|
||||||
onSubscribeFirst() {
|
onSubscribeFirst(): void {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onUnsubscribeLast() {
|
onUnsubscribeLast(): void {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
subscribe(handler) {
|
subscribe(handler: T): () => void {
|
||||||
this._handlers.add(handler);
|
this._handlers.add(handler);
|
||||||
if (this._handlers.size === 1) {
|
if (this._handlers.size === 1) {
|
||||||
this.onSubscribeFirst();
|
this.onSubscribeFirst();
|
||||||
@ -37,7 +35,7 @@ export class BaseObservable {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
unsubscribe(handler) {
|
unsubscribe(handler: T | null): null {
|
||||||
if (handler) {
|
if (handler) {
|
||||||
this._handlers.delete(handler);
|
this._handlers.delete(handler);
|
||||||
if (this._handlers.size === 0) {
|
if (this._handlers.size === 0) {
|
||||||
@ -48,14 +46,14 @@ export class BaseObservable {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsubscribeAll() {
|
unsubscribeAll(): void {
|
||||||
if (this._handlers.size !== 0) {
|
if (this._handlers.size !== 0) {
|
||||||
this._handlers.clear();
|
this._handlers.clear();
|
||||||
this.onUnsubscribeLast();
|
this.onUnsubscribeLast();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get hasSubscriptions() {
|
get hasSubscriptions(): boolean {
|
||||||
return this._handlers.size !== 0;
|
return this._handlers.size !== 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,12 +61,10 @@ export class BaseObservable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function tests() {
|
export function tests() {
|
||||||
class Collection extends BaseObservable {
|
class Collection extends BaseObservable<{}> {
|
||||||
constructor() {
|
firstSubscribeCalls: number= 0;
|
||||||
super();
|
firstUnsubscribeCalls: number = 0;
|
||||||
this.firstSubscribeCalls = 0;
|
|
||||||
this.firstUnsubscribeCalls = 0;
|
|
||||||
}
|
|
||||||
onSubscribeFirst() { this.firstSubscribeCalls += 1; }
|
onSubscribeFirst() { this.firstSubscribeCalls += 1; }
|
||||||
onUnsubscribeLast() { this.firstUnsubscribeCalls += 1; }
|
onUnsubscribeLast() { this.firstUnsubscribeCalls += 1; }
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user