nsh-engine 0.8.1

Engine thread that drives the JS host and the core and emits mount batches.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
//! End-to-end hot module replacement: bundle a small app for development,
//! edit its modules and apply the updates in place.

use std::{path::Path, time::Duration};

use crossbeam_channel::{Receiver, Sender};
use nsh_core::{
    ApproxTextEngine, EventKind, EventPayload, MountBatch, MountItem, NodeId, Size, geometry::px,
};
use nsh_engine::{Bundle, Engine, EngineConfig, HotUpdate, MountSink};

struct ChannelSink(Sender<MountBatch>, Sender<()>, Sender<String>);

impl MountSink for ChannelSink {
    fn submit(&self, batch: MountBatch) {
        let _ = self.0.send(batch);
    }

    fn hot_update_failed(&self) {
        let _ = self.1.send(());
    }

    fn report_error(&self, message: &str) {
        let _ = self.2.send(message.to_owned());
    }
}

const APP: &str = r#"
import { Image, mount, signal, Text, View } from "nsh";
import { Counter } from "./Counter";
import dot from "./dot.png";

function App() {
  const n = signal(0);
  return <View><Image src={dot} /><Text onPress={() => n.set(n() + 1)}>app {n()}</Text><Counter /></View>;
}

mount(App);
"#;

const COUNTER: &str = r#"
import { signal, Text } from "nsh";
import { greeting } from "./words";

export function Counter() {
  const c = signal(0);
  return <Text onPress={() => c.set(c() + 1)}>{greeting} {c()}</Text>;
}
"#;

const WORDS: &str = r#"export const greeting = "counter";"#;

/// The text views on screen: the latest text of each, and the tree.
#[derive(Default)]
struct Screen {
    texts: std::collections::BTreeMap<NodeId, String>,
    parents: std::collections::HashMap<NodeId, NodeId>,
}

impl Screen {
    fn apply(&mut self, batch: &MountBatch) {
        for item in &batch.items {
            match item {
                MountItem::Text { id, text, .. } => {
                    self.texts.insert(*id, text.text.clone());
                }
                MountItem::Insert { parent, child, .. } => {
                    self.parents.insert(*child, *parent);
                }
                MountItem::Remove { id } | MountItem::Delete { id } => {
                    self.parents.remove(id);
                }
                _ => {}
            }
        }
    }

    /// Texts attached to the root.
    fn shown(&self) -> Vec<(NodeId, &str)> {
        let attached = |mut id: NodeId| loop {
            if id == nsh_core::ROOT_ID {
                return true;
            }
            match self.parents.get(&id) {
                Some(&p) => id = p,
                None => return false,
            }
        };
        self.texts
            .iter()
            .filter(|(id, _)| attached(**id))
            .map(|(id, t)| (*id, t.as_str()))
            .collect()
    }

    fn find(&self, prefix: &str) -> Option<NodeId> {
        self.shown()
            .into_iter()
            .find(|(_, t)| t.starts_with(prefix))
            .map(|(id, _)| id)
    }

    fn texts(&self) -> Vec<String> {
        let mut t: Vec<String> = self
            .shown()
            .into_iter()
            .map(|(_, t)| t.to_owned())
            .collect();
        t.sort();
        t
    }
}

fn drain(rx: &Receiver<MountBatch>, screen: &mut Screen) {
    while let Ok(batch) = rx.recv_timeout(Duration::from_millis(300)) {
        screen.apply(&batch);
    }
}

fn update(dir: &Path, previous: &mut nsh_bundler::HotBundle, n: u32) -> Option<HotUpdate> {
    let next =
        nsh_bundler::bundle_hot(&dir.join("App.tsx"), &[nsh_base::registry().into()]).unwrap();
    let patch = next.patch(previous);
    *previous = next;
    match patch {
        nsh_bundler::HotPatch::Update { code, source_map } => Some(HotUpdate {
            name: format!("hot-{n}.js"),
            code,
            source_map: source_map.map(Into::into),
        }),
        other => panic!("expected an update, got {other:?}"),
    }
}

