Skip to content

Commit

Permalink
chore: clean constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk committed Jan 19, 2024
1 parent 3c5095d commit f57dc57
Show file tree
Hide file tree
Showing 23 changed files with 146 additions and 130 deletions.
10 changes: 4 additions & 6 deletions packages/abc/media/media.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Injectable } from '@angular/core';
import { Injectable, inject } from '@angular/core';
import { Observable, Subject, share } from 'rxjs';

import { AlainConfigService, AlainMediaConfig } from '@delon/util/config';
import { LazyService } from '@delon/util/other';

@Injectable({ providedIn: 'root' })
export class MediaService {
private readonly cogSrv = inject(AlainConfigService);
private readonly lazySrv = inject(LazyService);

private _cog!: AlainMediaConfig;
private loading = false;
private loaded = false;
Expand All @@ -24,11 +27,6 @@ export class MediaService {
)!;
}

constructor(
private cogSrv: AlainConfigService,
private lazySrv: LazyService
) {}

load(): this {
if (this.loading) {
if (this.loaded) {
Expand Down
8 changes: 3 additions & 5 deletions packages/acl/src/acl-guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ import type { ACLCanType, ACLGuardData } from './acl.type';

@Injectable({ providedIn: 'root' })
export class ACLGuardService {
constructor(
private srv: ACLService,
private router: Router,
private injector: Injector
) {}
private readonly srv = inject(ACLService);
private readonly router = inject(Router);
private readonly injector = inject(Injector);

process(data?: ACLGuardData): Observable<boolean> {
data = {
Expand Down
21 changes: 12 additions & 9 deletions packages/acl/src/acl-if.directive.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Directive, EmbeddedViewRef, Input, OnDestroy, TemplateRef, ViewContainerRef } from '@angular/core';
import { Directive, EmbeddedViewRef, Input, OnDestroy, TemplateRef, ViewContainerRef, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { Subscription, filter } from 'rxjs';

import { ACLService } from './acl.service';
Expand All @@ -10,23 +11,25 @@ import { ACLCanType } from './acl.type';
standalone: true
})
export class ACLIfDirective implements OnDestroy {
private readonly srv = inject(ACLService);
private readonly _viewContainer = inject(ViewContainerRef);
static ngAcceptInputType_except: boolean | string | undefined | null;

private _value!: ACLCanType;
private _change$: Subscription;
private _thenTemplateRef: TemplateRef<void> | null = null;
private _thenTemplateRef: TemplateRef<void> | null = inject(TemplateRef<void>);
private _elseTemplateRef: TemplateRef<void> | null = null;
private _thenViewRef: EmbeddedViewRef<void> | null = null;
private _elseViewRef: EmbeddedViewRef<void> | null = null;
private _except = false;

constructor(
templateRef: TemplateRef<void>,
private srv: ACLService,
private _viewContainer: ViewContainerRef
) {
this._change$ = this.srv.change.pipe(filter(r => r != null)).subscribe(() => this._updateView());
this._thenTemplateRef = templateRef;
constructor() {
this._change$ = this.srv.change
.pipe(
takeUntilDestroyed(),
filter(r => r != null)
)
.subscribe(() => this._updateView());
}

@Input()
Expand Down
22 changes: 14 additions & 8 deletions packages/acl/src/acl.directive.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Directive, ElementRef, Input, OnDestroy, Renderer2 } from '@angular/core';
import { Directive, ElementRef, Input, OnDestroy, Renderer2, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { Subscription, filter } from 'rxjs';

import { ACLService } from './acl.service';
Expand All @@ -10,6 +11,10 @@ import { ACLCanType } from './acl.type';
standalone: true
})
export class ACLDirective implements OnDestroy {
private readonly el: HTMLElement = inject(ElementRef).nativeElement;
private readonly renderer = inject(Renderer2);
private readonly srv = inject(ACLService);

private _value!: ACLCanType;
private change$: Subscription;

Expand All @@ -26,20 +31,21 @@ export class ACLDirective implements OnDestroy {
private set(value: ACLCanType): void {
this._value = value;
const CLS = 'acl__hide';
const el = this.el.nativeElement;
const el = this.el;
if (this.srv.can(this._value)) {
this.renderer.removeClass(el, CLS);
} else {
this.renderer.addClass(el, CLS);
}
}

constructor(
private el: ElementRef,
private renderer: Renderer2,
protected srv: ACLService
) {
this.change$ = this.srv.change.pipe(filter(r => r != null)).subscribe(() => this.set(this._value));
constructor() {
this.change$ = this.srv.change
.pipe(
takeUntilDestroyed(),
filter(r => r != null)
)
.subscribe(() => this.set(this._value));
}

ngOnDestroy(): void {
Expand Down
14 changes: 6 additions & 8 deletions packages/auth/src/social/social.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { DOCUMENT } from '@angular/common';
import { Inject, Injectable, OnDestroy } from '@angular/core';
import { Injectable, OnDestroy, inject } from '@angular/core';
import { Router } from '@angular/router';
import { Observable, Observer } from 'rxjs';

import { DA_SERVICE_TOKEN, ITokenModel, ITokenService } from '../token/interface';
import { DA_SERVICE_TOKEN, ITokenModel } from '../token/interface';

const OPENTYPE = '_delonAuthSocialType';
const HREFCALLBACK = '_delonAuthSocialCallbackByHref';
Expand All @@ -13,16 +13,14 @@ export type SocialOpenType = 'href' | 'window';

@Injectable()
export class SocialService implements OnDestroy {
private readonly tokenService = inject(DA_SERVICE_TOKEN);
private readonly doc = inject(DOCUMENT);
private readonly router = inject(Router);

private _win: Window | null = null;
private _winTime: any;
private observer!: Observer<ITokenModel | null>;

constructor(
@Inject(DA_SERVICE_TOKEN) private tokenService: ITokenService,
@Inject(DOCUMENT) private doc: any,
private router: Router
) {}

/**
* 使用窗体打开授权页,返回值是 `Observable<ITokenModel>` 用于订阅授权后返回的结果
*
Expand Down
24 changes: 17 additions & 7 deletions packages/auth/src/store/cookie-storage.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { TestBed } from '@angular/core/testing';

import { CookieOptions, CookieService } from '@delon/util/browser';

import { CookieStorageStore } from './cookie-storage.service';
Expand All @@ -16,13 +18,21 @@ describe('auth: cookie-storage', () => {
beforeEach(() => {
data = {};
putSpy = jasmine.createSpy('put').and.callFake((key: string, value: string) => (data[key] = value));
store = new CookieStorageStore({
put: putSpy,
get: jasmine.createSpy('get').and.callFake((key: string) => data[key]),
remove: jasmine.createSpy('remove').and.callFake((key: string) => {
delete data[key];
})
} as unknown as CookieService);
TestBed.configureTestingModule({
providers: [
{
provide: CookieService,
useValue: {
put: putSpy,
get: jasmine.createSpy('get').and.callFake((key: string) => data[key]),
remove: jasmine.createSpy('remove').and.callFake((key: string) => {
delete data[key];
})
}
}
]
});
store = new CookieStorageStore();
});

it('should be never return null', () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/auth/src/store/cookie-storage.service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { inject } from '@angular/core';

import { CookieService } from '@delon/util/browser';

import { IStore } from './interface';
Expand All @@ -11,7 +13,7 @@ import { ITokenModel } from '../token/interface';
* ```
*/
export class CookieStorageStore implements IStore {
constructor(private srv: CookieService) {}
private readonly srv = inject(CookieService);

get(key: string): ITokenModel {
try {
Expand Down
6 changes: 3 additions & 3 deletions packages/auth/src/token/jwt/jwt.guard.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Inject, Injectable, inject } from '@angular/core';
import { Injectable, inject } from '@angular/core';
import { CanActivateChildFn, CanActivateFn, CanMatchFn } from '@angular/router';

import { JWTTokenModel } from './jwt.model';
import { CheckJwt, ToLogin } from '../helper';
import { DA_SERVICE_TOKEN, ITokenService } from '../interface';
import { DA_SERVICE_TOKEN } from '../interface';

@Injectable({ providedIn: 'root' })
export class AuthJWTGuardService {
constructor(@Inject(DA_SERVICE_TOKEN) private srv: ITokenService) {}
private readonly srv = inject(DA_SERVICE_TOKEN);

process(url?: string): boolean {
const cog = this.srv.options;
Expand Down
6 changes: 3 additions & 3 deletions packages/auth/src/token/simple/simple.guard.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Inject, Injectable, inject } from '@angular/core';
import { Injectable, inject } from '@angular/core';
import { CanActivateChildFn, CanActivateFn, CanMatchFn } from '@angular/router';

import { SimpleTokenModel } from './simple.model';
import { CheckSimple, ToLogin } from '../helper';
import { DA_SERVICE_TOKEN, ITokenService } from '../interface';
import { DA_SERVICE_TOKEN } from '../interface';

@Injectable({ providedIn: 'root' })
export class AuthSimpleGuardService {
constructor(@Inject(DA_SERVICE_TOKEN) private srv: ITokenService) {}
private readonly srv = inject(DA_SERVICE_TOKEN);

process(url?: string): boolean {
const res = CheckSimple(this.srv.get() as SimpleTokenModel);
Expand Down
12 changes: 5 additions & 7 deletions packages/auth/src/token/token.service.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
import { inject, Inject, Injectable, OnDestroy } from '@angular/core';
import { inject, Injectable, OnDestroy } from '@angular/core';
import { BehaviorSubject, interval, Observable, Subject, Subscription, filter, map, share } from 'rxjs';

import { AlainAuthConfig, AlainConfigService } from '@delon/util/config';

import { AuthReferrer, ITokenModel, ITokenService } from './interface';
import { mergeConfig } from '../auth.config';
import { DA_STORE_TOKEN, IStore } from '../store/interface';
import { DA_STORE_TOKEN } from '../store/interface';

export function DA_SERVICE_TOKEN_FACTORY(): ITokenService {
return new TokenService(inject(AlainConfigService), inject(DA_STORE_TOKEN));
return new TokenService(inject(AlainConfigService));
}

/**
* 维护Token信息服务,[在线文档](https://ng-alain.com/auth)
*/
@Injectable()
export class TokenService implements ITokenService, OnDestroy {
private readonly store = inject(DA_STORE_TOKEN);
private refresh$ = new Subject<ITokenModel>();
private change$ = new BehaviorSubject<ITokenModel | null>(null);
private interval$?: Subscription;
private _referrer: AuthReferrer = {};
private _options: AlainAuthConfig;

constructor(
configSrv: AlainConfigService,
@Inject(DA_STORE_TOKEN) private store: IStore
) {
constructor(configSrv: AlainConfigService) {
this._options = mergeConfig(configSrv);
}

Expand Down
17 changes: 8 additions & 9 deletions packages/cache/src/cache.service.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Platform } from '@angular/cdk/platform';
import { HttpClient } from '@angular/common/http';
import { Inject, Injectable, OnDestroy } from '@angular/core';
import { Injectable, OnDestroy, inject } from '@angular/core';
import { BehaviorSubject, Observable, of, map, tap } from 'rxjs';

import { addSeconds } from 'date-fns';

import { AlainCacheConfig, AlainConfigService } from '@delon/util/config';
import { deepGet } from '@delon/util/other';

import { CacheNotifyResult, CacheNotifyType, ICache, ICacheStore } from './interface';
import { CacheNotifyResult, CacheNotifyType, ICache } from './interface';
import { DC_STORE_STORAGE_TOKEN } from './local-storage-cache.service';

@Injectable({ providedIn: 'root' })
export class CacheService implements OnDestroy {
private readonly store = inject(DC_STORE_STORAGE_TOKEN);
private readonly http = inject(HttpClient);
private readonly platform = inject(Platform);

private readonly memory: Map<string, ICache> = new Map<string, ICache>();
private readonly notifyBuffer: Map<string, BehaviorSubject<CacheNotifyResult>> = new Map<
string,
Expand All @@ -24,19 +28,14 @@ export class CacheService implements OnDestroy {
private freqTime: any;
private cog: AlainCacheConfig;

constructor(
cogSrv: AlainConfigService,
@Inject(DC_STORE_STORAGE_TOKEN) private store: ICacheStore,
private http: HttpClient,
private platform: Platform
) {
constructor(cogSrv: AlainConfigService) {
this.cog = cogSrv.merge('cache', {
mode: 'promise',
reName: '',
prefix: '',
meta_key: '__cache_meta'
})!;
if (!platform.isBrowser) return;
if (!this.platform.isBrowser) return;
this.loadMeta();
this.startExpireNotify();
}
Expand Down
4 changes: 2 additions & 2 deletions packages/cache/src/local-storage-cache.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { ICache, ICacheStore } from './interface';

export const DC_STORE_STORAGE_TOKEN = new InjectionToken<ICacheStore>('DC_STORE_STORAGE_TOKEN', {
providedIn: 'root',
factory: () => new LocalStorageCacheService(inject(Platform))
factory: () => new LocalStorageCacheService()
});

export class LocalStorageCacheService implements ICacheStore {
constructor(private platform: Platform) {}
private readonly platform = inject(Platform);

get(key: string): ICache | null {
if (!this.platform.isBrowser) {
Expand Down
6 changes: 3 additions & 3 deletions packages/chart/card/card.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
OnChanges,
TemplateRef,
ViewEncapsulation,
booleanAttribute
booleanAttribute,
inject
} from '@angular/core';

import { NzCardComponent } from 'ng-zorro-antd/card';
Expand All @@ -25,6 +26,7 @@ import { NzSpinComponent } from 'ng-zorro-antd/spin';
imports: [NzCardComponent, NzSpinComponent, NzStringTemplateOutletDirective]
})
export class G2CardComponent implements OnChanges {
private readonly cdr = inject(ChangeDetectorRef);
/** 是否显示边框 */
@Input({ transform: booleanAttribute }) bordered = false;
@Input() avatar?: string | TemplateRef<void> | null;
Expand All @@ -42,8 +44,6 @@ export class G2CardComponent implements OnChanges {
/** 是否显示Loading */
@Input({ transform: booleanAttribute }) loading = false;

constructor(private cdr: ChangeDetectorRef) {}

ngOnChanges(): void {
this.cdr.detectChanges();
}
Expand Down
Loading

0 comments on commit f57dc57

Please sign in to comment.