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

Commit

Permalink
move decorators out of WidgetBase
Browse files Browse the repository at this point in the history
  • Loading branch information
agubler committed Sep 18, 2017
1 parent de035a8 commit 4aa9353
Show file tree
Hide file tree
Showing 16 changed files with 614 additions and 596 deletions.
64 changes: 2 additions & 62 deletions src/WidgetBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
BeforeProperties,
BeforeRender,
CoreProperties,
DiffPropertyFunction,
DiffPropertyReaction,
DNode,
Render,
Expand All @@ -23,6 +22,7 @@ import {
WidgetBaseInterface,
WidgetProperties
} from './interfaces';
import { diffProperty } from './decorators/diffProperty';
import RegistryHandler from './RegistryHandler';
import NodeHandler from './NodeHandler';
import { isWidgetBaseConstructor, WIDGET_BASE_TYPE, Registry } from './Registry';
Expand Down Expand Up @@ -57,66 +57,6 @@ interface ReactionFunctionConfig {
export type BoundFunctionData = { boundFunc: (...args: any[]) => any, scope: any };

const decoratorMap = new Map<Function, Map<string, any[]>>();

/**
* Decorator that can be used to register a function to run as an aspect to `render`
*/
export function afterRender(method: Function): (target: any) => void;
export function afterRender(): (target: any, propertyKey: string) => void;
export function afterRender(method?: Function) {
return handleDecorator((target, propertyKey) => {
target.addDecorator('afterRender', propertyKey ? target[propertyKey] : method);
});
}

/**
* Decorator that can be used to register a reducer function to run as an aspect before to `render`
*/
export function beforeRender(method: Function): (target: any) => void;
export function beforeRender(): (target: any, propertyKey: string) => void;
export function beforeRender(method?: Function) {
return handleDecorator((target, propertyKey) => {
target.addDecorator('beforeRender', propertyKey ? target[propertyKey] : method);
});
}

/**
* Decorator that can be used to register a function as a specific property diff
*
* @param propertyName The name of the property of which the diff function is applied
* @param diffType The diff type, default is DiffType.AUTO.
* @param diffFunction A diff function to run if diffType if DiffType.CUSTOM
*/
export function diffProperty(propertyName: string, diffFunction: DiffPropertyFunction, reactionFunction?: Function) {
return handleDecorator((target, propertyKey) => {
target.addDecorator(`diffProperty:${propertyName}`, diffFunction.bind(null));
target.addDecorator('registeredDiffProperty', propertyName);
if (reactionFunction || propertyKey) {
target.addDecorator('diffReaction', {
propertyName,
reaction: propertyKey ? target[propertyKey] : reactionFunction
});
}
});
}

/**
* Generic decorator handler to take care of whether or not the decorator was called at the class level
* or the method level.
*
* @param handler
*/
export function handleDecorator(handler: (target: any, propertyKey?: string) => void) {
return function (target: any, propertyKey?: string, descriptor?: PropertyDescriptor) {
if (typeof target === 'function') {
handler(target.prototype, undefined);
}
else {
handler(target, propertyKey);
}
};
}

const boundAuto = auto.bind(null);

/**
Expand Down Expand Up @@ -153,7 +93,7 @@ export class WidgetBase<P = WidgetProperties, C extends DNode = DNode> extends E
private _coreProperties: CoreProperties = {} as CoreProperties;

/**
* cached chldren map for instance management
* cached children map for instance management
*/
private _cachedChildrenMap: Map<string | number | Promise<WidgetBaseConstructor> | WidgetBaseConstructor, WidgetCacheWrapper[]>;

Expand Down
14 changes: 14 additions & 0 deletions src/decorators/afterRender.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { handleDecorator } from './handleDecorator';

/**
* Decorator that can be used to register a function to run as an aspect to `render`
*/
export function afterRender(method: Function): (target: any) => void;
export function afterRender(): (target: any, propertyKey: string) => void;
export function afterRender(method?: Function) {
return handleDecorator((target, propertyKey) => {
target.addDecorator('afterRender', propertyKey ? target[propertyKey] : method);
});
}

export default afterRender;
2 changes: 1 addition & 1 deletion src/decorators/beforeProperties.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { handleDecorator } from './../WidgetBase';
import { handleDecorator } from './handleDecorator';
import { BeforeProperties } from './../interfaces';

/**
Expand Down
14 changes: 14 additions & 0 deletions src/decorators/beforeRender.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { handleDecorator } from './handleDecorator';

/**
* Decorator that can be used to register a reducer function to run as an aspect before to `render`
*/
export function beforeRender(method: Function): (target: any) => void;
export function beforeRender(): (target: any, propertyKey: string) => void;
export function beforeRender(method?: Function) {
return handleDecorator((target, propertyKey) => {
target.addDecorator('beforeRender', propertyKey ? target[propertyKey] : method);
});
}

export default beforeRender;
24 changes: 24 additions & 0 deletions src/decorators/diffProperty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { handleDecorator } from './handleDecorator';
import { DiffPropertyFunction } from './../interfaces';

/**
* Decorator that can be used to register a function as a specific property diff
*
* @param propertyName The name of the property of which the diff function is applied
* @param diffType The diff type, default is DiffType.AUTO.
* @param diffFunction A diff function to run if diffType if DiffType.CUSTOM
*/
export function diffProperty(propertyName: string, diffFunction: DiffPropertyFunction, reactionFunction?: Function) {
return handleDecorator((target, propertyKey) => {
target.addDecorator(`diffProperty:${propertyName}`, diffFunction.bind(null));
target.addDecorator('registeredDiffProperty', propertyName);
if (reactionFunction || propertyKey) {
target.addDecorator('diffReaction', {
propertyName,
reaction: propertyKey ? target[propertyKey] : reactionFunction
});
}
});
}

export default diffProperty;
18 changes: 18 additions & 0 deletions src/decorators/handleDecorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Generic decorator handler to take care of whether or not the decorator was called at the class level
* or the method level.
*
* @param handler
*/
export function handleDecorator(handler: (target: any, propertyKey?: string) => void) {
return function (target: any, propertyKey?: string, descriptor?: PropertyDescriptor) {
if (typeof target === 'function') {
handler(target.prototype, undefined);
}
else {
handler(target, propertyKey);
}
};
}

export default handleDecorator;
5 changes: 4 additions & 1 deletion src/decorators/inject.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import WeakMap from '@dojo/shim/WeakMap';
import { handleDecorator, WidgetBase } from './../WidgetBase';
import { WidgetBase } from './../WidgetBase';
import { handleDecorator } from './handleDecorator';
import { Injector } from './../Injector';
import { beforeProperties } from './beforeProperties';
import { RegistryLabel } from './../interfaces';
Expand Down Expand Up @@ -61,3 +62,5 @@ export function inject({ name, getProperties }: InjectConfig) {
})(target);
});
}

