Skip to content

Commit 15ee7bc

Browse files
authored
Merge pull request #73 from sfeir-open-source/feat/71-refactor-emit-emitNotBroadcast
feat: #71 - refactor emit / emitNotBroadcast
2 parents ca81288 + 6e62dd6 commit 15ee7bc

File tree

18 files changed

+252
-150
lines changed

18 files changed

+252
-150
lines changed

src/client/layouts/common/slide-view/slide-view-slave.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import { MASTER_SLAVE_CHANNEL } from '@event-bus/event-bus-resolver';
44
import { TalkControlSlave } from '@client/talk-control-slave/talk-control-slave';
55

6+
/**
7+
* Class to use plugins with SlideView component, and have access to eventBusSlave.
8+
*/
69
export class SlideViewSlave extends TalkControlSlave {
710
init() {
8-
super.init();
9-
this.eventBusSlave.on(MASTER_SLAVE_CHANNEL, 'touchPointer', postMessage);
11+
this.eventBusSlave.on(MASTER_SLAVE_CHANNEL, 'registerPlugin', ({ pluginName }) => this.registerPlugin(pluginName));
1012
}
1113
}

src/client/talk-control-master/talk-control-master.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -52,33 +52,33 @@ export class TalkControlMaster {
5252
afterInitialisation() {
5353
// Forward initialization event to server
5454
this.eventBusMaster.on(MASTER_SLAVE_CHANNEL, 'initialized', data => {
55-
this.eventBusMaster.emit(MASTER_SERVER_CHANNEL, 'init', data);
55+
this.eventBusMaster.broadcast(MASTER_SERVER_CHANNEL, 'init', data);
5656
this._initPlugins();
5757
});
5858

5959
// Forward "showNotes" events to slave
60-
this.eventBusMaster.on(MASTER_SLAVE_CHANNEL, 'sendNotesToMaster', data => this.eventBusMaster.emit(MASTER_SLAVE_CHANNEL, 'sendNotesToSlave', data));
60+
this.eventBusMaster.on(MASTER_SLAVE_CHANNEL, 'sendNotesToMaster', data => this.eventBusMaster.broadcast(MASTER_SLAVE_CHANNEL, 'sendNotesToSlave', data));
6161
// Forward "gotoSlide" events to slave
62-
this.eventBusMaster.on(MASTER_SERVER_CHANNEL, 'gotoSlide', data => this.eventBusMaster.emit(MASTER_SLAVE_CHANNEL, 'gotoSlide', data));
62+
this.eventBusMaster.on(MASTER_SERVER_CHANNEL, 'gotoSlide', data => this.eventBusMaster.broadcast(MASTER_SLAVE_CHANNEL, 'gotoSlide', data));
6363

6464
// Forward plugin event to server to broadcast it to all masters
65-
this.eventBusMaster.on(MASTER_SLAVE_CHANNEL, 'pluginEventIn', data => this.eventBusMaster.emit(MASTER_SERVER_CHANNEL, 'pluginEventIn', data));
66-
this.eventBusMaster.on(MASTER_SERVER_CHANNEL, 'pluginEventOut', data => this.eventBusMaster.emit(MASTER_SLAVE_CHANNEL, data.origin, data));
65+
this.eventBusMaster.on(MASTER_SLAVE_CHANNEL, 'pluginEventIn', data => this.eventBusMaster.broadcast(MASTER_SERVER_CHANNEL, 'pluginEventIn', data));
66+
this.eventBusMaster.on(MASTER_SERVER_CHANNEL, 'pluginEventOut', data => this.eventBusMaster.broadcast(MASTER_SLAVE_CHANNEL, data.origin, data));
6767

6868
// Forward "sendPointerEventToMaster" to server to broadcast to all masters
69-
this.eventBusMaster.on(MASTER_SLAVE_CHANNEL, 'sendPointerEventToMaster', data => this.eventBusMaster.emit(MASTER_SERVER_CHANNEL, 'sendPointerEventToMaster', data));
69+
this.eventBusMaster.on(MASTER_SLAVE_CHANNEL, 'sendPointerEventToMaster', data => this.eventBusMaster.broadcast(MASTER_SERVER_CHANNEL, 'sendPointerEventToMaster', data));
7070
// Forward "pointerEvent" events to slave
71-
this.eventBusMaster.on(MASTER_SERVER_CHANNEL, 'pointerEvent', data => this.eventBusMaster.emit(MASTER_SLAVE_CHANNEL, 'pointerEvent', data));
71+
this.eventBusMaster.on(MASTER_SERVER_CHANNEL, 'pointerEvent', data => this.eventBusMaster.broadcast(MASTER_SLAVE_CHANNEL, 'pointerEvent', data));
7272
}
7373

7474
_registerPlugin(plugin, name) {
7575
if (plugin.usedByAComponent) { // Plugins used by a component and need slave (ex: keyboard)
76-
this.eventBusMaster.on(MASTER_SLAVE_CHANNEL, plugin.type, event => this.eventBusMaster.emit(MASTER_SERVER_CHANNEL, plugin.type, event));
77-
this.eventBusMaster.emit(MASTER_SLAVE_CHANNEL, 'registerPlugin', { pluginName: name });
76+
this.eventBusMaster.on(MASTER_SLAVE_CHANNEL, plugin.type, event => this.eventBusMaster.broadcast(MASTER_SERVER_CHANNEL, plugin.type, event));
77+
this.eventBusMaster.broadcast(MASTER_SLAVE_CHANNEL, 'registerPlugin', { pluginName: name });
7878
} else {
7979
// Other plugins like bluetooth devices
8080
plugin.init();
81-
plugin.onEvent(event => this.eventBusMaster.emit(MASTER_SERVER_CHANNEL, plugin.type, event));
81+
plugin.onEvent(event => this.eventBusMaster.broadcast(MASTER_SERVER_CHANNEL, plugin.type, event));
8282
}
8383
}
8484

@@ -94,17 +94,17 @@ export class TalkControlMaster {
9494
}
9595
});
9696

