A self hosted chat application with end-to-end encrypted messaging.
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
import { Subject } from "../observe/Observable"; import { Model } from "./AbstractModel"; import { Observer } from "../observe/Observer";
export class UserModel implements Model, Subject { /** * @type {Observer[]} List of subscribers. In real life, the list of * subscribers can be stored more comprehensively (categorized by event * type, etc.). */ private observers: Observer[] = []; private state: number = 0;
constructor() { } /** * The subscription management methods. */ public attach(observer: Observer): void { console.log('Subject: Attached an observer.'); this.observers.push(observer); }
public detach(observer: Observer): void { const observerIndex = this.observers.indexOf(observer); this.observers.splice(observerIndex, 1); console.log('Subject: Detached an observer.'); }
/** * Trigger an update in each subscriber. */ public notify(): void { console.log('Subject: Notifying observers...'); for (const observer of this.observers) { observer.update(this.state); } }
public someBusinessMethod(num: number): void { this.state = num; this.helperMethod(); console.log(`Subject: My state has just changed to: ${this.state}`); this.notify(); }
private helperMethod() {}
}
|