Skip to content

Commit

Permalink
add @nger/di @nger/decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
meepobrother committed Nov 12, 2019
1 parent 275be2a commit 457d910
Show file tree
Hide file tree
Showing 23 changed files with 175 additions and 146 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@
/hooks
/platforms
/cloud
/**/package.json
/**/package.json
/**/node_modules
/dist
/**/dist
3 changes: 1 addition & 2 deletions packages/ims-decorator/__test__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import {
makeDecorator,
getContext,
isClassAst,
isPropertyAst,
getDesignParamTypes
isPropertyAst
} from '../lib/index';
import { expect } from 'chai'
class ImsDemo { }
Expand Down
2 changes: 1 addition & 1 deletion packages/ims-decorator/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "reflect-metadata";
import { Injector } from 'nger-di';
import { Injector } from '@nger/di';
export interface Type<T> extends Function {
new(...args: any[]): T;
}
Expand Down
46 changes: 25 additions & 21 deletions packages/ims-decorator/package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
{
"name": "ims-decorator",
"version": "1.0.6",
"description": "> TODO: description",
"author": "imeepos <1037483576@qq.com>",
"homepage": "http://meepo.com.cn",
"license": "ISC",
"main": "lib/index",
"directories": {
"lib": "lib",
"test": "__tests__"
},
"files": [
"lib"
],
"scripts": {
"test": "echo \"Error: run tests from root\" && exit 1"
},
"gitHead": "b86981067c7da7653379ae461b9427f4aea193a7",
"dependencies": {
"reflect-metadata": "*"
}
"name": "@nger/decorator",
"version": "1.0.7",
"description": "> TODO: description",
"author": "imeepos <1037483576@qq.com>",
"homepage": "http://meepo.com.cn",
"license": "ISC",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"directories": {
"lib": "lib",
"test": "__tests__"
},
"files": [ "lib" ],
"publishConfig": {
"registry": "http://registry.npmjs.org/",
"access": "public"
},
"scripts": {
"test": "echo \"Error: run tests from root\" && exit 1"
},
"gitHead": "b86981067c7da7653379ae461b9427f4aea193a7",
"dependencies": {
"reflect-metadata": "*",
"@nger/di": "*"
}
}
9 changes: 9 additions & 0 deletions packages/ims-decorator/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"include": [
"lib"
],
"exclude": [
"node_modules"
]
}
13 changes: 13 additions & 0 deletions packages/ims-decorator/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


"@nger/di@*":
version "1.0.7"
resolved "https://registry.yarnpkg.com/@nger/di/-/di-1.0.7.tgz#bc96262a65250e8709fed853640b2f0d79b66cb6"
integrity sha512-hsE08gt/71gaemjX3/JzyaaT3x92UJHzOBtfLSK/I7nVJFVYUbB6bTNfAWqBheZmGO6fAjZR8XBSHsviqtiWfg==

reflect-metadata@*:
version "0.1.13"
resolved "https://registry.yarnpkg.com/reflect-metadata/-/reflect-metadata-0.1.13.tgz#67ae3ca57c972a2aa1642b10fe363fe32d49dc08"
integrity sha512-Ts1Y/anZELhSsjMcU605fU9RE4Oi3p5ORujwbIKXfWa+0Zxs510Qrmrce5/Jowq3cHSZSJqBjypxmHarc+vEWg==
2 changes: 0 additions & 2 deletions packages/nger-core/lib/cli/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ export interface CommandOptions {
};
}
export const Command = makeDecorator<CommandOptions>(CommandMetadataKey);

export interface Command<O, R> {
action(options: O): R;
}

