Skip to content

Commit

Permalink
Merge pull request #377 from Mikek2252/master
Browse files Browse the repository at this point in the history
Ensure storage objects are destroyed on application teardown in tests
  • Loading branch information
fsmanuel committed Feb 6, 2024
2 parents 3248c5d + 3947c10 commit c8f9856
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
7 changes: 5 additions & 2 deletions addon/adapters/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { get } from '@ember/object';
import BaseAdapter from './base';
import { getStorage, _buildKey } from '../helpers/storage';
import StorageArray from '../local/array';
import { getOwner } from '@ember/application';

export default BaseAdapter.extend({
_storage: getStorage('local'),
Expand All @@ -11,8 +12,10 @@ export default BaseAdapter.extend({

if (!indices[type]) {
let storageKey = _buildKey(this, 'index-' + type);

indices[type] = StorageArray.extend({ _storageKey: storageKey }).create();
let owner = getOwner(this);
indices[type] = StorageArray.extend({ _storageKey: storageKey }).create(
owner.ownerInjection()
);
}

return indices[type];
Expand Down
7 changes: 5 additions & 2 deletions addon/adapters/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { get } from '@ember/object';
import BaseAdapter from './base';
import { getStorage, _buildKey } from '../helpers/storage';
import StorageArray from '../session/array';
import { getOwner } from '@ember/application';

export default BaseAdapter.extend({
_storage: getStorage('session'),
Expand All @@ -11,8 +12,10 @@ export default BaseAdapter.extend({

if (!indices[type]) {
let storageKey = _buildKey(this, 'index-' + type);

indices[type] = StorageArray.extend({ _storageKey: storageKey }).create();
let owner = getOwner(this);
indices[type] = StorageArray.extend({ _storageKey: storageKey }).create(
owner.ownerInjection()
);
}

return indices[type];
Expand Down
8 changes: 7 additions & 1 deletion addon/mixins/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { set, get } from '@ember/object';
import { isArray, A } from '@ember/array';
import { getStorage } from '../helpers/storage';
import { copy } from 'ember-copy';
import { getOwner } from '@ember/application';
import { associateDestroyableChild } from '@ember/destroyable';

export default Mixin.create({
_storageKey: null,
Expand Down Expand Up @@ -41,7 +43,10 @@ export default Mixin.create({
// Keep in sync with other windows
this._addStorageListener();

return this._super(...arguments);
this._super(...arguments);

let owner = getOwner(this);
associateDestroyableChild(owner, this);
},

_getInitialContentCopy() {
Expand Down Expand Up @@ -86,6 +91,7 @@ export default Mixin.create({
},

_save() {
if (this.isDestroying || this.isDestroyed) return;
const storage = this._storage();
const content = get(this, 'content');
const storageKey = get(this, '_storageKey');
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"ember-cli-babel": "^7.26.11",
"ember-cli-string-utils": "^1.1.0",
"ember-cli-version-checker": "^5.1.2",
"ember-copy": "^2.0.1"
"ember-copy": "^2.0.1",
"ember-destroyable-polyfill": "^2.0.3"
},
"devDependencies": {
"@ember/optional-features": "^2.0.0",
Expand Down

0 comments on commit c8f9856

Please sign in to comment.