/monogamous

boot single-instance application

Primary LanguageJavaScriptMIT LicenseMIT

Monogamous

Only one instance of an app at a time.

Build Status: Linux Build Status: Windows

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 here
  • reboot : another instance was attempted.
  • end : a call to end() 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; eg mono.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 to sock;failure to connect means an instance is running
  • end() : {Function} closes socket server