Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Ball committed Apr 22, 2022
2 parents 0c24c90 + 243eed3 commit 53bb3de
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 24 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
"@typescript-eslint/no-empty-function": "off",
"react/display-name": "off",
"@typescript-eslint/explicit-module-boundary-types": "off"
}
},
"ignorePatterns": ["jacscript/samples/*.js"]
}
2 changes: 1 addition & 1 deletion jacscript
2 changes: 1 addition & 1 deletion pxt-jacdac
Submodule pxt-jacdac updated 87 files
+1 −1 accelerometer-server/pxt.json
+1 −1 accelerometer/pxt.json
+1 −1 acidity/pxt.json
+1 −1 air-pressure/pxt.json
+1 −1 air-quality-index/pxt.json
+1 −1 arcade-gamepad/pxt.json
+1 −1 azure-iot-hub-health-server/pxt.json
+1 −1 azure-iot-hub-health/pxt.json
+1 −1 bluetooth-transport/pxt.json
+1 −1 button-server/pxt.json
+1 −1 button/pxt.json
+1 −1 buzzer/pxt.json
+1 −1 character-screen/pxt.json
+1 −1 codal-message-bus-server/pxt.json
+1 −1 codal-message-bus/pxt.json
+1 −1 color/pxt.json
+1 −1 compass/pxt.json
+1 −1 controller/pxt.json
+1 −1 dc-current-measurement/pxt.json
+1 −1 dc-voltage-measurement/pxt.json
+1 −1 devices/microbit/pxt.json
+1 −1 distance/pxt.json
+1 −1 dmx/pxt.json
+1 −1 dot-matrix/pxt.json
+1 −1 e-co2/pxt.json
+1 −1 flex/pxt.json
+1 −1 gamepad/pxt.json
+1 −1 gyroscope/pxt.json
+1 −1 heart-rate/pxt.json
+1 −1 hid-joystick-server/pxt.json
+1 −1 hid-joystick/pxt.json
+1 −1 hid-keyboard-server/pxt.json
+1 −1 hid-keyboard/pxt.json
+1 −1 hid-mouse-server/pxt.json
+1 −1 hid-mouse/pxt.json
+1 −1 humidity/pxt.json
+1 −1 illuminance/pxt.json
+1 −1 led-display/pxt.json
+1 −1 led-strip/pxt.json
+1 −1 led/pxt.json
+1 −1 light-bulb/pxt.json
+1 −1 light-level/pxt.json
+1 −1 magnetometer/pxt.json
+1 −1 model-runner/pxt.json
+1 −1 motion/pxt.json
+1 −1 motor/pxt.json
+1 −1 multitouch/pxt.json
+1 −1 potentiometer/pxt.json
+1 −1 power/pxt.json
+1 −1 pulse-oximeter/pxt.json
+1 −1 pxt.json
+1 −1 rain-gauge/pxt.json
+1 −1 real-time-clock/pxt.json
+1 −1 reflected-light/pxt.json
+1 −1 relay/pxt.json
+1 −1 rng/pxt.json
+1 −1 rotary-encoder/pxt.json
+1 −1 sensor-aggregator/pxt.json
+1 −1 servers/pxt.json
+1 −1 servo/pxt.json
+1 −1 seven-segment-display/pxt.json
+1 −1 soil-moisture/pxt.json
+1 −1 solenoid/pxt.json
+1 −1 sound-level/pxt.json
+1 −1 speech-synthesis/pxt.json
+1 −1 switch/pxt.json
+1 −1 tcp/pxt.json
+1 −1 temperature/pxt.json
+1 −1 tools/dongle/pxt.json
+1 −1 tools/farm-beats/pxt.json
+1 −1 tools/hid-events/pxt.json
+1 −1 tools/hid-servers/pxt.json
+1 −1 tools/microbit-machine/pxt.json
+1 −1 tools/microbit-oob/pxt.json
+1 −1 tools/multitool/pxt.json
+1 −1 tools/uploader/pxt.json
+1 −1 traffic-light/pxt.json
+1 −1 tvoc/pxt.json
+1 −1 uv-index/pxt.json
+1 −1 version.ts
+1 −1 vibration-motor/pxt.json
+1 −1 water-level/pxt.json
+1 −1 weight-scale/pxt.json
+1 −1 wifi-server/pxt.json
+1 −1 wifi/pxt.json
+1 −1 wind-direction/pxt.json
+1 −1 wind-speed/pxt.json
5 changes: 3 additions & 2 deletions src/components/blockly/BlockMinimap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
ComponentManager,
} from "blockly"
import React, { useCallback, useEffect, useRef, useState } from "react"
import ReactDOM from "react-dom"
import { createRoot } from "react-dom/client"
import { arrayConcatMany } from "../../../jacdac-ts/src/jdom/utils"
import { svgPointerPoint } from "../widgets/svgutils"
import useWorkspaceEvent from "./useWorkspaceEvent"
Expand Down Expand Up @@ -221,7 +221,8 @@ class MinimapPlugin implements IPositionable {
class: "minimap",
})
utils.dom.insertAfter(this.svgGroup_, this.workspace_.getBubbleCanvas())
ReactDOM.render(this.render(), this.svgGroup_)
const root = createRoot(this.svgGroup_)
root.render(this.render())
}

