Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions application/static/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Metacom } from './metacom.js';

const metacom = new Metacom(location.host);
const { api } = metacom;
window.api = api;

const ALPHA_UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const ALPHA_LOWER = 'abcdefghijklmnopqrstuvwxyz';
Expand Down
14 changes: 11 additions & 3 deletions application/static/metacom.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export class Metacom {
this.api = {};
this.callId = 0;
this.calls = new Map();
this.socket.onmessage = ({ data }) => {
this.socket.addEventListener('message', ({ data }) => {
try {
const packet = JSON.parse(data);
const { callback, event } = packet;
Expand All @@ -21,7 +21,14 @@ export class Metacom {
} catch (err) {
console.error(err);
}
};
});
}

ready() {
return new Promise(resolve => {
if (this.socket.readyState === WebSocket.OPEN) resolve();
else this.socket.addEventListener('open', resolve);
});
}

async load(...interfaces) {
Expand Down Expand Up @@ -60,10 +67,11 @@ export class Metacom {
}

socketCall(iname, ver) {
return methodName => (args = {}) => {
return methodName => async (args = {}) => {
const callId = ++this.callId;
const interfaceName = ver ? `${iname}.${ver}` : iname;
const target = interfaceName + '/' + methodName;
await this.ready();
return new Promise((resolve, reject) => {
this.calls.set(callId, [resolve, reject]);
const packet = { call: callId, [target]: args };
Expand Down