From f3adccd620635c625bc09080f71d47e76378b2b0 Mon Sep 17 00:00:00 2001 From: Manuel Wiedenmann Date: Sun, 14 May 2023 14:51:34 +0200 Subject: [PATCH] Deprecate initializer --- .github/workflows/ci.yml | 4 +-- README.md | 32 ++++++++++++++++--- addon/initializers/local-storage-adapter.js | 12 +++++++ addon/mixins/adapters/import-export.js | 4 +++ index.js | 20 +++++++++++- tests/dummy/app/controllers/projects.js | 20 ++++++------ tests/dummy/config/environment.js | 1 + .../import-export-test.js | 22 +++++++------ .../local-storage-adapter-test.js | 29 ----------------- 9 files changed, 88 insertions(+), 56 deletions(-) rename tests/unit/{adapters => helpers}/import-export-test.js (85%) delete mode 100644 tests/unit/initializers/local-storage-adapter-test.js diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 24c6188..df34d7f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/README.md b/README.md index cff2ef6..b9221cc 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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`. diff --git a/addon/initializers/local-storage-adapter.js b/addon/initializers/local-storage-adapter.js index d1ea4de..972e318 100644 --- a/addon/initializers/local-storage-adapter.js +++ b/addon/initializers/local-storage-adapter.js @@ -1,3 +1,4 @@ +import { deprecate } from '@ember/debug'; import Store from '@ember-data/store'; import { importData, @@ -6,6 +7,17 @@ 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, + { + for: 'ember-local-storage', + id: 'ember-local-storage.initializers.local-storage-adapter', + since: '2.0.5', + until: '3.0.0', + url: 'https://github.com/funkensturm/ember-local-storage#deprecations', + } + ); Store.reopen({ _emberLocalStoragePatched: true, importData: function (json, options) { diff --git a/addon/mixins/adapters/import-export.js b/addon/mixins/adapters/import-export.js index 898ec1a..bc51af1 100644 --- a/addon/mixins/adapters/import-export.js +++ b/addon/mixins/adapters/import-export.js @@ -11,7 +11,9 @@ export default Mixin.create({ 'Using the import-export mixin has been deprecated and will be removed in version 3.0.0', false, { + for: 'ember-local-storage', id: 'ember-local-storage.mixins.adapters.import-export', + since: '2.0.5', until: '3.0.0', url: 'https://github.com/funkensturm/ember-local-storage#deprecations', } @@ -24,7 +26,9 @@ export default Mixin.create({ 'Using the import-export mixin has been deprecated and will be removed in version 3.0.0', false, { + for: 'ember-local-storage', id: 'ember-local-storage.mixins.adapters.import-export', + since: '2.0.5', until: '3.0.0', url: 'https://github.com/funkensturm/ember-local-storage#deprecations', } diff --git a/index.js b/index.js index 877b50b..7b3f38f 100644 --- a/index.js +++ b/index.js @@ -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 @@ -39,6 +47,10 @@ module.exports = { if (options.fileExport && this.hasEmberData) { this.needsFileExport = true; } + + if (options.loadInitializer === false) { + this.loadInitializer = options.loadInitializer; + } } }, @@ -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); }); @@ -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); }, diff --git a/tests/dummy/app/controllers/projects.js b/tests/dummy/app/controllers/projects.js index 88eb902..ce73a79 100644 --- a/tests/dummy/app/controllers/projects.js +++ b/tests/dummy/app/controllers/projects.js @@ -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(); @@ -67,7 +71,7 @@ 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 }); }); @@ -75,14 +79,12 @@ export default class extends Controller { @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 diff --git a/tests/dummy/config/environment.js b/tests/dummy/config/environment.js index 9cfc7b9..8947cd6 100644 --- a/tests/dummy/config/environment.js +++ b/tests/dummy/config/environment.js @@ -24,6 +24,7 @@ module.exports = function (environment) { 'ember-local-storage': { fileExport: true, + // loadInitializer: false, // namespace: 'els-demo', }, }; diff --git a/tests/unit/adapters/import-export-test.js b/tests/unit/helpers/import-export-test.js similarity index 85% rename from tests/unit/adapters/import-export-test.js rename to tests/unit/helpers/import-export-test.js index 1696c32..1ffe7da 100644 --- a/tests/unit/adapters/import-export-test.js +++ b/tests/unit/helpers/import-export-test.js @@ -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(); }); @@ -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'); @@ -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({ @@ -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'); @@ -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'); @@ -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); @@ -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'); @@ -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); diff --git a/tests/unit/initializers/local-storage-adapter-test.js b/tests/unit/initializers/local-storage-adapter-test.js deleted file mode 100644 index 31da911..0000000 --- a/tests/unit/initializers/local-storage-adapter-test.js +++ /dev/null @@ -1,29 +0,0 @@ -import { module, test } from 'qunit'; -import { setupApplicationTest } from 'ember-qunit'; -import { initialize } from '../../../initializers/local-storage-adapter'; - -let store; - -module('Unit | Initializer | local storage adapter', function (hooks) { - setupApplicationTest(hooks); - - hooks.beforeEach(function () { - store = this.owner.lookup('service:store'); - }); - - hooks.afterEach(function () { - store = null; - }); - - test('it adds importData to DS.Store', function (assert) { - initialize(); - - assert.strictEqual(typeof store.importData, 'function'); - }); - - test('it adds exportData to DS.Store', function (assert) { - initialize(); - - assert.strictEqual(typeof store.exportData, 'function'); - }); -});