From 3c846a284fa7ee9b7644ae191439c52e168d97ca Mon Sep 17 00:00:00 2001 From: cipchk Date: Mon, 20 Jan 2025 00:42:14 +0800 Subject: [PATCH] fix(util:config): support signal of `attach` --- packages/util/config/config.service.ts | 27 +++++++++++++++----------- packages/util/config/config.spec.ts | 20 ++++++++++++------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/packages/util/config/config.service.ts b/packages/util/config/config.service.ts index 3331d00872..b15723e13e 100644 --- a/packages/util/config/config.service.ts +++ b/packages/util/config/config.service.ts @@ -1,5 +1,6 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ -import { Inject, Injectable, Optional } from '@angular/core'; +import { inject, Injectable } from '@angular/core'; +import { SIGNAL, SignalNode } from '@angular/core/primitives/signals'; import { deepMergeKey } from '@delon/util/other'; import type { NzSafeAny } from 'ng-zorro-antd/core/types'; @@ -8,11 +9,7 @@ import { AlainConfig, AlainConfigKey, ALAIN_CONFIG } from './config.types'; @Injectable({ providedIn: 'root' }) export class AlainConfigService { - private config: AlainConfig; - - constructor(@Optional() @Inject(ALAIN_CONFIG) defaultConfig?: AlainConfig) { - this.config = { ...defaultConfig }; - } + private readonly config = { ...inject(ALAIN_CONFIG, { optional: true }) }; get(componentName: T, key?: string): AlainConfig[T] { const res = ((this.config[componentName] as { [key: string]: unknown }) || {}) as NzSafeAny; @@ -23,12 +20,20 @@ export class AlainConfigService { return deepMergeKey({}, true, ...defaultValues, this.get(componentName)); } + /** + * 将配置附加到当前实例中,支持 Signal 信号 + */ attach(componentThis: unknown, componentName: T, defaultValues: AlainConfig[T]): void { - Object.assign(componentThis as any, this.merge(componentName, defaultValues)); - } - - attachKey(componentThis: unknown, componentName: T, key: string): void { - Object.assign(componentThis as any, this.get(componentName, key)); + const data = this.merge(componentName, defaultValues); + Object.entries(data as Object).forEach(([key, value]) => { + const t = componentThis as any; + const s = t[key]?.[SIGNAL] as SignalNode; + if (s != null) { + s.value = value; + } else { + t[key] = value; + } + }); } set(componentName: T, value: AlainConfig[T]): void { diff --git a/packages/util/config/config.spec.ts b/packages/util/config/config.spec.ts index 2a49f3ecb3..e264f640ce 100644 --- a/packages/util/config/config.spec.ts +++ b/packages/util/config/config.spec.ts @@ -1,7 +1,6 @@ +import { signal } from '@angular/core'; import { TestBed } from '@angular/core/testing'; -import type { NzSafeAny } from 'ng-zorro-antd/core/types'; - import type { AlainChartConfig } from './chart/chart.type'; import { AlainConfigService } from './config.service'; @@ -21,10 +20,17 @@ describe('util: config', () => { expect(srv.get('chart')?.theme).toBe('dark'); }); - it('#attachKey', () => { - const res: NzSafeAny = {}; - srv.set('chart', { theme: 'a' }); - srv.attachKey(res, 'chart', 'theme'); - expect(res.theme).toBe('a'); + describe('#attach', () => { + it('support signal', () => { + class MockSignal { + guard_url = signal('a'); + + constructor() { + srv.attach(this, 'acl', { guard_url: 'override' }); + } + } + const obj = new MockSignal(); + expect(obj.guard_url()).toBe('override'); + }); }); });