Skip to content

Commit

Permalink
Merge pull request #2880 from mainmatter/es6-stores-refactoring
Browse files Browse the repository at this point in the history
refactor(ember-simple-auth): local-storage, session-storage, ephemeral to ES6 class.
  • Loading branch information
BobrImperator authored Dec 26, 2024
2 parents f9ba64a + 581453f commit 123e0a2
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 46 deletions.
17 changes: 9 additions & 8 deletions packages/ember-simple-auth/src/session-stores/ephemeral.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import BaseStore from './base';
@extends BaseStore
@public
*/
export default BaseStore.extend({
init() {
this._super(...arguments);
export default class EphemeralStore extends BaseStore {
constructor(owner, args) {
super(owner, args);

this.clear();
},
}

/**
Persists `data`. This replaces all currently stored data.
Expand All @@ -29,7 +30,7 @@ export default BaseStore.extend({
this._data = JSON.stringify(data || {});

return Promise.resolve();
},
}

/**
Returns all data currently stored as a plain object.
Expand All @@ -43,7 +44,7 @@ export default BaseStore.extend({
const data = JSON.parse(this._data) || {};

return Promise.resolve(data);
},
}

/**
Clears the store.
Expand All @@ -58,5 +59,5 @@ export default BaseStore.extend({
this._data = '{}';

return Promise.resolve();
},
});
}
}
18 changes: 9 additions & 9 deletions packages/ember-simple-auth/src/session-stores/local-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import isFastBoot from '../utils/is-fastboot';
@extends BaseStore
@public
*/
export default BaseStore.extend({
export default class LocalStorageStore extends BaseStore {
/**
The `localStorage` key the store persists data in.
Expand All @@ -32,7 +32,7 @@ export default BaseStore.extend({
@default 'ember_simple_auth-session'
@public
*/
key: 'ember_simple_auth-session',
key = 'ember_simple_auth-session';

init() {
this._super(...arguments);
Expand All @@ -44,13 +44,13 @@ export default BaseStore.extend({
if (!this.get('_isFastBoot')) {
window.addEventListener('storage', this._boundHandler);
}
},
}

willDestroy() {
if (!this.get('_isFastBoot')) {
window.removeEventListener('storage', this._boundHandler);
}
},
}

/**
Persists the `data` in the `localStorage`.
Expand All @@ -67,7 +67,7 @@ export default BaseStore.extend({
localStorage.setItem(this.key, data);

return Promise.resolve();
},
}

/**
Returns all data currently stored in the `localStorage` as a plain object.
Expand All @@ -81,7 +81,7 @@ export default BaseStore.extend({
let data = localStorage.getItem(this.key);

return Promise.resolve(JSON.parse(data) || {});
},
}

/**
Clears the store by deleting the
Expand All @@ -98,7 +98,7 @@ export default BaseStore.extend({
this._lastData = {};

return Promise.resolve();
},
}

_handleStorageEvent(e) {
if (e.key === this.get('key')) {
Expand All @@ -109,5 +109,5 @@ export default BaseStore.extend({
}
});
}
},
});
}
}
18 changes: 9 additions & 9 deletions packages/ember-simple-auth/src/session-stores/session-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import isFastBoot from '../utils/is-fastboot';
@extends BaseStore
@public
*/
export default BaseStore.extend({
export default class SessionStorageStore extends BaseStore {
/**
The `sessionStorage` key the store persists data in.
Expand All @@ -28,7 +28,7 @@ export default BaseStore.extend({
@default 'ember_simple_auth-session'
@public
*/
key: 'ember_simple_auth-session',
key = 'ember_simple_auth-session';

init() {
this._super(...arguments);
Expand All @@ -39,13 +39,13 @@ export default BaseStore.extend({
if (!this.get('_isFastBoot')) {
window.addEventListener('storage', bind(this, this._handleStorageEvent));
}
},
}

willDestroy() {
if (!this.get('_isFastBoot')) {
window.removeEventListener('storage', bind(this, this._handleStorageEvent));
}
},
}

/**
Persists the `data` in the `sessionStorage`.
Expand All @@ -62,7 +62,7 @@ export default BaseStore.extend({
sessionStorage.setItem(this.key, data);

return Promise.resolve();
},
}

/**
Returns all data currently stored in the `sessionStorage` as a plain object.
Expand All @@ -76,7 +76,7 @@ export default BaseStore.extend({
let data = sessionStorage.getItem(this.key);

return Promise.resolve(JSON.parse(data) || {});
},
}

/**
Clears the store by deleting the
Expand All @@ -93,7 +93,7 @@ export default BaseStore.extend({
this._lastData = {};

return Promise.resolve();
},
}

_handleStorageEvent(e) {
if (e.key === this.get('key')) {
Expand All @@ -104,5 +104,5 @@ export default BaseStore.extend({
}
});
}
},
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module('EphemeralStore', function (hooks) {
itBehavesLikeAStore({
hooks,
store() {
return Ephemeral.create();
return new Ephemeral();
},
});
});
21 changes: 13 additions & 8 deletions packages/test-esa/tests/unit/session-stores/local-storage-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,28 @@ import { module } from 'qunit';
import LocalStorage from 'ember-simple-auth/session-stores/local-storage';
import itBehavesLikeAStore from './shared/store-behavior';
import itBehavesLikeAStorageEventHandler from './shared/storage-event-handler-behavior';
import { setupTest } from 'ember-qunit';

class TestLocalStorage extends LocalStorage {
_isFastBoot = false;
}

module('LocalStorageStore', function (hooks) {
setupTest(hooks);

itBehavesLikeAStore({
hooks,
store() {
return LocalStorage.create({
_isFastBoot: false,
});
store(_sinon, owner) {
owner.register('session-store:local-storage', TestLocalStorage);
return owner.lookup('session-store:local-storage');
},
});

itBehavesLikeAStorageEventHandler({
hooks,
store() {
return LocalStorage.create({
_isFastBoot: false,
});
store(_sinon, owner) {
owner.register('session-store:local-storage', TestLocalStorage);
return owner.lookup('session-store:local-storage');
},
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,28 @@ import { module } from 'qunit';
import SessionStorage from 'ember-simple-auth/session-stores/session-storage';
import itBehavesLikeAStore from './shared/store-behavior';
import itBehavesLikeAStorageEventHandler from './shared/storage-event-handler-behavior';
import { setupTest } from 'ember-qunit';

class TestSessionStorage extends SessionStorage {
_isFastBoot = false;
}

module('SessionStorageStore', function (hooks) {
setupTest(hooks);

itBehavesLikeAStore({
hooks,
store() {
return SessionStorage.create({
_isFastBoot: false,
});
store(_sinon, owner) {
owner.register('session-store:session-storage', TestSessionStorage);
return owner.lookup('session-store:session-storage');
},
});

itBehavesLikeAStorageEventHandler({
hooks,
store() {
return SessionStorage.create({
_isFastBoot: false,
});
store(_sinon, owner) {
owner.register('session-store:session-storage', TestSessionStorage);
return owner.lookup('session-store:session-storage');
},
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export default function (options) {

hooks.beforeEach(function () {
sinon = sinonjs.createSandbox();
store = options.store();
});

hooks.afterEach(function () {
Expand All @@ -21,6 +20,7 @@ export default function (options) {
hooks.beforeEach(function () {
sinon.spy(window, 'addEventListener');
sinon.spy(window, 'removeEventListener');
store = options.store(sinon, this.owner);
});

hooks.afterEach(function () {
Expand All @@ -29,8 +29,6 @@ export default function (options) {
});

test('binds to "storage" events on the window when created', function (assert) {
store = options.store();

assert.ok(window.addEventListener.calledOnce);
});

Expand Down

0 comments on commit 123e0a2

Please sign in to comment.