Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(util:config): support signal of attach #1882

Merged
merged 1 commit into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions packages/util/config/config.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<T extends AlainConfigKey>(componentName: T, key?: string): AlainConfig[T] {
const res = ((this.config[componentName] as { [key: string]: unknown }) || {}) as NzSafeAny;
Expand All @@ -23,12 +20,20 @@ export class AlainConfigService {
return deepMergeKey({}, true, ...defaultValues, this.get(componentName));
}

/**
* 将配置附加到当前实例中,支持 Signal 信号
*/
attach<T extends AlainConfigKey>(componentThis: unknown, componentName: T, defaultValues: AlainConfig[T]): void {
Object.assign(componentThis as any, this.merge(componentName, defaultValues));
}

attachKey<T extends AlainConfigKey>(componentThis: unknown, componentName: T, key: string): void {
Object.assign(componentThis as any, this.get(componentName, key));
const data = this.merge<T>(componentName, defaultValues);
Object.entries(data as Object).forEach(([key, value]) => {
const t = componentThis as any;
const s = t[key]?.[SIGNAL] as SignalNode<any>;
if (s != null) {
s.value = value;
} else {
t[key] = value;
}
});
}

set<T extends AlainConfigKey>(componentName: T, value: AlainConfig[T]): void {
Expand Down
20 changes: 13 additions & 7 deletions packages/util/config/config.spec.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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<string>('a');

constructor() {
srv.attach(this, 'acl', { guard_url: 'override' });
}
}
const obj = new MockSignal();
expect(obj.guard_url()).toBe('override');
});
});
});
Loading