本文整理匯總了TypeScript中@decorators/di.Container.get方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Container.get方法的具體用法?TypeScript Container.get怎麽用?TypeScript Container.get使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類@decorators/di.Container
的用法示例。
在下文中一共展示了Container.get方法的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();
}
}
示例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);
}
}
示例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);
});
}
示例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
}
示例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);
}));
}