Skip to content

Commit

Permalink
fix: prefetch hook on lazy engine
Browse files Browse the repository at this point in the history
  • Loading branch information
xg-wang committed Nov 15, 2019
1 parent de82c16 commit d19c248
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 16 deletions.
11 changes: 5 additions & 6 deletions addon/initializers/prefetch.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import Route from '@ember/routing/route';
import RouteMixin from 'ember-prefetch/mixins/route';
import { gte } from 'ember-compatibility-helpers';

let hasInitialized = false;
Route.reopen(RouteMixin);

export function initialize() {
if (!hasInitialized) {
hasInitialized = true;

Route.reopen(RouteMixin);
export function initialize(application) {
if (gte('3.6.0')) {
application.inject('route:application', '__prefetch', 'service:prefetch');
}
}

Expand Down
4 changes: 1 addition & 3 deletions addon/instance-initializers/prefetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { resolve } from 'rsvp';
import { gte } from 'ember-compatibility-helpers';

export function initialize(instance) {
if (gte('3.6.0')) {
instance.inject('route:application', '__prefetch', 'service:prefetch');
} else {
if (!gte('3.6.0')) {
// Delete all of this in the Ember 3.8 LTS and rev major
const ROUTER_NAME = 'router:main';
const router = instance.lookup(ROUTER_NAME);
Expand Down
10 changes: 5 additions & 5 deletions addon/services/prefetch.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Service, { inject as service } from '@ember/service';
import { schedule } from '@ember/runloop';
import { createPrefetchChangeSet } from '../-private/diff-route-info';
import RSVP from 'rsvp';
import { Promise, all } from 'rsvp';
import { gte } from 'ember-compatibility-helpers';

let PrefetchService;
Expand All @@ -13,23 +12,24 @@ if (gte('3.6.0')) {
router: service('router'),
init() {
this._super(...arguments);
let privateRouter = this.router._router._routerMicrolib;
let seenRoutes = new WeakMap();

this.router.on('routeWillChange', transition => {
if (transition.to && substatesRegex.test(transition.to.name)) {
return;
}

schedule('actions', () => {
let routePromises = transition.routeInfos.map((info) => info._routePromise);
all(routePromises).then(() => {
if (!this.isDestroying && !this.isDestroyed) {
let privateRouter = this.router._router._routerMicrolib;
let changeSet = createPrefetchChangeSet(privateRouter, transition);
if (changeSet.shouldCall) {
for (let i = 0; i < changeSet.for.length; i++) {
let { route, fullParams } = changeSet.for[i];
if (seenRoutes.has(route)) continue;

route._prefetched = new RSVP.Promise(r => {
route._prefetched = new Promise(r => {
return r(route.prefetch(fullParams, transition));
});
seenRoutes.set(route, true);
Expand Down
1 change: 0 additions & 1 deletion testem.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ module.exports = {
// --no-sandbox is needed when running Chrome inside a container
process.env.CI ? '--no-sandbox' : null,
'--headless',
'--disable-gpu',
'--disable-dev-shm-usage',
'--disable-software-rasterizer',
'--mute-audio',
Expand Down
1 change: 0 additions & 1 deletion tests/acceptance/engine-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ module('lazy engine loading', function(hooks) {
);
this.owner.register('template:application', hbs`{{outlet}}`);
this.owner.register('controller:application', Controller.extend());
debugger;
AppRouter.map(function() {
this.mount('blog');
});
Expand Down

0 comments on commit d19c248

Please sign in to comment.