本文整理匯總了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);
});
示例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)));
}
}
}
示例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();
}
}
};
示例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();
}
}
}
示例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!');
}
}
示例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);
};