private render() {
Expand Down
4 changes: 3 additions & 1 deletion src/components/blockly/dsl/servicesbase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
SRV_INFRASTRUCTURE,
SRV_JACSCRIPT_CLOUD,
SRV_JACSCRIPT_CONDITION,
SRV_JACSCRIPT_MANAGER,
SRV_LOGGER,
SRV_PROTO_TEST,
SRV_PROXY,
Expand Down Expand Up @@ -127,7 +128,8 @@ const ignoredServices = [
SRV_PROXY,
SRV_UNIQUE_BRAIN,
SRV_JACSCRIPT_CLOUD,
SRV_JACSCRIPT_CONDITION
SRV_JACSCRIPT_CONDITION,
SRV_JACSCRIPT_MANAGER,
]

const customMessages = [
Expand Down
23 changes: 18 additions & 5 deletions src/components/blockly/dsl/workers/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,26 @@ export interface WorkerResponse {
}

export class WorkerProxy extends JDEventSource {
readonly pendings: SMap<{
resolve: (res: any) => void
reject: (err: any) => void
}> = {}
readonly pendings: Record<
string,
{
resolve: (res: any) => void
reject: (err: any) => void
}
> = {}
constructor(readonly worker: Worker, readonly workerid: VMType) {
super()
this.worker.addEventListener("message", this.handleMessage.bind(this))
this.handleMessage = this.handleMessage.bind(this)
this.worker.addEventListener("message", this.handleMessage)
}

terminate() {
delete _workers[this.workerid]
this.worker.removeEventListener("message", this.handleMessage)
this.worker.terminate()
Object.values(this.pendings).forEach(({ reject }) =>
reject(new Error("worker terminated"))
)
}

private handleMessage(event: MessageEvent) {
Expand Down
12 changes: 9 additions & 3 deletions src/components/blockly/fields/ReactField.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import React from "react"
import ReactDOM from "react-dom"
import { createRoot } from "react-dom/client"
import Blockly from "blockly"
import JacdacProvider from "../../../jacdac/Provider"
import { ReactNode } from "react"
Expand Down Expand Up @@ -39,6 +39,8 @@ export default class ReactField<T> extends ReactFieldBase<T> {
SERIALIZABLE = true
public readonly events = new JDEventSource()
protected div_: Element
// React root
private root_: any
protected view: SVGElement
protected darkMode: "light" | "dark" = "dark"

Expand Down Expand Up @@ -117,7 +119,8 @@ export default class ReactField<T> extends ReactFieldBase<T> {

showEditor_() {
this.div_ = Blockly.DropDownDiv.getContentDiv()
ReactDOM.render(this.render(), this.div_)
this.root_ = createRoot(this.div_)
this.root_.render(this.render())
const border = this.sourceBlock_.getColourTertiary()
Blockly.DropDownDiv.setColour(this.sourceBlock_.getColour(), border)

Expand All @@ -139,7 +142,8 @@ export default class ReactField<T> extends ReactFieldBase<T> {
// this blows on hot reloads
try {
this.events.emit(UNMOUNT)
ReactDOM.unmountComponentAtNode(this.div_)
this.root_?.unmount()
this.root_ = undefined
} catch (e) {
console.error(e)
}
Expand Down Expand Up @@ -181,6 +185,8 @@ export default class ReactField<T> extends ReactFieldBase<T> {

dispose() {
this.view = undefined
this.root_?.unmount()
this.root_ = undefined
super.dispose()
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/components/blockly/fields/ReactInlineField.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { ReactNode } from "react"
import ReactDOM from "react-dom"
import { createRoot } from "react-dom/client"
import ReactField from "./ReactField"
import { child } from "../../widgets/svg"
import DarkModeProvider from "../../ui/DarkModeProvider"
Expand All @@ -13,6 +13,8 @@ import { SnackbarProvider } from "notistack"
export default class ReactInlineField<T = unknown> extends ReactField<T> {
protected container: HTMLDivElement
protected resizeObserver: ResizeObserver
// React root
private root_: any

// eslint-disable-next-line @typescript-eslint/no-explicit-any
constructor(options?: any) {
Expand Down Expand Up @@ -58,14 +60,16 @@ export default class ReactInlineField<T = unknown> extends ReactField<T> {
)
this.resizeObserver.observe(this.container)

ReactDOM.render(this.renderBlock(), this.container)
this.root_ = createRoot(this.container)
this.root_.render(this.renderBlock())
return fo
}

dispose() {
if (this.container) {
ReactDOM.unmountComponentAtNode(this.container)
this.root_?.unmount()
this.container = undefined
this.root_ = undefined
}
if (this.resizeObserver) {
this.resizeObserver.disconnect()
Expand Down
12 changes: 11 additions & 1 deletion src/components/dashboard/DashboardJascriptManager.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react"
import React, { ChangeEvent } from "react"
import { Alert, Grid } from "@mui/material"
// tslint:disable-next-line: no-submodule-imports match-default-export-name
import PlayArrowIcon from "@mui/icons-material/PlayArrow"
Expand Down Expand Up @@ -45,6 +45,14 @@ function JacscriptManagerToolbar(

const handleRun = () => runningRegister?.sendSetBoolAsync(true, true)
const handleStop = () => runningRegister?.sendSetBoolAsync(false, true)
const handleAutoStartChange = (
event: ChangeEvent<HTMLInputElement>,
checked: boolean
) => autoStartRegister?.sendSetBoolAsync(checked, true)
const handleLoggingChange = (
event: ChangeEvent<HTMLInputElement>,
checked: boolean
) => loggingRegister?.sendSetBoolAsync(checked, true)

return (
<Grid container spacing={1}>
Expand All @@ -61,13 +69,15 @@ function JacscriptManagerToolbar(
label="auto start"
checked={autoStart}
disabled={autoStart === undefined}
onChange={handleAutoStartChange}
/>
</Grid>
<Grid item>
<SwitchWithLabel
label="logging"
checked={logging}
disabled={logging === undefined}
onChange={handleLoggingChange}
/>
</Grid>
{noProgram && (
Expand Down
2 changes: 1 addition & 1 deletion src/components/hooks/useLocalStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import useStorage from "./useStorage"

const storage = (() => {
try {
typeof window !== "undefined" && window.localStorage
return typeof window !== "undefined" && window.localStorage
} catch {
return undefined
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/hooks/useSessionStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import useStorage from "./useStorage"

const storage = (() => {
try {
typeof window !== "undefined" && window.sessionStorage
return typeof window !== "undefined" && window.sessionStorage
} catch {
return undefined
}
Expand Down
4 changes: 1 addition & 3 deletions src/components/jacscript/JacscriptEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import {
useRegisterUnpackedValue,
} from "../../jacdac/useRegisterValue"
import DeviceAvatar from "../devices/DeviceAvatar"
import useBus from "../../jacdac/useBus"
import useServices from "../hooks/useServices"
import { jacscriptBridge } from "../blockly/dsl/workers/vm.proxy"
import { OutPipe } from "../../../jacdac-ts/src/jdom/pipes"
Expand Down Expand Up @@ -96,7 +95,6 @@ function JacscriptExecutor(props: { service: JDService }) {
function JacscriptEditorWithContext() {
const { dsls, workspaceJSON, roleManager, setWarnings } =
useContext(BlockContext)
const bus = useBus()
const [program, setProgram] = useState<VMProgram>()
const [jscProgram, setJscProgram] = useState<JacscriptProgram>()
const [jscCompiled, setJscCompiled] = useState<JacscriptCompileResponse>()
Expand All @@ -110,7 +108,7 @@ function JacscriptEditorWithContext() {

useEffect(() => {
// start the bridge (and worker)
const _bridge = jacscriptBridge()
jacscriptBridge()
// currently now way to stop it
return () => {}
}, [])
Expand Down
3 changes: 2 additions & 1 deletion src/workers/vm/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"types": [],
"module": "ESNext",
"moduleResolution": "node",
"declaration": true
"declaration": true,
"allowSyntheticDefaultImports": true
},
"exclude": ["node_modules"]
}

0 comments on commit 53bb3de

Please sign in to comment.