monogamous
monogamous copied to clipboard
boot single-instance application
Monogamous
Only one instance of an app at a time.
Install
npm install monogamous
NOTE: If you are using this for Electron, you should know that this feature was added as an option. See here and here.
Usage (using Electron as an example)
Decorating main process entrypoint
//index.js
import monogamous from 'monogamous'
import main from './main' //main process app stuff
import app from 'app'
booter = monogamous({ sock: 'myapp'}, { other: 'args'})
/**
* this presumes your `app.on('ready')` is inside your boot method
*/
booter.on('boot', main.boot.bind(main))
booter.on('reboot', main.reboot.bind(main))
booter.on('error', function(err) { console.error('ops', err) })
booter.boot({ more: 'args'})
Inside main process entrypoint
//index.js
import monogamous from 'monogamous'
import main from './main' //main process app stuff
import app from 'app'
booter = monogamous({ sock: 'myapp'}, { other: 'args'})
booter.on('boot', main.boot.bind(main))
booter.on('reboot', main.reboot.bind(main))
booter.on('error', function(err) { console.error('ops', err) })
//electron's ready event gets it going
app.on('ready', booter.boot.bind(booter))
Events
boot: raised if an instance is not running. Your app may start up pristine herereboot: another instance was attempted.end: a call toend()shutdown the instance server
boot and reboot events receive an merged arguments object merging the following inputs,
in order of precedence:
- args passed to monogamous creation; eg
monogamous({ sock: 'foo'}, {these:'arepassedthru'}) - process argv , hashed (using minimist)
- args passed to
boot; egmono.boot({ these:'arealsopassedthru'})
API
Monogamous Factory
//only the 'sock' property is required to name your socket
let booter = monogamous({ sock: 'keepitsimple' }, [other args...])
Instance Methods
boot([args]): {Function} tries to connect tosock;failure to connect means an instance is runningend(): {Function} closes socket server