Skip to content

Commit

Permalink
refactor(esa): remove Evented API from the internal store
Browse files Browse the repository at this point in the history
  • Loading branch information
BobrImperator committed Dec 25, 2024
1 parent ac784f7 commit f792d26
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
26 changes: 24 additions & 2 deletions packages/ember-simple-auth/src/internal-session.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { isEmpty, isNone } from '@ember/utils';
import ObjectProxy from '@ember/object/proxy';
import Evented from '@ember/object/evented';
import { set } from '@ember/object';
import { debug, assert } from '@ember/debug';
import { getOwner, setOwner } from '@ember/application';
import { isTesting } from '@embroider/macros';

class SessionEventTarget extends EventTarget {}

/**
__An internal implementation of Session. Communicates with stores and emits events.__
Expand All @@ -14,7 +15,7 @@ import { isTesting } from '@embroider/macros';
@private
*/

export default ObjectProxy.extend(Evented, {
export default ObjectProxy.extend({
/**
Triggered whenever the session is successfully authenticated. This happens
when the session gets authenticated via
Expand Down Expand Up @@ -46,6 +47,7 @@ export default ObjectProxy.extend(Evented, {
store: null,
isAuthenticated: false,
attemptedTransition: null,
sessionEvents: null,

init() {
this._super(...arguments);
Expand All @@ -55,6 +57,7 @@ export default ObjectProxy.extend(Evented, {
storeFactory = 'session-store:test';
}

this.sessionEvents = new SessionEventTarget();
this.set('store', getOwner(this).lookup(storeFactory));
this._busy = false;
this._bindToStoreEvents();
Expand Down Expand Up @@ -269,4 +272,23 @@ export default ObjectProxy.extend(Evented, {
setOwner(authenticator, owner);
return authenticator;
},

on(event, cb) {
this.sessionEvents.addEventListener(event, cb);
},

off(event, cb) {
this.sessionEvents.removeEventListener(event, cb);
},

trigger(event, value) {
let customEvent;
if (value) {
customEvent = new CustomEvent(event, { detail: value });
} else {
customEvent = new CustomEvent(event);
}

this.sessionEvents.dispatchEvent(customEvent);
},
});
26 changes: 13 additions & 13 deletions packages/test-esa/tests/unit/internal-session-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ module('InternalSession', function (hooks) {
test('triggers the "invalidationSucceeded" event', async function (assert) {
assert.expect(1);
let triggered = false;
session.one('invalidationSucceeded', () => {
session.on('invalidationSucceeded', () => {
triggered = true;
});
authenticator.trigger('sessionDataInvalidated');
Expand Down Expand Up @@ -217,7 +217,7 @@ module('InternalSession', function (hooks) {

test('does not trigger the "authenticationSucceeded" event', async function (assert) {
let triggered = false;
session.one('authenticationSucceeded', () => (triggered = true));
session.on('authenticationSucceeded', () => (triggered = true));
await session.restore();

assert.notOk(triggered);
Expand Down Expand Up @@ -329,7 +329,7 @@ module('InternalSession', function (hooks) {

test('triggers the "authenticationSucceeded" event', async function (assert) {
let triggered = false;
session.one('authenticationSucceeded', () => (triggered = true));
session.on('authenticationSucceeded', () => (triggered = true));

await session.authenticate('authenticator:test');

Expand Down Expand Up @@ -395,7 +395,7 @@ module('InternalSession', function (hooks) {
assert.expect(1);
let triggered = false;
sinon.stub(authenticator, 'authenticate').rejects('error auth');
session.one('authenticationSucceeded', () => (triggered = true));
session.on('authenticationSucceeded', () => (triggered = true));

try {
await session.authenticate('authenticator:test');
Expand Down Expand Up @@ -486,7 +486,7 @@ module('InternalSession', function (hooks) {

test('triggers the "invalidationSucceeded" event', async function (assert) {
let triggered = false;
session.one('invalidationSucceeded', () => (triggered = true));
session.on('invalidationSucceeded', () => (triggered = true));

await session.invalidate();

Expand Down Expand Up @@ -551,7 +551,7 @@ module('InternalSession', function (hooks) {
assert.expect(1);
sinon.stub(authenticator, 'invalidate').rejects('error');
let triggered = false;
session.one('invalidationSucceeded', () => (triggered = true));
session.on('invalidationSucceeded', () => (triggered = true));

try {
await session.invalidate();
Expand Down Expand Up @@ -723,7 +723,7 @@ module('InternalSession', function (hooks) {
test('does not trigger the "authenticationSucceeded" event', async function (assert) {
assert.expect(1);
let triggered = false;
session.one('authenticationSucceeded', () => (triggered = true));
session.on('authenticationSucceeded', () => (triggered = true));
store.trigger('sessionDataUpdated', {
some: 'other property',
authenticated: { authenticator: 'authenticator:test' },
Expand All @@ -746,7 +746,7 @@ module('InternalSession', function (hooks) {
test('triggers the "authenticationSucceeded" event', async function (assert) {
assert.expect(1);
let triggered = false;
session.one('authenticationSucceeded', () => (triggered = true));
session.on('authenticationSucceeded', () => (triggered = true));
store.trigger('sessionDataUpdated', {
some: 'other property',
authenticated: { authenticator: 'authenticator:test' },
Expand Down Expand Up @@ -828,7 +828,7 @@ module('InternalSession', function (hooks) {
test('triggers the "invalidationSucceeded" event', async function (assert) {
assert.expect(1);
let triggered = false;
session.one('invalidationSucceeded', () => (triggered = true));
session.on('invalidationSucceeded', () => (triggered = true));
store.trigger('sessionDataUpdated', {
some: 'other property',
authenticated: { authenticator: 'authenticator:test' },
Expand All @@ -851,7 +851,7 @@ module('InternalSession', function (hooks) {
test('does not trigger the "invalidationSucceeded" event', async function (assert) {
assert.expect(1);
let triggered = false;
session.one('invalidationSucceeded', () => (triggered = true));
session.on('invalidationSucceeded', () => (triggered = true));
store.trigger('sessionDataUpdated', {
some: 'other property',
authenticated: { authenticator: 'authenticator:test' },
Expand Down Expand Up @@ -921,7 +921,7 @@ module('InternalSession', function (hooks) {
test('triggers the "invalidationSucceeded" event', async function (assert) {
assert.expect(1);
let triggered = false;
session.one('invalidationSucceeded', () => (triggered = true));
session.on('invalidationSucceeded', () => (triggered = true));
store.trigger('sessionDataUpdated', { some: 'other property' });

await new Promise(resolve => {
Expand All @@ -941,7 +941,7 @@ module('InternalSession', function (hooks) {
test('does not trigger the "invalidationSucceeded" event', async function (assert) {
assert.expect(1);
let triggered = false;
session.one('invalidationSucceeded', () => (triggered = true));
session.on('invalidationSucceeded', () => (triggered = true));
store.trigger('sessionDataUpdated', { some: 'other property' });

await new Promise(resolve => {
Expand All @@ -954,7 +954,7 @@ module('InternalSession', function (hooks) {

test('it does not trigger the "sessionInvalidationFailed" event', async function (assert) {
let triggered = false;
session.one('sessionInvalidationFailed', () => (triggered = true));
session.on('sessionInvalidationFailed', () => (triggered = true));

await session.invalidate();

Expand Down

0 comments on commit f792d26

Please sign in to comment.