Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
disjukr committed May 19, 2022
1 parent 5cd17e0 commit f3616f4
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 22 deletions.
18 changes: 9 additions & 9 deletions src/glue/iframe.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Disposable, Socket } from "../socket.ts";
import { Closer, Socket } from "../socket.ts";
import { checkAndRetryUntilSuccess } from "./misc.ts";
import { createGlue, isGlueEvent } from "./index.ts";

Expand All @@ -7,18 +7,18 @@ const key = "<glue>";
export interface CreateIframeSocketConfig {
iframeElement: HTMLIFrameElement;
iframeOrigin: string;
onDisposed?: () => void;
onClosed?: () => void;
}
export async function createIframeSocket(
config: CreateIframeSocketConfig,
): Promise<Disposable & Socket> {
const { iframeElement, iframeOrigin, onDisposed } = config;
): Promise<Closer & Socket> {
const { iframeElement, iframeOrigin, onClosed } = config;
await handshake(iframeElement);
const glue = createGlue();
globalThis.addEventListener("message", messageHandler);
onceIframeReloaded(iframeElement, dispose);
onceIframeReloaded(iframeElement, close);
return {
dispose,
close,
read: glue.read,
async write(data) {
const { contentWindow } = iframeElement;
Expand All @@ -33,10 +33,10 @@ export async function createIframeSocket(
if (!isGlueEvent(event)) return;
glue.recv(event.data[1]);
}
function dispose() {
function close() {
globalThis.removeEventListener("message", messageHandler);
glue.dispose();
onDisposed?.();
glue.close();
onClosed?.();
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/glue/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface WrpRequest extends EventEmitter<WrpRequestEvents> {
}
interface WrpRequestEvents {
"cancel-response": void;
"dispose": void;
"close": void;
}

interface RequestHandlingState {
Expand Down Expand Up @@ -124,7 +124,7 @@ export async function createWrpHost(
}
} finally {
for (const reqId in states) {
states[reqId].request.emit("dispose", undefined);
states[reqId].request.emit("close", undefined);
delete states[reqId];
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/react/useWrpIframeSocket.ts
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 {
Expand All @@ -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!;
Expand All @@ -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>();
}
Expand Down
5 changes: 3 additions & 2 deletions src/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface Writer {
write(p: Uint8Array): Promise<number>;
}

export interface Disposable {
dispose(): void;
// same as `Deno.Closer`
export interface Closer {
close(): void;
}

0 comments on commit f3616f4

Please sign in to comment.