Skip to content

Commit

Permalink
Deprecate and merge .define into the constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed May 31, 2019
1 parent 22f71d4 commit 28bc67f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 37 deletions.
50 changes: 23 additions & 27 deletions readme.md
Expand Up @@ -71,7 +71,7 @@ Create your options definition file, for example `options-init.js`:

```js
/* globals OptionsSync */
new OptionsSync().define({
new OptionsSync({
defaults: {
yourStringOption: 'green',
anyBooleans: true,
Expand Down Expand Up @@ -111,15 +111,15 @@ new OptionsSync().syncForm(document.querySelector('form#options-form'));

Done. Any defaults or saved options will be loaded into the form and any change will automatically be saved via `chrome.storage.sync`

In alternative you can put your fields in a custom`<options-sync>` element instead of `<form>` and they'll be automatically synchronized. You can specify the `storageName` via attribute, like:
In alternative you can put your fields in a custom`<options-sync>` element instead of `<form>` and they'll be automatically synchronized. You can specify the `storageName` via attribute, like:

```html
<options-sync storageName="my-options">
<input type="color" name="color">
</options-sync>
```

<strong>Warning:</strong> Custom Elements are supported by Firefox 63+ (November 2018)
<strong>Warning:</strong> Custom Elements are only supported by Firefox 63+ (November 2018)

</details>

Expand Down Expand Up @@ -154,7 +154,7 @@ In your `options-init.js` file, extend the call by including an array of functio

```js
/* globals OptionsSync */
new OptionsSync().define({
new OptionsSync({
defaults: {
color: 'red',
},
Expand All @@ -177,39 +177,19 @@ Notice `OptionsSync.migrations.removeUnused`: it's a helper method that removes

## API

#### const opts = new OptionsSync([options])

Returns an instance linked to the chosen storage.

##### storageName

Type: `string`
Default: `'options'`

The key used to store data in `chrome.storage.sync`

##### logging

Type: `boolean`
Default: `true`

Determines whether info and warnings (on sync, updating form, etc.) should be logged to the console or not. Recommended when used in content scripts.

#### opts.define(setup)

To be used in the background only, this is used to initiate the options. It's not required but it's recommended as a way to define which options the extension supports.
#### const optionsStorage = new OptionsSync([setup])

##### setup

Type: `object`

It should follow this format:
Optional. It should follow this format:

```js
{
defaults: { // recommended
color: 'blue'
},
},
migrations: [ // optional
savedOptions => {
if(savedOptions.oldStuff) {
Expand All @@ -220,6 +200,8 @@ It should follow this format:
}
```

Returns an instance linked to the chosen storage.

###### defaults

Type: `object`
Expand All @@ -232,6 +214,20 @@ Type: `array`

A list of functions to call when the extension is updated. The function will have this signature: `(savedOptions, defaults)`. In this function, alter the `savedOptions`. Don't return anything.

###### storageName

Type: `string`
Default: `'options'`

The key used to store data in `chrome.storage.sync`

###### logging

Type: `boolean`
Default: `true`

Whether info and warnings (on sync, updating form, etc.) should be logged to the console or not.

#### opts.getAll()

This returns a Promise that will resolve with **all** the options stored, as an object.
Expand Down
20 changes: 10 additions & 10 deletions source/index.ts
Expand Up @@ -4,6 +4,14 @@ declare namespace OptionsSync {
logging?: boolean;
}

interface Definitions {
defaults: Options;
/**
* A list of functions to call when the extension is updated.
*/
migrations: Migration[];
}

/**
A map of options as strings or booleans. The keys will have to match the form fields' `name` attributes.
*/
Expand Down Expand Up @@ -32,14 +40,6 @@ declare namespace OptionsSync {
],
}
*/

interface Definitions {
defaults: Options;
/**
* A list of functions to call when the extension is updated.
*/
migrations: Migration[];
}
}

// eslint-disable-next-line no-redeclare
Expand All @@ -65,7 +65,7 @@ class OptionsSync {
@constructor Returns an instance linked to the chosen storage.
@param options - Configuration to determine where options are stored.
*/
constructor(options: OptionsSync.ModuleOptions = {}) {
constructor(options: OptionsSync.ModuleOptions & Partial<OptionsSync.Definitions> = {}) {
if (typeof options === 'string') {
options = {
storageName: options
Expand Down Expand Up @@ -112,7 +112,7 @@ class OptionsSync {

this._log('group', 'Appling definitions');
this._log('info', 'Current options:', options);
if (defs.migrations.length > 0) {
if (defs.migrations && defs.migrations.length > 0) {
this._log('info', 'Running', defs.migrations.length, 'migrations');
defs.migrations.forEach(migrate => migrate(options, defs.defaults));
}
Expand Down

0 comments on commit 28bc67f

Please sign in to comment.