Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use ngOnChanges instead of effect to avoid signal-writes error #25

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions projects/ngneat/overview/src/lib/teleport/teleport.directive.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { Directive, EmbeddedViewRef, OnDestroy, TemplateRef, effect, inject, input } from '@angular/core';
import {
Directive,
EmbeddedViewRef,
OnChanges,
OnDestroy,
SimpleChanges,
TemplateRef,
inject,
input,
} from '@angular/core';
import { Subscription } from 'rxjs';

import { TeleportService } from './teleport.service';
Expand All @@ -7,7 +16,7 @@ import { TeleportService } from './teleport.service';
selector: '[teleportTo]',
standalone: true,
})
export class TeleportDirective implements OnDestroy {
export class TeleportDirective implements OnChanges, OnDestroy {
readonly teleportTo = input<string | null | undefined>();

private viewRef: EmbeddedViewRef<any>;
Expand All @@ -16,20 +25,21 @@ export class TeleportDirective implements OnDestroy {
private tpl = inject(TemplateRef);
private service = inject(TeleportService);

constructor() {
effect(() => {
const teleportTo = this.teleportTo();

if (typeof teleportTo === 'string') {
this.dispose();

this.subscription = this.service.outlet$(teleportTo).subscribe((outlet) => {
if (outlet) {
this.viewRef = outlet.createEmbeddedView(this.tpl);
}
});
}
});
ngOnChanges(changes: SimpleChanges): void {
// Note: Do not use `effect` to observe changes to `teleportTo`, as
// this would result in running `createEmbeddedView` within that effect.
// `createEmbeddedView` may create a component whose constructor might
// trigger another effect or write to some signals, given that we are
// operating within a reactive context.
if (changes.teleportTo && typeof this.teleportTo() === 'string') {
this.dispose();

this.subscription = this.service.outlet$(this.teleportTo()).subscribe((outlet) => {
if (outlet) {
this.viewRef = outlet.createEmbeddedView(this.tpl);
}
});
}
}

ngOnDestroy(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import { filter, map } from 'rxjs/operators';
})
export class TeleportService {
private outlets = new BehaviorSubject<string>('');
private asObservable = this.outlets.asObservable();

outlet$(name: string) {
return this.asObservable.pipe(
return this.outlets.pipe(
filter((current) => current === name),
map((name) => this.ports.get(name))
);
Expand Down
Loading