当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript eventemitter3.emit函数代码示例

本文整理汇总了TypeScript中eventemitter3.emit函数的典型用法代码示例。如果您正苦于以下问题:TypeScript emit函数的具体用法?TypeScript emit怎么用?TypeScript emit使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了emit函数的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: it

    it("should remove event emitter listeners on end of life", function() {
        let life: Lifetime = new Lifetime();
        let eventEmitter: EventEmitter3.EventEmitter = new EventEmitter();

        let listener1: Spy = createSpy("listener1");
        let listener2: Spy = createSpy("listener2");

        life.on(eventEmitter, "change", listener1, this);
        life.on(eventEmitter, "change", listener2, this);
        expect(eventEmitter.listeners("change").length).toBe(2);

        expect(listener1).not.toHaveBeenCalled();
        expect(listener2).not.toHaveBeenCalled();

        eventEmitter.emit("change");
        expect(listener1).toHaveBeenCalledTimes(1);
        expect(listener2).toHaveBeenCalledTimes(1);

        life.endLife();
        expect(eventEmitter.listeners("change").length).toBe(0);

        eventEmitter.emit("change");
        expect(listener1).toHaveBeenCalledTimes(1);
        expect(listener2).toHaveBeenCalledTimes(1);
    });
开发者ID:stephenjelfs,项目名称:lifetime,代码行数:25,代码来源:Lifetime.spec.ts

示例2: sendMessageRaw

  // send message, or queue it if connection is not open
  private sendMessageRaw(message: Object) {
    switch (this.status) {
      case this.wsImpl.OPEN:
        let serializedMessage: string = JSON.stringify(message);
        try {
          JSON.parse(serializedMessage);
        } catch (e) {
          this.eventEmitter.emit('error', new Error(`Message must be JSON-serializable. Got: ${message}`));
        }

        this.client.send(serializedMessage);
        break;
      case this.wsImpl.CONNECTING:
        this.unsentMessagesQueue.push(message);

        break;
      default:
        if (!this.reconnecting) {
          this.eventEmitter.emit('error', new Error('A message was not sent because socket is not connected, is closing or ' +
            'is already closed. Message was: ' + JSON.stringify(message)));
        }
    }
  }
开发者ID:apollostack,项目名称:test-websocket-server,代码行数:24,代码来源:client.ts

示例3: async

    this.client.onopen = async () => {
      if (this.status === this.wsImpl.OPEN) {
        this.clearMaxConnectTimeout();
        this.closedByUser = false;
        this.eventEmitter.emit(this.reconnecting ? 'reconnecting' : 'connecting');

        try {
          const connectionParams: ConnectionParams = await this.connectionParams();

          // Send CONNECTION_INIT message, no need to wait for connection to success (reduce roundtrips)
          this.sendMessage(undefined, MessageTypes.GQL_CONNECTION_INIT, connectionParams);
          this.flushUnsentMessagesQueue();
        } catch (error) {
          this.sendMessage(undefined, MessageTypes.GQL_CONNECTION_ERROR, error);
          this.flushUnsentMessagesQueue();
        }
      }
    };
开发者ID:apollostack,项目名称:test-websocket-server,代码行数:18,代码来源:client.ts

示例4: close

  public close(isForced = true, closedByUser = true) {
    this.clearInactivityTimeout();
    if (this.client !== null) {
      this.closedByUser = closedByUser;

      if (isForced) {
        this.clearCheckConnectionInterval();
        this.clearMaxConnectTimeout();
        this.clearTryReconnectTimeout();
        this.unsubscribeAll();
        this.sendMessage(undefined, MessageTypes.GQL_CONNECTION_TERMINATE, null);
      }

      this.client.close();
      this.client = null;
      this.eventEmitter.emit('disconnected');

      if (!isForced) {
        this.tryReconnect();
      }
    }
  }
开发者ID:apollostack,项目名称:test-websocket-server,代码行数:22,代码来源:client.ts

示例5: processReceivedData

  private processReceivedData(receivedData: any) {
    let parsedMessage: any;
    let opId: string;

    try {
      parsedMessage = JSON.parse(receivedData);
      opId = parsedMessage.id;
    } catch (e) {
      throw new Error(`Message must be JSON-parseable. Got: ${receivedData}`);
    }

    if (
      [ MessageTypes.GQL_DATA,
        MessageTypes.GQL_COMPLETE,
        MessageTypes.GQL_ERROR,
      ].indexOf(parsedMessage.type) !== -1 && !this.operations[opId]
    ) {
      this.unsubscribe(opId);

      return;
    }

    switch (parsedMessage.type) {
      case MessageTypes.GQL_CONNECTION_ERROR:
        if (this.connectionCallback) {
          this.connectionCallback(parsedMessage.payload);
        }
        break;

      case MessageTypes.GQL_CONNECTION_ACK:
        this.eventEmitter.emit(this.reconnecting ? 'reconnected' : 'connected');
        this.reconnecting = false;
        this.backoff.reset();
        this.maxConnectTimeGenerator.reset();

        if (this.connectionCallback) {
          this.connectionCallback();
        }
        break;

      case MessageTypes.GQL_COMPLETE:
        this.operations[opId].handler(null, null);
        delete this.operations[opId];
        break;

      case MessageTypes.GQL_ERROR:
        this.operations[opId].handler(this.formatErrors(parsedMessage.payload), null);
        delete this.operations[opId];
        break;

      case MessageTypes.GQL_DATA:
        const parsedPayload = !parsedMessage.payload.errors ?
          parsedMessage.payload : {...parsedMessage.payload, errors: this.formatErrors(parsedMessage.payload.errors)};
        this.operations[opId].handler(null, parsedPayload);
        break;

      case MessageTypes.GQL_CONNECTION_KEEP_ALIVE:
        const firstKA = typeof this.wasKeepAliveReceived === 'undefined';
        this.wasKeepAliveReceived = true;

        if (firstKA) {
          this.checkConnection();
        }

        if (this.checkConnectionIntervalId) {
          clearInterval(this.checkConnectionIntervalId);
          this.checkConnection();
        }
        this.checkConnectionIntervalId = setInterval(this.checkConnection.bind(this), this.wsTimeout);
        break;

      default:
        throw new Error('Invalid message type!');
    }
  }
开发者ID:apollostack,项目名称:test-websocket-server,代码行数:75,代码来源:client.ts

示例6:

 this.client.onerror = (err: Error) => {
   // Capture and ignore errors to prevent unhandled exceptions, wait for
   // onclose to fire before attempting a reconnect.
   this.eventEmitter.emit('error', err);
 };
开发者ID:apollostack,项目名称:test-websocket-server,代码行数:5,代码来源:client.ts


注:本文中的eventemitter3.emit函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。