feat: format and rebuild

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2019-06-29 17:02:30 +08:00
parent aeedbc4222
commit f541290f47
12 changed files with 33 additions and 57 deletions

View File

@ -1,7 +1,7 @@
import { VaildError } from './interfaces'
import { METADATA_KEY } from './constants'
import { Container, interfaces as inversify_interfaces } from 'inversify' import { Container, interfaces as inversify_interfaces } from 'inversify'
import { TYPE, interfaces as express_interfaces } from 'inversify-express-utils' import { TYPE, interfaces as express_interfaces } from 'inversify-express-utils'
import { METADATA_KEY } from './constants'
import { VaildError } from './interfaces'
import { getVaildMethodMetadata, getVaildControllerMetadata, getVaildModelMetadata } from './utils' import { getVaildMethodMetadata, getVaildControllerMetadata, getVaildModelMetadata } from './utils'
let handler = { let handler = {

View File

@ -1,7 +1,7 @@
import 'reflect-metadata' import 'reflect-metadata'
import { METADATA_KEY, VAILD_TYPE } from './constants'; import { METADATA_KEY, VAILD_TYPE } from './constants';
import { getVaildControllerMetadata, getVaildModelMetadata } from './utils'
import { VaildFunction, interfaces } from './interfaces' import { VaildFunction, interfaces } from './interfaces'
import { getVaildControllerMetadata, getVaildModelMetadata } from './utils'
/** /**
* ParameterVaild * ParameterVaild
*/ */

View File

@ -1,5 +1,5 @@
import { METADATA_KEY } from './constants'
import { interfaces } from './interfaces' import { interfaces } from './interfaces'
import { METADATA_KEY } from './constants'
function getVaildControllerMetadata(model: Object) { function getVaildControllerMetadata(model: Object) {
let controllerMetadata: interfaces.MethodMetadata = Reflect.getMetadata( let controllerMetadata: interfaces.MethodMetadata = Reflect.getMetadata(

View File

@ -1,6 +1,6 @@
import { lazyInjectNamed } from '@cc-server/ioc'
import { controller, httpPost, requestBody } from '@cc-server/binding'; import { controller, httpPost, requestBody } from '@cc-server/binding';
import { namespace, listener, interfaces, io, TYPE } from '@cc-server/ws' import { namespace, listener, interfaces, io, TYPE } from '@cc-server/ws'
import { lazyInjectNamed } from '@cc-server/ioc'
@namespace('/', (socket: io.Socket, next: (err?: any) => void) => { @namespace('/', (socket: io.Socket, next: (err?: any) => void) => {
console.log(socket.nsp.name, socket.id, 'before connection'); console.log(socket.nsp.name, socket.id, 'before connection');

View File

@ -4,24 +4,15 @@ const connStr = process.env.FAAS_MONGO_URL || 'mongodb://192.168.0.2:27017';
const dbName = process.env.FAAS_MONGO_DB || "faas"; const dbName = process.env.FAAS_MONGO_DB || "faas";
export class MongoDBConnection { export class MongoDBConnection {
private static isConnected: boolean = false;
private static db: Db; private static db: Db;
public static getConnection(result: (connection: Db) => void) { public static async getConnection(): Promise<Db> {
if (this.isConnected) { if (!this.db) { this.db = await this.connect() }
return result(this.db); return this.db;
} else {
this.connect((error: Error, db: Db) => {
return result(this.db);
});
}
} }
private static connect(result: (error: Error, db: Db) => void) { private static async connect(): Promise<Db> {
MongoClient.connect(connStr, { useNewUrlParser: true }, (err, client) => { let client = await MongoClient.connect(connStr, { useNewUrlParser: true });
this.db = client.db(dbName); return client.db(dbName);
this.isConnected = true;
return result(err, this.db);
});
} }
} }

View File

@ -1,7 +1,7 @@
import { Db, ObjectID, Collection } from 'mongodb';
import { MongoDBConnection } from './connection';
import { provide } from '@cc-server/ioc'
import { DBClient } from '@cc-server/db' import { DBClient } from '@cc-server/db'
import { MongoDBConnection } from './connection'
import { Db, ObjectID, Collection } from 'mongodb'
import { provide, postConstruct } from '@cc-server/ioc'
@provide(DBClient) @provide(DBClient)
export class MongoDBClient<T = any> implements DBClient { export class MongoDBClient<T = any> implements DBClient {
@ -9,13 +9,12 @@ export class MongoDBClient<T = any> implements DBClient {
private db: Db; private db: Db;
private collection: Collection<T>; private collection: Collection<T>;
constructor() { @postConstruct()
MongoDBConnection.getConnection((connection) => { private async init() {
this.db = connection; this.db = await MongoDBConnection.getConnection();
if (this.table) { if (this.table) {
this.collection = this.db.collection(this.table); this.collection = this.db.collection(this.table);
} }
});
} }
public getProvide<P>(): P { public getProvide<P>(): P {

View File

@ -1,2 +1,2 @@
export * from './client' export * from 'mongodb'
export * from 'mongodb'; export * from './client'

View File

@ -19,6 +19,7 @@
"url": "git+https://github.com/502647092/cc-server-parent.git" "url": "git+https://github.com/502647092/cc-server-parent.git"
}, },
"dependencies": { "dependencies": {
"@cc-server/ioc": "^0.5.0",
"inversify": "^5.0.1", "inversify": "^5.0.1",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"socket.io": "^2.2.0" "socket.io": "^2.2.0"

View File

@ -1,9 +1,9 @@
import "reflect-metadata"; import "reflect-metadata";
import { Container } from 'inversify'
import { interfaces, BroadcastMessage } from './interfaces'
import { TYPE } from './constants'
import { getNamespaces, getNamespaceMetadata, getNamespaceListenerMetadata } from './utils'
import * as io from 'socket.io' import * as io from 'socket.io'
import { Container } from 'inversify'
import { TYPE } from './constants'
import { interfaces, BroadcastMessage } from './interfaces'
import { getNamespaces, getNamespaceMetadata, getNamespaceListenerMetadata } from './utils'
export function buildWebSocket(container: Container, server: io.Server) { export function buildWebSocket(container: Container, server: io.Server) {
let constructors = getNamespaces(); let constructors = getNamespaces();
@ -53,10 +53,6 @@ function flatten(arr: Array<any>) {
} }
function applyMiddlewares(namespaceEventMetadata: interfaces.ListenerMetadata[], socket: io.Socket) { function applyMiddlewares(namespaceEventMetadata: interfaces.ListenerMetadata[], socket: io.Socket) {
// socket.use((packet: io.Packet, next: (err?: any) => void) => {
// Reflect.defineMetadata(TYPE.SocketContext, socket, packet);
// next();
// })
let middlewares = [...new Set(flatten(namespaceEventMetadata.map((data) => data.middleware)))]; let middlewares = [...new Set(flatten(namespaceEventMetadata.map((data) => data.middleware)))];
for (const middleware of middlewares) { for (const middleware of middlewares) {
socket.use((packet: io.Packet, next: (err?: any) => void) => { middleware(socket, packet, next); }); socket.use((packet: io.Packet, next: (err?: any) => void) => { middleware(socket, packet, next); });

View File

@ -1,8 +1,7 @@
import { inject, injectable, decorate } from "inversify";
import { interfaces } from './interfaces' import { interfaces } from './interfaces'
import { METADATA_KEY, TYPE } from './constants' import { METADATA_KEY } from './constants'
import { injectable, decorate } from "inversify";
import { getNamespaceListenerMetadata, getNamespacesMetadata } from './utils' import { getNamespaceListenerMetadata, getNamespacesMetadata } from './utils'
import { provideNamed, fluentProvide } from "@cc-server/ioc/src";
/** /**
* Socket.io Namespace * Socket.io Namespace
@ -17,10 +16,6 @@ export function namespace(name?: string, ...middleware: interfaces.Middleware[])
target: target target: target
}; };
decorate(injectable(), target); decorate(injectable(), target);
//decorate(fluentProvide(TYPE.Namespace)
// .inSingletonScope()
// .whenTargetNamed(target.constructor.name)
// .done(), target);
Reflect.defineMetadata(METADATA_KEY.namespace, currentMetadata, target); Reflect.defineMetadata(METADATA_KEY.namespace, currentMetadata, target);
const previousMetadata: interfaces.NamespaceMetadata[] = getNamespacesMetadata(); const previousMetadata: interfaces.NamespaceMetadata[] = getNamespacesMetadata();
Reflect.defineMetadata(METADATA_KEY.namespace, [currentMetadata, ...previousMetadata], Reflect); Reflect.defineMetadata(METADATA_KEY.namespace, [currentMetadata, ...previousMetadata], Reflect);

View File

@ -1,9 +1,8 @@
import * as io from 'socket.io' import * as io from 'socket.io'
export { io }
export * from './debug'
export * from './builder' export * from './builder'
export * from './decorators' export * from './decorators'
export * from './interfaces' export * from './interfaces'
export * from './debug'
export { TYPE } from './constants' export { TYPE } from './constants'
export { getSocketContext } from './utils'
export { io }

View File

@ -1,5 +1,5 @@
import { METADATA_KEY, TYPE } from './constants'
import { interfaces } from './interfaces' import { interfaces } from './interfaces'
import { METADATA_KEY } from './constants'
function getNamespaces() { function getNamespaces() {
return getNamespacesMetadata().map((target) => target.target); return getNamespacesMetadata().map((target) => target.target);
@ -29,14 +29,9 @@ function getNamespaceListenerMetadata(target: any) {
return eventMetadata; return eventMetadata;
} }
function getSocketContext(packet: any) {
return Reflect.getMetadata(TYPE.SocketContext, packet);
}
export { export {
getNamespaces, getNamespaces,
getNamespaceMetadata, getNamespaceMetadata,
getNamespacesMetadata, getNamespacesMetadata,
getNamespaceListenerMetadata, getNamespaceListenerMetadata
getSocketContext
} }