-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(module:message): fix container instance not destroyed in HMR (#3859)
- Loading branch information
Wendell
committed
Aug 9, 2019
1 parent
8081cdf
commit 07e86a5
Showing
8 changed files
with
94 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* @license | ||
* Copyright Alibaba.com All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE | ||
*/ | ||
|
||
// tslint:disable no-any | ||
|
||
import { Injectable } from '@angular/core'; | ||
|
||
import { environment } from '../environments/environment'; | ||
|
||
interface SingletonRegistryItem { | ||
target: any; | ||
} | ||
|
||
/** | ||
* When running in test, singletons should not be destroyed. So we keep references of singletons | ||
* in this global variable. | ||
*/ | ||
const testSingleRegistry = new Map<string, SingletonRegistryItem>(); | ||
|
||
/** | ||
* Some singletons should have life cycle that is same to Angular's. This service make sure that | ||
* those singletons get destroyed in HMR. | ||
*/ | ||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class NzSingletonService { | ||
private get singletonRegistry(): Map<string, SingletonRegistryItem> { | ||
return environment.isTestMode ? testSingleRegistry : this._singletonRegistry; | ||
} | ||
|
||
/** | ||
* This registry is used to register singleton in dev mode. | ||
* So that singletons get destroyed when hot module reload happens. | ||
* | ||
* This works in prod mode too but with no specific effect. | ||
*/ | ||
private _singletonRegistry = new Map<string, SingletonRegistryItem>(); | ||
|
||
registerSingletonWithKey(key: string, target: any): void { | ||
const alreadyHave = this.singletonRegistry.has(key); | ||
const item: SingletonRegistryItem = alreadyHave ? this.singletonRegistry.get(key)! : this.withNewTarget(target); | ||
|
||
if (!alreadyHave) { | ||
this.singletonRegistry.set(key, item); | ||
} | ||
} | ||
|
||
getSingletonWithKey<T>(key: string): T | null { | ||
return this.singletonRegistry.has(key) ? (this.singletonRegistry.get(key)!.target as T) : null; | ||
} | ||
|
||
private withNewTarget(target: any): SingletonRegistryItem { | ||
return { | ||
target | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,3 @@ title: | |
## en-US | ||
|
||
Normal messages as feedbacks. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters