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.

93 lines
2.7 KiB

  1. import { Observer } from "./Observer";
  2. import { ObserverData } from "./ObserverData";
  3. /**
  4. * The Subject interface declares a set of methods for managing subscribers.
  5. */
  6. export interface Subject<T> {
  7. // Attach an observer to the subject.
  8. attach(observer: Observer<T>): void;
  9. // Detach an observer from the subject.
  10. detach(observer: Observer<T>): void;
  11. // Notify all observers about an event.
  12. notify(param: any): void;
  13. }
  14. /**
  15. * The Subject owns some important state and notifies observers when the state
  16. * changes.
  17. */
  18. // class ConcreteSubject implements Subject {
  19. // /**
  20. // * @type {number} For the sake of simplicity, the Subject's state, essential
  21. // * to all subscribers, is stored in this variable.
  22. // */
  23. // public state: number;
  24. // /**
  25. // * @type {Observer[]} List of subscribers. In real life, the list of
  26. // * subscribers can be stored more comprehensively (categorized by event
  27. // * type, etc.).
  28. // */
  29. // private observers: Observer[] = [];
  30. // /**
  31. // * The subscription management methods.
  32. // */
  33. // public attach(observer: Observer): void {
  34. // console.log('Subject: Attached an observer.');
  35. // this.observers.push(observer);
  36. // }
  37. // public detach(observer: Observer): void {
  38. // const observerIndex = this.observers.indexOf(observer);
  39. // this.observers.splice(observerIndex, 1);
  40. // console.log('Subject: Detached an observer.');
  41. // }
  42. // /**
  43. // * Trigger an update in each subscriber.
  44. // */
  45. // public notify(): void {
  46. // console.log('Subject: Notifying observers...');
  47. // for (const observer of this.observers) {
  48. // observer.update(this);
  49. // }
  50. // }
  51. // /**
  52. // * Usually, the subscription logic is only a fraction of what a Subject can
  53. // * really do. Subjects commonly hold some important business logic, that
  54. // * triggers a notification method whenever something important is about to
  55. // * happen (or after it).
  56. // */
  57. // public someBusinessLogic(): void {
  58. // console.log('\nSubject: I\'m doing something important.');
  59. // this.state = Math.floor(Math.random() * (10 + 1));
  60. // console.log(`Subject: My state has just changed to: ${this.state}`);
  61. // this.notify();
  62. // }
  63. // }
  64. // /**
  65. // * The client code.
  66. // */
  67. // const subject = new ConcreteSubject();
  68. // const observer1 = new ConcreteObserverA();
  69. // subject.attach(observer1);
  70. // const observer2 = new ConcreteObserverB();
  71. // subject.attach(observer2);
  72. // subject.someBusinessLogic();
  73. // subject.someBusinessLogic();
  74. // subject.detach(observer2);
  75. // subject.someBusinessLogic();