#[test]
fn edits_replace_modules_and_keep_unrelated_state() {
    let dir = std::env::temp_dir().join(format!("nsh-hot-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    std::fs::write(dir.join("App.tsx"), APP).unwrap();
    std::fs::write(dir.join("Counter.tsx"), COUNTER).unwrap();
    std::fs::write(dir.join("words.ts"), WORDS).unwrap();
    // A 1x1 PNG: asset modules are part of development bundles too.
    std::fs::write(
        dir.join("dot.png"),
        [
            0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0, 0, 0, 0x0d, 0x49, 0x48, 0x44, 0x52,
            0, 0, 0, 1, 0, 0, 0, 1, 8, 6, 0, 0, 0, 0x1f, 0x15, 0xc4, 0x89, 0, 0, 0, 0x0d, 0x49,
            0x44, 0x41, 0x54, 0x78, 0x9c, 0x63, 0, 1, 0, 0, 5, 0, 1, 0x0d, 0x0a, 0x2d, 0xb4, 0, 0,
            0, 0, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82,
        ],
    )
    .unwrap();

    let mut bundle =
        nsh_bundler::bundle_hot(&dir.join("App.tsx"), &[nsh_base::registry().into()]).unwrap();
    let (code, map) = bundle.script();
    // As `nsh dev` compiles it: without the functions' source text.
    let bytecode =
        nsh_js::compile_bytecode_with(&code, nsh_js::Strip::Source, &[nsh_base::registry().into()])
            .unwrap();
    let (tx, rx) = crossbeam_channel::unbounded();
    let (failed_tx, failed) = crossbeam_channel::unbounded();
    let (errors_tx, errors) = crossbeam_channel::unbounded();
    let config = EngineConfig {
        // SAFETY: compiled just now by this runtime.
        app: nsh_engine::App::Js(
            unsafe { Bundle::bytecode(bytecode) }.with_source_map(map.unwrap()),
        ),
        viewport: Size::new(px(390.0), px(844.0)),
        trace: false,
        content_culling: false,
        network: None,
        websocket: None,
        refuse_connections: None,
        filesystem: None,
        assets: None,
        environment: Default::default(),
        header_heights: (nsh_core::Points(44.0), nsh_core::Points(96.0)),
        limits: nsh_engine::Limits::default(),
        ui_sets: Vec::new(),
        runtime: None,
        support_dir: None,
        cache: nsh_engine::CacheOptions::off(),
        storage: None,
        clock: Default::default(),
        context: Default::default(),
        pinned: Default::default(),
        clipboard: None,
        host: Default::default(),
        picker: None,
        audio: None,
        windows: None,
        login_item: None,
        data_dir: None,
    };
    let engine = Engine::start(
        config,
        || ApproxTextEngine,
        ChannelSink(tx, failed_tx, errors_tx),
    )
    .unwrap();
    let control = engine.control();
    let mut screen = Screen::default();
    drain(&rx, &mut screen);
    assert_eq!(screen.texts(), ["app 0", "counter 0"]);

    // Some state in both components.
    let press = |id| control.dispatch_event(id, EventKind::Press, EventPayload::None);
    press(screen.find("app").unwrap());
    press(screen.find("app").unwrap());
    press(screen.find("counter").unwrap());
    drain(&rx, &mut screen);
    assert_eq!(screen.texts(), ["app 2", "counter 1"]);

    // A component edit: only that component renders again.
    std::fs::write(
        dir.join("Counter.tsx"),
        COUNTER.replace("{greeting} {c()}", "{greeting}: {c()}"),
    )
    .unwrap();
    control.hot_update(update(&dir, &mut bundle, 1).unwrap());
    drain(&rx, &mut screen);
    assert_eq!(screen.texts(), ["app 2", "counter: 0"]);

    // A plain module edit reaches the components that import it.
    press(screen.find("counter").unwrap());
    std::fs::write(dir.join("words.ts"), WORDS.replace("counter", "taps")).unwrap();
    control.hot_update(update(&dir, &mut bundle, 2).unwrap());
    drain(&rx, &mut screen);
    assert_eq!(screen.texts(), ["app 2", "taps: 0"]);

    // An edit of the root component renders it again, and what it renders:
    // their own state starts afresh.
    std::fs::write(dir.join("App.tsx"), APP.replace("app {n()}", "app! {n()}")).unwrap();
    control.hot_update(update(&dir, &mut bundle, 3).unwrap());
    drain(&rx, &mut screen);
    assert_eq!(screen.texts(), ["app! 0", "taps: 0"]);

    // A component that throws reports the error and renders nothing; the
    // fix renders it again.
    let counter = COUNTER.replace("{greeting} {c()}", "{greeting}: {c()}");
    let broken = counter.replace(
        "const c = signal(0);",
        "const c = signal(0);\n  if (c() === 0) throw new Error(\"broken counter\");",
    );
    std::fs::write(dir.join("Counter.tsx"), &broken).unwrap();
    control.hot_update(update(&dir, &mut bundle, 4).unwrap());
    drain(&rx, &mut screen);
    assert_eq!(screen.texts(), ["app! 0"]);
    let error = errors.try_recv().expect("the error is reported");
    assert!(error.contains("broken counter"), "{error}");
    assert!(error.contains("Counter.tsx:"), "not symbolicated: {error}");
    std::fs::write(dir.join("Counter.tsx"), &counter).unwrap();
    control.hot_update(update(&dir, &mut bundle, 5).unwrap());
    drain(&rx, &mut screen);
    assert_eq!(screen.texts(), ["app! 0", "taps: 0"]);
    assert!(failed.try_recv().is_err(), "no update needed a reload");

    // A native module the running bundle does not import: a reload.
    std::fs::write(
        dir.join("Counter.tsx"),
        format!(
            "import {{ isAvailable }} from \"nsh/desktop\";\nconsole.log(isAvailable);\n{counter}"
        ),
    )
    .unwrap();
    let next =
        nsh_bundler::bundle_hot(&dir.join("App.tsx"), &[nsh_base::registry().into()]).unwrap();
    assert!(matches!(next.patch(&bundle), nsh_bundler::HotPatch::Reload));
    let _ = std::fs::remove_dir_all(&dir);
}

const NAV_APP: &str = r#"
import { mount, signal, Text, View } from "nsh";
import { NavPage, NavStack, createNavStack } from "nsh/base";

type Route = { name: string };
const nav = createNavStack<Route>([{ name: "home" }]);
const taps = signal(0);

function Label(props: { route: string }) {
  return <Text>label {props.route}</Text>;
}

function Local() {
  const c = signal(0);
  return <Text onPress={() => c.set(c() + 1)}>local {c()}</Text>;
}

function App() {
  return (
    <View style={{ flex: 1 }}>
    <Local />
    <NavStack state={nav} style={{ flex: 1 }}>
      {(route) => (
        <NavPage title={route.name}>
          <View>
            <Text onPress={() => nav.push({ name: `page${nav.routes().length}` })}>push {route.name}</Text>
            <Text onPress={() => taps.set(taps() + 1)}>taps {taps()}</Text>
            <Label route={route.name} />
          </View>
        </NavPage>
      )}
    </NavStack>
    </View>
  );
}

MOUNT
"#;

/// Edits of the entry keep the mounted tree, the top-level signals and the
/// navigator's screens, with `mount(App)` and with an inline function.
#[test]
fn entry_edits_keep_navigation_and_state() {
    for (n, mount) in ["mount(App);", "mount(() => <App />);"]
        .into_iter()
        .enumerate()
    {
        let app = NAV_APP.replace("MOUNT", mount);
        let dir = std::env::temp_dir().join(format!("nsh-hot-nav-{}-{n}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(dir.join("App.tsx"), &app).unwrap();
        let sets = [nsh_base::registry()];
        let described: Vec<_> = sets.iter().map(Into::into).collect();
        let mut bundle = nsh_bundler::bundle_hot(&dir.join("App.tsx"), &described).unwrap();
        let (code, map) = bundle.script();
        let (tx, rx) = crossbeam_channel::unbounded();
        let (failed_tx, failed) = crossbeam_channel::unbounded();
        let (errors_tx, errors) = crossbeam_channel::unbounded();
        let config = EngineConfig {
            app: nsh_engine::App::Js(Bundle::source(code).with_source_map(map.unwrap())),
            viewport: Size::new(px(390.0), px(844.0)),
            trace: false,
            content_culling: false,
            network: None,
            websocket: None,
            refuse_connections: None,
            filesystem: None,
            assets: None,
            environment: Default::default(),
            header_heights: (nsh_core::Points(44.0), nsh_core::Points(96.0)),
            limits: nsh_engine::Limits::default(),
            ui_sets: sets.to_vec(),
            runtime: None,
            support_dir: None,
            cache: nsh_engine::CacheOptions::off(),
            storage: None,
            clock: Default::default(),
            context: Default::default(),
            pinned: Default::default(),
            clipboard: None,
            host: Default::default(),
            picker: None,
            audio: None,
            windows: None,
            login_item: None,
            data_dir: None,
        };
        let engine = Engine::start(
            config,
            || ApproxTextEngine,
            ChannelSink(tx, failed_tx, errors_tx),
        )
        .unwrap();
        let control = engine.control();
        let mut screen = Screen::default();
        drain(&rx, &mut screen);
        assert_eq!(
            screen.texts(),
            ["label home", "local 0", "push home", "taps 0"],
            "{mount}"
        );

        // A pushed screen and a top-level signal's state.
        let press = |id| control.dispatch_event(id, EventKind::Press, EventPayload::None);
        press(screen.find("push home").unwrap());
        drain(&rx, &mut screen);
        press(screen.find("taps").unwrap());
        press(screen.find("local").unwrap());
        drain(&rx, &mut screen);
        let state = [
            "label home",
            "label page1",
            "local 1",
            "push home",
            "push page1",
            "taps 1",
            "taps 1",
        ];
        assert_eq!(screen.texts(), state, "{mount}");

        // An edit of another component of the entry: only it renders again
        // (the state of the others stays).
        let edited = app.replace("label {props.route}", "label: {props.route}");
        std::fs::write(dir.join("App.tsx"), &edited).unwrap();
        control.hot_update(update(&dir, &mut bundle, 1).unwrap());
        drain(&rx, &mut screen);
        let labelled = state.map(|t| t.replace("label ", "label: "));
        assert_eq!(screen.texts(), labelled, "{mount}");

        // An edit of the root component renders the tree again (its
        // components' own state starts afresh), from the kept navigator and
        // signal: the pushed screen stays.
        let edited = edited.replace(
            "<NavStack state={nav} style={{ flex: 1 }}>",
            "<NavStack state={nav} style={{ flex: 1, gap: 1 }}>",
        );
        std::fs::write(dir.join("App.tsx"), &edited).unwrap();
        control.hot_update(update(&dir, &mut bundle, 2).unwrap());
        drain(&rx, &mut screen);
        let fresh = labelled.map(|t| t.replace("local 1", "local 0"));
        assert_eq!(screen.texts(), fresh, "{mount}");

        // A changed initializer starts afresh.
        let edited = edited.replace("signal(0)", "signal(10)");
        std::fs::write(dir.join("App.tsx"), &edited).unwrap();
        control.hot_update(update(&dir, &mut bundle, 3).unwrap());
        drain(&rx, &mut screen);
        assert!(
            screen.texts().contains(&"taps 10".to_owned()),
            "{:?}",
            screen.texts()
        );
        assert!(
            screen.texts().contains(&"push page1".to_owned()),
            "{:?}",
            screen.texts()
        );

        assert!(failed.try_recv().is_err(), "no update needed a reload");
        assert!(errors.try_recv().is_err(), "no errors");
        let _ = std::fs::remove_dir_all(&dir);
    }
}

const NESTED_NAV_APP: &str = r#"
import { mount, Text, View } from "nsh";
import { NavPage, NavStack, createNavStack } from "nsh/base";

const nav = createNavStack<{ name: string }>([{ name: "home" }]);

function Shell(props: { title: string; children: JSX.Element }) {
  return <NavPage title={props.title}><View>{props.children}</View></NavPage>;
}

function HomePage() {
  return <Shell title="Home"><Text>home page</Text></Shell>;
}

function App() {
  return (
    <NavStack state={nav} style={{ flex: 1 }}>
      {() => <HomePage />}
    </NavStack>
  );
}

mount(App);
"#;

/// A route's page made by a component that renders another (each a hot
/// proxy, an accessor around the next) is still the stack's own screen:
/// the stack unwraps every accessor down to the `<NavPage>`, rather than
/// putting a screen in a screen, which no platform shows.
#[test]
fn nested_hot_components_render_the_stacks_screen() {
    let dir = std::env::temp_dir().join(format!("nsh-hot-nested-nav-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    std::fs::write(dir.join("App.tsx"), NESTED_NAV_APP).unwrap();
    let sets = [nsh_base::registry()];
    let described: Vec<_> = sets.iter().map(Into::into).collect();
    let mut bundle = nsh_bundler::bundle_hot(&dir.join("App.tsx"), &described).unwrap();
    let (code, map) = bundle.script();
    let (tx, rx) = crossbeam_channel::unbounded();
    let (failed_tx, _failed) = crossbeam_channel::unbounded();
    let (errors_tx, errors) = crossbeam_channel::unbounded();
    let config = EngineConfig {
        app: nsh_engine::App::Js(Bundle::source(code).with_source_map(map.unwrap())),
        viewport: Size::new(px(390.0), px(844.0)),
        trace: false,
        content_culling: false,
        network: None,
        websocket: None,
        refuse_connections: None,
        filesystem: None,
        assets: None,
        environment: Default::default(),
        header_heights: (nsh_core::Points(44.0), nsh_core::Points(96.0)),
        limits: nsh_engine::Limits::default(),
        ui_sets: sets.to_vec(),
        runtime: None,
        support_dir: None,
        cache: nsh_engine::CacheOptions::off(),
        storage: None,
        clock: Default::default(),
        context: Default::default(),
        pinned: Default::default(),
        clipboard: None,
        host: Default::default(),
        picker: None,
        audio: None,
        windows: None,
        login_item: None,
        data_dir: None,
    };
    let engine = Engine::start(
        config,
        || ApproxTextEngine,
        ChannelSink(tx, failed_tx, errors_tx),
    )
    .unwrap();
    let mut kinds = std::collections::HashMap::new();
    let mut parents = std::collections::HashMap::new();
    let mut screen = Screen::default();
    while let Ok(batch) = rx.recv_timeout(Duration::from_millis(300)) {
        screen.apply(&batch);
        for item in &batch.items {
            match item {
                MountItem::Create { id, kind } => {
                    kinds.insert(*id, *kind);
                }
                MountItem::Insert { parent, child, .. } => {
                    parents.insert(*child, *parent);
                }
                _ => {}
            }
        }
    }
    assert_eq!(screen.texts(), ["home page"]);
    let screens: Vec<_> = kinds
        .iter()
        .filter(|(_, k)| **k == nsh_core::ViewKind::Screen)
        .map(|(id, _)| *id)
        .collect();
    assert_eq!(screens.len(), 1, "one screen, not one in another");
    assert_eq!(
        parents.get(&screens[0]).and_then(|p| kinds.get(p)),
        Some(&nsh_core::ViewKind::Stack),
        "the page's screen is the stack's child"
    );

    // An edit of the inner component renders the screen again.
    let edited = NESTED_NAV_APP.replace(
        "<View>{props.children}</View>",
        "<View><Text>shell</Text>{props.children}</View>",
    );
    std::fs::write(dir.join("App.tsx"), edited).unwrap();
    engine
        .control()
        .hot_update(update(&dir, &mut bundle, 1).unwrap());
    drain(&rx, &mut screen);
    assert_eq!(screen.texts(), ["home page", "shell"]);
    assert!(errors.try_recv().is_err(), "no errors");
    let _ = std::fs::remove_dir_all(&dir);
}