Skip to content

Commit

Permalink
fix(channel): messageQueue type
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Aug 31, 2022
1 parent 696db68 commit 1197ec4
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/channel/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ import JsonBig from '../utils/json-big';
import { pascalToSnake } from '../utils/string';
import { Encoded } from '../utils/encoder';
import {
BaseError, ChannelCallError, ChannelPingTimedOutError, UnknownChannelStateError,
BaseError,
ChannelCallError,
ChannelPingTimedOutError,
UnexpectedTsError,
UnknownChannelStateError,
} from '../utils/errors';

interface ChannelAction {
Expand Down Expand Up @@ -129,7 +133,7 @@ export const state = new WeakMap<Channel, Encoded.Transaction>();
const fsm = new WeakMap<Channel, ChannelFsm>();
const websockets = new WeakMap<Channel, W3CWebSocket>();
export const eventEmitters = new WeakMap<Channel, EventEmitter>();
const messageQueue = new WeakMap<Channel, string[]>();
const messageQueue = new WeakMap<Channel, object[]>();
const messageQueueLocked = new WeakMap<Channel, boolean>();
const actionQueue = new WeakMap<Channel, ChannelAction[]>();
const actionQueueLocked = new WeakMap<Channel, boolean>();
Expand Down Expand Up @@ -209,7 +213,7 @@ export function enqueueAction(
void dequeueAction(channel);
}

async function handleMessage(channel: Channel, message: string): Promise<void> {
async function handleMessage(channel: Channel, message: object): Promise<void> {
const fsmState = fsm.get(channel);
if (fsmState == null) throw new UnknownChannelStateError();
const { handler, state: st } = fsmState;
Expand All @@ -219,11 +223,13 @@ async function handleMessage(channel: Channel, message: string): Promise<void> {
async function dequeueMessage(channel: Channel): Promise<void> {
const locked: boolean = messageQueueLocked.get(channel) ?? false;
if (locked) return;
const messages: string[] = messageQueue.get(channel) ?? [];
const messages = messageQueue.get(channel);
if (messages == null) throw new UnexpectedTsError();
if (messages.length === 0) return;
messageQueueLocked.set(channel, true);
while (messages.length > 0) {
const message: string = messages.shift() ?? '';
const message = messages.shift();
if (message == null) throw new UnexpectedTsError();
try {
await handleMessage(channel, message);
} catch (error) {
Expand Down

0 comments on commit 1197ec4

Please sign in to comment.