Skip to content

Commit

Permalink
Merge pull request #2526 from BobrImperator/feat-remove-evented-api-f…
Browse files Browse the repository at this point in the history
…rom-session

feat: remove deprecated Evented API usage from the public session service
  • Loading branch information
BobrImperator authored Mar 24, 2023
2 parents 40edc9c + 698fa2f commit 6b3f57e
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 133 deletions.
69 changes: 2 additions & 67 deletions packages/ember-simple-auth/addon/services/session.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { alias, readOnly } from '@ember/object/computed';
import { A } from '@ember/array';
import Service from '@ember/service';
import Evented from '@ember/object/evented';
import { getOwner } from '@ember/application';
import { assert, deprecate } from '@ember/debug';
import { assert } from '@ember/debug';
import Configuration from '../configuration';

import {
Expand All @@ -16,20 +14,6 @@ import {

const SESSION_DATA_KEY_PREFIX = /^data\./;

let enableEventsDeprecation = true;
function deprecateSessionEvents() {
if (enableEventsDeprecation) {
deprecate("Ember Simple Auth: The session service's events API is deprecated; to add custom behavior to the authentication or invalidation handling, override the handleAuthentication or handleInvalidation methods.", false, {
id: 'ember-simple-auth.events.session-service',
until: '4.0.0',
for: 'ember-simple-auth',
since: {
enabled: '3.1.0'
}
});
}
}

function assertSetupHasBeenCalled(isSetupCalled) {
if (!isSetupCalled) {
assert("Ember Simple Auth: session#setup wasn't called. Make sure to call session#setup in your application route's beforeModel hook.", false);
Expand All @@ -54,10 +38,9 @@ function assertSetupHasBeenCalled(isSetupCalled) {
@class SessionService
@module ember-simple-auth/services/session
@extends Ember.Service
@uses Ember.Evented
@public
*/
export default Service.extend(Evented, {
export default Service.extend({
/**
Triggered whenever the session is successfully authenticated. This happens
when the session gets authenticated via
Expand Down Expand Up @@ -140,7 +123,6 @@ export default Service.extend(Evented, {
init() {
this._super(...arguments);
this.set('session', getOwner(this).lookup('session:main'));
this._forwardSessionEvents();
},

set(key, value) {
Expand All @@ -153,53 +135,6 @@ export default Service.extend(Evented, {
}
},

_forwardSessionEvents() {
A([
'authenticationSucceeded',
'invalidationSucceeded'
]).forEach((event) => {
const session = this.get('session');
// the internal session won't be available in route unit tests
if (session) {
session.on(event, () => {
enableEventsDeprecation = false;
this.trigger(event, ...arguments);
enableEventsDeprecation = true;
});
}
});
},

on() {
deprecateSessionEvents();

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

one() {
deprecateSessionEvents();

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

off() {
deprecateSessionEvents();

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

has() {
deprecateSessionEvents();

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

trigger() {
deprecateSessionEvents();

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

_setupHandlers() {
this.get('session').on('authenticationSucceeded', () => this.handleAuthentication(Configuration.routeAfterAuthentication));
this.get('session').on('invalidationSucceeded', () => this.handleInvalidation(Configuration.rootURL));
Expand Down
66 changes: 0 additions & 66 deletions packages/ember-simple-auth/tests/unit/services/session-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,72 +31,6 @@ module('SessionService', function(hooks) {
sinon.restore();
});

test('forwards the "authenticationSucceeded" event from the session', async function(assert) {
assert.expect(2);
let deprecations = [];
registerDeprecationHandler((message, options, next) => {
deprecations.push(message);

next(message, options);
});
let triggered = false;
sinon.stub(sessionService, 'handleAuthentication');
sessionService.one('authenticationSucceeded', () => (triggered = true));
session.trigger('authenticationSucceeded');

await new Promise(resolve => {
next(() => {
assert.ok(triggered);
assert.equal(deprecations.filter(deprecation => deprecation.includes('Ember Simple Auth:')).length, 1); // the call to .one above triggers a deprecation but forwarding the event should *not* trigger a deprecation
resolve();
});
});
});

test('forwards the "invalidationSucceeded" event from the session', async function(assert) {
assert.expect(2);
let deprecations = [];
registerDeprecationHandler((message, options, next) => {
deprecations.push(message);

next(message, options);
});
let triggered = false;
sessionService.one('invalidationSucceeded', () => (triggered = true));
session.trigger('invalidationSucceeded');

await new Promise(resolve => {
next(() => {
assert.ok(triggered);
assert.equal(deprecations.filter(deprecation => deprecation.includes('Ember Simple Auth:')).length, 1); // the call to .one above triggers a deprecation but forwarding the event should *not* trigger a deprecation
resolve();
});
});
});

test('deprecates using the "Evented" API', function(assert) {
assert.expect(6);
let deprecations = [];
registerDeprecationHandler((message, options, next) => {
deprecations.push(message);

next(message, options);
});

let handler = () => {};
sessionService.trigger('invalidationSucceeded');
sessionService.on('invalidationSucceeded', handler);
sessionService.off('invalidationSucceeded', handler);
sessionService.has('invalidationSucceeded');
sessionService.one('invalidationSucceeded', handler);

let emberSimpleAuthDeprecations = deprecations.filter(deprecation => deprecation.includes('Ember Simple Auth:'));
assert.equal(emberSimpleAuthDeprecations.length, 5);
for (let deprecation of emberSimpleAuthDeprecations) {
assert.equal(deprecation, "Ember Simple Auth: The session service's events API is deprecated; to add custom behavior to the authentication or invalidation handling, override the handleAuthentication or handleInvalidation methods.");
}
});

module('isAuthenticated', function() {
test('is read from the session', function(assert) {
session.set('isAuthenticated', true);
Expand Down

0 comments on commit 6b3f57e

Please sign in to comment.