97-
this.eventBusMaster.emit(MASTER_SERVER_CHANNEL, 'getPluginsToActivate');
97+
this.eventBusMaster.broadcast(MASTER_SERVER_CHANNEL, 'getPluginsToActivate');
9898
}
9999

100100
forwardEvents() {
101-
const forward = (key => data => this.eventBusMaster.emit(MASTER_SLAVE_CHANNEL, key, data)).bind(this);
101+
const forward = (key => data => this.eventBusMaster.broadcast(MASTER_SLAVE_CHANNEL, key, data)).bind(this);
102102
this.eventBusMaster.on(MASTER_SERVER_CHANNEL, 'slideNumber', forward('slideNumber'));
103103
this.eventBusMaster.on(MASTER_SERVER_CHANNEL, 'currentSlide', forward('currentSlide'));
104104
}
105105

106106
onFramesLoaded() {
107-
this.eventBusMaster.emit(MASTER_SLAVE_CHANNEL, 'init');
107+
this.eventBusMaster.broadcast(MASTER_SLAVE_CHANNEL, 'init');
108108
this.focusFrame.focus();
109109
document.addEventListener('click', () => this.focusFrame.focus());
110110
}

src/client/talk-control-slave/talk-control-slave.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ export class TalkControlSlave {
1818
this.engine = EngineResolver.getEngine(params.engineName);
1919
this.shadowRoot = params.shadowRoot || undefined;
2020
this.eventBusSlave.on(MASTER_SLAVE_CHANNEL, 'init', this.init.bind(this));
21-
this._touchPosition = {
22-
touchstart: { clientX: 0, clientY: 0 },
23-
touchend: { clientX: 0, clientY: 0 }
24-
};
2521
}
2622

2723
init() {
@@ -33,22 +29,26 @@ export class TalkControlSlave {
3329
this.eventBusSlave.on(MASTER_SLAVE_CHANNEL, 'gotoSlide', data => {
3430
this.engine.goToSlide(data.slide, this.delta);
3531
if (!this.delta) {
36-
this.eventBusSlave.emit(MASTER_SLAVE_CHANNEL, 'sendNotesToMaster', this.engine.getSlideNotes());
32+
this.eventBusSlave.broadcast(MASTER_SLAVE_CHANNEL, 'sendNotesToMaster', this.engine.getSlideNotes());
3733
}
3834
});
3935

40-
this.eventBusSlave.on(MASTER_SLAVE_CHANNEL, 'registerPlugin', ({ pluginName }) => {
41-
loadPluginModule(pluginName)
42-
.then(plugin => {
43-
plugin.instance.init(this.shadowRoot);
44-
plugin.instance.onEvent((type, event) => this.eventBusSlave.emit(MASTER_SLAVE_CHANNEL, type, event));
45-
})
46-
.catch(e => {
47-
console.error('Unable to load plugin module', e);
48-
});
49-
});
36+
this.eventBusSlave.on(MASTER_SLAVE_CHANNEL, 'registerPlugin', ({ pluginName }) => this.registerPlugin(pluginName));
37+
38+
// Broadcast the initialized event only on the 'main' slave
39+
if (!this.delta) {
40+
this.eventBusSlave.broadcast(MASTER_SLAVE_CHANNEL, 'initialized', { slides });
41+
}
42+
}
5043

51-
// Emit the initialized event only on the 'main' slave
52-
if (!this.delta) this.eventBusSlave.emit(MASTER_SLAVE_CHANNEL, 'initialized', { slides });
44+
registerPlugin(pluginName) {
45+
loadPluginModule(pluginName)
46+
.then(plugin => {
47+
plugin.instance.init(this.shadowRoot);
48+
plugin.instance.onEvent((type, event) => this.eventBusSlave.broadcast(MASTER_SLAVE_CHANNEL, type, event));
49+
})
50+
.catch(e => {
51+
console.error('Unable to load plugin module', e);
52+
});
5353
}
5454
}

