Skip to content
Open
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
3 changes: 3 additions & 0 deletions libs/ngxtension/inject-rate-limited/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# ngxtension/inject-rate-limited

Secondary entry point of `ngxtension`. It can be used by importing from `ngxtension/inject-rate-limited`.
5 changes: 5 additions & 0 deletions libs/ngxtension/inject-rate-limited/ng-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"lib": {
"entryFile": "src/index.ts"
}
}
1 change: 1 addition & 0 deletions libs/ngxtension/inject-rate-limited/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './inject-rate-limited';
95 changes: 95 additions & 0 deletions libs/ngxtension/inject-rate-limited/src/inject-rate-limited.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import {
inject,
InjectionToken,
Injector,
Signal,
signal,
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { assertInjector } from 'ngxtension/assert-injector';
import {
asyncScheduler,
auditTime,
debounceTime,
sampleTime,
Subject,
ThrottleConfig,
throttleTime,
} from 'rxjs';

const NGXTENSION_RATE_LIMIT = new InjectionToken('NGXTENSION_RATE_LIMIT', {
providedIn: 'root',
factory: () => 200,
});

export type RateLimitedSignal<T> = Signal<T> & {
next: (value: T) => void;
};

export interface RateLimitedSignalOptions {
/**
* Time window in milliseconds used by the operator.
* Defaults to the NGXTENSION_RATE_LIMIT injection token.
*/
durationMs?: number;

/**
* RxJS operator used to rate-limit updates.
* @default debounceTime
*/
operator?:
| typeof debounceTime
| typeof throttleTime
| typeof sampleTime
| typeof auditTime;

/**
* Optional injector override.
*/
injector?: Injector;

/**
* Configuration for `throttleTime`, if used.
* @default { leading: true, trailing: false }
*/
config?: ThrottleConfig;
}

/**
* Creates a signal that buffers and emits updates using a rate-limiting RxJS operator.
*/
export function injectRateLimited<T>(
initialValue: T,
options: RateLimitedSignalOptions = {},
): RateLimitedSignal<T> {
return assertInjector(injectRateLimited, options?.injector, () => {
const defaultRateLimit = inject(NGXTENSION_RATE_LIMIT);

const {
durationMs: delayMs = defaultRateLimit,
operator = debounceTime,
config = {
leading: true,
trailing: false,
},
} = options;

const subject = new Subject<T>();
const inner = signal<T>(initialValue);

subject
.pipe(operator(delayMs, asyncScheduler, config), takeUntilDestroyed())
.subscribe((value) => inner.set(value));

return new Proxy(inner.asReadonly(), {
get(target, prop, receiver) {
switch (prop) {
case 'next':
return (value: T) => subject.next(value);
default:
return Reflect.get(target, prop, receiver);
}
},
});
});
}
3 changes: 3 additions & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@
"ngxtension/inject-query-params": [
"libs/ngxtension/inject-query-params/src/index.ts"
],
"ngxtension/inject-rate-limited": [
"libs/ngxtension/inject-rate-limited/src/index.ts"
],
"ngxtension/inject-route-data": [
"libs/ngxtension/inject-route-data/src/index.ts"
],
Expand Down