Defines the configuration for a function that generates a FactoryProvider
for wrapper objects.
core/abstraction/factories/wrappers.ts
Properties |
|
nativeToken |
nativeToken:
|
Type: InjectionToken<Native>
|
Optional |
(Optional) The token to use when injecting the native object into the factory. Default is |
import { FactoryProvider, InjectionToken } from '@angular/core';
import { UniversalService } from '@bespunky/angular-zen/universal';
import { GoogleMapsApiService } from '../../api/google-maps-api.service';
import { Native, NativeOf, Wrapper } from '../types/abstraction';
import { FactoryGeneratorConfig } from './common';
import { NativeInstance, WrapperInstance } from './tokens';
type WrapperFactory<TWrapper extends Wrapper> = (api: GoogleMapsApiService, native: NativeOf<TWrapper>, ...deps: any[]) => TWrapper;
/**
* Creates the factory that will be used in the provider created by `createWrapperFactoryProvider`.
* See `createWrapperFactoryProvider` for more details.
*
* @template TWrapper The type of wrapper generated by the factory.
* @param {WrapperFactory<TWrapper>} produceWrapper The function that will create the wrapper object. Receives the `GoogleMapsApiService`, the native object and the dependencies specified in `deps` when configuring the generator.
* @returns A factory that receives the `GoogleMapsApiService`, `UniversalService`, and the native object to wrap along with any additional dependencies, then returns a new wrapper object on browsers and `null` on non-browsers.
*/
function createWrapperFactory<TWrapper extends Wrapper>(produceWrapper: WrapperFactory<TWrapper>)
{
// Called by Angular's Dependency Injector
return (api: GoogleMapsApiService, universal: UniversalService, native: NativeOf<TWrapper>, ...deps: any[]) =>
{
// TODO: Test with Angular Universal app and see if this doesn't break the chain of contained directives
if (!universal.isPlatformBrowser) return null;
return produceWrapper(api, native, ...deps);
};
}
/**
* Defines the configuration for a function that generates a `FactoryProvider` for wrapper objects.
*
* @export
* @interface WrapperFactoryGeneratorConfig
* @extends {FactoryGeneratorConfig<Wrapper>}
*/
export interface WrapperFactoryGeneratorConfig extends FactoryGeneratorConfig<Wrapper>
{
/**
* (Optional) The token to use when injecting the native object into the factory. Default is `NativeInstance`.
*
* @type {InjectionToken<Native>}
*/
nativeToken?: InjectionToken<Native>;
}
/**
* Applies default values for undefined values in the config object.
*
* @export
* @param {WrapperFactoryGeneratorConfig} config The optional config.
* @returns {Required<WrapperFactoryGeneratorConfig>} The filled config.
*/
export function configWrapperFactoryProviderGeneratorDefaults(config: WrapperFactoryGeneratorConfig): Required<WrapperFactoryGeneratorConfig>
{
return {
token : config.token || WrapperInstance,
nativeToken: config.nativeToken || NativeInstance,
deps : config.deps || []
};
}
/**
* Creates a factory provider for the a natve object injection token.
* The factory runs the specified function and passes the `GoogleMapsApiService`, the native object to be wrapped and the specified dependencies to it.
*
* By default, the generator will create the factory provider for the `WrapperInstance` token, and inject `NativeInstance` as a dependency.
* To change this behavior, specify the appropriate tokens in the config argument.
*
* Note: The factory will detect non-browser platforms and return `null` instead of calling the function.
*
* @export
* @template TWrapper The type of wrapper generated by the factory.
* @param {WrapperFactory<TWrapper>} produceWrapper The function that will create the wrapper object. Receives the `GoogleMapsApiService`, the native object and the dependencies specified in `deps`.
* @param {WrapperFactoryGeneratorConfig} [config={}] (Optional) A configuration object for the generator.
* @returns {FactoryProvider} A `FactoryProvider` object for the specified token.
*/
export function createWrapperFactoryProvider<TWrapper extends Wrapper>(produceWrapper: WrapperFactory<TWrapper>, config: WrapperFactoryGeneratorConfig = {}): FactoryProvider
{
const { token, nativeToken, deps } = configWrapperFactoryProviderGeneratorDefaults(config);
return {
provide : token,
useFactory: createWrapperFactory(produceWrapper),
deps : [GoogleMapsApiService, UniversalService, nativeToken, ...deps]
};
}