Skip to content

Commit

Permalink
fix(cdk): angular v19 "allowSignalWrites" warning
Browse files Browse the repository at this point in the history
  • Loading branch information
hakimio committed Nov 21, 2024
1 parent 432a486 commit c461bfa
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions projects/cdk/utils/miscellaneous/directive-binding.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import type {InjectOptions, ProviderToken, Signal, WritableSignal} from '@angular/core';
import {effect, inject, isSignal, signal} from '@angular/core';
import type {
CreateEffectOptions,
InjectOptions,
ProviderToken,
Signal,
WritableSignal,
} from '@angular/core';
import {effect, inject, isSignal, signal, VERSION} from '@angular/core';

type SignalLikeTypeOf<T> = T extends Signal<infer R> ? R : T;

Expand All @@ -18,30 +24,32 @@ export function tuiDirectiveBinding<
const result: any = isSignal(initial) ? initial : signal(initial);

Check warning on line 24 in projects/cdk/utils/miscellaneous/directive-binding.ts

View check run for this annotation

codefactor.io / CodeFactor

projects/cdk/utils/miscellaneous/directive-binding.ts#L24

Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)
const directive: any = inject(token, options);

Check warning on line 25 in projects/cdk/utils/miscellaneous/directive-binding.ts

View check run for this annotation

codefactor.io / CodeFactor

projects/cdk/utils/miscellaneous/directive-binding.ts#L25

Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)
const output = directive[`${key.toString()}Change`];
const angularVersion = parseInt(VERSION.major, 10);
const isAngular19 = angularVersion >= 19;
const effectOptions: CreateEffectOptions = isAngular19
? {}
: {allowSignalWrites: true};

// TODO: Figure out why effects are executed all the time and not just when result changes (check with Angular 18)
let previous: any;

Check warning on line 34 in projects/cdk/utils/miscellaneous/directive-binding.ts

View check run for this annotation

codefactor.io / CodeFactor

projects/cdk/utils/miscellaneous/directive-binding.ts#L34

Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)

effect(
() => {
const value: any = result();

if (previous === value) {
return;
}

if (isSignal(directive[key])) {
directive[key].set(value);
} else {
directive[key] = value;
}

directive.ngOnChanges?.({});
output?.emit?.(value);
previous = value;
},
{allowSignalWrites: true},
);
effect(() => {
const value: any = result();

Check warning on line 37 in projects/cdk/utils/miscellaneous/directive-binding.ts

View check run for this annotation

codefactor.io / CodeFactor

projects/cdk/utils/miscellaneous/directive-binding.ts#L37

Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)

if (previous === value) {
return;
}

if (isSignal(directive[key])) {
directive[key].set(value);
} else {
directive[key] = value;
}

directive.ngOnChanges?.({});
output?.emit?.(value);
previous = value;
}, effectOptions);

return result;
}

0 comments on commit c461bfa

Please sign in to comment.