Dealing with Injection Context
Injection Context
is introduced with Angular 14. Based on the documentation, The dependency injection (DI) system relies internally on a runtime context where the current injector is available. This means that injectors can only work when code is executed in this context.Furthermore, as
@ngbox/signal-utils
relies on Angular's effect API internally, all operators must function within the injection context. Fortunately, once both the signal and operators are registered in the injection context, they handle all the necessary logic for you, enabling you to easily create an effect using a custom operator.However, when you create a pipeSignal outside of injection context, you must at least pass an Injector to let all the internal logic works.
createSignalPipe
createSignalPipe
is a function can optionally take Injector as an input and returns an instance of createSignal
function. This allows you to execute your code outside of the injection context.@Injectable()
export class MyService {
private signalPipe = createSignalPipe();
readonly intervalRef = createInterval();
readonly last3FibonacciNumber = this.signalPipe(
this.intervalRef.interval(),
filter((value) => isFibonacci(value)),
bufferCount(3)
);
}
Passing an Injector as a parameter
createSignal
function has an overload that allows you to pass an injector.@Injectable()
export class MyService {
private injector = inject(Injector);
readonly intervalRef = createInterval();
readonly last3FibonacciNumber = signalPipe(
{ source: this.intervalRef.interval, injector: this.injector },
filter((value) => isFibonacci(value)),
bufferCount(3)
);
}
Once the injected or given injector destroyed, @ngbox/signal-utils
clears up all the internal necessary logic to not be a cause of a memory leak.