Skip to content

Commit

Permalink
add support for "specs" message
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Feb 21, 2023
1 parent 729f98f commit c5574e6
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
2 changes: 1 addition & 1 deletion jacdac-ts
2 changes: 1 addition & 1 deletion pxt-jacdac
Submodule pxt-jacdac updated 101 files
22 changes: 16 additions & 6 deletions src/components/ServiceManagerContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import { JSONTryParse } from "../../jacdac-ts/src/jdom/utils"
import {
BrowserFileStorage,
HostedFileStorage,
IFileStorage,
FileStorage,
} from "../../jacdac-ts/src/embed/filestorage"
import { IThemeMessage } from "../../jacdac-ts/src/embed/protocol"
import {
EmbedMessage,
EmbedSpecsMessage,
EmbedThemeMessage,
} from "../../jacdac-ts/src/embed/protocol"
import {
ModelStore,
HostedModelStore,
Expand Down Expand Up @@ -50,7 +54,7 @@ export class LocalStorageSettings implements ISettings {

export interface ServiceManagerContextProps {
isHosted: boolean
fileStorage: IFileStorage
fileStorage: FileStorage
modelStore: ModelStore
}

Expand All @@ -71,15 +75,21 @@ export const ServiceManagerProvider = ({ children }) => {

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const handleMessage = useCallback(
(ev: MessageEvent<any>) => {
(ev: MessageEvent<EmbedMessage>) => {
const msg = ev.data
if (msg?.source !== "jacdac") return
switch (msg.type) {
case "theme": {
const themeMsg = msg as IThemeMessage
const themeMsg = msg as EmbedThemeMessage
toggleDarkMode(themeMsg.data.type)
break
}
case "specs": {
const specMsg = msg as EmbedSpecsMessage
const { services } = specMsg.data
bus.setCustomServiceSpecifications(services)
break
}
}
},
[toggleDarkMode]
Expand All @@ -94,7 +104,7 @@ export const ServiceManagerProvider = ({ children }) => {

function createProps(): ServiceManagerContextProps {
const isHosted = UIFlags.hosted
let fileStorage: IFileStorage = new BrowserFileStorage()
let fileStorage: FileStorage = new BrowserFileStorage()
let modelStore: ModelStore = undefined
if (isHosted) {
console.log(`starting hosted services`)
Expand Down
17 changes: 15 additions & 2 deletions src/components/tools/ServiceSpecificationEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React, { useMemo } from "react"
import React, { useEffect, useMemo } from "react"
import { Grid } from "@mui/material"
import { parseServiceSpecificationMarkdownToJSON } from "../../../jacdac-ts/jacdac-spec/spectool/jdspec"
import { serviceMap } from "../../../jacdac-ts/src/jdom/spec"
import RandomGenerator from "../RandomGenerator"
import useLocalStorage from "../hooks/useLocalStorage"
import HighlightTextField from "../ui/HighlightTextField"
import ServiceSpecification from "../specification/ServiceSpecification"
import ServiceSpecificationSource from "../specification/ServiceSpecificationSource"
import { EmbedSpecsMessage } from "../../../jacdac-ts/src/embed/protocol"

const SERVICE_SPECIFICATION_STORAGE_KEY =
"jacdac:servicespecificationeditorsource"
Expand All @@ -27,6 +27,19 @@ export default function ServiceSpecificationEditor() {
json.shortId ||
`0x${json.classIdentifier.toString(16)}`
).toLowerCase()}`

useEffect(() => {
const services: jdspec.ServiceSpec[] =
json && !json.errors?.length ? [json] : []
window.postMessage({
source: "jacdac",
type: "specs",
data: {
services,
},
} as EmbedSpecsMessage)
}, [json])

return (
<Grid spacing={2} container>
<Grid item xs={12}>
Expand Down
6 changes: 3 additions & 3 deletions src/pages/tools/model-uploader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
import SensorAggregatorConfigView from "../../components/SensorAggregatorConfigView"
import ServiceManagerContext from "../../components/ServiceManagerContext"
import useChange from "../../jacdac/useChange"
import { IFile } from "../../../jacdac-ts/src/embed/protocol"
import { EmbedFile } from "../../../jacdac-ts/src/embed/protocol"
import { prettySize } from "../../../jacdac-ts/src/jdom/pretty"
import RegisterTrend from "../../components/RegisterTrend"
import { useRegisterUnpackedValue } from "../../jacdac/useRegisterValue"
Expand Down Expand Up @@ -149,7 +149,7 @@ export default function ModelUploader() {
setImporting(false)
}
}
const handleLoadModel = (model: IFile) => async () => {
const handleLoadModel = (model: EmbedFile) => async () => {
try {
setImporting(true)
console.log(`loading model`, model)
Expand All @@ -162,7 +162,7 @@ export default function ModelUploader() {
setImporting(false)
}
}
const handleLoadInputConfiguration = (model: IFile) => async () => {
const handleLoadInputConfiguration = (model: EmbedFile) => async () => {
try {
setImporting(true)
console.log(`loading model`, model)
Expand Down

0 comments on commit c5574e6

Please sign in to comment.