-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: useDirectiveForHost utility in Angular (#199)
- Loading branch information
Showing
3 changed files
with
61 additions
and
94 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,65 @@ | ||
import type {OnChanges, OnDestroy, SimpleChanges} from '@angular/core'; | ||
import {Directive, ElementRef, inject, Input} from '@angular/core'; | ||
import type {OnChanges} from '@angular/core'; | ||
import {DestroyRef, Directive, ElementRef, inject, Input} from '@angular/core'; | ||
import type {Directive as AgnosUIDirective} from '@agnos-ui/core'; | ||
|
||
// All calls of the directive in this class are done asynchronously (with await 0) | ||
// in order to avoid ExpressionChangedAfterItHasBeenCheckedError | ||
// or the corresponding issue with signals (https://github.com/angular/angular/issues/50320) | ||
// This is relevant especially if calling the directive changes variables used in a template. | ||
|
||
@Directive({ | ||
standalone: true, | ||
selector: '[auUse]', | ||
}) | ||
export class UseDirective<T> implements OnChanges, OnDestroy { | ||
@Input('auUse') | ||
use: AgnosUIDirective<T> | undefined; | ||
export const useDirectiveForHost = <T>(use?: AgnosUIDirective<T>, params?: T) => { | ||
const ref = inject(ElementRef); | ||
|
||
@Input('auUseParams') | ||
params: T | undefined; | ||
|
||
#ref = inject(ElementRef); | ||
let instance = use?.(ref.nativeElement, params as T); | ||
|
||
#directive: AgnosUIDirective<T> | undefined; | ||
#directiveInstance?: ReturnType<AgnosUIDirective<T>>; | ||
|
||
async #destroyDirectiveInstance() { | ||
const directiveInstance = this.#directiveInstance; | ||
this.#directiveInstance = undefined; | ||
if (directiveInstance?.destroy) { | ||
async function destroyDirectiveInstance() { | ||
const oldInstance = instance; | ||
instance = undefined; | ||
use = undefined; | ||
if (oldInstance?.destroy) { | ||
await 0; | ||
directiveInstance.destroy?.(); | ||
oldInstance.destroy?.(); | ||
} | ||
} | ||
|
||
async ngOnChanges(changes: SimpleChanges): Promise<void> { | ||
if (this.use !== this.#directive) { | ||
this.#destroyDirectiveInstance(); | ||
const directive = this.use; | ||
this.#directive = directive; | ||
if (directive) { | ||
inject(DestroyRef).onDestroy(destroyDirectiveInstance); | ||
|
||
async function update(newUse?: AgnosUIDirective<T>, newParams?: T) { | ||
if (newUse !== use) { | ||
destroyDirectiveInstance(); | ||
use = newUse; | ||
params = newParams; | ||
if (newUse) { | ||
await 0; | ||
// checks that the directive did not change while waiting: | ||
if (directive === this.#directive && !this.#directiveInstance) { | ||
this.#directiveInstance = directive(this.#ref.nativeElement, this.params as T); | ||
if (use === newUse && !instance) { | ||
instance = use(ref.nativeElement, params as T); | ||
} | ||
} | ||
} else if (changes['params']) { | ||
} else if (newParams != params) { | ||
params = newParams; | ||
await 0; | ||
this.#directiveInstance?.update?.(this.params as T); | ||
instance?.update?.(params as T); | ||
} | ||
} | ||
|
||
ngOnDestroy(): void { | ||
this.#destroyDirectiveInstance(); | ||
this.#directive = undefined; | ||
return {update}; | ||
}; | ||
|
||
@Directive({ | ||
standalone: true, | ||
selector: '[auUse]', | ||
}) | ||
export class UseDirective<T> implements OnChanges { | ||
@Input('auUse') | ||
use: AgnosUIDirective<T> | undefined; | ||
|
||
@Input('auUseParams') | ||
params: T | undefined; | ||
|
||
#useDirective = useDirectiveForHost<T>(); | ||
|
||
ngOnChanges() { | ||
this.#useDirective.update(this.use, this.params); | ||
} | ||
} |
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