src/common/event-bus/event-bus-resolver.js

+18-18
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,35 @@ export class EventBusResolver {
3434

3535
/**
3636
*
37-
* @param {MASTER_SERVER_CHANNEL | MASTER_SLAVE_CHANNEL} dest - Channel to which to emit
37+
* @param {MASTER_SERVER_CHANNEL | MASTER_SLAVE_CHANNEL} channel - Channel on which to broadcast
3838
* @param {string} key - Event key to fire
39-
* @param {*} data - Data to emit
39+
* @param {*} data - Data to broadcast
4040
* @throws Will throw an error if key is not specified or if dest is incorrect
4141
*/
42-
emit(dest, key, data) {
43-
if (![MASTER_SERVER_CHANNEL, MASTER_SLAVE_CHANNEL].includes(dest)) {
44-
throw new Error(`'${dest}' is not a known destination.`);
42+
broadcast(channel, key, data) {
43+
if (![MASTER_SERVER_CHANNEL, MASTER_SLAVE_CHANNEL].includes(channel)) {
44+
throw new Error(`'${channel}' is not a known channelination.`);
4545
}
4646

47-
eventBusLogger.log(`EMIT (broadcast) '${key}' on ${dest} with: ${data ? JSON.stringify(data) : 'no data'}`);
48-
this.channels[dest].emit(key, data);
47+
eventBusLogger.log(`BROADCAST "${key}" on channel ${channel} with: ${data ? JSON.stringify(data) : 'no data'}`);
48+
this.channels[channel].broadcast(key, data);
4949
}
5050

5151
/**
52-
* Emit data for the dedicated channel passed in parameter on given event key
52+
* Emit data for the target passed in parameter on given event key
5353
*
54-
* @param {MASTER_SERVER_CHANNEL | MASTER_SLAVE_CHANNEL} dest - Channel to which to emit
55-
* @param {string} key
56-
* @param {any} data
57-
* @param {any} channel
54+
* @param {MASTER_SERVER_CHANNEL | MASTER_SLAVE_CHANNEL} channel - Channel on which to emit
55+
* @param {string} key - Event name
56+
* @param {any} data - Values
57+
* @param {any} target - Socket or window to which the event will be sent
5858
*/
59-
emitNotBroadcast(dest, key, data, channel) {
60-
if (![MASTER_SERVER_CHANNEL, MASTER_SLAVE_CHANNEL].includes(dest)) {
61-
throw new Error(`'${dest}' is not a known destination.`);
59+
emitTo(channel, key, data, target) {
60+
if (![MASTER_SERVER_CHANNEL, MASTER_SLAVE_CHANNEL].includes(channel)) {
61+
throw new Error(`'${channel}' is not a known destination.`);
6262
}
6363

64-
eventBusLogger.log(`EMIT (notBroadcast) '${key}' on ${dest} to dedicate channel ${channel} with: ${data ? JSON.stringify(data) : 'no data'}`);
65-
this.channels[dest].emitNotBroadcast(key, data, channel);
64+
eventBusLogger.log(`EMIT "${key}" on channel ${channel} to target "${target.id}" with: ${data ? JSON.stringify(data) : 'no data'}`);
65+
this.channels[channel].emitTo(key, data, target);
6666
}
6767

6868
/**
@@ -77,7 +77,7 @@ export class EventBusResolver {
7777
throw new Error(`'${src}' is not a known source.`);
7878
}
7979

80-
eventBusLogger.log(`ON Multiple '${key}' on ${src}`);
80+
eventBusLogger.log(`SET onMultiple '${key}' on ${src}`);
8181
this.channels[src].onMultiple(key, callback);
8282
}
8383

src/common/event-bus/event-bus.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

3-
const NO_KEY_PROVIDED = 'No key provided';
3+
export const NO_KEY_PROVIDED = 'No key provided';
44
const DUPLICATE_ENTRY = 'Duplicate Entry';
5-
const NO_CHANNEL_PROVIDED = 'No Channel provided';
5+
export const NO_TARGET_PROVIDED = 'No socket or window provided';
66

77
/**
88
* @classdesc Event bus implementation
@@ -52,13 +52,13 @@ export class EventBus {
5252
}
5353

5454
/**
55-
* Emit data for each callback registered on given event key
55+
* Broadcast data for each callback registered on given event key
5656
*
5757
* @param {string} key - Event key to fire
58-
* @param {any} data - Data to emit
58+
* @param {any} data - Data to broadcast
5959
* @throws Will throw an error if key is not specified
6060
*/
61-
emit(key, data) {
61+
broadcast(key, data) {
6262
if (!key) {
6363
throw new Error(NO_KEY_PROVIDED);
6464
}
@@ -76,17 +76,17 @@ export class EventBus {
7676
}
7777

7878
/**
79-
* Emit data for the dedicated channel passed in parameter on given event key
80-
* @param {string} key
81-
* @param {any} data
82-
* @param {any} channel
79+
* Emit data for the target passed in parameter on given event key
80+
* @param {string} key - Event name
81+
* @param {any} data - Values
82+
* @param {any} target - Socket or window to which the event will be sent
8383
*/
84-
emitNotBroadcast(key, data, channel) {
84+
emitTo(key, data, target) {
8585
if (!key) {
8686
throw new Error(NO_KEY_PROVIDED);
8787
}
88-
if (!channel) {
89-
throw new Error(NO_CHANNEL_PROVIDED);
88+
if (!target) {
89+
throw new Error(NO_TARGET_PROVIDED);
9090
}
9191

9292
// Do nothing on super class

src/common/event-bus/postmessage/event-bus-postmessage.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ export class EventBusPostMessage extends EventBus {
2323
}
2424

2525
/**
26-
* Emit data via window and for each callback registered on given event key
26+
* Broadcast data via window and for each callback registered on given event key
2727
*
2828
* @param {string} key - Event key to fire
29-
* @param {any} data - Data to emit
29+
* @param {any} data - Data to broadcast
3030
* @throws Will throw an error if key is not specified
3131
*/
32-
emit(key, data) {
33-
super.emit(key, data);
32+
broadcast(key, data) {
33+
super.broadcast(key, data);
3434
// Inner broadcast (same app)
3535
this.windows.forEach(w =>
3636
w.postMessage(
@@ -44,16 +44,16 @@ export class EventBusPostMessage extends EventBus {
4444
}
4545

4646
/**
47-
* Emit data for the dedicated channel passed in parameter on given event key
48-
* @param {string} key
49-
* @param {any} data
50-
* @param {any} channel
47+
* Emit data for the window passed in parameter on given event key
48+
* @param {string} key - Event name
49+
* @param {any} data - Values
50+
* @param {any} window - Window to which the event will be sent
5151
*/
52-
emitNotBroadcast(key, data, channel) {
52+
emitTo(key, data, window) {
5353
// Call for sanity checks
54-
super.emitNotBroadcast(key, data, channel);
54+
super.emitTo(key, data, window);
5555

56-
channel.postMessage(
56+
window.postMessage(
5757
{
5858
type: key,
5959
data

src/common/event-bus/websockets/event-bus-websockets-client.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -38,36 +38,36 @@ export class EventBusWebsocketsClient extends EventBus {
3838
on(key, callback) {
3939
try {
4040
super.on(key, callback);
41-
this.onMultiple(key,callback);
41+
this.onMultiple(key, callback);
4242
} catch (e) {
4343
eventBusLogger.log('on event bus client error: ', [key, e.message], true);
4444
}
4545
}
4646

4747
/**
48-
* Emit data for each local callback and each connected socket
48+
* Broadcast data for each local callback and each connected socket
4949
*
5050
* @param {string} key - Event key to fire
51-
* @param {any} data - Data to emit
51+
* @param {any} data - Data to broadcast
5252
* @throws Will throw an error if key is not specified
5353
*/
54-
emit(key, data) {
54+
broadcast(key, data) {
5555
// Inner broadcast (same app)
56-
super.emit(key, data);
56+
super.broadcast(key, data);
5757
// System broadcast (several devices)
5858
this.io.emit(key, data);
5959
}
6060

6161
/**
62-
* Emit data for the dedicated channel passed in parameter on given event key
63-
* @param {string} key
64-
* @param {any} data
65-
* @param {any} channel
62+
* Emit data for the socket passed in parameter on given event key
63+
* @param {string} key - Event name
64+
* @param {any} data - Values
65+
* @param {any} socket - Socket to which the event will be sent
6666
*/
67-
emitNotBroadcast(key, data, channel) {
67+
emitTo(key, data, socket) {
6868
// Call for sanity checks
69-
super.emitNotBroadcast(key, data, channel);
69+
super.emitTo(key, data, socket);
7070

71-
channel.emit(key, data);
71+
socket.emit(key, data);
7272
}
7373
}

0 commit comments

Comments
 (0)