Skip to content

Commit

Permalink
feat(api): expose WindowRef and ReflectRef services as public api
Browse files Browse the repository at this point in the history
This will allow users to configure/customise the access to global Window object and from where
Reflect API is loaded
  • Loading branch information
gund committed Aug 27, 2022
1 parent 88f187f commit c634e20
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import {
DynamicComponentInjectorToken,
} from '../component-injector';
import { InputsType, IoFactoryService, IoService, OutputsType } from '../io';
import { extractNgParamTypes, getCtorParamTypes, isOnDestroy } from '../util';
import { WindowRefService } from '../window-ref';
import { extractNgParamTypes, isOnDestroy } from '../util';
import { ReflectRefService } from '../window-ref/reflect-ref.service';

export interface DynamicDirectiveDef<T> {
type: Type<T>;
Expand Down Expand Up @@ -99,10 +99,6 @@ export class DynamicDirectivesDirective implements OnDestroy, DoCheck {
return this.componentRef.injector;
}

private get reflect() {
return (this.windowRef.nativeWindow as any).Reflect;
}

private dirRef = new Map<Type<unknown>, DirectiveRef<unknown>>();
private dirIo = new Map<Type<unknown>, IoService>();
private dirsDiffer = this.iterableDiffers
Expand All @@ -113,7 +109,7 @@ export class DynamicDirectivesDirective implements OnDestroy, DoCheck {
private injector: Injector,
private iterableDiffers: IterableDiffers,
private ioFactoryService: IoFactoryService,
private windowRef: WindowRefService,
private reflectRefService: ReflectRefService,
@Inject(DynamicComponentInjectorToken)
@Optional()
private componentInjector?: DynamicComponentInjector,
Expand Down Expand Up @@ -270,8 +266,8 @@ export class DynamicDirectivesDirective implements OnDestroy, DoCheck {
return (
// First try Angular Compiler's metadata
extractNgParamTypes(dirType) ??
// Then fallback to Typescript Reflect API
getCtorParamTypes(dirType, this.reflect) ??
// Then fallback to Reflect API
this.reflectRefService.getCtorParamTypes(dirType) ??
// Bailout
[]
);
Expand Down
7 changes: 0 additions & 7 deletions projects/ng-dynamic-component/src/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@ import { OnChanges, OnDestroy, Type } from '@angular/core';

export function noop(): void {}

export function getCtorParamTypes(
ctor: unknown,
reflect: { getMetadata: (type: string, obj: unknown) => unknown[] },
): unknown[] {
return reflect.getMetadata('design:paramtypes', ctor);
}

/**
* Extract type arguments from Angular Directive/Component
*/
Expand Down
2 changes: 2 additions & 0 deletions projects/ng-dynamic-component/src/lib/window-ref/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from './window-ref.service';
export * from './window-ref-browser';
export * from './reflect-ref.service';
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Injectable, Type } from '@angular/core';

import { WindowRefService } from './window-ref.service';

/**
* Reflect API subsystem required for lib to work
*/
export interface ReflectRef {
getMetadata: (type: string, obj: unknown) => unknown[];
}

/**
* Retrieves Reflect API from global Window object
* of {@link WindowRefService} as a `Reflect` property
*/
export function windowReflectFactory(windowRef: WindowRefService) {
return (windowRef.nativeWindow as any).Reflect;
}

/**
* Holds {@link ReflectRef} that is used by {@link ReflectRefService}
*/
@Injectable({
providedIn: 'root',
useFactory: windowReflectFactory,
deps: [WindowRefService],
})
export class ReflectRefToken extends ((class {} as any) as Type<ReflectRef>) {}

@Injectable({ providedIn: 'root' })
export class ReflectRefService {
readonly reflect: ReflectRef = this.reflectRefToken;

constructor(private reflectRefToken: ReflectRefToken) {}

getCtorParamTypes(ctor: Type<unknown>): unknown[] {
return this.reflect.getMetadata('design:paramtypes', ctor);
}
}
1 change: 1 addition & 0 deletions projects/ng-dynamic-component/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export * from './lib/dynamic.component';
export * from './lib/dynamic-io';
export * from './lib/dynamic-attributes';
export * from './lib/dynamic-directives';
export * from './lib/window-ref';

0 comments on commit c634e20

Please sign in to comment.