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.

92 lines
2.6 KiB

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