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