Skip to content

Commit 28eeaaf

Browse files
committed
Fix incorrect link names
1 parent 08d267e commit 28eeaaf

File tree

4 files changed

+27
-54
lines changed

4 files changed

+27
-54
lines changed

mindy-website/src/lib.rs

Lines changed: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(clippy::boxed_local)]
22

3-
use std::{collections::HashMap, time::Duration};
3+
use std::time::Duration;
44

55
use embedded_graphics::pixelcolor::Rgb888;
66
use embedded_graphics_web_simulator::{
@@ -36,8 +36,8 @@ pub fn init_logging() {
3636
console_error_panic_hook::set_once();
3737
}
3838

39-
fn pack_point(x: i16, y: i16) -> u32 {
40-
((y as u32) << 16) | (x as u32)
39+
fn pack_point(position: PackedPoint2) -> u32 {
40+
((position.y as u32) << 16) | (position.x as u32)
4141
}
4242

4343
fn unpack_point(position: u32) -> PackedPoint2 {
@@ -153,7 +153,7 @@ impl WebLogicVM {
153153
position: u32,
154154
code: &str,
155155
links: Box<[u32]>,
156-
) -> Result<LinkNames, String> {
156+
) -> Result<js_sys::Map, String> {
157157
let ast = self.logic_parser.parse(code).map_err(|e| e.to_string())?;
158158

159159
let position = unpack_point(position);
@@ -187,19 +187,14 @@ impl WebLogicVM {
187187
)
188188
.map_err(|e| e.to_string())?;
189189

190-
Ok(LinkNames(
191-
processor
192-
.state
193-
.links()
194-
.iter()
195-
.map(|l| {
196-
(
197-
pack_point(l.building.position.x, l.building.position.y),
198-
l.name.clone(),
199-
)
200-
})
201-
.collect(),
202-
))
190+
let names = js_sys::Map::new();
191+
for link in processor.state.links() {
192+
names.set(
193+
&pack_point(link.building.position).into(),
194+
&link.name.clone().into(),
195+
);
196+
}
197+
Ok(names)
203198
}
204199

205200
pub fn remove_building(&mut self, position: u32) {
@@ -212,23 +207,6 @@ impl WebLogicVM {
212207
.map(|b| JsString::from_char_code(b.block.name.as_u16str().as_slice()))
213208
}
214209

215-
pub fn processor_links(&self, position: u32) -> Option<Vec<JsString>> {
216-
if let Some(building) = self.vm.building(unpack_point(position))
217-
&& let BuildingData::Processor(processor) = &*building.data.borrow()
218-
{
219-
Some(
220-
processor
221-
.state
222-
.links()
223-
.iter()
224-
.map(|l| JsString::from(l.name.as_str()))
225-
.collect(),
226-
)
227-
} else {
228-
None
229-
}
230-
}
231-
232210
pub fn set_target_fps(&mut self, target_fps: f64) {
233211
self.delta = fps_to_delta(target_fps);
234212
self.tick_secs = delta_to_time(self.delta);
@@ -284,14 +262,3 @@ impl DisplayKind {
284262
}
285263
}
286264
}
287-
288-
#[wasm_bindgen]
289-
pub struct LinkNames(HashMap<u32, String>);
290-
291-
#[wasm_bindgen]
292-
impl LinkNames {
293-
#[wasm_bindgen(indexing_getter)]
294-
pub fn get(&self, position: u32) -> Option<String> {
295-
self.0.get(&position).cloned()
296-
}
297-
}

mindy-website/www/src/components/LogicVMFlow.tsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,17 @@ export default function LogicVMFlow() {
156156
nodeId: node.id,
157157
});
158158

159-
// TODO: is this order reliable?
160-
response.links.forEach((label, i) => {
161-
reactFlow.updateEdge(connections[i].edgeId, {
162-
label,
163-
});
164-
});
159+
// FIXME: assumes no links are removed by the VM
160+
for (const connection of connections) {
161+
const target = reactFlow.getNode(connection.target);
162+
if (target != null) {
163+
reactFlow.updateEdge(connection.edgeId, {
164+
label: response.links.get(
165+
target.data.position,
166+
),
167+
});
168+
}
169+
}
165170
}
166171

167172
break;

mindy-website/www/src/workers/vm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ interface BuildingAddedResponse {
5757
interface ProcessorCodeSetResponse {
5858
type: "processorCodeSet";
5959
position: number;
60-
links?: string[];
60+
links?: Map<number, string>;
6161
error?: string;
6262
}
6363

mindy-website/www/src/workers/vm.worker.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,16 @@ void init().then(() => {
4545
const { position, code, links } = request;
4646

4747
let error = undefined;
48+
let linkNames = undefined;
4849
try {
49-
vm.set_processor_config(position, code, links);
50+
linkNames = vm.set_processor_config(position, code, links);
5051
} catch (e: unknown) {
5152
error = String(e);
5253
}
5354
postMessage({
5455
type: "processorCodeSet",
5556
position,
56-
links: vm.processor_links(position),
57+
links: linkNames,
5758
error,
5859
});
5960

0 commit comments

Comments
 (0)