export function isCommandClassAst(val: ClassAst): val is ClassAst<CommandOptions> {
return val.metadataKey === CommandMetadataKey;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/nger-core/lib/cli/option.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { makeDecorator, PropertyContext, PropertyAst } from "ims-decorator";
export const OptionMetadataKey = 'OptionMetadataKey'
import { Options } from 'yargs'
import { Options } from 'yargs';
export interface OptionOptions extends Options { }
export const Option = makeDecorator<OptionOptions>(OptionMetadataKey);
export interface Option<O, R> { }
Expand Down
1 change: 0 additions & 1 deletion packages/nger-core/lib/decorators/content-children.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,3 @@ export class ContentChildrenPropertyAst extends PropertyContext<ContentChildrenO
export function isContentChildrenPropertyAst(ast: PropertyAst): ast is PropertyAst<ContentChildrenOptions> {
return ast.metadataKey === ContentChildrenMetadataKey;
}

2 changes: 1 addition & 1 deletion packages/nger-core/lib/decorators/host.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { makeDecorator,TypeDecorator, ConstructorContext, ConstructorAst } from 'ims-decorator';
import { makeDecorator, TypeDecorator, ConstructorContext, ConstructorAst } from 'ims-decorator';
export const HostMetadataKey = 'HostMetadataKey';
export interface HostOptions { }
export interface HostDecorator {
Expand Down
2 changes: 1 addition & 1 deletion packages/nger-core/lib/decorators/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ declare global {
interface IntrinsicElements {
[elemName: string]: any;
}
interface ElementClass {}
interface ElementClass { }
interface ElementAttributesProperty extends NgerComponent<any> { }
interface ElementChildrenAttribute { }
}
Expand Down
4 changes: 2 additions & 2 deletions packages/nger-core/lib/decorators/output.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { makeDecorator, ClassAst, PropertyContext, PropertyAst } from 'ims-decorator';
import { Subject, Subscription } from 'rxjs'
import { makeDecorator, PropertyContext, PropertyAst } from 'ims-decorator';
import { Subject, Subscription } from 'rxjs';

export const OutputMetadataKey = 'OutputMetadataKey';
export interface OutputOptions {
Expand Down
2 changes: 1 addition & 1 deletion packages/nger-core/lib/decorators/self.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { makeDecorator,TypeDecorator, ConstructorContext, ConstructorAst } from 'ims-decorator';
import { makeDecorator, TypeDecorator, ConstructorContext, ConstructorAst } from 'ims-decorator';
export const SelfMetadataKey = 'SelfMetadataKey';
export interface SelfOptions { }
export interface SelfDecorator {
Expand Down
2 changes: 1 addition & 1 deletion packages/nger-core/lib/decorators/skip-self.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { makeDecorator,TypeDecorator, ConstructorContext, ConstructorAst } from 'ims-decorator';
import { makeDecorator, TypeDecorator, ConstructorContext, ConstructorAst } from 'ims-decorator';
export const SkipSelfMetadataKey = 'SkipSelfMetadataKey';
export interface SkipSelfOptions { }
export interface SkipSelfDecorator {
Expand Down
70 changes: 35 additions & 35 deletions packages/nger-core/lib/platform/application_init_status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,43 @@ import { InjectionToken } from 'nger-di';
export const APP_INITIALIZER = new InjectionToken<Array<() => void>>('Application Initializer');
@Injectable()
export class ApplicationInitStatus {
// TODO(issue/24571): remove '!'.
private resolve !: Function;
// TODO(issue/24571): remove '!'.
private reject !: Function;
private initialized = false;
public readonly donePromise: Promise<any>;
public readonly done = false;
// TODO(issue/24571): remove '!'.
private resolve !: Function;
// TODO(issue/24571): remove '!'.
private reject !: Function;
private initialized = false;
public readonly donePromise: Promise<any>;
public readonly done = false;

constructor(@Inject(APP_INITIALIZER) @Optional() private appInits: (() => any)[]) {
this.donePromise = new Promise((res, rej) => {
this.resolve = res;
this.reject = rej;
});
}

/** @internal */
runInitializers() {
if (this.initialized) {
return;
constructor(@Inject(APP_INITIALIZER) @Optional() private appInits: (() => any)[]) {
this.donePromise = new Promise((res, rej) => {
this.resolve = res;
this.reject = rej;
});
}
const asyncInitPromises: Promise<any>[] = [];
const complete = () => {
(this as { done: boolean }).done = true;
this.resolve();
};
if (this.appInits) {
for (let i = 0; i < this.appInits.length; i++) {
const initResult = this.appInits[i]();
if (isPromise(initResult)) {
asyncInitPromises.push(initResult);

/** @internal */
runInitializers() {
if (this.initialized) {
return;
}
}
}
Promise.all(asyncInitPromises).then(() => { complete(); }).catch(e => { this.reject(e); });
if (asyncInitPromises.length === 0) {
complete();
const asyncInitPromises: Promise<any>[] = [];
const complete = () => {
(this as { done: boolean }).done = true;
this.resolve();
};
if (this.appInits) {
for (let i = 0; i < this.appInits.length; i++) {
const initResult = this.appInits[i]();
if (isPromise(initResult)) {
asyncInitPromises.push(initResult);
}
}
}
Promise.all(asyncInitPromises).then(() => { complete(); }).catch(e => { this.reject(e); });
if (asyncInitPromises.length === 0) {
complete();
}
this.initialized = true;
}
this.initialized = true;
}
}
4 changes: 2 additions & 2 deletions packages/nger-core/lib/platform/createPlatform.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injector, InjectionToken,InjectFlags } from 'nger-di'
import { Injector, InjectionToken, InjectFlags } from 'nger-di'
import { PlatformRef } from './platform_ref'
import { PLATFORM_INITIALIZER } from './application_tokens'
let _platform: PlatformRef;
Expand All @@ -15,7 +15,7 @@ export function createPlatform(injector: Injector): PlatformRef {
'There can be only one platform. Destroy the previous one to create a new one.');
}
_platform = injector.get(PlatformRef);
const inits = injector.get<Function[]>(PLATFORM_INITIALIZER, undefined,InjectFlags.Optional) || [];
const inits = injector.get<Function[]>(PLATFORM_INITIALIZER, undefined, InjectFlags.Optional) || [];
if (inits) inits.forEach((init: any) => init());
return _platform;
}
2 changes: 1 addition & 1 deletion packages/nger-core/lib/platform/parser_visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TypeContext } from 'ims-decorator'
import { Inject, InjectMetadataKey, InjectPropertyAst } from '../decorators/inject'
import { EntityRepositoryMetadataKey, EntityRepositoryPropertyAst } from '../orm/index';
import { ConnectionToken } from '../tokens';
import { Connection } from 'typeorm'
import { Connection } from 'typeorm';
import { Injector } from 'nger-di'
import { Http } from '../sdk/http';
import { GetMetadataKey, GetPropertyAst } from '../http/get'
Expand Down
2 changes: 1 addition & 1 deletion packages/nger-core/lib/sdk/nger-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class NgerConfig {
} else {
}
}

get(name: INgerConfigKey): INgerConfig[INgerConfigKey] {
const config: any = this.config[name];
if (config) {
Expand Down
3 changes: 1 addition & 2 deletions packages/nger-di/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// export * from './injector';
export * from './util';
export * from './injection_token';
export * from './type';
export { Injector, StaticInjector,IToken } from './injector_ng';
export { Injector, StaticInjector, IToken } from './injector_ng';
2 changes: 0 additions & 2 deletions packages/nger-di/lib/injector_ng.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { InjectionToken } from './injection_token';
import { ɵɵinject } from './injector_compatibility';
import { ɵɵdefineInjectable } from './def';
import { ConstructorProvider, ExistingProvider, FactoryProvider, StaticClassProvider, StaticProvider, ValueProvider } from './type';
// import { Inject, Optional, Self, SkipSelf } from './metadata';

export const SOURCE = '__source';
const _THROW_IF_NOT_FOUND = Symbol.for(`_THROW_IF_NOT_FOUND`);
export const THROW_IF_NOT_FOUND = _THROW_IF_NOT_FOUND;
Expand Down
72 changes: 36 additions & 36 deletions packages/nger-di/lib/util.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
import { Type } from './type';
export function stringify(token: any): string {
if (typeof token === 'string') {
return token;
}
if (token instanceof Array) {
return '[' + token.map(stringify).join(', ') + ']';
}
if (token == null) {
return '' + token;
}
if (token.overriddenName) {
return `${token.overriddenName}`;
}
if (token.name) {
return `${token.name}`;
}
const res = token.toString();
if (res == null) {
return '' + res;
}
const newLineIndex = res.indexOf('\n');
return newLineIndex === -1 ? res : res.substring(0, newLineIndex);
if (typeof token === 'string') {
return token;
}
if (token instanceof Array) {
return '[' + token.map(stringify).join(', ') + ']';
}
if (token == null) {
return '' + token;
}
if (token.overriddenName) {
return `${token.overriddenName}`;
}
if (token.name) {
return `${token.name}`;
}
const res = token.toString();
if (res == null) {
return '' + res;
}
const newLineIndex = res.indexOf('\n');
return newLineIndex === -1 ? res : res.substring(0, newLineIndex);
}

export function getClosureSafeProperty<T>(objWithPropertyToExtract: T): string {
for (let key in objWithPropertyToExtract) {
if (objWithPropertyToExtract[key] === getClosureSafeProperty as any) {
return key;
for (let key in objWithPropertyToExtract) {
if (objWithPropertyToExtract[key] === getClosureSafeProperty as any) {
return key;
}
}
}
throw Error('Could not find renamed property on target object.');
throw Error('Could not find renamed property on target object.');
}

export function forwardRef(forwardRefFn: ForwardRefFn): Type<any> {
(<any>forwardRefFn).__forward_ref__ = forwardRef;
(<any>forwardRefFn).toString = function () { return stringify(this()); };
return (<Type<any>><any>forwardRefFn);
(<any>forwardRefFn).__forward_ref__ = forwardRef;
(<any>forwardRefFn).toString = function () { return stringify(this()); };
return (<Type<any>><any>forwardRefFn);
}
export interface ForwardRefFn { (): any; }
const __forward_ref__ = getClosureSafeProperty({ __forward_ref__: getClosureSafeProperty });

export function resolveForwardRef<T>(type: T): T {
const fn: any = type;
if (typeof fn === 'function' && fn.hasOwnProperty(__forward_ref__) &&
fn.__forward_ref__ === forwardRef) {
return fn();
} else {
return type;
}
const fn: any = type;
if (typeof fn === 'function' && fn.hasOwnProperty(__forward_ref__) &&
fn.__forward_ref__ === forwardRef) {
return fn();
} else {
return type;
}
}
Loading

0 comments on commit 457d910

Please sign in to comment.