From a638631091562a0e9ecd6316cd2f628abd78b965 Mon Sep 17 00:00:00 2001 From: Thomas Wang Date: Wed, 8 Sep 2021 22:24:55 -0700 Subject: [PATCH 1/3] [CLEANUP beta] deprecate-router-events --- .../-internals/routing/lib/system/route.ts | 48 +------ .../-internals/routing/lib/system/router.ts | 73 +++-------- packages/@ember/deprecated-features/index.ts | 1 - .../tests/routing/decoupled_basic_test.js | 81 ------------ .../router_service_test/events_test.js | 117 ------------------ tests/docs/expected.js | 2 - 6 files changed, 18 insertions(+), 304 deletions(-) diff --git a/packages/@ember/-internals/routing/lib/system/route.ts b/packages/@ember/-internals/routing/lib/system/route.ts index df8778be0bc..585f1735b71 100644 --- a/packages/@ember/-internals/routing/lib/system/route.ts +++ b/packages/@ember/-internals/routing/lib/system/route.ts @@ -22,8 +22,7 @@ import { } from '@ember/-internals/runtime'; import { isProxy, lookupDescriptor, symbol } from '@ember/-internals/utils'; import Controller from '@ember/controller'; -import { assert, deprecate, info, isTesting } from '@ember/debug'; -import { ROUTER_EVENTS } from '@ember/deprecated-features'; +import { assert, info, isTesting } from '@ember/debug'; import { dependentKeyCompat } from '@ember/object/compat'; import { once } from '@ember/runloop'; import { classify } from '@ember/string'; @@ -2448,49 +2447,4 @@ Route.reopen({ }, }); -export let ROUTER_EVENT_DEPRECATIONS: any; -if (ROUTER_EVENTS) { - ROUTER_EVENT_DEPRECATIONS = { - on(name: string) { - this._super(...arguments); - let hasDidTransition = name === 'didTransition'; - let hasWillTransition = name === 'willTransition'; - - if (hasDidTransition) { - deprecate( - 'You attempted to listen to the "didTransition" event which is deprecated. Please inject the router service and listen to the "routeDidChange" event.', - false, - { - id: 'deprecate-router-events', - until: '4.0.0', - url: 'https://deprecations.emberjs.com/v3.x#toc_deprecate-router-events', - for: 'ember-source', - since: { - enabled: '3.11.0', - }, - } - ); - } - - if (hasWillTransition) { - deprecate( - 'You attempted to listen to the "willTransition" event which is deprecated. Please inject the router service and listen to the "routeWillChange" event.', - false, - { - id: 'deprecate-router-events', - until: '4.0.0', - url: 'https://deprecations.emberjs.com/v3.x#toc_deprecate-router-events', - for: 'ember-source', - since: { - enabled: '3.11.0', - }, - } - ); - } - }, - }; - - Route.reopen(ROUTER_EVENT_DEPRECATIONS); -} - export default Route; diff --git a/packages/@ember/-internals/routing/lib/system/router.ts b/packages/@ember/-internals/routing/lib/system/router.ts index 07245e09051..53c5d59fef3 100644 --- a/packages/@ember/-internals/routing/lib/system/router.ts +++ b/packages/@ember/-internals/routing/lib/system/router.ts @@ -6,8 +6,7 @@ import { BucketCache } from '@ember/-internals/routing'; import RouterService from '@ember/-internals/routing/lib/services/router'; import { A as emberA, Evented, Object as EmberObject, typeOf } from '@ember/-internals/runtime'; import Controller from '@ember/controller'; -import { assert, deprecate, info } from '@ember/debug'; -import { ROUTER_EVENTS } from '@ember/deprecated-features'; +import { assert, info } from '@ember/debug'; import EmberError from '@ember/error'; import { cancel, once, run, scheduleOnce } from '@ember/runloop'; import { DEBUG } from '@glimmer/env'; @@ -20,7 +19,6 @@ import Route, { hasDefaultSerialize, RenderOptions, ROUTE_CONNECTIONS, - ROUTER_EVENT_DEPRECATIONS, } from './route'; import RouterState from './router_state'; @@ -47,10 +45,6 @@ function defaultDidTransition(this: EmberRouter, infos: PrivateRouteInfo[]) { this.notifyPropertyChange('url'); this.set('currentState', this.targetState); - // Put this in the runloop so url will be accurate. Seems - // less surprising than didTransition being out of sync. - once(this, this.trigger, 'didTransition'); - if (DEBUG) { // @ts-expect-error namespace isn't public if (this.namespace.LOG_TRANSITIONS) { @@ -63,11 +57,8 @@ function defaultDidTransition(this: EmberRouter, infos: PrivateRouteInfo[]) { function defaultWillTransition( this: EmberRouter, oldInfos: PrivateRouteInfo[], - newInfos: PrivateRouteInfo[], - transition: Transition + newInfos: PrivateRouteInfo[] ) { - once(this, this.trigger, 'willTransition', transition); - if (DEBUG) { // @ts-expect-error namespace isn't public if (this.namespace.LOG_TRANSITIONS) { @@ -380,50 +371,22 @@ class EmberRouter extends EmberObject.extend(Evented) implements Evented { }); } + // TODO: merge into routeDidChange didTransition(infos: PrivateRouteInfo[]) { - if (ROUTER_EVENTS) { - if (router.didTransition !== defaultDidTransition) { - deprecate( - 'You attempted to override the "didTransition" method which is deprecated. Please inject the router service and listen to the "routeDidChange" event.', - false, - { - id: 'deprecate-router-events', - until: '4.0.0', - url: 'https://deprecations.emberjs.com/v3.x#toc_deprecate-router-events', - for: 'ember-source', - since: { - enabled: '3.11.0', - }, - } - ); - } - } + assert( + 'You attempted to override the "didTransition" method which has been deprecated. Please inject the router service and listen to the "routeDidChange" event.', + router.didTransition === defaultDidTransition + ); router.didTransition(infos); } - willTransition( - oldInfos: PrivateRouteInfo[], - newInfos: PrivateRouteInfo[], - transition: Transition - ) { - if (ROUTER_EVENTS) { - if (router.willTransition !== defaultWillTransition) { - deprecate( - 'You attempted to override the "willTransition" method which is deprecated. Please inject the router service and listen to the "routeWillChange" event.', - false, - { - id: 'deprecate-router-events', - until: '4.0.0', - url: 'https://deprecations.emberjs.com/v3.x#toc_deprecate-router-events', - for: 'ember-source', - since: { - enabled: '3.11.0', - }, - } - ); - } - } - router.willTransition(oldInfos, newInfos, transition); + // TODO: merge into routeWillChange + willTransition(oldInfos: PrivateRouteInfo[], newInfos: PrivateRouteInfo[]) { + assert( + 'You attempted to override the "willTransition" method which has been deprecated. Please inject the router service and listen to the "routeWillChange" event.', + router.willTransition === defaultWillTransition + ); + router.willTransition(oldInfos, newInfos); } triggerEvent( @@ -1406,7 +1369,7 @@ class EmberRouter extends EmberObject.extend(Evented) implements Evented { ``` @method didTransition - @public + @private @since 1.2.0 */ // Set with reopen to allow overriding via extend @@ -1419,7 +1382,7 @@ class EmberRouter extends EmberObject.extend(Evented) implements Evented { Triggers the router level `willTransition` hook. @method willTransition - @public + @private @since 1.11.0 */ // Set with reopen to allow overriding via extend @@ -1649,6 +1612,7 @@ export function triggerEvent( if (ignoreFailure) { return; } + // TODO: update? throw new EmberError( `Can't trigger action '${name}' because your app hasn't finished transitioning into its first route. To trigger an action on destination routes during a transition, you can call \`.send()\` on the \`Transition\` object passed to the \`model/beforeModel/afterModel\` hooks.` ); @@ -1868,7 +1832,4 @@ EmberRouter.reopen({ }), }); -if (ROUTER_EVENTS) { - EmberRouter.reopen(ROUTER_EVENT_DEPRECATIONS); -} export default EmberRouter; diff --git a/packages/@ember/deprecated-features/index.ts b/packages/@ember/deprecated-features/index.ts index 5b93040f31c..11ded0a9acf 100644 --- a/packages/@ember/deprecated-features/index.ts +++ b/packages/@ember/deprecated-features/index.ts @@ -3,5 +3,4 @@ // These versions should be the version that the deprecation was _introduced_, // not the version that the feature will be removed. -export const ROUTER_EVENTS = !!'4.0.0'; export const ASSIGN = !!'4.0.0-beta.1'; diff --git a/packages/ember/tests/routing/decoupled_basic_test.js b/packages/ember/tests/routing/decoupled_basic_test.js index 175055830b4..ce9a0b2a1b6 100644 --- a/packages/ember/tests/routing/decoupled_basic_test.js +++ b/packages/ember/tests/routing/decoupled_basic_test.js @@ -1055,59 +1055,6 @@ moduleFor( ); } - ['@test Router `willTransition` hook passes in cancellable transition'](assert) { - assert.expect(8); - this.router.reopen({ - willTransition(_, _2, transition) { - assert.ok(true, 'willTransition was called'); - if (transition.intent.url !== '/') { - transition.abort(); - } - }, - }); - - this.router.map(function () { - this.route('nork'); - this.route('about'); - }); - - this.add( - 'route:loading', - Route.extend({ - activate() { - assert.ok(false, 'LoadingRoute was not entered'); - }, - }) - ); - - this.add( - 'route:nork', - Route.extend({ - activate() { - assert.ok(false, 'NorkRoute was not entered'); - }, - }) - ); - - this.add( - 'route:about', - Route.extend({ - activate() { - assert.ok(false, 'AboutRoute was not entered'); - }, - }) - ); - - let deprecation = /You attempted to override the "willTransition" method which is deprecated\./; - - return expectDeprecationAsync(() => { - return this.visit('/').then(() => { - this.handleURLAborts(assert, '/nork', deprecation); - this.handleURLAborts(assert, '/about', deprecation); - }); - }, deprecation); - } - ['@test Aborting/redirecting the transition in `willTransition` prevents LoadingRoute from being entered']( assert ) { @@ -1197,34 +1144,6 @@ moduleFor( }); } - async ['@test `didTransition` event fires on the router'](assert) { - assert.expect(3); - - this.router.map(function () { - this.route('nork'); - }); - - await this.visit('/'); - - let router = this.applicationInstance.lookup('router:main'); - router.one('didTransition', function () { - assert.ok(true, 'didTransition fired on initial routing'); - }); - - await this.visit('/'); - - router.one('didTransition', function () { - assert.ok(true, 'didTransition fired on the router'); - assert.equal( - router.get('url'), - '/nork', - 'The url property is updated by the time didTransition fires' - ); - }); - - await this.visit('/nork'); - } - ['@test `activate` event fires on the route'](assert) { assert.expect(4); diff --git a/packages/ember/tests/routing/router_service_test/events_test.js b/packages/ember/tests/routing/router_service_test/events_test.js index 5b36af5f350..2f2946d184a 100644 --- a/packages/ember/tests/routing/router_service_test/events_test.js +++ b/packages/ember/tests/routing/router_service_test/events_test.js @@ -696,120 +696,3 @@ moduleFor( } } ); - -moduleFor( - 'Router Service - deprecated events', - class extends RouterTestCase { - '@test willTransition events are deprecated'() { - return this.visit('/').then(() => { - expectDeprecation(() => { - this.routerService['_router'].on('willTransition', () => {}); - }, 'You attempted to listen to the "willTransition" event which is deprecated. Please inject the router service and listen to the "routeWillChange" event.'); - }); - } - - async '@test willTransition events are deprecated on routes'() { - this.add( - 'route:application', - Route.extend({ - init() { - this._super(...arguments); - this.on('willTransition', () => {}); - }, - }) - ); - await expectDeprecationAsync( - () => this.visit('/'), - 'You attempted to listen to the "willTransition" event which is deprecated. Please inject the router service and listen to the "routeWillChange" event.' - ); - } - - async '@test didTransition events are deprecated on routes'() { - this.add( - 'route:application', - Route.extend({ - init() { - this._super(...arguments); - this.on('didTransition', () => {}); - }, - }) - ); - await expectDeprecationAsync( - () => this.visit('/'), - 'You attempted to listen to the "didTransition" event which is deprecated. Please inject the router service and listen to the "routeDidChange" event.' - ); - } - - '@test other events are not deprecated on routes'() { - this.add( - 'route:application', - Route.extend({ - init() { - this._super(...arguments); - this.on('fixx', () => {}); - }, - }) - ); - expectNoDeprecation(() => { - return this.visit('/'); - }); - } - - '@test didTransition events are deprecated'() { - return this.visit('/').then(() => { - expectDeprecation(() => { - this.routerService['_router'].on('didTransition', () => {}); - }, 'You attempted to listen to the "didTransition" event which is deprecated. Please inject the router service and listen to the "routeDidChange" event.'); - }); - } - - '@test other events are not deprecated'() { - return this.visit('/').then(() => { - expectNoDeprecation(() => { - this.routerService['_router'].on('wat', () => {}); - }); - }); - } - } -); - -moduleFor( - 'Router Service: deprecated willTransition hook', - class extends RouterTestCase { - get routerOptions() { - return { - willTransition() { - this._super(...arguments); - // Overrides - }, - }; - } - - async '@test willTransition hook is deprecated'() { - await expectDeprecationAsync( - () => this.visit('/'), - 'You attempted to override the "willTransition" method which is deprecated. Please inject the router service and listen to the "routeWillChange" event.' - ); - } - } -); -moduleFor( - 'Router Service: deprecated didTransition hook', - class extends RouterTestCase { - get routerOptions() { - return { - didTransition() { - this._super(...arguments); - // Overrides - }, - }; - } - - async '@test didTransition hook is deprecated'() { - await expectDeprecationAsync( - () => this.visit('/'), - 'You attempted to override the "didTransition" method which is deprecated. Please inject the router service and listen to the "routeDidChange" event.' - ); - } - } -); diff --git a/tests/docs/expected.js b/tests/docs/expected.js index 58791765c56..99454eb58d1 100644 --- a/tests/docs/expected.js +++ b/tests/docs/expected.js @@ -182,7 +182,6 @@ module.exports = { 'didInsertElement', 'didReceiveAttrs', 'didRender', - 'didTransition', 'didUpdate', 'didUpdateAttrs', 'disabled', @@ -573,7 +572,6 @@ module.exports = { 'willDestroyElement', 'willInsertElement', 'willRender', - 'willTransition', 'willUpdate', 'without', 'wrap', From 9446348aef3fadd66c1647fd4dbc4d08bf6c39a0 Mon Sep 17 00:00:00 2001 From: Thomas Wang Date: Wed, 8 Sep 2021 22:58:52 -0700 Subject: [PATCH 2/3] add docs/expected back --- tests/docs/expected.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/docs/expected.js b/tests/docs/expected.js index 99454eb58d1..58791765c56 100644 --- a/tests/docs/expected.js +++ b/tests/docs/expected.js @@ -182,6 +182,7 @@ module.exports = { 'didInsertElement', 'didReceiveAttrs', 'didRender', + 'didTransition', 'didUpdate', 'didUpdateAttrs', 'disabled', @@ -572,6 +573,7 @@ module.exports = { 'willDestroyElement', 'willInsertElement', 'willRender', + 'willTransition', 'willUpdate', 'without', 'wrap', From a076e8521d31fcc46c6fc9ff80281f153e106d30 Mon Sep 17 00:00:00 2001 From: Thomas Wang Date: Sat, 11 Sep 2021 12:24:55 -0700 Subject: [PATCH 3/3] removed used test code --- .../tests/routing/decoupled_basic_test.js | 14 ++-------- .../ember/tests/routing/model_loading_test.js | 27 ------------------- .../tests/routing/template_rendering_test.js | 27 ------------------- 3 files changed, 2 insertions(+), 66 deletions(-) diff --git a/packages/ember/tests/routing/decoupled_basic_test.js b/packages/ember/tests/routing/decoupled_basic_test.js index ce9a0b2a1b6..2383ffef257 100644 --- a/packages/ember/tests/routing/decoupled_basic_test.js +++ b/packages/ember/tests/routing/decoupled_basic_test.js @@ -52,20 +52,10 @@ moduleFor( console.error = originalConsoleError; } - handleURLAborts(assert, path, deprecated) { + handleURLAborts(assert, path) { run(() => { let router = this.applicationInstance.lookup('router:main'); - let result; - - if (deprecated !== undefined) { - expectDeprecation(() => { - result = router.handleURL(path); - }); - } else { - result = router.handleURL(path); - } - - result.then( + router.handleURL(path).then( function () { assert.ok(false, 'url: `' + path + '` was NOT to be handled'); }, diff --git a/packages/ember/tests/routing/model_loading_test.js b/packages/ember/tests/routing/model_loading_test.js index 81f4bc23186..557f27924aa 100644 --- a/packages/ember/tests/routing/model_loading_test.js +++ b/packages/ember/tests/routing/model_loading_test.js @@ -29,33 +29,6 @@ moduleFor( console.error = originalConsoleError; } - handleURLAborts(assert, path, deprecated) { - run(() => { - let router = this.applicationInstance.lookup('router:main'); - let result; - - if (deprecated !== undefined) { - expectDeprecation(() => { - result = router.handleURL(path); - }); - } else { - result = router.handleURL(path); - } - - result.then( - function () { - assert.ok(false, 'url: `' + path + '` was NOT to be handled'); - }, - function (reason) { - assert.ok( - reason && reason.message === 'TransitionAborted', - 'url: `' + path + '` was to be aborted' - ); - } - ); - }); - } - async ['@test warn on URLs not included in the route set'](assert) { await this.visit('/'); diff --git a/packages/ember/tests/routing/template_rendering_test.js b/packages/ember/tests/routing/template_rendering_test.js index 842eb0e5540..47c51e049b0 100644 --- a/packages/ember/tests/routing/template_rendering_test.js +++ b/packages/ember/tests/routing/template_rendering_test.js @@ -29,33 +29,6 @@ moduleFor( console.error = originalConsoleError; } - handleURLAborts(assert, path, deprecated) { - run(() => { - let router = this.applicationInstance.lookup('router:main'); - let result; - - if (deprecated !== undefined) { - expectDeprecation(() => { - result = router.handleURL(path); - }); - } else { - result = router.handleURL(path); - } - - result.then( - function () { - assert.ok(false, 'url: `' + path + '` was NOT to be handled'); - }, - function (reason) { - assert.ok( - reason && reason.message === 'TransitionAborted', - 'url: `' + path + '` was to be aborted' - ); - } - ); - }); - } - get currentPath() { let currentPath; expectDeprecation(() => {