├── index.html
└── src
└── js
└── index.js
/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
7 |
8 | HW24-js-gof
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/src/js/index.js:
--------------------------------------------------------------------------------
1 | class PubSub {
2 | constructor() {
3 | this.handlers = [];
4 | }
5 |
6 | subscribe(event, handler, context) {
7 | if (typeof context === 'undefined') {
8 | context = handler;
9 | }
10 | this.handlers.push({event: event, handler: handler.bind(context)});
11 | }
12 |
13 | publish(event, args) {
14 | this.handlers.forEach((topic) => {
15 | if (topic.event === event) {
16 | topic.handler(args)
17 | }
18 | })
19 | }
20 | }
21 |
22 | /*
23 | * Mediator Pattern
24 | */
25 | class Mediator extends PubSub {
26 | constructor(opts) {
27 | super(); // get handlers
28 | }
29 |
30 | attachToObject(obj) {
31 | obj.handlers = [];
32 | obj.publish = this.publish;
33 | obj.subscribe = this.subscribe;
34 | }
35 | }
36 |
37 | var Billy = (function () {
38 | var messages = []
39 |
40 | return {
41 | setMsg(msg) {
42 | messages.push(msg)
43 | return messages
44 | },
45 | subscribeOn(channel, event) {
46 | {
47 | this.subscribe(channel, event);
48 | }
49 | }
50 | }
51 | })();
52 | var Rozy = (function () {
53 | var messages = []
54 |
55 | return {
56 | setMsg(msg) {
57 | messages.push(msg)
58 | return messages
59 | },
60 | sendMsg(channel, msg) {
61 | this.publish(channel, msg)
62 | },
63 | subscribeOn(channel, event) {
64 | this.subscribe(channel, event);
65 | }
66 | }
67 | })();
68 | var Jack = (function () {
69 | var messages = []
70 |
71 | return {
72 | setMsg(msg) {
73 | messages.push(msg)
74 | return messages
75 | },
76 | subscribeOn(channel, event) {
77 | {
78 | this.subscribe(channel, event);
79 | }
80 | }
81 | }
82 | })();
83 |
84 | var mediator = new Mediator()
85 | mediator.attachToObject(Rozy)
86 | mediator.attachToObject(Billy)
87 | mediator.attachToObject(Jack)
88 |
89 | Rozy.subscribeOn('rozy-from-billy', function (msg) {
90 | console.group('Billy sent first')
91 | Billy.setMsg(msg)
92 | console.log(msg)
93 | Jack.publish('rozy-to-jack', 'Sorry, i love Billy')
94 | console.groupEnd()
95 | });
96 | Rozy.subscribeOn('rozy-to-billy', function (msg) {
97 | console.group('rozy-to-billy')
98 | Rozy.setMsg(msg)
99 | console.log(msg)
100 | console.groupEnd()
101 | });
102 |
103 |
104 | Billy.subscribeOn('rozy-to-billy', function (msg) {
105 | console.group('rozy-to-billy')
106 | Rozy.setMsg(msg)
107 | console.log(msg)
108 | console.groupEnd()
109 | });
110 |
111 | Jack.subscribeOn('rozy-to-jack', function (msg) {
112 | console.group('rozy-to-jack')
113 | Rozy.setMsg(msg)
114 | console.log(msg)
115 | console.groupEnd()
116 | })
117 |
118 | // Rozy.sendMsg('rozy-from-jack', 'hi Rozy, i love you')
119 | Rozy.sendMsg('rozy-from-billy', 'hi Rozy, i love you')
120 |
--------------------------------------------------------------------------------