ing
This commit is contained in:
parent
88a2218e17
commit
100a0060bc
|
@ -1,8 +1,9 @@
|
|||
import {
|
||||
MetadataKeyType,
|
||||
PropertyKeyType,
|
||||
} from './type';
|
||||
|
||||
import * as Util from './util';
|
||||
import {TypeUtil} from './util';
|
||||
|
||||
|
||||
export class Metadata {
|
||||
|
@ -41,8 +42,8 @@ export class Metadata {
|
|||
* ```
|
||||
*
|
||||
*/
|
||||
public static get(key: string, target: any, propertyKey?: PropertyKeyType): any {
|
||||
return Reflect.getMetadata(key, Util.getClass(target), propertyKey!);
|
||||
public static get(key: MetadataKeyType, target: any, propertyKey?: PropertyKeyType): any {
|
||||
return Reflect.getMetadata(key, TypeUtil.getClass(target), propertyKey!);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -80,8 +81,8 @@ export class Metadata {
|
|||
* ```
|
||||
*
|
||||
*/
|
||||
public static getOwn(key: string, target: any, propertyKey?: string | symbol): any {
|
||||
return Reflect.getOwnMetadata(key, Util.getClass(target), propertyKey!);
|
||||
public static getOwn(key: MetadataKeyType, target: any, propertyKey?: PropertyKeyType): any {
|
||||
return Reflect.getOwnMetadata(key, TypeUtil.getClass(target), propertyKey!);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -112,7 +113,7 @@ export class Metadata {
|
|||
* ```
|
||||
*
|
||||
*/
|
||||
public static getType(target: any, propertyKey?: string | symbol): any {
|
||||
public static getType(target: any, propertyKey?: PropertyKeyType): any {
|
||||
return Reflect.getMetadata(DESIGN_TYPE, target, propertyKey!);
|
||||
}
|
||||
|
||||
|
@ -144,7 +145,7 @@ export class Metadata {
|
|||
* ```
|
||||
*
|
||||
*/
|
||||
public static getOwnType(target: any, propertyKey?: string | symbol): any {
|
||||
public static getOwnType(target: any, propertyKey?: PropertyKeyType): any {
|
||||
return Reflect.getMetadata(DESIGN_TYPE, target, propertyKey!);
|
||||
}
|
||||
|
||||
|
@ -176,7 +177,7 @@ export class Metadata {
|
|||
* ```
|
||||
*
|
||||
*/
|
||||
public static getReturnType(target: any, propertyKey?: string | symbol): any {
|
||||
public static getReturnType(target: any, propertyKey?: PropertyKeyType): any {
|
||||
return Reflect.getMetadata(DESIGN_RETURN_TYPE, target, propertyKey!);
|
||||
}
|
||||
|
||||
|
@ -208,7 +209,7 @@ export class Metadata {
|
|||
* ```
|
||||
*
|
||||
*/
|
||||
public static getOwnReturnType(target: any, propertyKey?: string | symbol): any {
|
||||
public static getOwnReturnType(target: any, propertyKey?: PropertyKeyType): any {
|
||||
return Reflect.getOwnMetadata(DESIGN_RETURN_TYPE, target, propertyKey!);
|
||||
}
|
||||
|
||||
|
@ -247,8 +248,8 @@ export class Metadata {
|
|||
* ```
|
||||
*
|
||||
*/
|
||||
public static has(key: string, target: any, propertyKey?: string | symbol): boolean {
|
||||
return Reflect.hasMetadata(key, Util.getClass(target), propertyKey!);
|
||||
public static has(key: MetadataKeyType, target: any, propertyKey?: PropertyKeyType): boolean {
|
||||
return Reflect.hasMetadata(key, TypeUtil.getClass(target), propertyKey!);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -286,8 +287,8 @@ export class Metadata {
|
|||
* ```
|
||||
*
|
||||
*/
|
||||
public static hasOwn(key: string, target: any, propertyKey?: string | symbol): boolean {
|
||||
return Reflect.hasOwnMetadata(key, Util.getClass(target), propertyKey!);
|
||||
public static hasOwn(key: MetadataKeyType, target: any, propertyKey?: PropertyKeyType): boolean {
|
||||
return Reflect.hasOwnMetadata(key, TypeUtil.getClass(target), propertyKey!);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -325,8 +326,8 @@ export class Metadata {
|
|||
* ```
|
||||
*
|
||||
*/
|
||||
public static delete(key: string, target: any, propertyKey?: string | symbol): boolean {
|
||||
return Reflect.deleteMetadata(key, Util.getClass(target), propertyKey!);
|
||||
public static delete(key: MetadataKeyType, target: any, propertyKey?: PropertyKeyType): boolean {
|
||||
return Reflect.deleteMetadata(key, TypeUtil.getClass(target), propertyKey!);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -364,7 +365,7 @@ export class Metadata {
|
|||
* ```
|
||||
*
|
||||
*/
|
||||
public static setParamTypes(target: any, propertyKey: string | symbol, value: any): void {
|
||||
public static setParamTypes(target: any, propertyKey: PropertyKeyType, value: any): void {
|
||||
return this.set(DESIGN_PARAM_TYPES, value, target.prototype, propertyKey);
|
||||
}
|
||||
|
||||
|
@ -372,7 +373,7 @@ export class Metadata {
|
|||
* Get all metadata for a metadataKey.
|
||||
* @param metadataKey
|
||||
*/
|
||||
public static getTargetsFromPropertyKey = (metadataKey: string | symbol): any[] =>
|
||||
public static getTargetsFromPropertyKey = (metadataKey: MetadataKeyType): any[] =>
|
||||
PROPERTIES.has(metadataKey) ? PROPERTIES.get(metadataKey) || [] : []
|
||||
|
||||
/**
|
||||
|
@ -415,17 +416,17 @@ export class Metadata {
|
|||
* ```
|
||||
*
|
||||
*/
|
||||
public static set(key: string, value: any, target: any, propertyKey?: string | symbol): void {
|
||||
public static set(key: MetadataKeyType, value: any, target: any, propertyKey?: PropertyKeyType): void {
|
||||
|
||||
const targets: any[] = PROPERTIES.has(key) ? PROPERTIES.get(key) || [] : [];
|
||||
const classConstructor = Util.getClass(target);
|
||||
const classConstructor = TypeUtil.getClass(target);
|
||||
|
||||
if (targets.indexOf(classConstructor) === -1) {
|
||||
targets.push(classConstructor);
|
||||
PROPERTIES.set(key, targets);
|
||||
}
|
||||
|
||||
Reflect.defineMetadata(key, value, Util.getClass(target), propertyKey!);
|
||||
Reflect.defineMetadata(key, value, TypeUtil.getClass(target), propertyKey!);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -456,7 +457,7 @@ export class Metadata {
|
|||
* ```
|
||||
*
|
||||
*/
|
||||
public static getParamTypes(target: any, propertyKey?: string | symbol): any[] {
|
||||
public static getParamTypes(target: any, propertyKey?: PropertyKeyType): any[] {
|
||||
return Reflect.getMetadata(DESIGN_PARAM_TYPES, target, propertyKey!) || [];
|
||||
}
|
||||
|
||||
|
@ -488,7 +489,7 @@ export class Metadata {
|
|||
* ```
|
||||
*
|
||||
*/
|
||||
public static getOwnParamTypes(target: any, propertyKey?: string | symbol): any[] {
|
||||
public static getOwnParamTypes(target: any, propertyKey?: PropertyKeyType): any[] {
|
||||
return Reflect.getOwnMetadata(DESIGN_PARAM_TYPES, target, propertyKey!) || [];
|
||||
}
|
||||
}
|
||||
|
@ -517,4 +518,4 @@ const DESIGN_RETURN_TYPE = 'design:returntype';
|
|||
* @private
|
||||
* @type {string}
|
||||
*/
|
||||
const PROPERTIES: Map<string | symbol, any[]> = new Map<string | symbol, any[]>();
|
||||
const PROPERTIES: Map<MetadataKeyType, any[]> = new Map<MetadataKeyType, any[]>();
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
export type IdentityType<T> = T | symbol;
|
||||
export type PropertyKeyType = IdentityType<string>;
|
||||
export type MetadataKeyType = IdentityType<string>;
|
||||
|
||||
export const Type = Function;
|
||||
export interface Type<T> extends Function {
|
||||
export const ConstructorType = Function;
|
||||
export interface ConstructorType<T = {}> extends Function {
|
||||
new (...args: any[]): T;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,25 +1,29 @@
|
|||
import {
|
||||
ConstructorType,
|
||||
DecoratorType,
|
||||
DecoratorParametersType,
|
||||
PrimitiveType,
|
||||
PropertyKeyType,
|
||||
} from './type';
|
||||
|
||||
|
||||
export class TypeUtil {
|
||||
/**
|
||||
* Get the provide constructor.
|
||||
* @param targetClass
|
||||
*/
|
||||
export const getContructor = (targetClass: any): Function =>
|
||||
typeof targetClass === 'function'
|
||||
public static getContructor(targetClass: any): ConstructorType {
|
||||
return typeof targetClass === 'function'
|
||||
? targetClass
|
||||
: targetClass.constructor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the provide constructor if target is an instance.
|
||||
* @param target
|
||||
* @returns {*}
|
||||
*/
|
||||
export function getClass(target: any): any {
|
||||
public static getClass(target: any): any {
|
||||
return target.prototype ? target : target.constructor;
|
||||
}
|
||||
|
||||
|
@ -28,8 +32,8 @@ export function getClass(target: any): any {
|
|||
* @param target
|
||||
* @returns {symbol}
|
||||
*/
|
||||
export function getClassOrSymbol(target: any): any {
|
||||
return typeof target === 'symbol' ? target : getClass(target);
|
||||
public static getClassOrSymbol(target: any): any {
|
||||
return typeof target === 'symbol' ? target : TypeUtil.getClass(target);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -37,10 +41,10 @@ export function getClassOrSymbol(target: any): any {
|
|||
* @param target
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function isPrimitiveOrPrimitiveClass(target: any): boolean {
|
||||
return isString(target)
|
||||
|| isNumber(target)
|
||||
|| isBoolean(target);
|
||||
public static isPrimitiveOrPrimitiveClass(target: any): boolean {
|
||||
return TypeUtil.isString(target)
|
||||
|| TypeUtil.isNumber(target)
|
||||
|| TypeUtil.isBoolean(target);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -48,14 +52,14 @@ export function isPrimitiveOrPrimitiveClass(target: any): boolean {
|
|||
* @param target
|
||||
* @returns {PrimitiveType}
|
||||
*/
|
||||
export function primitiveOf(target: any): PrimitiveType {
|
||||
if (isString(target)) {
|
||||
public static primitiveOf(target: any): PrimitiveType {
|
||||
if (TypeUtil.isString(target)) {
|
||||
return PrimitiveType.STRING;
|
||||
}
|
||||
if (isNumber(target)) {
|
||||
if (TypeUtil.isNumber(target)) {
|
||||
return PrimitiveType.NUMBER;
|
||||
}
|
||||
if (isBoolean(target)) {
|
||||
if (TypeUtil.isBoolean(target)) {
|
||||
return PrimitiveType.BOOLEAN;
|
||||
}
|
||||
return PrimitiveType.ANY;
|
||||
|
@ -66,7 +70,7 @@ export function primitiveOf(target: any): PrimitiveType {
|
|||
* @param target
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function isString(target: any): boolean {
|
||||
public static isString(target: any): boolean {
|
||||
return typeof target === 'string' || target instanceof String || target === String;
|
||||
}
|
||||
|
||||
|
@ -75,7 +79,7 @@ export function isString(target: any): boolean {
|
|||
* @param target
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function isNumber(target: any): boolean {
|
||||
public static isNumber(target: any): boolean {
|
||||
return typeof target === 'number' || target instanceof Number || target === Number;
|
||||
}
|
||||
|
||||
|
@ -84,7 +88,7 @@ export function isNumber(target: any): boolean {
|
|||
* @param target
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function isBoolean(target: any): boolean {
|
||||
public static isBoolean(target: any): boolean {
|
||||
return typeof target === 'boolean' || target instanceof Boolean || target === Boolean;
|
||||
}
|
||||
|
||||
|
@ -93,7 +97,7 @@ export function isBoolean(target: any): boolean {
|
|||
* @param target
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function isArray(target: any): boolean {
|
||||
public static isArray(target: any): boolean {
|
||||
return Array.isArray(target);
|
||||
}
|
||||
|
||||
|
@ -102,11 +106,11 @@ export function isArray(target: any): boolean {
|
|||
* @param target
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function isArrayOrArrayClass(target: any): boolean {
|
||||
public static isArrayOrArrayClass(target: any): boolean {
|
||||
if (target === Array) {
|
||||
return true;
|
||||
}
|
||||
return isArray(target);
|
||||
return TypeUtil.isArray(target);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -114,8 +118,8 @@ export function isArrayOrArrayClass(target: any): boolean {
|
|||
* @param target
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function isCollection(target: any): boolean {
|
||||
return isArrayOrArrayClass(target)
|
||||
public static isCollection(target: any): boolean {
|
||||
return TypeUtil.isArrayOrArrayClass(target)
|
||||
|| target === Map
|
||||
|| target instanceof Map
|
||||
|| target === Set
|
||||
|
@ -131,7 +135,7 @@ export function isCollection(target: any): boolean {
|
|||
* @param target
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function isDate(target: any): boolean {
|
||||
public static isDate(target: any): boolean {
|
||||
return target === Date || target instanceof Date;
|
||||
}
|
||||
|
||||
|
@ -140,7 +144,19 @@ export function isDate(target: any): boolean {
|
|||
* @param target
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function isObject(target: any): boolean {
|
||||
public static isMethod(target: any, propertyKey: PropertyKeyType): boolean {
|
||||
if (typeof(target[propertyKey]) === undefined) {
|
||||
return false;
|
||||
}
|
||||
return typeof target[propertyKey] === 'function';
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param target
|
||||
* @returns {boolean}
|
||||
*/
|
||||
public static isObject(target: any): boolean {
|
||||
return target === Object;
|
||||
}
|
||||
|
||||
|
@ -149,12 +165,12 @@ export function isObject(target: any): boolean {
|
|||
* @param target
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function isClass(target: any): boolean {
|
||||
return !isPrimitiveOrPrimitiveClass(target)
|
||||
&& !isObject(target)
|
||||
&& !isDate(target)
|
||||
public static isClass(target: any): boolean {
|
||||
return !TypeUtil.isPrimitiveOrPrimitiveClass(target)
|
||||
&& !TypeUtil.isObject(target)
|
||||
&& !TypeUtil.isDate(target)
|
||||
&& target !== undefined
|
||||
&& !isPromise(target);
|
||||
&& !TypeUtil.isPromise(target);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -162,40 +178,40 @@ export function isClass(target: any): boolean {
|
|||
* @param value
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function isEmpty(value: any): boolean {
|
||||
public static isEmpty(value: any): boolean {
|
||||
return value === '' || value === null || value === undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get object name
|
||||
*/
|
||||
export const nameOf = (obj: any): string => {
|
||||
public static nameOf(obj: any): string {
|
||||
switch (typeof obj) {
|
||||
default:
|
||||
return '' + obj;
|
||||
case 'symbol':
|
||||
return nameOfSymbol(obj);
|
||||
return TypeUtil.nameOfSymbol(obj);
|
||||
case 'function':
|
||||
return nameOfClass(obj);
|
||||
return TypeUtil.nameOfClass(obj);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the provide name.
|
||||
* @param targetClass
|
||||
*/
|
||||
export const nameOfClass = (targetClass: any) => {
|
||||
public static nameOfClass(targetClass: any): string {
|
||||
return typeof targetClass === 'function'
|
||||
? targetClass.name
|
||||
: targetClass.constructor.name;
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Get symbol name.
|
||||
* @param sym
|
||||
*/
|
||||
export const nameOfSymbol = (sym: symbol): string =>
|
||||
sym.toString().replace('Symbol(', '').replace(')', '');
|
||||
|
||||
public static nameOfSymbol(sym: symbol): string {
|
||||
return sym.toString().replace('Symbol(', '').replace(')', '');
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param out
|
||||
|
@ -203,17 +219,17 @@ export const nameOfSymbol = (sym: symbol): string =>
|
|||
* @param {{[p: string]: (collection: any[], value: any) => any}} reducers
|
||||
* @returns {any}
|
||||
*/
|
||||
export function deepExtends(out: any, obj: any, reducers: { [key: string]: (collection: any[], value: any) => any } = {}): any {
|
||||
public static deepExtends(out: any, obj: any, reducers: { [key: string]: (collection: any[], value: any) => any } = {}): any {
|
||||
|
||||
if (obj === undefined || obj === null) {
|
||||
return obj;
|
||||
}
|
||||
|
||||
if (isPrimitiveOrPrimitiveClass(obj) || typeof obj === 'symbol' || typeof obj === 'function') {
|
||||
if (TypeUtil.isPrimitiveOrPrimitiveClass(obj) || typeof obj === 'symbol' || typeof obj === 'function') {
|
||||
return obj;
|
||||
}
|
||||
|
||||
if (isArrayOrArrayClass(obj)) {
|
||||
if (TypeUtil.isArrayOrArrayClass(obj)) {
|
||||
out = out || [];
|
||||
} else {
|
||||
out = out || {};
|
||||
|
@ -224,7 +240,7 @@ export function deepExtends(out: any, obj: any, reducers: { [key: string]: (coll
|
|||
return collection;
|
||||
};
|
||||
const set = (key: string | number, value: any) => {
|
||||
if (isArrayOrArrayClass(obj)) {
|
||||
if (TypeUtil.isArrayOrArrayClass(obj)) {
|
||||
out.push(value);
|
||||
} else {
|
||||
out[key] = value;
|
||||
|
@ -242,14 +258,14 @@ export function deepExtends(out: any, obj: any, reducers: { [key: string]: (coll
|
|||
return;
|
||||
}
|
||||
|
||||
if (isPrimitiveOrPrimitiveClass(value) || typeof value === 'function') {
|
||||
if (TypeUtil.isPrimitiveOrPrimitiveClass(value) || typeof value === 'function') {
|
||||
set(key, value);
|
||||
return;
|
||||
}
|
||||
|
||||
if (isArrayOrArrayClass(value)) {
|
||||
if (TypeUtil.isArrayOrArrayClass(value)) {
|
||||
|
||||
value = value.map((v: any) => deepExtends(undefined, v));
|
||||
value = value.map((v: any) => TypeUtil.deepExtends(undefined, v));
|
||||
|
||||
set(key, []
|
||||
.concat(out[key] || [], value)
|
||||
|
@ -260,14 +276,14 @@ export function deepExtends(out: any, obj: any, reducers: { [key: string]: (coll
|
|||
}
|
||||
|
||||
// Object
|
||||
if (isArrayOrArrayClass(obj)) {
|
||||
set(key, deepExtends(undefined, value, reducers));
|
||||
if (TypeUtil.isArrayOrArrayClass(obj)) {
|
||||
set(key, TypeUtil.deepExtends(undefined, value, reducers));
|
||||
} else {
|
||||
set(key, deepExtends(out[key], value, reducers));
|
||||
set(key, TypeUtil.deepExtends(out[key], value, reducers));
|
||||
}
|
||||
});
|
||||
|
||||
if (isArrayOrArrayClass(out)) {
|
||||
if (TypeUtil.isArrayOrArrayClass(out)) {
|
||||
out.reduce((collection: any[], value: any) => defaultReducer(collection, value), []);
|
||||
}
|
||||
|
||||
|
@ -279,7 +295,7 @@ export function deepExtends(out: any, obj: any, reducers: { [key: string]: (coll
|
|||
* @param target
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function isPromise(target: any): boolean {
|
||||
public static isPromise(target: any): boolean {
|
||||
return target === Promise || target instanceof Promise;
|
||||
}
|
||||
|
||||
|
@ -288,7 +304,7 @@ export function isPromise(target: any): boolean {
|
|||
* @param target
|
||||
* @returns {any}
|
||||
*/
|
||||
export function getInheritedClass(target: any): any {
|
||||
public static getInheritedClass(target: any): any {
|
||||
return Object.getPrototypeOf(target);
|
||||
}
|
||||
|
||||
|
@ -297,7 +313,7 @@ export function getInheritedClass(target: any): any {
|
|||
* @param {any[]} args
|
||||
* @returns {DecoratorType}
|
||||
*/
|
||||
export function getDecoratorType(args: any[]): DecoratorType {
|
||||
public static getDecoratorType(args: any[]): DecoratorType {
|
||||
const [, propertyKey, descriptor] = args;
|
||||
|
||||
if (typeof descriptor === 'number') {
|
||||
|
@ -316,7 +332,7 @@ export function getDecoratorType(args: any[]): DecoratorType {
|
|||
* @param {PropertyKeyType} propertyKey
|
||||
* @returns {PropertyDescriptor}
|
||||
*/
|
||||
export function descriptorOf(target: any, propertyKey: PropertyKeyType): PropertyDescriptor {
|
||||
public static descriptorOf(target: any, propertyKey: PropertyKeyType): PropertyDescriptor {
|
||||
return Object.getOwnPropertyDescriptor(target && target.prototype || target, propertyKey)!;
|
||||
}
|
||||
|
||||
|
@ -326,11 +342,11 @@ export function descriptorOf(target: any, propertyKey: PropertyKeyType): Propert
|
|||
* @param {string} propertyKey
|
||||
* @returns {DecoratorParametersType}
|
||||
*/
|
||||
export function decoratorArgs(target: any, propertyKey: string): DecoratorParametersType {
|
||||
public static decoratorArgs(target: any, propertyKey: string): DecoratorParametersType {
|
||||
return [
|
||||
target,
|
||||
propertyKey,
|
||||
descriptorOf(target, propertyKey)!,
|
||||
TypeUtil.descriptorOf(target, propertyKey)!,
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -339,14 +355,14 @@ export function decoratorArgs(target: any, propertyKey: string): DecoratorParame
|
|||
* @param target
|
||||
* @returns {Array}
|
||||
*/
|
||||
export function ancestorsOf(target: any): any[] {
|
||||
public static ancestorsOf(target: any): any[] {
|
||||
const classes = [];
|
||||
|
||||
let currentTarget = getClass(target);
|
||||
let currentTarget = TypeUtil.getClass(target);
|
||||
|
||||
while (nameOf(currentTarget) !== '') {
|
||||
while (TypeUtil.nameOf(currentTarget) !== '') {
|
||||
classes.unshift(currentTarget);
|
||||
currentTarget = getInheritedClass(currentTarget);
|
||||
currentTarget = TypeUtil.getInheritedClass(currentTarget);
|
||||
}
|
||||
|
||||
return classes;
|
||||
|
@ -358,7 +374,7 @@ export function ancestorsOf(target: any): any[] {
|
|||
* @param {string} name
|
||||
* @param {Function} callback
|
||||
*/
|
||||
export function applyBefore(target: any, name: string, callback: Function): void {
|
||||
public static applyBefore(target: any, name: string, callback: Function): void {
|
||||
const original = target[name];
|
||||
target[name] = function (...args: any[]): any {
|
||||
callback(...args);
|
||||
|
@ -372,7 +388,7 @@ export function applyBefore(target: any, name: string, callback: Function): void
|
|||
* @param {number} time
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
export function promiseTimeout(promise: Promise<any>, time: number = 1000): Promise<{ ok: boolean, response: any }> {
|
||||
public static promiseTimeout(promise: Promise<any>, time: number = 1000): Promise<{ ok: boolean, response: any }> {
|
||||
const timeout = (p: Promise<any>, t: number) => new Promise((resolve) => {
|
||||
p.then((response) => {
|
||||
resolve();
|
||||
|
@ -388,3 +404,5 @@ export function promiseTimeout(promise: Promise<any>, time: number = 1000): Prom
|
|||
timeout(promise, time),
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
import {
|
||||
MetadataKeyType,
|
||||
PropertyKeyType,
|
||||
} from '@overflow/commons/core/type';
|
||||
|
||||
export interface Annotation {
|
||||
metadataKey(): MetadataKeyType;
|
||||
classDecorator?<TFunction extends Function>(target: TFunction): TFunction | void;
|
||||
propertyDecorator?(target: Object, propertyKey: PropertyKeyType): void;
|
||||
methodDecorator?<T>(target: Object, propertyKey: PropertyKeyType,
|
||||
|
|
|
@ -1,17 +1,18 @@
|
|||
import {
|
||||
ConstructorType,
|
||||
DecoratorType,
|
||||
Type,
|
||||
MetadataKeyType,
|
||||
Metadata,
|
||||
TypeUtil,
|
||||
} from '@overflow/commons/core/type';
|
||||
|
||||
import * as TypeUtil from '@overflow/commons/core/type/util';
|
||||
|
||||
import * as Constants from './constants';
|
||||
import {
|
||||
Annotation,
|
||||
} from './annotation';
|
||||
|
||||
export class Decorator {
|
||||
public static create = (AnnotationClass: Type<Annotation>) => {
|
||||
public static create = (AnnotationClass: ConstructorType<Annotation>) => {
|
||||
|
||||
return (...handlerArgs: any[]) => {
|
||||
let annotation: Annotation = new AnnotationClass(...handlerArgs);
|
||||
|
@ -20,8 +21,15 @@ export class Decorator {
|
|||
return (...decoratorArgs: any[]) => {
|
||||
let decoratorType: DecoratorType = TypeUtil.getDecoratorType(decoratorArgs);
|
||||
|
||||
const [target, propertyKey, descriptorOrParameterIndex] = decoratorArgs;
|
||||
|
||||
// let type = typeof decoratorArgs[0] === 'function' ? decoratorArgs[0].prototype : decoratorArgs[0];
|
||||
// let clazz = TypeUtil.getClass(decoratorArgs[0]);
|
||||
if (typeof(annotation.metadataKey) === 'undefined') {
|
||||
throw new Error(`Annotation[@${name}] must be implements metadataKey().`);
|
||||
}
|
||||
let metadataKey: MetadataKeyType = annotation.metadataKey.call(annotation);
|
||||
Metadata.set(metadataKey, annotation, target, propertyKey);
|
||||
|
||||
switch(decoratorType) {
|
||||
case DecoratorType.CLASS:
|
||||
|
@ -29,26 +37,27 @@ export class Decorator {
|
|||
throw new Error(`Cannot apply @${name} decorator on Class.`);
|
||||
}
|
||||
|
||||
return annotation.classDecorator.call(annotation, decoratorArgs[0]);
|
||||
return annotation.classDecorator.call(annotation, target);
|
||||
case DecoratorType.PROPERTY:
|
||||
if (typeof(annotation.propertyDecorator) === 'undefined') {
|
||||
throw new Error(`Cannot apply @${name} decorator on Property.`);
|
||||
}
|
||||
|
||||
return annotation.propertyDecorator.call(annotation, decoratorArgs[0], decoratorArgs[1]);
|
||||
return annotation.propertyDecorator.call(annotation, target, propertyKey);
|
||||
case DecoratorType.METHOD:
|
||||
if (typeof(annotation.methodDecorator) === 'undefined') {
|
||||
throw new Error(`Cannot apply @${name} decorator on Method.`);
|
||||
}
|
||||
|
||||
return annotation.methodDecorator.call(annotation, decoratorArgs[0], decoratorArgs[1], decoratorArgs[2]);
|
||||
return annotation.methodDecorator.call(annotation, target, propertyKey, descriptorOrParameterIndex);
|
||||
case DecoratorType.PARAMETER:
|
||||
if (typeof(annotation.parameterDecorator) === 'undefined') {
|
||||
throw new Error(`Cannot apply @${name} decorator on Parameter.`);
|
||||
}
|
||||
|
||||
return annotation.parameterDecorator.call(annotation, decoratorArgs[0], decoratorArgs[1], decoratorArgs[2]);
|
||||
return annotation.parameterDecorator.call(annotation, target, propertyKey, descriptorOrParameterIndex);
|
||||
default:
|
||||
throw new Error(`Cannot determine decorator[@${name}] type.`);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
|
52
src/ts/@overflow/commons/redux/decorators/action_mapping.ts
Normal file
52
src/ts/@overflow/commons/redux/decorators/action_mapping.ts
Normal file
|
@ -0,0 +1,52 @@
|
|||
import {
|
||||
MetadataKeyType,
|
||||
PropertyKeyType,
|
||||
TypeUtil,
|
||||
} from '@overflow/commons/core/type';
|
||||
|
||||
import {
|
||||
Annotation,
|
||||
Decorator,
|
||||
} from '@overflow/commons/decorator';
|
||||
|
||||
export const ActionMappingMetadataKey = Symbol('ActionMappingAnnotation');
|
||||
|
||||
export interface ActionMappingAnnotationAttributes {
|
||||
value: string;
|
||||
}
|
||||
|
||||
export class ActionMappingAnnotation extends Annotation {
|
||||
public readonly attributes: ActionMappingAnnotationAttributes;
|
||||
|
||||
public constructor(value: string | ActionMappingAnnotationAttributes) {
|
||||
super();
|
||||
|
||||
if (undefined === value) {
|
||||
throw new Error(`value attribute must be specified.`);
|
||||
}
|
||||
|
||||
if (TypeUtil.isString(value)) {
|
||||
this.attributes = {
|
||||
value: <string>value,
|
||||
};
|
||||
} else {
|
||||
this.attributes = <ActionMappingAnnotationAttributes>value;
|
||||
}
|
||||
}
|
||||
|
||||
public metadataKey(): MetadataKeyType {
|
||||
return ActionMappingMetadataKey;
|
||||
}
|
||||
|
||||
public classDecorator<TFunction extends Function>(target: TFunction): TFunction | void {
|
||||
console.log('ActionMapping');
|
||||
}
|
||||
|
||||
public methodDecorator<T>(target: Object, propertyKey: PropertyKeyType,
|
||||
descriptor: TypedPropertyDescriptor<T>): TypedPropertyDescriptor<T> | void {
|
||||
console.log('ActionMapping');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export const ActionMapping = Decorator.create(ActionMappingAnnotation);
|
|
@ -1,3 +1,4 @@
|
|||
export * from './action_mapping';
|
||||
export * from './reducer';
|
||||
export * from './rest_api';
|
||||
export * from './rpc_api';
|
||||
|
|
|
@ -1,13 +1,17 @@
|
|||
import * as TypeUtil from '@overflow/commons/core/type/util';
|
||||
import {
|
||||
MetadataKeyType,
|
||||
TypeUtil,
|
||||
} from '@overflow/commons/core/type';
|
||||
|
||||
import {
|
||||
Annotation,
|
||||
Decorator,
|
||||
} from '@overflow/commons/decorator';
|
||||
|
||||
|
||||
export const ReducerMetadataKey = Symbol('ReducerAnnotation');
|
||||
|
||||
export interface ReducerAnnotationAttributes {
|
||||
name: string;
|
||||
name?: string;
|
||||
}
|
||||
|
||||
export class ReducerAnnotation extends Annotation {
|
||||
|
@ -15,6 +19,14 @@ export class ReducerAnnotation extends Annotation {
|
|||
|
||||
public constructor(name: string | ReducerAnnotationAttributes) {
|
||||
super();
|
||||
|
||||
if (undefined === name) {
|
||||
this.attributes = {
|
||||
name: '',
|
||||
};
|
||||
return;
|
||||
}
|
||||
|
||||
if (TypeUtil.isString(name)) {
|
||||
this.attributes = {
|
||||
name: <string>name,
|
||||
|
@ -24,6 +36,10 @@ export class ReducerAnnotation extends Annotation {
|
|||
}
|
||||
}
|
||||
|
||||
public metadataKey(): MetadataKeyType {
|
||||
return ReducerMetadataKey;
|
||||
}
|
||||
|
||||
public classDecorator<TFunction extends Function>(target: TFunction): TFunction | void {
|
||||
console.log('Reducer');
|
||||
}
|
||||
|
|
|
@ -1,9 +1,15 @@
|
|||
import * as TypeUtil from '@overflow/commons/core/type/util';
|
||||
import {
|
||||
MetadataKeyType,
|
||||
PropertyKeyType,
|
||||
TypeUtil,
|
||||
} from '@overflow/commons/core/type';
|
||||
|
||||
import {
|
||||
Annotation,
|
||||
Decorator,
|
||||
} from '@overflow/commons/decorator';
|
||||
import { PropertyKeyType } from '@overflow/commons/core/type';
|
||||
|
||||
export const RestAPIMetadataKey = Symbol('RestAPIAnnotation');
|
||||
|
||||
export interface RestAPIAnnotationAttributes {
|
||||
entryPath: string;
|
||||
|
@ -14,6 +20,11 @@ export class RestAPIAnnotation extends Annotation {
|
|||
|
||||
public constructor(entryPath: string | RestAPIAnnotationAttributes) {
|
||||
super();
|
||||
|
||||
if (undefined === entryPath) {
|
||||
throw new Error(`entryPath attribute must be specified.`);
|
||||
}
|
||||
|
||||
if (TypeUtil.isString(entryPath)) {
|
||||
this.attributes = {
|
||||
entryPath: <string>entryPath,
|
||||
|
@ -23,6 +34,10 @@ export class RestAPIAnnotation extends Annotation {
|
|||
}
|
||||
}
|
||||
|
||||
public metadataKey(): MetadataKeyType {
|
||||
return RestAPIMetadataKey;
|
||||
}
|
||||
|
||||
public methodDecorator<T>(target: Object, propertyKey: PropertyKeyType,
|
||||
descriptor: TypedPropertyDescriptor<T>): TypedPropertyDescriptor<T> | void {
|
||||
console.log('RestAPI');
|
||||
|
|
|
@ -1,9 +1,15 @@
|
|||
import * as TypeUtil from '@overflow/commons/core/type/util';
|
||||
import {
|
||||
MetadataKeyType,
|
||||
PropertyKeyType,
|
||||
TypeUtil,
|
||||
} from '@overflow/commons/core/type';
|
||||
|
||||
import {
|
||||
Annotation,
|
||||
Decorator,
|
||||
} from '@overflow/commons/decorator';
|
||||
import { PropertyKeyType } from '@overflow/commons/core/type';
|
||||
|
||||
export const RpcAPIMetadataKey = Symbol('RpcAPIAnnotation');
|
||||
|
||||
export interface RpcAPIAnnotationAttributes {
|
||||
method: string;
|
||||
|
@ -14,6 +20,11 @@ export class RpcAPIAnnotation extends Annotation {
|
|||
|
||||
public constructor(method: string | RpcAPIAnnotationAttributes) {
|
||||
super();
|
||||
|
||||
if (undefined === method) {
|
||||
throw new Error(`method attribute must be specified.`);
|
||||
}
|
||||
|
||||
if (TypeUtil.isString(method)) {
|
||||
this.attributes = {
|
||||
method: <string>method,
|
||||
|
@ -23,6 +34,10 @@ export class RpcAPIAnnotation extends Annotation {
|
|||
}
|
||||
}
|
||||
|
||||
public metadataKey(): MetadataKeyType {
|
||||
return RpcAPIMetadataKey;
|
||||
}
|
||||
|
||||
public methodDecorator<T>(target: Object, propertyKey: PropertyKeyType,
|
||||
descriptor: TypedPropertyDescriptor<T>): TypedPropertyDescriptor<T> | void {
|
||||
console.log('RestAPI');
|
||||
|
|
|
@ -1,6 +1,17 @@
|
|||
import {
|
||||
Action,
|
||||
} from './action';
|
||||
import {
|
||||
ConstructorType,
|
||||
Metadata,
|
||||
TypeUtil,
|
||||
} from '@overflow/commons/core/type';
|
||||
import {
|
||||
ActionMappingAnnotation,
|
||||
ActionMappingMetadataKey,
|
||||
ReducerMetadataKey,
|
||||
ReducerAnnotation,
|
||||
} from '@overflow/commons/redux/decorators';
|
||||
|
||||
export default class LPCReducer {
|
||||
|
||||
|
@ -31,12 +42,39 @@ export default class LPCReducer {
|
|||
return state;
|
||||
}
|
||||
|
||||
/**
|
||||
* registerReducers
|
||||
*/
|
||||
public registerReducers(reducerTypes: ConstructorType[]): void {
|
||||
for (let reducerType of reducerTypes) {
|
||||
let ra: ReducerAnnotation = Metadata.get(ReducerMetadataKey, reducerType);
|
||||
if (undefined === ra) {
|
||||
continue;
|
||||
}
|
||||
console.log(`name: ${ra.attributes.name}`);
|
||||
let ama: ActionMappingAnnotation = Metadata.get(ActionMappingMetadataKey, reducerType);
|
||||
if (undefined === ama) {
|
||||
continue;
|
||||
}
|
||||
console.log(`actionMapping-value: ${ama.attributes.value}`);
|
||||
|
||||
let properties = Object.keys(reducerType.prototype);
|
||||
for (let propertyKey of properties) {
|
||||
if (!TypeUtil.isMethod(reducerType.prototype, propertyKey)) {
|
||||
console.log(`property: ${propertyKey}`);
|
||||
continue;
|
||||
}
|
||||
console.log(`method: ${propertyKey}`);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* registerReducer
|
||||
*/
|
||||
public registerReducer(reducerType: any): void {
|
||||
public registerReducer(reducerType: ConstructorType): void {
|
||||
console.log(``);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,14 +1,18 @@
|
|||
import {
|
||||
ActionMapping,
|
||||
Reducer,
|
||||
RestAPI,
|
||||
} from '@overflow/commons/redux/decorators';
|
||||
|
||||
@Reducer('@overflow/modules/member/MemberReducer')
|
||||
@Reducer()
|
||||
@ActionMapping('@overflow/modules/member/MemberReducer')
|
||||
export default class MemberReducer {
|
||||
public path: string;
|
||||
/**
|
||||
* signin
|
||||
*/
|
||||
@RestAPI('/account/signin')
|
||||
@ActionMapping('/signin')
|
||||
public signin(state: any, result: any, error: any): any {
|
||||
|
||||
return state;
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
import { ConstructorType } from '@overflow/commons/core/type';
|
||||
import MemberReducer from '@overflow/modules/member/reducer/member_reducer';
|
||||
|
||||
|
||||
const reducers: any[] = [
|
||||
const reducers: ConstructorType[] = [
|
||||
MemberReducer,
|
||||
];
|
||||
|
||||
|
@ -11,7 +12,7 @@ const state: any = {
|
|||
|
||||
export interface ReduxConfig {
|
||||
state: any;
|
||||
reducers: any[];
|
||||
reducers: ConstructorType[];
|
||||
}
|
||||
|
||||
const reduxConfig: ReduxConfig = {
|
||||
|
|
|
@ -58,6 +58,7 @@ class WebAppApplication {
|
|||
try {
|
||||
this.renderLoading();
|
||||
let reducer: WebAppReducer = WebAppReducer.getInstance();
|
||||
reducer.registerReducers(config.redux.reducers);
|
||||
this.store = createStore(reducer.reducer, config.redux.state);
|
||||
|
||||
this.renderApp();
|
||||
|
|
|
@ -14,7 +14,7 @@ class WebApp extends React.Component<Props, State> {
|
|||
|
||||
public render(): JSX.Element {
|
||||
return (
|
||||
<b>Hello...</b>
|
||||
<b>Hello.</b>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user