This commit is contained in:
snoop 2017-07-19 19:47:47 +09:00
commit 402adebb44
5 changed files with 38 additions and 8 deletions

View File

@ -2,12 +2,19 @@ import * as React from 'react';
import { RouteComponentProps } from 'react-router'; import { RouteComponentProps } from 'react-router';
import SignInContainer from '@overflow/member/react/SignIn'; import SignInContainer from '@overflow/member/react/SignIn';
import WebSocketRPC from '@overflow/commons/websocket/WebSocketRPC'; import WebSocketRPC from '@overflow/commons/websocket/WebSocketRPC';
import AppContext from '@overflow/commons/context';
import inject from '@overflow/commons/context/decorator/inject'; import inject from '@overflow/commons/context/decorator/inject';
class Signin extends React.Component<RouteComponentProps<object>, object> { class Signin extends React.Component<RouteComponentProps<object>, object> {
@inject() @inject()
private client: WebSocketRPC; private client: WebSocketRPC;
public constructor(props?: RouteComponentProps<object>, context?: object) {
super(props, context);
let con = AppContext.get<WebSocketRPC>();
}
public render(): JSX.Element { public render(): JSX.Element {
return ( return (
<SignInContainer/> <SignInContainer/>

View File

@ -2,7 +2,7 @@ import inject from '../context/decorator/inject';
import WebSocketRPC from '../websocket/WebSocketRPC'; import WebSocketRPC from '../websocket/WebSocketRPC';
abstract class Service { abstract class Service {
@inject(WebSocketRPC) @inject()
private webSocketRPC: WebSocketRPC; private webSocketRPC: WebSocketRPC;
private name: string; private name: string;
protected constructor(name: string) { protected constructor(name: string) {

View File

@ -1,15 +1,17 @@
import * as METADATA from '../constants'; import * as METADATA from '../constants';
export interface Config {
qualifier?: string;
required?: boolean;
}
const inject = <TFunction extends Function>(type?: TFunction) => { const inject = (config?: Config) => {
return (target: Object, propertyKey: string | symbol, parameterIndex?: number): void => { return (target: Object, propertyKey: string | symbol, parameterIndex?: number): void => {
if (typeof parameterIndex === 'number') { if (typeof parameterIndex === 'number') {
// tagParameter(target, targetKey, index, metadata); // tagParameter(target, targetKey, index, metadata);
} else { } else {
let types = Reflect.getMetadata('design:type', target, propertyKey); let types = Reflect.getMetadata('design:type', target, propertyKey);
let aaa = type;
console.log(aaa);
// tagProperty(target, targetKey, metadata); // tagProperty(target, targetKey, metadata);
} }
}; };

View File

@ -1,17 +1,29 @@
import * as METADATA from '../constants'; import * as METADATA from '../constants';
function injectable(): ClassDecorator { export interface Config {
qualifier?: string;
scope?: Scope;
}
export enum Scope {
SINGLETON = 1,
PROTOTYPE,
}
const injectable = (config?: Config) => {
return <TFunction extends Function>(target: TFunction): TFunction | void => { return <TFunction extends Function>(target: TFunction): TFunction | void => {
if (Reflect.hasOwnMetadata(METADATA.PARAM_TYPES, target) === true) { if (Reflect.hasOwnMetadata(METADATA.PARAM_TYPES, target) === true) {
throw new Error('Cannot apply @injectable decorator multiple times.'); throw new Error('Cannot apply @injectable decorator multiple times.');
} }
let types = Reflect.getMetadata(METADATA.DESIGN_PARAM_TYPES, target) || []; let types = Reflect.getMetadata(METADATA.DESIGN_PARAM_TYPES, target);
Reflect.defineMetadata(METADATA.PARAM_TYPES, types, target); Reflect.defineMetadata(METADATA.PARAM_TYPES, types, target);
const name = target.name;
return target; return target;
}; };
} };
export default injectable; export default injectable;

View File

@ -1,5 +1,5 @@
class AppContext { class AppContext {
private static context: AppContext; private static context: AppContext = null;
private constructor() { private constructor() {
@ -9,7 +9,16 @@ class AppContext {
return null; return null;
} }
public static get<T>(): T {
return null;
}
public static getContext(): AppContext { public static getContext(): AppContext {
if (null === AppContext.context) {
AppContext.context = new AppContext();
}
return AppContext.context; return AppContext.context;
} }
} }