Skip to content

Commit a1c4fb1

Browse files
authored
fix(util:config): support signal of attach (#1882)
1 parent c04531c commit a1c4fb1

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

packages/util/config/config.service.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
import { Inject, Injectable, Optional } from '@angular/core';
2+
import { inject, Injectable } from '@angular/core';
3+
import { SIGNAL, SignalNode } from '@angular/core/primitives/signals';
34

45
import { deepMergeKey } from '@delon/util/other';
56
import type { NzSafeAny } from 'ng-zorro-antd/core/types';
@@ -8,11 +9,7 @@ import { AlainConfig, AlainConfigKey, ALAIN_CONFIG } from './config.types';
89

910
@Injectable({ providedIn: 'root' })
1011
export class AlainConfigService {
11-
private config: AlainConfig;
12-
13-
constructor(@Optional() @Inject(ALAIN_CONFIG) defaultConfig?: AlainConfig) {
14-
this.config = { ...defaultConfig };
15-
}
12+
private readonly config = { ...inject(ALAIN_CONFIG, { optional: true }) };
1613

1714
get<T extends AlainConfigKey>(componentName: T, key?: string): AlainConfig[T] {
1815
const res = ((this.config[componentName] as { [key: string]: unknown }) || {}) as NzSafeAny;
@@ -23,12 +20,20 @@ export class AlainConfigService {
2320
return deepMergeKey({}, true, ...defaultValues, this.get(componentName));
2421
}
2522

23+
/**
24+
* 将配置附加到当前实例中,支持 Signal 信号
25+
*/
2626
attach<T extends AlainConfigKey>(componentThis: unknown, componentName: T, defaultValues: AlainConfig[T]): void {
27-
Object.assign(componentThis as any, this.merge(componentName, defaultValues));
28-
}
29-
30-
attachKey<T extends AlainConfigKey>(componentThis: unknown, componentName: T, key: string): void {
31-
Object.assign(componentThis as any, this.get(componentName, key));
27+
const data = this.merge<T>(componentName, defaultValues);
28+
Object.entries(data as Object).forEach(([key, value]) => {
29+
const t = componentThis as any;
30+
const s = t[key]?.[SIGNAL] as SignalNode<any>;
31+
if (s != null) {
32+
s.value = value;
33+
} else {
34+
t[key] = value;
35+
}
36+
});
3237
}
3338

3439
set<T extends AlainConfigKey>(componentName: T, value: AlainConfig[T]): void {

packages/util/config/config.spec.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
import { signal } from '@angular/core';
12
import { TestBed } from '@angular/core/testing';
23

3-
import type { NzSafeAny } from 'ng-zorro-antd/core/types';
4-
54
import type { AlainChartConfig } from './chart/chart.type';
65
import { AlainConfigService } from './config.service';
76

@@ -21,10 +20,17 @@ describe('util: config', () => {
2120
expect(srv.get('chart')?.theme).toBe('dark');
2221
});
2322

24-
it('#attachKey', () => {
25-
const res: NzSafeAny = {};
26-
srv.set('chart', { theme: 'a' });
27-
srv.attachKey(res, 'chart', 'theme');
28-
expect(res.theme).toBe('a');
23+
describe('#attach', () => {
24+
it('support signal', () => {
25+
class MockSignal {
26+
guard_url = signal<string>('a');
27+
28+
constructor() {
29+
srv.attach(this, 'acl', { guard_url: 'override' });
30+
}
31+
}
32+
const obj = new MockSignal();
33+
expect(obj.guard_url()).toBe('override');
34+
});
2935
});
3036
});

0 commit comments

Comments
 (0)