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

Add syncWindows option #299

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,22 @@ See the [CHANGELOG](CHANGELOG.md)

### Configuration

Global configuration can be set in your app's `config/environment.js`. E.g.

```js
// config/environment.js
module.exports = function() {
var ENV = {
'ember-local-storage': {
// Config options belong here
}
}
};
```

#### 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.
You can set `namespace` and `keyDelimiter` options. 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 Down Expand Up @@ -96,6 +109,10 @@ NOTE: However, there are environments where the detection fails and the support
},
```

#### Window Sync

You can set the `syncWindows` option to `false` to disable immediate syncing across windows or tabs. The default is `true`.

### Object & Array

#### Object
Expand Down
21 changes: 14 additions & 7 deletions addon/helpers/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ function createStorage(context, key, modelKey, options) {

const initialState = {},
defaultState = {
_storageKey: storageKey
_storageKey: storageKey,
_syncWindows: _addonConfig(context).syncWindows !== false
},
StorageFactory = owner.lookup(storageFactory);

Expand Down Expand Up @@ -148,28 +149,34 @@ function _modelKey(model) {
}

// TODO: v2.0 - Make modulePrefix the default
function _getNamespace(appConfig, addonConfig) {
function _getNamespace(context) {
// For backward compatibility this is a opt-in feature
let namespace = addonConfig.namespace;
let namespace = _addonConfig(context).namespace;

// Shortcut for modulePrefix
if (namespace === true) {
namespace = appConfig.modulePrefix
let appConfig = getOwner(context).resolveRegistration('config:environment');
namespace = appConfig.modulePrefix;
}

return namespace;
}

// TODO: Add migration helper
function _buildKey(context, key) {
let appConfig = getOwner(context).resolveRegistration('config:environment');
let addonConfig = appConfig && appConfig['ember-local-storage'] || {};
let namespace = _getNamespace(appConfig, addonConfig);
let namespace = _getNamespace(context);
let delimiter = addonConfig.keyDelimiter || ':';

return namespace ? `${namespace}${delimiter}${key}` : key;
}

function _addonConfig(context) {
let appConfig = getOwner(context).resolveRegistration('config:environment');
let addonConfig = appConfig && appConfig['ember-local-storage'] || {};

return addonConfig;
}

// Testing helper
function _resetStorages() {
storages = {};
Expand Down
5 changes: 4 additions & 1 deletion addon/mixins/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default Mixin.create({
const storage = this._storage();
const storageKey = get(this, '_storageKey');
const initialContent = get(this, '_initialContent');
const syncWindows = get(this, '_syncWindows')

let serialized, content;

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

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

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