Comments (10)
Hey, I’ll reply a bit later, I’m onto phone for next the week.
from until-destroy.
Sure, take your time.
from until-destroy.
Considering the above example, how the operator will be used for non-component classes? Services, NgModules, pipes, etc (since they all may implement the OnDestroy interface)?
from until-destroy.
It should work the same.
from until-destroy.
@Injectable()
export class BarService {
destroy$ = untilDestroyed();
init() {
interval(1000).pipe(
this.destroy$
).subscribe(console.log)
}
}
@Component({
selector: 'app-foo',
templateUrl: './foo.component.html',
providers: [
BarService
],
})
from until-destroy.
There're some cases I've noticed where it doesn't work compared to the existing behavior:
@NgModule()
export class SomeModule {
destroy$ = untilDestroyed(); // No provider for ChangeDetectorRef!
}
@Injectable({ providedIn: 'root' })
export class RootService {
destroy$ = untilDestroyed(); // No provider for ChangeDetectorRef!
}
Embedded views:
@Pipe({ name: 'impure', pure: false })
export class ImpurePipe implements PipeTransform {
destroy$ = untilDestroyed();
constructor() {
new Subject()
.pipe(
this.destroy$,
finalize(() => console.log('Finalized')) // Not called
)
.subscribe();
}
transform(value: string) {
return 'Hey';
}
ngOnDestroy(): void {
console.log('Called when `shown` becomes `false`.');
}
}
@Directive({ selector: '[myDirective]' })
export class MyDirective {
destroy$ = untilDestroyed();
constructor() {
new Subject()
.pipe(
this.destroy$,
finalize(() => console.log('Finalized')) // Not called
)
.subscribe();
}
ngOnDestroy(): void {
console.log('Called when `shown` becomes `false`.');
}
}
@Component({
selector: 'app-root',
template: `
<button (click)="shown = !shown">Toggle</button>
<ng-template [ngIf]="shown">
<div myDirective></div>
{{ "" | impure }}
</ng-template>
`
})
export class AppComponent {
shown = true;
}
from until-destroy.
NgModule
- I ignored it on purpose because it's a rare use case, IMO.providedIn: root
- That's make sense. It only works when used with component/directive providers.Embedded views
- That's a nice catch.For some reason, it seems to use the VCR of the host component. I wonder if this is by design or a bug.
from until-destroy.
Actually, it makes sense because we are injecting ChangeDetectorRef.
from until-destroy.
Hmm what about doing something like this (quick pseudo code):
const symbol = Symbol('untilDestroyed');
const patched = Symbol('patched');
export function untilDestroyed(instance: any) {
const proto = Object.getPrototypeOf(instance);
if (!proto[patched]) {
proto[patched] = true;
const original = proto.ngOnDestroy;
proto.ngOnDestroy = function () {
original?.apply(this, arguments);
this[symbol].next();
this[symbol].complete();
}
}
instance[symbol] = new Subject<void>();
return takeUntil(instance[symbol].asObservable())
}
from until-destroy.
This code works, but I don't see any benefit over our current approach. I'm closing the issue for now.
from until-destroy.
Related Issues (20)
- Add a util cancel Promises
- Angular 14 ut could not run correctly HOT 1
- Angular 14 ut could not run correctly with a minimal reproducible example
- Angular 14 ut could not run correctly with a a minimal reproducible example HOT 7
- Can I ask what's the utility for this package?
- How to use until-destroy-migration? HOT 4
- NG0204: Token InjectionToken X is missing a ɵprov definition after Angular 15 upgrade HOT 20
- Angular 15 Directive Composition API: Cannot access [component] before initialization. HOT 4
- [Documentation] For using `untilDestroyed` in non Angular apps HOT 1
- untilDestroyed assumes ngDevMode is defined HOT 2
- Warnings for github actions builds
- Component is undefined if @UntilDestroy() is used HOT 2
- Supporting other types of Subscriptions HOT 1
- Support TS 5.0 non experimental decorators for the UntilDestroy decorator
- ERROR Error: ASSERTION ERROR: token must be defined [Expected=> null != undefined <=Actual] HOT 5
- Angular 16 takeUntilDestroyed HOT 7
- Need upgrade for Angular 17 HOT 2
- Debugger in source code
- Error when using Target 2022 - Similar issue link provided HOT 9
- Use decorator with Singleton service (providedIn: 'root') HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from until-destroy.