Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: adds sync option to disable window syncing #368

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default Ember.Component.extend({

#### Namespace & keyDelimiter

In you apps `config/environment.js` you can set a `namespace` and a `keyDelimiter`. For backward compatibility this is a opt-in feature.
In your `config/environment.js` you can set a `namespace` and a `keyDelimiter` option. For backward compatibility this is a opt-in feature.

**Important:** Don't turn this feature on for existing apps. You will lose access to existing keys.

Expand All @@ -81,6 +81,24 @@ module.exports = function() {
};
```

#### Window Sync

In your `config/environment.js` you can set a `sync` option. For backward compatibility this is a opt-in feature and defaults to true.

If set to `false`, this will disable immediate syncing across tabs/windows.

```js
// config/environment.js
module.exports = function() {
var ENV = {
modulePrefix: 'my-app',
'ember-local-storage': {
sync: true // disable syncing across windows
}
}
};
```

#### ember-data support

This addon autodetects if you use ember-data and will include the support for ember-data adapters and serializes by default. You can opt out of this behavior by setting the `includeEmberDataSupport` option to `false`:
Expand Down
14 changes: 14 additions & 0 deletions addon/helpers/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,12 @@ function createStorage(context, key, modelKey) {
const storageKey = modelKey
? `${storageFactory}:${modelKey}`
: storageFactory;
let appConfig = owner.resolveRegistration('config:environment');
let addonConfig = (appConfig && appConfig['ember-local-storage']) || {};

const defaultState = {
_storageKey: _buildKey(context, storageKey),
_sync: _getSync(appConfig, addonConfig),
};
const StorageFactory = owner.factoryFor(storageFactory);

Expand Down Expand Up @@ -130,6 +133,17 @@ function _getNamespace(appConfig, addonConfig) {
return namespace;
}

function _getSync(appConfig, addonConfig) {
// For backward compatibility this is a opt-in feature
let sync = addonConfig.sync;

if (sync === undefined) {
sync = appConfig.sync;
}

return sync;
}

// TODO: Add migration helper
function _buildKey(context, key) {
let appConfig = getOwner(context).resolveRegistration('config:environment');
Expand Down
5 changes: 4 additions & 1 deletion addon/mixins/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default Mixin.create({
const storage = this._storage();
const storageKey = get(this, '_storageKey');
const initialContent = get(this, '_initialContent');
const sync = get(this, '_sync');

let serialized, content;

Expand All @@ -39,7 +40,9 @@ export default Mixin.create({
this.set('content', content);

// Keep in sync with other windows
this._addStorageListener();
if (sync !== false) {
this._addStorageListener();
}

return this._super(...arguments);
},
Expand Down