Skip to content

Commit

Permalink
fix(linting): fixup some linting issues
Browse files Browse the repository at this point in the history
- This is a quick fix to get ci running again
- As a result I still need better defined messages
  • Loading branch information
vandycknick committed Oct 21, 2019
1 parent 538222c commit b109897
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 17 deletions.
17 changes: 11 additions & 6 deletions src/WebTty.UI/services/serializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ import { encode, decode } from "@msgpack/msgpack"
import { OpenNewTabReply, StdOutMessage, OpenNewTabRequest, StdInputRequest, ResizeTabMessage } from "@webtty/messages"
import { BinaryMessageFormat } from "utils/BinaryFormat"

type Commands = any
type Events = any
type Messages =
| OpenNewTabReply
| StdOutMessage
| OpenNewTabRequest
| OpenNewTabReply
| ResizeTabMessage
| StdInputRequest

const getName = (message: unknown): string => {
if (message instanceof OpenNewTabRequest) return "OpenNewTabRequest"
Expand All @@ -13,14 +18,14 @@ const getName = (message: unknown): string => {
return ""
}

const serializeCommands = (message: OpenNewTabRequest | ResizeTabMessage | StdInputRequest): ArrayBuffer => {
const serializeCommands = (message: Messages): ArrayBuffer => {
const bytes = encode(message.toJSON())
const payload = encode([getName(message), bytes]).slice()
const data = BinaryMessageFormat.write(payload)
return data
}

async function* deserializeMessages(dataStream: AsyncIterable<MessageEvent>): AsyncIterable<any> {
async function* deserializeMessages(dataStream: AsyncIterable<MessageEvent>): AsyncIterable<Messages> {
for await (const event of dataStream) {
if (!event) continue

Expand All @@ -38,7 +43,7 @@ async function* deserializeMessages(dataStream: AsyncIterable<MessageEvent>): As

case "StdOutMessage": {
const stdOut = StdOutMessage.fromJS(payload)
stdOut.data = Array.from((payload as any)["Data"])
stdOut.data = Array.from((payload as { Data: number[] })["Data"])
yield stdOut
break
}
Expand All @@ -50,5 +55,5 @@ async function* deserializeMessages(dataStream: AsyncIterable<MessageEvent>): As
}
}

export { Commands, Events }
export { Messages }
export { serializeCommands, deserializeMessages }
8 changes: 4 additions & 4 deletions src/WebTty.UI/services/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { produce } from "immer"
import { TerminalState, TerminalActions, TERMINAL_NEW_TAB_CREATED } from "./types"
import AsyncQueue from "../utils/AsyncQueue"
import { OpenNewTabRequest, OpenNewTabReply, StdOutMessage, ResizeTabMessage, StdInputRequest } from "@webtty/messages"
import { Commands, Events } from "./serializers"
import { Messages } from "./serializers"

const initialState = {
tabId: undefined,
}

type DispatchCommand = (command: Commands) => void
type DispatchCommand = (command: Messages) => void
type Dispatch<A, R = void> = (action: A) => R

const terminalReducer = (state: TerminalState = initialState, action: TerminalActions): TerminalState =>
Expand All @@ -34,7 +34,7 @@ const writeStdIn = (dispatch: DispatchCommand) => (id: string, payload: string):
dispatch(input)
}

async function* stdoutMessageStream(id: string, messageStream: AsyncQueue<Events>): AsyncIterableIterator<string> {
async function* stdoutMessageStream(id: string, messageStream: AsyncQueue<Messages>): AsyncIterableIterator<string> {
const decoder = new TextDecoder()
for await (const message of messageStream) {
if (message instanceof StdOutMessage) {
Expand All @@ -43,7 +43,7 @@ async function* stdoutMessageStream(id: string, messageStream: AsyncQueue<Events
}
}

const newTabMessageStream = (messageStream: AsyncQueue<Events>) =>
const newTabMessageStream = (messageStream: AsyncQueue<Messages>) =>
async function(dispatch: Dispatch<TerminalActions>): Promise<void> {
for await (const message of messageStream) {
if (message instanceof OpenNewTabReply) {
Expand Down
6 changes: 3 additions & 3 deletions src/WebTty.UI/services/useWebTty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { terminalReducer, stdoutMessageStream } from "./terminal"
import AsyncQueue from "../utils/AsyncQueue"
import { openNewTab, resizeTerminal, writeStdIn, newTabMessageStream } from "./terminal"
import { TerminalState } from "./types"
import { serializeCommands, Commands, Events, deserializeMessages } from "./serializers"
import { serializeCommands, Messages, deserializeMessages } from "./serializers"

type WebTtyConnection = {
state: TerminalState
Expand All @@ -22,7 +22,7 @@ const useWebTty = (endpoint: string): WebTtyConnection => {
const [state, dispatch] = useReducer(terminalReducer, { tabId: undefined })

const actions = useMemo(() => {
const dispatchCommand = (command: Commands): void => {
const dispatchCommand = (command: Messages): void => {
const serialized = serializeCommands(command)
sendMessage(serialized)
}
Expand All @@ -34,7 +34,7 @@ const useWebTty = (endpoint: string): WebTtyConnection => {
}
}, [sendMessage])

const messageStream = useMemo((): AsyncQueue<Events> => {
const messageStream = useMemo((): AsyncQueue<Messages> => {
const eventStream = deserializeMessages(dataStream)
const queue = AsyncQueue.from(eventStream)
return queue
Expand Down
2 changes: 1 addition & 1 deletion src/WebTty.UI/utils/AsyncQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class AsyncQueue<T> {
listeners.push(queue)
;(async function(): Promise<void> {
try {
for await (let message of iterator) {
for await (const message of iterator) {
listeners.forEach(l => {
if (l.isDisposed) return

Expand Down
6 changes: 3 additions & 3 deletions src/WebTty.UI/utils/fromEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ interface EventEmitter {

function fromEmitter<T>(
eventEmitter: EventEmitter,
data: string = "message",
error: string = "error",
close: string = "close",
data = "message",
error = "error",
close = "close",
): AsyncIterable<T> {
const queue = new AsyncQueue<T>()

Expand Down

0 comments on commit b109897

Please sign in to comment.