Skip to content

Commit 5205045

Browse files
committed
Add Blink integration (though broken?).
1 parent 1f8406d commit 5205045

File tree

9 files changed

+11352
-87
lines changed

9 files changed

+11352
-87
lines changed

packages/webio/dist/blink.bundle.js

Lines changed: 11242 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/webio/dist/blink.bundle.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/webio/dist/mux.bundle.js

Lines changed: 10 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/webio/dist/mux.bundle.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import WebIO from "../WebIO";
2+
3+
/**
4+
* Tell TypeScript that the `Blink` global exists.
5+
*
6+
* @todo This could be typed out but is probably pretty low priority.
7+
*/
8+
declare const Blink: any;
9+
10+
if (Blink && Blink.sock) {
11+
const webIO = new WebIO();
12+
webIO.setSendCallback((message) => {
13+
Blink.msg("webio", message);
14+
});
15+
Blink.handlers.webio = (message: any) => {
16+
webIO.dispatch(message.data);
17+
};
18+
19+
if (typeof window !== "undefined") {
20+
(window as any).WebIO = webIO;
21+
}
22+
} else {
23+
console.error("WebIO is unable to initialize (Blink is not connected)!");
24+
}

packages/webio/src/providers/mux-entrypoint.ts

Lines changed: 0 additions & 67 deletions
This file was deleted.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import debug from "debug";
2+
const log = debug("WebIO:Mux");
3+
4+
import WebIO from "../WebIO";
5+
import {WebIOWireMessage} from "../message";
6+
7+
/**
8+
* Get the standard url of the Mux/WebIO WebSocket.
9+
*/
10+
export const getMuxWSUrl = (): string => {
11+
const {protocol, host, pathname} = window.location;
12+
const wsProtocol = protocol == "https:" ? "wss:" : "ws:";
13+
const basePath = pathname[pathname.length - 1] == "/" ? pathname : pathname + "/";
14+
const wsPath = basePath + "webio-socket";
15+
16+
return `${wsProtocol}//${host}${wsPath}`;
17+
};
18+
19+
/**
20+
* Create a WebIO instance connected to a Mux WebSocket.
21+
* @param webIO - the WebIO instance to connect.
22+
* @param wsUrl - the url of the WebSocket to connect to.
23+
*/
24+
export const connectWebIOToWebSocket = (
25+
webIO: WebIO,
26+
wsUrl: string = getMuxWSUrl(),
27+
) => {
28+
return new Promise<void>((resolve, reject) => {
29+
const webSocket = new WebSocket(wsUrl);
30+
31+
webSocket.onopen = () => {
32+
webIO.setSendCallback((msg: WebIOWireMessage) => {
33+
webSocket.send(JSON.stringify(msg));
34+
});
35+
resolve();
36+
};
37+
38+
webSocket.onclose = (closeEvent) => {
39+
// Rejecting a resolved or already-rejected promise is a no-op.
40+
reject(closeEvent);
41+
};
42+
43+
webSocket.onerror = (error) => {
44+
// Rejecting a resolved or already-rejected promise is a no-op.
45+
reject(error);
46+
};
47+
48+
webSocket.onmessage = ({data}) => {
49+
const message = JSON.parse(data);
50+
webIO.dispatch(message);
51+
}
52+
})
53+
};
54+
55+
56+
const muxEntrypoint = async () => {
57+
const webIO = new WebIO();
58+
// We do window as any to allow defining new members.
59+
(window as any).WebIO = webIO;
60+
await connectWebIOToWebSocket(webIO);
61+
log("Connected WebIO to WebSocket.")
62+
};
63+
64+
muxEntrypoint();

packages/webio/webpack.config.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,18 @@ module.exports = {
1111
devtool: "cheap-module-source-map",
1212

1313
entry: {
14-
'mux': ['@babel/polyfill', 'providers/mux-entrypoint.ts']
14+
// Running in browser, we need polyfill for ES2015 support.
15+
// We don't really need *all* of the babel polyfill but it's not
16+
// **that** big. We could have our own custom entrypoint where
17+
// we only import relevant core-js modules if we wanted.
18+
'mux': ['@babel/polyfill', 'providers/mux.ts'],
19+
// Babel isn't necessary in Electron (right?).
20+
'blink': ['@babel/polyfill', 'providers/blink.ts'],
1521
},
1622

1723
// https://webpack.js.org/configuration/output/
1824
output: {
19-
path: path.resolve( __dirname, 'dist/' ),
25+
path: path.resolve(__dirname, 'dist/'),
2026
filename: '[name].bundle.js',
2127
},
2228

src/providers/blink.jl

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ using AssetRegistry
22
using Base64: stringmime
33

44
const blinksetup = joinpath(dirname(@__FILE__), "..", "..",
5-
"assets", "providers",
6-
"blink_setup.js")
5+
"packages", "webio", "dist",
6+
"blink.bundle.js")
77

88
using Sockets
99

@@ -14,10 +14,7 @@ end
1414
function Blink.body!(p::Blink.Page, x::Union{Node, Scope, AbstractWidget})
1515
wait(p)
1616

17-
bp = AssetRegistry.register(WebIO.bundlepath)
1817
bs = AssetRegistry.register(blinksetup)
19-
20-
Blink.loadjs!(p, bp)
2118
Blink.loadjs!(p, bs)
2219

2320
conn = BlinkConnection(p)

0 commit comments

Comments
 (0)