export default inject;
3 changes: 2 additions & 1 deletion src/mixins/I18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import i18n, {
} from '@dojo/i18n/i18n';
import { VNodeProperties } from '@dojo/interfaces/vdom';
import { Constructor, DNode, WidgetProperties } from './../interfaces';
import { afterRender, WidgetBase } from './../WidgetBase';
import { WidgetBase } from './../WidgetBase';
import { afterRender } from './../decorators/afterRender';
import { isHNode } from './../d';

export interface I18nProperties extends WidgetProperties {
Expand Down
4 changes: 3 additions & 1 deletion src/mixins/Themeable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { ClassesFunction, Constructor, WidgetProperties } from './../interfaces'
import { Registry } from './../Registry';
import { Injector } from './../Injector';
import { inject } from './../decorators/inject';
import { diffProperty, WidgetBase, handleDecorator } from './../WidgetBase';
import { WidgetBase } from './../WidgetBase';
import { handleDecorator } from './../decorators/handleDecorator';
import { diffProperty } from './../decorators/diffProperty';
import { shallow } from './../diff';

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/Container.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as registerSuite from 'intern!object';
import * as assert from 'intern/chai!assert';
import { v , w } from '../../src/d';
import { diffProperty, WidgetBase } from '../../src/WidgetBase';
import { WidgetBase } from '../../src/WidgetBase';
import { diffProperty } from './../../src/decorators/diffProperty';
import { always } from '../../src/diff';
import { Container } from './../../src/Container';
import { Registry } from './../../src/Registry';
Expand Down
Loading

0 comments on commit 4aa9353

Please sign in to comment.