Skip to content

Commit

Permalink
fix(abc:reuse-tab): fix re-trigger the _onReuseInit event (#1308)
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk authored Aug 7, 2021
1 parent f895bf0 commit 4282fc2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
10 changes: 6 additions & 4 deletions packages/abc/reuse-tab/reuse-tab.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TestBed } from '@angular/core/testing';
import { fakeAsync, TestBed, tick } from '@angular/core/testing';
import { ActivatedRoute, Router, RouteReuseStrategy } from '@angular/router';
import { filter } from 'rxjs/operators';

Expand Down Expand Up @@ -513,7 +513,7 @@ describe('abc: reuse-tab(service)', () => {
snapshot.routeConfig.loadChildren = {};
expect(srv.retrieve(getSnapshot(2))).toBeNull();
});
it(`should be run _onReuseInit event hook`, () => {
it(`should be run _onReuseInit event hook`, fakeAsync(() => {
const handle = {
componentRef: {
instance: {
Expand All @@ -524,8 +524,10 @@ describe('abc: reuse-tab(service)', () => {
const snapshot = getSnapshot(3);
// handle
srv.store(snapshot, handle);
srv.shouldAttach(snapshot);
// mock activate router
srv.store(snapshot, null);
tick(101);
expect(handle.componentRef.instance._onReuseInit).toHaveBeenCalled();
});
}));
});
});
16 changes: 8 additions & 8 deletions packages/abc/reuse-tab/reuse-tab.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Router,
ROUTER_CONFIGURATION
} from '@angular/router';
import { BehaviorSubject, Observable, Unsubscribable } from 'rxjs';
import { BehaviorSubject, Observable, timer, Unsubscribable } from 'rxjs';

import { Menu, MenuService } from '@delon/theme';
import { ScrollService } from '@delon/util/browser';
Expand Down Expand Up @@ -433,6 +433,12 @@ export class ReuseTabService implements OnDestroy {
}
this._cached.push(item);
} else {
// Current handler is null when activate routes
// For better reliability, we need to wait for the component to be attached before call _onReuseInit
const cahcedComponentRef = this._cached[idx]._handle?.componentRef;
if (_handle == null && cahcedComponentRef != null) {
timer(100).subscribe(() => this.runHook('_onReuseInit', cahcedComponentRef));
}
this._cached[idx] = item;
}
this.removeUrlBuffer = null;
Expand All @@ -457,13 +463,7 @@ export class ReuseTabService implements OnDestroy {
const data = this.get(url);
const ret = !!(data && data._handle);
this.di('#shouldAttach', ret, url);
if (ret) {
const compRef = data!._handle.componentRef;
if (compRef) {
this.componentRef = compRef;
this.runHook('_onReuseInit', compRef);
}
} else {
if (!ret) {
this._cachedChange.next({ active: 'add', url, list: this._cached });
}
return ret;
Expand Down

0 comments on commit 4282fc2

Please sign in to comment.