From ab72e4efc8832a444bc3b9a290b4a87cc4937142 Mon Sep 17 00:00:00 2001 From: vladyulik Date: Thu, 19 Jun 2025 22:42:45 +0300 Subject: [PATCH 1/4] Improve example usage --- JavaScript/event-aggregation.js | 48 +++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/JavaScript/event-aggregation.js b/JavaScript/event-aggregation.js index ce9c63b..f3e3ba1 100644 --- a/JavaScript/event-aggregation.js +++ b/JavaScript/event-aggregation.js @@ -3,36 +3,56 @@ const { EventEmitter } = require('node:events'); class Point { + static #bus = new EventEmitter(); + #x; #y; + #id; - constructor({ x, y }, emitter) { + constructor({ x, y }) { this.#x = x; this.#y = y; + this.#id = Symbol('point'); - emitter.on('move', ({ x, y }) => { + Point.#bus.on(`move:${this.#id.description}`, ({ x, y }) => { this.#x += x; this.#y += y; }); - emitter.on('clone', (callback) => { - const point = new Point({ x: this.#x, y: this.#y }, emitter); - callback(point); + Point.#bus.on(`clone:${this.#id.description}`, (callback) => { + callback(new Point({ x: this.#x, y: this.#y })); }); - emitter.on('toString', (callback) => { + Point.#bus.on(`toString:${this.#id.description}`, (callback) => { callback(`(${this.#x}, ${this.#y})`); }); } + + move(delta) { + Point.#bus.emit(`move:${this.#id.description}`, delta); + return this; + } + + clone() { + let copy; + Point.#bus.emit(`clone:${this.#id.description}`, (c) => (copy = c)); + return copy; + } + + toString() { + let str; + Point.#bus.emit(`toString:${this.#id.description}`, (s) => (str = s)); + return str; + } } // Usage -const emitter = new EventEmitter(); -const p1 = new Point({ x: 10, y: 20 }, emitter); -emitter.emit('toString', console.log); -emitter.emit('clone', (c0) => { - emitter.emit('toString', console.log); - emitter.emit('move', { x: -5, y: 10 }); - emitter.emit('toString', console.log); -}); +const p1 = new Point({ x: 10, y: 20 }); +console.log(p1.toString()); + +const c1 = p1.clone(); +console.log(c1.toString()); + +c1.move({ x: -5, y: 10 }); +console.log(c1.toString()); From f1fb78e701a059bb00e7ea9573502a088681e5e1 Mon Sep 17 00:00:00 2001 From: vladyulik Date: Thu, 19 Jun 2025 22:56:05 +0300 Subject: [PATCH 2/4] Improve example usage --- JavaScript/event-compose.js | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/JavaScript/event-compose.js b/JavaScript/event-compose.js index 2b9ccbf..ad2a07f 100644 --- a/JavaScript/event-compose.js +++ b/JavaScript/event-compose.js @@ -5,34 +5,37 @@ const { EventEmitter } = require('node:events'); class Point { #x; #y; + #emitter; constructor({ x, y }) { this.#x = x; this.#y = y; - this.emitter = new EventEmitter(); + this.#emitter = new EventEmitter(); - this.emitter.on('move', ({ x, y }) => { + this.#emitter.on('move', ({ x, y }) => { this.#x += x; this.#y += y; }); - this.emitter.on('clone', (callback) => { - const point = new Point({ x: this.#x, y: this.#y }); - callback(point); - }); + this.#emitter.on('clone', () => new Point({ x: this.#x, y: this.#y })); - this.emitter.on('toString', (callback) => { - callback(`(${this.#x}, ${this.#y})`); - }); + this.#emitter.on('toString', () => `(${this.#x}, ${this.#y})`); + } + + emit(eventName, arg) { + const listener = this.#emitter.listeners(eventName)[0]; + if (!listener) throw new Error(`Unknown event: ${eventName}`); + return listener(arg); } } // Usage const p1 = new Point({ x: 10, y: 20 }); -p1.emitter.emit('toString', console.log); -p1.emitter.emit('clone', (c1) => { - c1.emitter.emit('toString', console.log); - c1.emitter.emit('move', { x: -5, y: 10 }); - c1.emitter.emit('toString', console.log); -}); +console.log(p1.emit('toString')); + +const c1 = p1.emit('clone'); +console.log(c1.emit('toString')); + +c1.emit('move', { x: -5, y: 10 }); +console.log(c1.emit('toString')); From 52078a57069fed2b20253f89ce30450350174540 Mon Sep 17 00:00:00 2001 From: vladyulik Date: Thu, 19 Jun 2025 23:08:49 +0300 Subject: [PATCH 3/4] Improve example usage --- JavaScript/event-target.js | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/JavaScript/event-target.js b/JavaScript/event-target.js index ec7cc86..27fd4cd 100644 --- a/JavaScript/event-target.js +++ b/JavaScript/event-target.js @@ -4,46 +4,50 @@ class PointEvent extends Event { constructor(type, detail = {}) { super(type); this.detail = detail; + this.result = undefined; } } class Point { #x; #y; + #target; constructor({ x, y }) { this.#x = x; this.#y = y; - this.target = new EventTarget(); + this.#target = new EventTarget(); - this.target.addEventListener('move', (event) => { - const { x: dx, y: dy } = event.detail; + this.#target.addEventListener('move', (e) => { + const { x: dx, y: dy } = e.detail; this.#x += dx; this.#y += dy; + e.result = this; }); - this.target.addEventListener('clone', (event) => { - event.detail.callback(new Point({ x: this.#x, y: this.#y })); + this.#target.addEventListener('clone', (e) => { + e.result = new Point({ x: this.#x, y: this.#y }); }); - this.target.addEventListener('toString', (event) => { - event.detail.callback(`(${this.#x}, ${this.#y})`); + this.#target.addEventListener('toString', (e) => { + e.result = `(${this.#x}, ${this.#y})`; }); } emit(type, detail = {}) { - this.target.dispatchEvent(new PointEvent(type, detail)); + const event = new PointEvent(type, detail); + this.#target.dispatchEvent(event); + return event.result; } } // Usage const p1 = new Point({ x: 10, y: 20 }); -p1.emit('toString', { callback: console.log }); -p1.emit('clone', { - callback: (c1) => { - c1.emit('toString', { callback: console.log }); - c1.emit('move', { x: -5, y: 10 }); - c1.emit('toString', { callback: console.log }); - }, -}); +console.log(p1.emit('toString')); + +const c1 = p1.emit('clone'); +console.log(c1.emit('toString')); + +c1.emit('move', { x: -5, y: 10 }); +console.log(c1.emit('toString')); From 75eaf1a929c2d776317b6e9de514744b92e83e5c Mon Sep 17 00:00:00 2001 From: vladyulik Date: Thu, 19 Jun 2025 23:22:02 +0300 Subject: [PATCH 4/4] Improve example usage --- JavaScript/event-aggregation.js | 38 +++++++++++---------------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/JavaScript/event-aggregation.js b/JavaScript/event-aggregation.js index f3e3ba1..de95642 100644 --- a/JavaScript/event-aggregation.js +++ b/JavaScript/event-aggregation.js @@ -17,42 +17,28 @@ class Point { Point.#bus.on(`move:${this.#id.description}`, ({ x, y }) => { this.#x += x; this.#y += y; + return this; }); - Point.#bus.on(`clone:${this.#id.description}`, (callback) => { - callback(new Point({ x: this.#x, y: this.#y })); - }); - - Point.#bus.on(`toString:${this.#id.description}`, (callback) => { - callback(`(${this.#x}, ${this.#y})`); - }); - } - - move(delta) { - Point.#bus.emit(`move:${this.#id.description}`, delta); - return this; - } + Point.#bus.on(`clone:${this.#id.description}`, () => new Point({ x: this.#x, y: this.#y })); - clone() { - let copy; - Point.#bus.emit(`clone:${this.#id.description}`, (c) => (copy = c)); - return copy; + Point.#bus.on(`toString:${this.#id.description}`, () => `(${this.#x}, ${this.#y})`); } - toString() { - let str; - Point.#bus.emit(`toString:${this.#id.description}`, (s) => (str = s)); - return str; + emit(type, payload) { + const listener = Point.#bus.listeners(`${type}:${this.#id.description}`)[0]; + if (!listener) throw new Error(`Unknown event: ${type}`); + return listener(payload); } } // Usage const p1 = new Point({ x: 10, y: 20 }); -console.log(p1.toString()); +console.log(p1.emit('toString')); -const c1 = p1.clone(); -console.log(c1.toString()); +const c1 = p1.emit('clone'); +console.log(c1.emit('toString')); -c1.move({ x: -5, y: 10 }); -console.log(c1.toString()); +c1.emit('move', { x: -5, y: 10 }); +console.log(c1.emit('toString'));