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 e0bee43
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions projects/cdk/utils/miscellaneous/directive-binding.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import type {InjectOptions, ProviderToken, Signal, WritableSignal} from '@angular/core';
import {
type CreateEffectOptions,

Check failure on line 2 in projects/cdk/utils/miscellaneous/directive-binding.ts

View workflow job for this annotation

GitHub Actions / Lint

Prefer using a top-level type-only import instead of inline type specifiers
type InjectOptions,

Check failure on line 3 in projects/cdk/utils/miscellaneous/directive-binding.ts

View workflow job for this annotation

GitHub Actions / Lint

Prefer using a top-level type-only import instead of inline type specifiers
type ProviderToken,

Check failure on line 4 in projects/cdk/utils/miscellaneous/directive-binding.ts

View workflow job for this annotation

GitHub Actions / Lint

Prefer using a top-level type-only import instead of inline type specifiers
type Signal,

Check failure on line 5 in projects/cdk/utils/miscellaneous/directive-binding.ts

View workflow job for this annotation

GitHub Actions / Lint

Prefer using a top-level type-only import instead of inline type specifiers
VERSION,
type WritableSignal,

Check failure on line 7 in projects/cdk/utils/miscellaneous/directive-binding.ts

View workflow job for this annotation

GitHub Actions / Lint

Prefer using a top-level type-only import instead of inline type specifiers
} from '@angular/core';
import {effect, inject, isSignal, signal} from '@angular/core';

type SignalLikeTypeOf<T> = T extends Signal<infer R> ? R : T;
Expand All @@ -18,30 +25,32 @@ export function tuiDirectiveBinding<
const result: any = isSignal(initial) ? initial : signal(initial);

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 directive: any = inject(token, options);

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

View check run for this annotation

codefactor.io / CodeFactor

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

Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)
const output = directive[`${key.toString()}Change`];
const angularVersion = parseInt(VERSION.major);

Check failure on line 28 in projects/cdk/utils/miscellaneous/directive-binding.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing radix parameter
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 35 in projects/cdk/utils/miscellaneous/directive-binding.ts

View check run for this annotation

codefactor.io / CodeFactor

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

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 38 in projects/cdk/utils/miscellaneous/directive-binding.ts

View check run for this annotation

codefactor.io / CodeFactor

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

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 e0bee43

Please sign in to comment.