פבר.02

event emitter - typescript

event emitter - typescript

יש לממש event emitter ב typescript

EventEmitter.ts

  1. interface IEventMap {
  2. [key: string]: Function[];
  3. }
  4.  
  5. class EventEmitter {
  6. private _eventMap: IEventMap = {};
  7. private _oncers: IEventMap = {};
  8.  
  9. on(name: string, cb: Function) {
  10. if (!this._eventMap[name]) {
  11. this._eventMap[name] = [];
  12. }
  13. this._eventMap[name].push(cb);
  14. }
  15. emit(name: string, ...args: any[]) {
  16. const callbacks = this._eventMap[name];
  17. if (callbacks) {
  18. for (const cb of callbacks) {
  19. cb(...args); //call all registered callbacks
  20. }
  21. }
  22. const funcs = this._oncers[name];
  23. if (funcs) {
  24. for (const cb of funcs) {
  25. cb(...args); //call all registered callbacks
  26. }
  27. delete this._oncers[name]; //ensure they are not called again
  28. }
  29. }
  30. off(name: string, cb: Function) {
  31. let callbacks = this._eventMap[name];
  32. if (callbacks) {
  33. // callbacks = callbacks.filter(func=> func !== cb);
  34. let i = callbacks.findIndex((item) => item === cb);
  35. callbacks.splice(i, 1);
  36. }
  37. }
  38. once(name: string, cb: Function) {
  39. if (!this._oncers[name]) {
  40. this._oncers[name] = [];
  41. }
  42. this._oncers[name].push(cb);
  43. }
  44. listeners(name: string) {
  45. return [...this._eventMap[name], ...this._oncers[name]];
  46. }
  47. eventNames() {
  48. return [...Object.keys(this._eventMap), ...Object.keys(this._oncers)];
  49. }
  50. removeAllListeners(name: string) {
  51. delete this._eventMap[name];
  52. delete this._oncers[name];
  53. }
  54. }
  55. export default EventEmitter;


Clock.ts

  1. import EventEmitter from "./EventEmitter";
  2.  
  3. export enum ClockEvents {
  4. TICK = "TICK",
  5. STARTED = "STARTED",
  6. STOPPED = "STOPPED"
  7. }
  8.  
  9. export class Clock extends EventEmitter {
  10. ms = 1000;
  11. count = 0;
  12. intervalID = 0;
  13.  
  14. constructor(ms = 1000) {
  15. super();
  16. this.ms = ms;
  17. }
  18. start() {
  19. this.intervalID = setInterval(() => {
  20. this._tick();
  21. }, this.ms);
  22. this.emit(ClockEvents.STARTED, this.count);
  23. }
  24. stop() {
  25. clearInterval(this.intervalID);
  26. this.emit(ClockEvents.STOPPED, this.count);
  27. }
  28. private _tick() {
  29. this.count++;
  30. this.emit(ClockEvents.TICK, this.count);
  31. }
  32. }

Index.ts.ts

  1. import { Clock, ClockEvents } from "./Clock";
  2.  
  3. let rolex = new Clock(500);
  4.  
  5. rolex.on(ClockEvents.TICK, (count: number) => {
  6. console.log(`ticking rolex ${count} times`);
  7. if (count >= 5) {
  8. rolex.stop();
  9. }
  10. });
  11.  
  12. rolex.once(ClockEvents.STARTED, (count: number) => {
  13. console.log(`rolex started at ${count} count`);
  14.  
  15. requestAnimationFrame(() => {
  16. console.log("on start - rolex.eventNames", rolex.eventNames());
  17. });
  18. });
  19.  
  20. rolex.once(ClockEvents.STOPPED, async (count: number) => {
  21. console.log(`rolex stopped after ${count} times`);
  22. requestAnimationFrame(() => {
  23. console.log("on stop - rolex.eventNames", rolex.eventNames());
  24. });
  25. });
  26.  
  27. console.log("rolex.eventNames", rolex.eventNames());
  28.  
  29. rolex.start();


תגיות:
שתף את הסיפור הזה:

תגובות(0)

השאירו תגובה

קפטצ'ה לא מתאימה

תגובה