-
Notifications
You must be signed in to change notification settings - Fork 1
/
TopicEventExample.js
71 lines (60 loc) · 1.76 KB
/
TopicEventExample.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict';
let LocalPubSubAdapter = require('@superbalist/js-pubsub').LocalPubSubAdapter;
let EventManager = require('../lib/EventManager');
let TopicEventMessageTranslator = require('../lib/translators/TopicEventMessageTranslator');
let TopicEvent = require('../lib/events/TopicEvent');
let adapter = new LocalPubSubAdapter();
let translator = new TopicEventMessageTranslator();
let manager = new EventManager(adapter, translator);
// listen for "*" event
manager.listen('events', '*', (event) => {
console.log("Listener '*' received new event on channel 'events':");
console.log(event);
});
// listen for "user/created" event
manager.listen('events', 'user/created', (event) => {
console.log("Listener 'user/created' received new event on channel 'events':");
console.log(event);
});
// listen for "user/*" event
manager.listen('events', 'user/*', (event) => {
console.log("Listener 'user/*' received new event on channel 'events':");
console.log(event);
});
// listen for "user/created/1.0" event
manager.listen('events', 'user/created/1.0', (event) => {
console.log("Listener 'user/created/1.0' received new event on channel 'events':");
console.log(event);
});
// listen for "order/created" event
manager.listen('events', 'order/created', (event) => {
console.log("Listener 'order/created' received new event on channel 'events':");
console.log(event);
});
// dispatch an event
let event = new TopicEvent(
'user',
'created',
'1.0',
{
user: {
id: 1456,
first_name: 'Joe',
last_name: 'Soap',
email: 'joe.soap@example.org',
},
}
);
manager.dispatch('events', event);
// dispatch an event
event = new TopicEvent(
'order',
'created',
'2.1',
{
order: {
id: 1456,
},
}
);
manager.dispatch('events', event);