Skip to content
This repository has been archived by the owner on Jul 30, 2018. It is now read-only.

Convert to use TS2.2 Class Mixins over dojo/compose #326

Merged
merged 15 commits into from
Feb 8, 2017
4 changes: 1 addition & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
module.exports = function (grunt) {
require('grunt-dojo2').initConfig(grunt, {
/* any custom configuration goes here */
});
require('grunt-dojo2').initConfig(grunt, {});
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"remap-istanbul": "^0.6.4",
"sinon": "^1.17.6",
"tslint": "^3.11.0",
"typescript": "~2.1.4"
"typescript": "rc"
},
"dependencies": {
"@dojo/compose": "2.0.0-beta.21",
Expand Down
72 changes: 62 additions & 10 deletions src/FactoryRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,66 @@
import { isComposeFactory } from '@dojo/compose/compose';
import Promise from '@dojo/shim/Promise';
import Map from '@dojo/shim/Map';
import {
WidgetBaseFactory,
FactoryRegistryInterface,
FactoryRegistryItem,
WidgetFactoryFunction
} from './interfaces';
import { WidgetBase, WidgetConstructor } from './WidgetBase';

/**
* A function the returns a Promise<WidgetConstructor>
*/
export type WidgetFactoryFunction = () => Promise<WidgetConstructor>

/**
* Factory Registry Item - Either WidgetConsructor, Promise for a WidgetConstructor or WidgetFactoryFunction
*/
export type FactoryRegistryItem = WidgetConstructor | Promise<WidgetConstructor> | WidgetFactoryFunction

/**
* Factory Registry Interface
*/
export interface FactoryRegistryInterface {

/**
* define a FactoryRegistryItem for a specified label
*
* @param factoryLabel The label of the factory to register
* @param registryItem The registry item to define
*/
define(factoryLabel: string, registryItem: FactoryRegistryItem): void;

/**
* Return a Factory or promise for a factory for the given label, null if an entry doesn't exist
*
* @param factoryLabel The label of the factory to return
* @returns The Factory or Promise for the label, `null` if no entry exists
*/
get(factoryLabel: string): WidgetConstructor | Promise<WidgetConstructor> | null;

/**
* Returns a boolean if an entry for the label exists
*
* @param factoryLabel The label to search for
* @returns boolean indicating if a factory exists
*/
has(factoryLabel: string): boolean;
}

/**
* Checks is the item is a subclass of WidgetBase (or a WidgetBase)
*
* @param item the item to check
* @returns true/false indicating is the item is a WidgetConstructor
*/
export function isWidgetBaseConstructor(item: any): item is WidgetConstructor {
return WidgetBase.isPrototypeOf(item) || item === WidgetBase;
}

/**
* The FactoryRegistry implementation
*/
export default class FactoryRegistry implements FactoryRegistryInterface {
protected registry: Map<string, FactoryRegistryItem>;

/**
* internal map of labels and FactoryRegistryItem
*/
private registry: Map<string, FactoryRegistryItem>;

constructor() {
this.registry = new Map<string, FactoryRegistryItem>();
Expand All @@ -26,18 +77,19 @@ export default class FactoryRegistry implements FactoryRegistryInterface {
this.registry.set(factoryLabel, registryItem);
}

get(factoryLabel: string): WidgetBaseFactory | Promise<WidgetBaseFactory> | null {
get(factoryLabel: string): WidgetConstructor | Promise<WidgetConstructor> | null {
if (!this.has(factoryLabel)) {
return null;
}

const item = this.registry.get(factoryLabel);

if (isComposeFactory(item) || item instanceof Promise) {
if (item instanceof Promise || isWidgetBaseConstructor(item)) {
return item;
}

const promise = (<WidgetFactoryFunction> item)();

this.registry.set(factoryLabel, promise);

return promise.then((factory) => {
Expand Down
Loading