本文整理汇总了TypeScript中@decorators/di.Container类的典型用法代码示例。如果您正苦于以下问题:TypeScript Container类的具体用法?TypeScript Container怎么用?TypeScript Container使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Container类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的TypeScript代码示例。
示例1: getController
/**
* Get controller instance from container or instantiate one
*
* @param {any} Controller
*
* @returns {ExpressClass}
*/
function getController(Controller: Type): ExpressClass {
try {
return Container.get(Controller);
} catch {
return new Controller();
}
}
开发者ID:serhiisol,项目名称:node-decorators,代码行数:14,代码来源:express.ts
示例2: function
return function(...args: any[]): any {
const next: NextFunction = args[args.length - 1];
let instance: Middleware | ServerMiddleware | Type;
try {
instance = Container.get(middleware);
} catch {
try {
instance = new (middleware as Type)();
} catch {
instance = middleware as any;
}
}
// first, assuming that middleware is a class, try to use it,
// otherwise use it as a function
const use = (instance as Middleware | ServerMiddleware).use ?
(instance as Middleware | ServerMiddleware).use : instance as Type;
try {
const result = use.apply(instance, args);
// if result of execution is a promise, add additional listener to catch error
if (result instanceof Promise) {
result.catch(next);
}
return result;
} catch (e) {
return next(e);
}
}
开发者ID:serhiisol,项目名称:node-decorators,代码行数:33,代码来源:middleware.ts
示例3: attachController
/**
* Attach Controller
*
* @param {SocketIO.Server} IO
* @param {Type} Controller
*/
function attachController(
io: SocketIO.Server,
controller: Type
) {
const instance: SocketClass = Container.get(controller);
const meta: SocketMeta = getMeta(instance);
const ioNS: SocketIO.Namespace = io.of(meta.namespace);
/**
* Apply io based events
*/
applyEvents(io, null, controller, EventType.IO);
/**
* Apply local listeners (socket based)
*/
ioNS.on('connection', (socket: SocketIO.Socket) => {
/**
* Apply all registered controller-based and event-based middlewares to socket
*/
(socket as any).use(socketMiddlewareHandler(io, socket, meta));
/**
* Apply socket based events
*/
applyEvents(io, socket, controller, EventType.Socket);
});
}
开发者ID:serhiisol,项目名称:node-decorators,代码行数:34,代码来源:socket.ts
示例4: getMiddleware
/**
* Instantiate middleware and invoke it with arguments
*
* @param {InjectionToken | Type} middleware
* @param {any[]} args
*/
function getMiddleware(middleware: InjectionToken | Type, args: any[]) {
const next: NextFunction = args[args.length - 1]; // last parameter is always the next function
let instance;
try {
// first, trying to get instance from the container
instance = Container.get(middleware);
} catch {
try {
// if container fails, trying to instantiate it
instance = new (middleware as Type)();
} catch {
// if instantiation fails, try to use it as is
instance = middleware as any;
}
}
// first, assuming that middleware is a class, try to use it,
// otherwise use it as a function
const result = instance.use ?
(instance as Middleware | ErrorMiddleware).use.apply(instance, args) :
(instance as Type).apply(instance, args);
// if result of execution is a promise, add additional listener to catch error
if (result instanceof Promise) {
result.catch(e => next(e));
}
return result
}
开发者ID:serhiisol,项目名称:node-decorators,代码行数:36,代码来源:middleware.ts
示例5: applyEvents
/**
* Apply listeners to socket or io
*
* @param {SocketIO.Server|SocketIO.Namespace} io
* @param {SocketIO.Socket} socket
* @param {Type} controller
* @param {EventType} type
*/
function applyEvents(
io: SocketIO.Server | SocketIO.Namespace,
socket: SocketIO.Socket,
controller: Type,
type: EventType
) {
const instance: SocketClass = Container.get(controller);
const meta: SocketMeta = getMeta(instance);
const ioSock: any = socket || io;
meta.listeners
.filter((listener: Listener) => listener.type === type)
.forEach((listener: Listener) => ioSock.on(listener.event, (...args) => {
const methodName: string = listener.methodName;
const newArgs: any[] = mapArguments(
io, socket || args[0], meta.params[methodName], args
);
return instance[methodName].apply(instance, newArgs);
}));
}
开发者ID:serhiisol,项目名称:node-decorators,代码行数:29,代码来源:socket.ts
注:本文中的@decorators/di.Container类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论