Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #4923 from trufflesuite/dashboard/events
Browse files Browse the repository at this point in the history
Hook up example events -> dashboard communication
  • Loading branch information
gnidan authored Mar 23, 2022
2 parents 9ea4fd6 + 73c3355 commit ac2f6c4
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 2 deletions.
14 changes: 14 additions & 0 deletions packages/dashboard-message-bus/lib/message/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ export interface LogMessage extends Message {
};
}

/**
* Message to log in Dashboard browser console
*/
export interface DebugMessage extends Message {
type: "debug";
payload: {
message: string;
};
}

/**
* Message intended to invalidate earlier messages.
* The payload is the ID of the message that should be invalidated.
Expand All @@ -51,6 +61,10 @@ export const isLogMessage = (message: Message): message is LogMessage => {
return message.type === "log";
};

export const isDebugMessage = (message: Message): message is DebugMessage => {
return message.type === "debug";
};

export const isInvalidateMessage = (
message: Message
): message is InvalidateMessage => {
Expand Down
4 changes: 3 additions & 1 deletion packages/dashboard-message-bus/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import axios from "axios";

any.shim();

export class MessageBusConnectionError extends Error {}

/**
* Convert any JS object or value to a base64 representation of it
*/
Expand Down Expand Up @@ -152,7 +154,7 @@ export const getMessageBusPorts = async (
}
}

throw new Error(
throw new MessageBusConnectionError(
`Could not connect to dashboard at http://${dashboardHost}:${dashboardPort}/ports`
);
};
7 changes: 6 additions & 1 deletion packages/dashboard/src/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import {
connectToMessageBusWithRetries,
isDashboardProviderMessage,
isInvalidateMessage,
isDebugMessage,
Message,
base64ToJson
} from "@truffle/dashboard-message-bus";
import { useWeb3React } from "@web3-react/core";
import { useEffect, useState } from "react";
import { getPorts } from "./utils/utils";
import { getPorts, respond } from "./utils/utils";
import Header from "./components/Header/Header";
import DashboardProvider from "./components/DashboardProvider/DashboardProvider";
import ConnectNetwork from "./components/ConnectNetwork";
Expand Down Expand Up @@ -66,6 +67,10 @@ function Dashboard() {
setDashboardProviderRequests(previousRequests =>
previousRequests.filter(request => request.id !== message.payload)
);
} else if (isDebugMessage(message)) {
const { payload } = message;
console.log(payload.message);
respond({ id: message.id }, connectedSocket);
}
}
);
Expand Down
47 changes: 47 additions & 0 deletions packages/events/defaultSubscribers/dashboard/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const {
connectToMessageBusWithRetries,
createMessage,
getMessageBusPorts,
sendAndAwait,
MessageBusConnectionError
} = require("@truffle/dashboard-message-bus");

module.exports = class DashboardMessageBusClient {
constructor(config) {
this.config = config.dashboard || {
host: "localhost",
port: 24012
};
}

async _getSocket() {
if (this._socket) {
return this._socket;
}

const { publishPort } = await getMessageBusPorts(
this.config.port,
this.config.host
);

this._socket = await connectToMessageBusWithRetries(
publishPort,
this.config.host
);

return this._socket;
}

async sendAndAwait({ type, payload }) {
try {
const socket = await this._getSocket();
const message = createMessage(type, payload);

return await sendAndAwait(socket, message);
} catch (err) {
if (!(err instanceof MessageBusConnectionError)) {
throw err;
} else return;
}
}
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const Writable = require("stream").Writable;
const DashboardMessageBusClient = require("./client");

module.exports = {
initialization: function (config) {
this.messageBus = new DashboardMessageBusClient(config);

this._logger = {
log: ((...args) => {
if (config.quiet) {
Expand All @@ -13,6 +16,16 @@ module.exports = {
};
},
handlers: {
"compile:start": [
async function () {
await this.messageBus.sendAndAwait({
type: "debug",
payload: {
message: "compile:start"
}
});
}
],
"rpc:request": [
function (event) {
const { payload } = event;
Expand Down

0 comments on commit ac2f6c4

Please sign in to comment.