Skip to content

Commit

Permalink
fix: change signal input modifier to accept whole value signal instea…
Browse files Browse the repository at this point in the history
…d of set function (#19)
  • Loading branch information
goetzrobin authored Oct 9, 2023
1 parent a3dead7 commit 8687663
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
17 changes: 6 additions & 11 deletions packages/platform/src/lib/signal-input-debounce.directive.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Directive, forwardRef, Input, OnDestroy} from '@angular/core';
import {Directive, forwardRef, Input, OnDestroy, Signal, WritableSignal} from '@angular/core';
import {SIGNAL_INPUT_MODIFIER, SignalInputModifier} from "./signal-input-modifier.token";
import {debounceTime, Subject, Subscription} from "rxjs";

Expand All @@ -14,24 +14,19 @@ export const DEBOUNCE_MODIFIER: any = {
providers: [DEBOUNCE_MODIFIER]
})
export class SignalInputDebounceDirective implements SignalInputModifier, OnDestroy {
private _debounced = new Subject();
private _debounced = new Subject<unknown>();
private _debouncedSub: Subscription | null = null;
private _signalToDebounce?: WritableSignal<unknown>

@Input()
set debounce(value: number) {
this._debouncedSub?.unsubscribe();
this._debouncedSub = this._debounced.pipe(debounceTime(value))
.subscribe(value => {
this.setSignal(value)
})
.subscribe(value => this._signalToDebounce?.set(value));
}

// we replace this in the registerOnSet method with the set function passed by the SignalInputDirective
private setSignal(value: unknown): void {
}

public registerOnSet(set: (value: unknown) => void): void {
this.setSignal = set
public registerValueSignal(valueSignal: WritableSignal<unknown>): void {
this._signalToDebounce = valueSignal
}

public onModelChange(value: unknown): void {
Expand Down
4 changes: 2 additions & 2 deletions packages/platform/src/lib/signal-input-modifier.token.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {InjectionToken} from "@angular/core";
import {InjectionToken, WritableSignal} from "@angular/core";

export interface SignalInputModifier {
onModelChange(value: unknown): void

registerOnSet(set: (value: unknown) => void): void;
registerValueSignal(signal: WritableSignal<unknown>): void;
}

export const SIGNAL_INPUT_MODIFIER = new InjectionToken<SignalInputModifier>('Custom signal input modifier');
2 changes: 1 addition & 1 deletion packages/platform/src/lib/signal-input.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class SignalInputDirective implements OnInit {
if (this.modifiers.length !== 1) {
throw Error('only one modifier per signal input field supported.')
} else if (this.formField) {
this.modifiers[0].registerOnSet(this.formField.value.set)
this.modifiers[0].registerValueSignal(this.formField.value)
}
}

Expand Down

0 comments on commit 8687663

Please sign in to comment.