• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

TypeScript di.Container类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
TypeScript theme.MenuService类代码示例发布时间:2022-05-28
下一篇:
TypeScript http-service.request函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap