From aea53d63e8cf2842b300d2208a4608f97314cbad Mon Sep 17 00:00:00 2001 From: Michael Kerr Date: Fri, 2 Feb 2024 13:41:11 +0100 Subject: [PATCH 1/2] Register destructor for storage objects --- addon/adapters/local.js | 7 +++++-- addon/adapters/session.js | 7 +++++-- addon/mixins/storage.js | 8 +++++++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/addon/adapters/local.js b/addon/adapters/local.js index 0333d18..0248778 100644 --- a/addon/adapters/local.js +++ b/addon/adapters/local.js @@ -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'), @@ -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]; diff --git a/addon/adapters/session.js b/addon/adapters/session.js index eb015fe..2d9cddd 100644 --- a/addon/adapters/session.js +++ b/addon/adapters/session.js @@ -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'), @@ -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]; diff --git a/addon/mixins/storage.js b/addon/mixins/storage.js index ae82651..b4c39a2 100644 --- a/addon/mixins/storage.js +++ b/addon/mixins/storage.js @@ -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, @@ -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() { @@ -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'); From 3947c10dfe12a47ae035d2ab5d582597f570ae4c Mon Sep 17 00:00:00 2001 From: Michael Kerr Date: Fri, 2 Feb 2024 13:41:22 +0100 Subject: [PATCH 2/2] Add destroyable polyfill --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 94cefa4..515c690 100644 --- a/package.json +++ b/package.json @@ -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",