Skip to content

Commit

Permalink
Switch to SafeObserver in MutationObserver classes to prevent errors …
Browse files Browse the repository at this point in the history
…during SSR (#654)
  • Loading branch information
pppdns authored Jul 29, 2024
1 parent 28ddc25 commit 649d9e6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
10 changes: 10 additions & 0 deletions libs/mutation-observer/src/classes/safe-observer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const SafeObserver =
typeof MutationObserver !== `undefined`
? MutationObserver
: class implements MutationObserver {
observe(): void {}
disconnect(): void {}
takeRecords(): MutationRecord[] {
return [];
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Output,
} from '@angular/core';

import {SafeObserver} from '../classes/safe-observer';
import {MUTATION_OBSERVER_INIT} from '../tokens/mutation-observer-init';
import {mutationObserverInitFactory} from '../utils/mutation-observer-init-factory';

Expand All @@ -22,7 +23,7 @@ import {mutationObserverInitFactory} from '../utils/mutation-observer-init-facto
},
],
})
export class MutationObserverDirective extends MutationObserver implements OnDestroy {
export class MutationObserverDirective extends SafeObserver implements OnDestroy {
@Output()
readonly waMutationObserver = new EventEmitter<MutationRecord[]>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {ElementRef, Inject, Injectable} from '@angular/core';
import {Observable} from 'rxjs';

import {SafeObserver} from '../classes/safe-observer';
import {MUTATION_OBSERVER_INIT} from '../tokens/mutation-observer-init';

@Injectable()
Expand All @@ -10,7 +11,7 @@ export class MutationObserverService extends Observable<readonly MutationRecord[
@Inject(MUTATION_OBSERVER_INIT) config: MutationObserverInit,
) {
super(subscriber => {
const observer = new MutationObserver(records => {
const observer = new SafeObserver(records => {
subscriber.next(records);
});

Expand Down

0 comments on commit 649d9e6

Please sign in to comment.