All files / skeleton/modules module.js

47.37% Statements 9/19
100% Branches 4/4
27.27% Functions 3/11
44.44% Lines 8/18
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117    1x     1x                         1x                                                                                                                                                           1x                     3x   2x 1x         1x  
import electron from 'electron';
 
const { ipcMain } = electron;
 
// Place to store the reference to the renderer process.
let renderer = null;
 
/**
 * Simple abstraction over electron's IPC. Ensures modules will not conflict with each other by
 * providing events namespace. It is also a security layer as it is the only communication channel
 * between your app and node environment.
 *
 * @param {string} name - name of the module
 * @class
 */
export default class Module {
 
    constructor(name) {
        this.name = name;
    }
 
    /**
     * Sends an general IPC event with data.
     *
     * @param {string} event - event name
     * @param {...*=}  data  - data to send
     */
    static sendGlobalEvent(event, ...data) {
        Module.sendInternal(event, ...data);
    }
 
    /**
     * Sends and IPC event with data.
     *
     * @param {string} event - event name
     * @param {...*=}  data  - data to send
     */
    send(event, ...data) {
        Module.sendInternal(this.getEventName(event), ...data);
    }
 
    /**
     * Registers a callback to a IPC event.
     *
     * @param {string}   event    - event name
     * @param {function} callback - callback to fire
     */
    on(event, callback) {
        ipcMain.on(this.getEventName(event), (receivedEvent, ...args) => {
            renderer = receivedEvent.sender;
            callback(receivedEvent, ...args);
        });
    }
 
    /**
     * Unregisters a callback.
     *
     * @param {string} module     - module name
     * @param {string} event      - the name of an event
     * @param {function} callback - listener to unregister
     */
    removeListener(module, event, callback) {
        ipcMain.removeListener(this.getEventName(event), callback);
    }
 
    /**
     * Unregisters all callbacks.
     *
     * @param {string} module - module name
     * @param {string} event  - the name of an event
     */
    removeAllListeners(module, event) {
        ipcMain.removeAllListeners(this.getEventName(event));
    }
 
    /**
     * Registers a once fired callback to a IPC event.
     *
     * @param {string}   event    - event name
     * @param {function} callback - callback to fire
     */
    once(event, callback) {
        ipcMain.once(this.getEventName(event), (receivedEvent, args) => {
            renderer = receivedEvent.sender;
            callback(receivedEvent, args);
        });
    }
 
    /**
     * Concatenates module name with event name.
     *
     * @param {string} event - event name
     * @returns {string}
     * @private
     */
    getEventName(event) {
        return `${this.name}__${event}`;
    }
 
    /**
     * Sends an IPC event.
     *
     * @param {string} event - event name
     * @param {*=}     data  - data to send
     * @private
     */
    static sendInternal(event, ...data) {
        if (!renderer) throw new Error('No reference to renderer process (meteor) yet.');
        // During the HCP update the window might have been already destroyed.
        if (!renderer.isDestroyed()) {
            renderer.send(event, ...data);
        }
    }
}
 
module.exports = Module;