Skip to content

Explain why it's not a singleton #15

@sindresorhus

Description

@sindresorhus

It's easy to think that it would make for a simpler API. No need to initialize it first. I thought so too at first.

But imagine if it had been a singleton:

index.js

const config = require('electron-config');
const someComponent = require('./some-component');

config.setDefaults({
  foo: true
});



config.get('foo');
//=> true

some-component.js

const config = require('electron-config');



config.get('foo');
//=> undefined

Note the undefined. So you would have to be careful about setting the defaults before any app imports, which is easy to forget.


The following a much safer and explicit pattern:

config.js

const config = require('electron-config');

module.exports = new Config({
  foo: true
});

index.js

const config = require('./config');
const someComponent = require('./some-component');



config.get('foo');
//=> true

some-component.js

const config = require('./config');



config.get('foo');
//=> true

Thoughts on this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions