electron-wincom v1.0.9
WinCom
A simple window communication plugin for your Electron application.
Installation
npm i electron-wincom --save
Usage
Can be included like this in the "Main" process:
const winCom = require('electron-wincom');
Can be included like this in the "Renderer" process:
const winCom = require('electron').remote.require('electron-wincom');
Methods
create(name, options)
Creates and returns a BrowserWindow instance which is also added to winCom.list
. The options
argument is an object that can take all the available options of the BrowserWindow
options
argument, with some added properties:
url
- Passes in the URL toBrowserWindowInstance.loadURL()
when creating the instance.showDevTools
- Open developer tools for the window.
add(name, win)
Takes a name and a BrowserWindow
instance and adds it to winCom.list
. It will throw if a
window with the given name is already in the list. Returns winCom
.
remove(name)
Takes a name and removes the BrowserWindow
instance from winCom.list
. Returns winCom
.
replace(name, win)
Replaces a window in winCom.list
with the given name. Does NOT throw if the name doesn't exist. Returns winCom
.
get(name)
Returns a BrowserWindow
instance stored under the given name
, otherwise null
.
has(name)
Returns true/false whether a BrowserWindow
instance with the given name
has been added.
getName(win)
Takes a BrowserWindow
instance and returns the name it's stored under or null
otherwise.
on(evName, callback)
Registers a listener with the given evName
and callback
. The callback may take one argument which is an object with
event data.
off(evName, handler)
Removes the listener for the given evName
and handler
.
emit(evName, [options])
Emits an event. options
is optional and can have any, or all, of these properties:
- The
options.data
argument can be of any value. - The
options.target
is a string that is the name of the target window. options.emittedBy
is theBrowserWindow
instance that's emitting the event. It is used to retrieve the name of the window, and will pass on that name to the recipient.
Events example
// Window 1.
// Require winCom from the main process.
const winCom = require('electron-wincom');
// Create our BrowserWindow instance. You can also use the [winCom.add()]
// method to add the window if you've already created it.
let win1 = winCom.create('first-window', {
url: 'the/path/to/the/template/window.html',
width: 600,
height: 800
});
// Keep reference to handler to remove it later on.
function handler (event) {
console.log(event.name); // "my-event"
console.log(event.data); // "some data"
console.log(event.target); // "first-window"
console.log(event.emittedBy); // "second-window"
};
// Registering listener.
winCom.on('my-event', handler);
// Window 2.
// Require winCom from the renderer process.
const winCom = require('electron').remote.require('electron-wincom');
// Create our BrowserWindow instance.
let win2 = winCom.create('second-window', {
url: 'the/path/to/the/template/window.html',
width: 400,
height: 400,
});
// Emit event. Second argument and all data inside it is optional.
winCom.emit('my-event', {
data: 'some data',
target: 'first-window',
emittedBy: win2
});