-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update roadmap * Add how ContextDisposerDirective works * Add specs and class for ContextDisposerDirective * Import/export ContextDisposerDirective * Update roadmap with disposer directive * Update version number * Fix ContextProviderComponent import * Avoid calls to syncProperties on provider reset * Add integration test for ContextDisposerDirective * Fix change detection problem on embedded view * Fix view duplication * Update documentation and revise title
- Loading branch information
1 parent
1a798b9
commit 84f5566
Showing
11 changed files
with
433 additions
and
17 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "ngx-context", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"scripts": { | ||
"ng": "ng", | ||
"start": "ng serve", | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { | ||
Directive, | ||
EmbeddedViewRef, | ||
Input, | ||
Optional, | ||
SkipSelf, | ||
TemplateRef, | ||
ViewContainerRef, | ||
} from '@angular/core'; | ||
import { Subject } from 'rxjs'; | ||
import { filter, takeUntil } from 'rxjs/operators'; | ||
import { parseKeys } from './internals'; | ||
import { ContextProviderComponent } from './provider.component'; | ||
|
||
@Directive({ | ||
selector: '[contextDisposer]', | ||
}) | ||
export class ContextDisposerDirective { | ||
private destroy$ = new Subject<void>(); | ||
private _dispose: string | string[] = ''; | ||
private view: EmbeddedViewRef<any>; | ||
|
||
@Input('contextDisposer') | ||
set dispose(dispose: string | string[]) { | ||
this._dispose = dispose || ''; | ||
} | ||
get dispose(): string | string[] { | ||
return this._dispose; | ||
} | ||
|
||
constructor( | ||
@Optional() | ||
private tempRef: TemplateRef<any>, | ||
@Optional() | ||
private vcRef: ViewContainerRef, | ||
@Optional() | ||
@SkipSelf() | ||
private provider: ContextProviderComponent, | ||
) {} | ||
|
||
private init(): void { | ||
const disposed: string[] = parseKeys(this.dispose); | ||
|
||
this.provider.reset$ | ||
.pipe(takeUntil(this.destroy$)) | ||
.subscribe(() => this.ngOnChanges()); | ||
|
||
if (this.provider.provide.length) | ||
this.provider.change$ | ||
.pipe( | ||
takeUntil(this.destroy$), | ||
filter(key => !!key), | ||
) | ||
.subscribe(providerKey => this.syncProperties(disposed, providerKey)); | ||
} | ||
|
||
private reset(): void { | ||
this.view = this.vcRef.createEmbeddedView(this.tempRef, new Context()); | ||
} | ||
|
||
private syncProperties(disposed: string[], providerKey: string): void { | ||
const key = this.provider.contextMap[providerKey] || providerKey; | ||
|
||
if (disposed.length && disposed.indexOf(key) < 0) return; | ||
|
||
const value = this.provider.component[providerKey]; | ||
|
||
this.view.context.$implicit[key] = value; | ||
this.view.context[key] = value; | ||
this.view.markForCheck(); | ||
} | ||
|
||
ngOnChanges() { | ||
this.ngOnDestroy(); | ||
this.reset(); | ||
|
||
if (this.provider && this.tempRef && this.vcRef) this.init(); | ||
} | ||
|
||
ngOnDestroy() { | ||
this.destroy$.next(); | ||
|
||
if (this.view) this.vcRef.clear(); | ||
} | ||
} | ||
|
||
export class Context { | ||
$implicit: { [key: string]: any } = {}; | ||
} |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export { NgxContextModule } from './lib/context.module'; | ||
export { ContextConsumerComponent } from './lib/consumer.component'; | ||
export { ContextConsumerDirective } from './lib/consumer.directive'; | ||
export { ContextDisposerDirective } from './lib/disposer.directive'; | ||
export { ContextProviderComponent } from './lib/provider.component'; | ||
export { ContextMap } from './lib/symbols'; |
Oops, something went wrong.