-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
23 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,13 @@ import { | |
defer, | ||
Deferred, | ||
} from "https://deno.land/x/[email protected]/core/runtime/async/observer.ts"; | ||
import { Disposable, Reader } from "../socket.ts"; | ||
import { Closer, Reader } from "../socket.ts"; | ||
import { chain } from "../misc.ts"; | ||
import { str2u8s } from "./misc.ts"; | ||
|
||
const key = "<glue>"; | ||
|
||
export interface Glue extends Disposable, Reader { | ||
export interface Glue extends Closer, Reader { | ||
recv(data: Uint8Array | string): void; | ||
} | ||
|
||
|
@@ -20,18 +20,18 @@ export function getGlue(): Glue { | |
|
||
export function createGlue(): Glue { | ||
const queue: Uint8Array[] = []; | ||
let disposed = false; | ||
let closed = false; | ||
let wait: Deferred<void> | undefined; | ||
return { | ||
dispose: () => disposed = true, | ||
close: () => closed = true, | ||
recv: (data) => { | ||
if (disposed) throw new Error("Glue has been disposed."); | ||
if (closed) throw new Error("Glue has been closed."); | ||
queue.push(typeof data === "string" ? str2u8s(data) : data); | ||
wait?.resolve(); | ||
}, | ||
read: chain(async (data) => { | ||
if (queue.length < 1) { | ||
if (disposed) return null; | ||
if (closed) return null; | ||
await (wait = defer()); | ||
wait = undefined; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { defer } from "https://deno.land/x/[email protected]/core/runtime/async/observer.ts"; | ||
import { Ref, useEffect, useRef, useState } from "react"; | ||
import { Disposable, Socket } from "../socket.ts"; | ||
import { Closer, Socket } from "../socket.ts"; | ||
import { createIframeSocket } from "../glue/iframe.ts"; | ||
|
||
export interface UseWrpIframeSocketResult { | ||
|
@@ -11,7 +11,7 @@ export default function useWrpIframeSocket(): UseWrpIframeSocketResult { | |
const iframeRef = useRef<HTMLIFrameElement>(null); | ||
const [socket, setSocket] = useState<Socket | undefined>(undefined); | ||
useEffect(() => { | ||
let socket: Disposable & Socket; | ||
let socket: Closer & Socket; | ||
let unmounted = false; | ||
let waitForReconnect = defer<void>(); | ||
const iframeElement = iframeRef.current!; | ||
|
@@ -35,7 +35,7 @@ export default function useWrpIframeSocket(): UseWrpIframeSocketResult { | |
void iframeElement.removeEventListener("load", tryReconnect); | ||
}; | ||
function tryReconnect() { | ||
if (socket) socket.dispose(); | ||
if (socket) socket.close(); | ||
waitForReconnect.resolve(); | ||
waitForReconnect = defer<void>(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters