Skip to content

Commit

Permalink
Deprecate initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
fsmanuel committed May 14, 2023
1 parent 338ceee commit 3eb44a6
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 56 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ jobs:
- ember-4.11
- ember-lts-4.12
- ember-release
- ember-beta
- ember-canary
# - ember-beta
# - ember-canary
- ember-default-with-jquery
- embroider-safe
# - embroider-optimized
Expand Down
32 changes: 27 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -532,10 +532,33 @@ module('basic acceptance test', function(hooks) {
## Deprecations
### mixins.adapters.import-export
### ember-local-storage.initializers.local-storage-adapter
until: 3.0.0
id: ember-local-storage.mixins.adapters.import-export
The initializer has been deprecated and will be removed in version 3.0.0. This is due to the fact that `ember-data >= 4.12` will no longer allow to `reopen` the `Store`. To remove the deprecation message you need to use the utility functions provided by the addon:
```javascript
import { importData, exportData } from 'ember-local-storage/helpers/import-export';
```
See the [Export & Import example](#export--import). When you are done you need to set `loadInitializer` to `false`:
```javascript
// config/environment.js
module.exports = function() {
var ENV = {
'ember-local-storage': {
loadInitializer: false
}
}
};
```
This will be the default behaviour for apps that use `ember-data >= 4.12`.
### ember-local-storage.mixins.adapters.import-export
until: 3.0.0
Using the import-export mixin has been deprecated and will be removed in version 3.0.0. You should use the utility functions provided by the addon:
Expand All @@ -545,10 +568,9 @@ import { importData, exportData } from 'ember-local-storage/helpers/import-expor
See the [Export & Import example](#export--import).
### storageFor - legacyKey
until: 2.0.0
id: ember-local-storage.storageFor.options.legacyKey
### ember-local-storage.storageFor.options.legacyKey
until: 2.0.0
Using `legacyKey` has been deprecated and will be removed in version 2.0.0. You should migrate your key to the new format. For `storageFor('settings')` that would be `storage:settings`.
Expand Down
10 changes: 10 additions & 0 deletions addon/initializers/local-storage-adapter.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { deprecate } from '@ember/debug';
import Store from '@ember-data/store';
import {
importData,
Expand All @@ -6,6 +7,15 @@ import {

export function initialize() {
if (!Store.prototype._emberLocalStoragePatched) {
deprecate(
'The initializer has been deprecated and will be removed in version 3.0.0. Find more information how to remove that deprecation by visiting the url.',
false,
{
id: 'ember-local-storage.initializers.local-storage-adapter',
until: '3.0.0',
url: 'https://github.com/funkensturm/ember-local-storage#deprecations',
}
);
Store.reopen({
_emberLocalStoragePatched: true,
importData: function (json, options) {
Expand Down
20 changes: 19 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ module.exports = {
npmDep.gt('2.0.0'))
) {
this.hasEmberData = true;
this.loadInitializer = true;
}

if (
npmDep.version &&
(npmDep.satisfies('>= 4.12.0') || npmDep.gt('4.12.0'))
) {
this.loadInitializer = false;
}

// determine if saveAs and Blob should be imported
Expand All @@ -39,6 +47,10 @@ module.exports = {
if (options.fileExport && this.hasEmberData) {
this.needsFileExport = true;
}

if (options.loadInitializer === false) {
this.loadInitializer = options.loadInitializer;
}
}
},

Expand All @@ -52,7 +64,7 @@ module.exports = {
},

treeForApp: function (tree) {
if (!this.needsFileExport) {
if (!this.loadInitializer) {
['initializers/local-storage-adapter.js'].forEach(function (file) {
tree = stew.rm(tree, file);
});
Expand All @@ -76,6 +88,12 @@ module.exports = {
});
}

if (!this.loadInitializer) {
['initializers/local-storage-adapter.js'].forEach(function (file) {
tree = stew.rm(tree, file);
});
}

return this._super.treeForAddon.call(this, tree);
},

Expand Down
20 changes: 11 additions & 9 deletions tests/dummy/app/controllers/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { inject as service } from '@ember/service';
import { tracked } from '@glimmer/tracking';
import { all, Promise } from 'rsvp';
import { storageFor } from 'ember-local-storage';
import {
importData,
exportData,
} from 'ember-local-storage/helpers/import-export';

function readFile(file) {
const reader = new FileReader();
Expand Down Expand Up @@ -67,22 +71,20 @@ export default class extends Controller {
@action
importData(event) {
readFile(event.target.files[0]).then((file) => {
this.store.importData(file.data).then(function () {
importData(this.store, file.data).then(function () {
// show a flash message or transitionTo somewehere
});
});
}

@action
exportData() {
this.store
.exportData(['projects', 'tasks', 'users'], {
download: true,
filename: 'my-data.json',
})
.then(function () {
// show a flash message or transitionTo somewehere
});
exportData(this.store, ['projects', 'tasks', 'users'], {
download: true,
filename: 'my-data.json',
}).then(function () {
// show a flash message or transitionTo somewehere
});
}

@action
Expand Down
1 change: 1 addition & 0 deletions tests/dummy/config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = function (environment) {

'ember-local-storage': {
fileExport: true,
// loadInitializer: false,
// namespace: 'els-demo',
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import { run } from '@ember/runloop';
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import testData from '../../helpers/test-data';
import { initialize } from 'ember-local-storage/initializers/local-storage-adapter';
import SessionStorageAdapter from 'ember-local-storage/adapters/session';
import {
importData,
exportData,
} from 'ember-local-storage/helpers/import-export';

module('Unit | Adapter | import/export', function (hooks) {
module('Unit | Helpers | import/export', function (hooks) {
setupTest(hooks);

hooks.beforeEach(function () {
initialize();
window.localStorage.clear();
window.sessionStorage.clear();
});
Expand All @@ -21,7 +23,7 @@ module('Unit | Adapter | import/export', function (hooks) {
const store = this.owner.lookup('service:store');

return run(function () {
return store.importData(testData.importFileContent);
return importData(store, testData.importFileContent);
})
.then(function () {
const posts = store.findAll('post');
Expand Down Expand Up @@ -49,7 +51,7 @@ module('Unit | Adapter | import/export', function (hooks) {
});
})
.then(function () {
return store.importData(testData.importFileContent);
return importData(store, testData.importFileContent);
})
.then(function () {
return hash({
Expand All @@ -70,7 +72,7 @@ module('Unit | Adapter | import/export', function (hooks) {
const store = this.owner.lookup('service:store');

return run(function () {
return store.importData(testData.importFileContent);
return importData(store, testData.importFileContent);
})
.then(function () {
const posts = store.findAll('post');
Expand Down Expand Up @@ -100,7 +102,7 @@ module('Unit | Adapter | import/export', function (hooks) {
const store = this.owner.lookup('service:store');

return run(function () {
return store.importData(testData.importFileContent);
return importData(store, testData.importFileContent);
})
.then(function () {
const posts = store.findAll('post');
Expand All @@ -112,7 +114,7 @@ module('Unit | Adapter | import/export', function (hooks) {
});
})
.then(function () {
return store.exportData(['posts', 'comments'], { json: false });
return exportData(store, ['posts', 'comments'], { json: false });
})
.then(function (data) {
assert.equal(data.data.length, 5);
Expand All @@ -125,7 +127,7 @@ module('Unit | Adapter | import/export', function (hooks) {
this.owner.register('adapter:post', SessionStorageAdapter);

return run(function () {
return store.importData(testData.importFileContent);
return importData(store, testData.importFileContent);
})
.then(function () {
const posts = store.findAll('post');
Expand All @@ -137,7 +139,7 @@ module('Unit | Adapter | import/export', function (hooks) {
});
})
.then(function () {
return store.exportData(['posts', 'comments'], { json: false });
return exportData(store, ['posts', 'comments'], { json: false });
})
.then(function (data) {
assert.equal(data.data.length, 5);
Expand Down
29 changes: 0 additions & 29 deletions tests/unit/initializers/local-storage-adapter-test.js

This file was deleted.

0 comments on commit 3eb44a6

Please sign in to comment.