我有一个播放器连接到一个频道,连接成功后,它会从该频道获取状态。
this.clientConnectedCallbacks.add(uid, (state) => {
if (this.clientConnectedCallbacks.has(uid)) {
this.clientConnectedCallbacks.remove(uid);
clearTimeout(this.clientConnectedTimeouts.get(uid));
this.clientConnectedTimeouts.remove(uid);
return state;
}
throw new Error('Client connection callback was no longer in the map.')
});
this.clientConnectedTimeouts.add(uid, setTimeout(() => {
this.clientConnectedCallbacks.remove(uid);
this.clientConnectedTimeouts.remove(uid);
throw new Error(`Client ${uid} connection request to ${this.channelId} timed out`);
}, this.clientTimeout));
我会尽可能地利用await / async,并且该函数位于其中,具有async标签,但这是一个非常低级的实现,因此我需要手动使函数异步返回。我想知道是否还有其他更优雅的想法/设计可以构建这样的东西?
我想在从通道接收到成功的连接消息后,从连接的通道异步地将状态返回给客户端(这是在完全不同的函数中发生的,该函数专门用于消息处理,但是可以访问this.clientConnectedCallbacks对象
但是我也想使客户端连接超时,以防万一承诺不能永远挂起。