Skip to content

Commit e07b59a

Browse files
committed
fix(Hooks): Fix corner case where ngOnDestroy is called on prototype not instance
1 parent fcc0249 commit e07b59a

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

projects/ngx-observable-lifecycle/src/lib/ngx-observable-lifecycle.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,27 @@ describe('ngx-observable-lifecycle', () => {
8484

8585
(instance as OnDestroy).ngOnDestroy();
8686
});
87+
88+
it('should not throw if onDestroy was invoked on difference instances', () => {
89+
const originalOnDestroySpy = jasmine.createSpy();
90+
91+
class TestClass implements OnDestroy {
92+
public ngOnDestroy(): void {
93+
originalOnDestroySpy();
94+
}
95+
}
96+
97+
const instance = new TestClass();
98+
const instance2 = new TestClass();
99+
100+
const { ngOnDestroy } = getObservableLifecycle(instance);
101+
102+
expect(originalOnDestroySpy).not.toHaveBeenCalled();
103+
104+
instance.ngOnDestroy();
105+
instance2.ngOnDestroy();
106+
107+
expect(originalOnDestroySpy).toHaveBeenCalled();
108+
});
87109
});
88110
});

projects/ngx-observable-lifecycle/src/lib/ngx-observable-lifecycle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function getSubjectForHook(componentInstance: PatchedComponentInstance<any>, hoo
5757

5858
proto[hook] = function (this: PatchedComponentInstance<typeof hook>) {
5959
(originalHook as () => void)?.call(this);
60-
this[hookSubject][hook].next();
60+
this[hookSubject]?.[hook]?.next();
6161
};
6262

6363
const originalOnDestroy = proto.ngOnDestroy;

0 commit comments

Comments
 (0)