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
//! A manual clock (`EngineConfig::clock`): the app's time moves only when
//! the engine is told to move it, and what comes due on the way runs in
//! order, each at its time; `Date` and `performance.now()` follow it.

use std::{
    sync::{Arc, Mutex},
    time::Duration,
};

use crossbeam_channel::Receiver;
use nsh_core::{ApproxTextEngine, MountBatch, MountItem, element::Root, prelude::*, time::Clock};
use nsh_engine::{
    App, Bundle, Engine, EngineConfig, FetchEvent, FetchEvents, FetchTask, HttpHead, HttpRequest,
    Network,
};

/// Sends the batches, and fails on the app's errors.
struct Sink(crossbeam_channel::Sender<MountBatch>);

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

    fn report_error(&self, message: &str) {
        panic!("the app failed: {message}");
    }

    fn report(&self, error: &nsh_engine::AppError) {
        panic!("the app failed: {error:?}");
    }
}

/// The time the clock starts at: 2026-01-01T00:00:00Z.
fn start_time() -> std::time::SystemTime {
    std::time::SystemTime::UNIX_EPOCH + Duration::from_millis(1_767_225_600_000)
}

const APP: &str = r#"import { mount, signal, Text } from "nsh";

const log = signal("");
const add = (what) => log.set(`${log()} ${what}@${performance.now()}`);
const started = new Date();
setTimeout(() => add("b"), 300);
setTimeout(() => {
  add("a");
  // What a timer schedules on the way comes due in the same move.
  setTimeout(() => add("c"), 150);
}, 100);
const ticks = signal(0);
const interval = setInterval(() => {
  ticks.set(ticks() + 1);
  if (ticks() === 3) clearInterval(interval);
}, 1000);
mount(() => (
  <Text>{`${started.toISOString()} ${Date.now() - started.getTime()} ${new Date() instanceof Date} ${typeof Date()} ticks ${ticks()}|${log()}`}</Text>
));
"#;

/// A Rust root whose task and interval wait on the clock.
#[component]
fn Waiting() -> impl IntoElement {
    let done = signal("waiting");
    let ticks = signal(0);
    spawn(async move {
        timer(Duration::from_millis(500)).await;
        done.set("done");
    });
    let interval = set_interval(Duration::from_millis(200), move || {
        ticks.update(|t| *t += 1)
    });
    std::mem::forget(interval);
    text(move || format!("{} {}", done.get(), ticks.get()))
}

fn start(clock: Clock) -> (Engine, Receiver<MountBatch>) {
    start_app("nsh-clock", APP, clock, None)
}

/// Starts the app `source` (written to a directory named after `name`) on
/// `clock`, with `network`.
fn start_app(
    name: &str,
    source: &str,
    clock: Clock,
    network: Option<Arc<dyn Network>>,
) -> (Engine, Receiver<MountBatch>) {
    let dir = crate::temporary_root().join(format!("{name}-{}", std::process::id()));
    std::fs::create_dir_all(&dir).unwrap();
    let entry = dir.join("App.tsx");
    std::fs::write(&entry, source).unwrap();
    let bundle = nsh_bundler::bundle(&nsh_bundler::BundleOptions {
        entry,
        minify: false,
        split: false,
        ui_sets: Vec::new(),
        ..Default::default()
    })
    .unwrap();
    let bytecode = nsh_js::compile_bytecode(&bundle.code, false).unwrap();
    let config = EngineConfig {
        clock,
        network,
        // SAFETY: compiled just now by this runtime.
        ..EngineConfig::new(nsh_engine::App::Js(unsafe { Bundle::bytecode(bytecode) }))
    };
    let (tx, rx) = crossbeam_channel::unbounded();
    (
        Engine::start(config, || ApproxTextEngine, Sink(tx)).unwrap(),
        rx,
    )
}

/// The last text the batches so far showed.
fn shown(rx: &Receiver<MountBatch>, last: &mut String) -> String {
    while let Ok(batch) = rx.try_recv() {
        for item in &batch.items {
            if let MountItem::Text { text, .. } = item {
                last.clone_from(&text.text);
            }
        }
    }
    last.clone()
}

/// Moves `engine`'s clock by `ms` and waits until it got there.
fn advance(engine: &Engine, ms: u64) {
    let (tx, rx) = crossbeam_channel::bounded(1);
    engine
        .control()
        .advance_clock(Duration::from_millis(ms), move || {
            let _ = tx.send(());
        });
    rx.recv_timeout(Duration::from_secs(10)).unwrap();
}

#[test]
fn a_manual_clock_moves_only_when_told_and_runs_what_comes_due_in_order() {
    let (engine, rx) = start(Clock::manual(start_time()));
    let mut last = String::new();
    advance(&engine, 0);
    assert_eq!(
        shown(&rx, &mut last),
        "2026-01-01T00:00:00.000Z 0 true string ticks 0|"
    );
    // Time stands still on its own.
    std::thread::sleep(Duration::from_millis(400));
    advance(&engine, 0);
    assert_eq!(
        shown(&rx, &mut last),
        "2026-01-01T00:00:00.000Z 0 true string ticks 0|"
    );
    // Each timer runs at its time, the one `a` scheduled included (the
    // text renders as `c` runs, at 250 ms).
    advance(&engine, 260);
    assert_eq!(
        shown(&rx, &mut last),
        "2026-01-01T00:00:00.000Z 250 true string ticks 0| a@100 c@250"
    );
    // The interval ticks at 1, 2 and 3 s, then stops.
    advance(&engine, 5000);
    assert_eq!(
        shown(&rx, &mut last),
        "2026-01-01T00:00:00.000Z 3000 true string ticks 3| a@100 c@250 b@300"
    );
}

/// Runs what was sent to `engine` and is due now: whether it is idle.
fn flush(engine: &Engine) -> bool {
    let (tx, rx) = crossbeam_channel::bounded(1);
    engine.control().flush(move |idle| {
        let _ = tx.send(idle);
    });
    rx.recv_timeout(Duration::from_secs(10)).unwrap()
}

/// Answers `/now` as it is asked, and holds the other requests until the
/// test answers them.
#[derive(Default)]
struct HeldNetwork(Mutex<Vec<FetchEvents>>);

struct Held;

impl FetchTask for Held {
    fn send(&mut self, _: Vec<u8>) {}
    fn finish(&mut self) {}
    fn pause(&mut self, _: bool) {}
    fn cancel(self: Box<Self>) {}
}

impl Network for HeldNetwork {
    fn fetch(&self, request: HttpRequest, mut events: FetchEvents) -> Box<dyn FetchTask> {
        if request.url.ends_with("/now") {
            events(FetchEvent::Head(HttpHead {
                status: 200,
                ..HttpHead::default()
            }));
            events(FetchEvent::Data(b"here".to_vec()));
            events(FetchEvent::End);
        } else {
            self.0.lock().unwrap().push(events);
        }
        Box::new(Held)
    }
}

const FLUSHED: &str = r#"import { mount, signal, Text } from "nsh";

const log = signal("");
const add = (what) => log.set(`${log()} ${what}@${performance.now()}`);
// Due now, a turn after another: a microtask, then a timer that sets one.
Promise.resolve().then(() => setTimeout(() => setTimeout(() => add("now"), 0), 0));
setTimeout(() => {
  add("later");
  fetch("https://example.com/held").then(() => {
    add("answered");
    // Answered as it is asked, at the end of the turn that asked.
    return fetch("https://example.com/now")
      .then((r) => r.text())
      .then(add);
  });
}, 100);
mount(() => <Text>{`log:${log()}`}</Text>);
"#;

#[test]
fn a_flush_runs_what_is_due_now_and_says_whether_the_engine_is_idle() {
    let network = Arc::new(HeldNetwork::default());
    let (engine, rx) = start_app(
        "nsh-clock-flush",
        FLUSHED,
        Clock::manual(start_time()),
        Some(network.clone()),
    );
    let mut last = String::new();
    // The first render, and the work due at its time, with the clock
    // where it was.
    assert!(flush(&engine));
    assert_eq!(shown(&rx, &mut last), "log: now@0");
    // A request in flight: its answer comes from elsewhere.
    advance(&engine, 100);
    assert!(!flush(&engine));
    assert_eq!(shown(&rx, &mut last), "log: now@0 later@100");
    let mut events = network.0.lock().unwrap().pop().unwrap();
    events(FetchEvent::Head(HttpHead {
        status: 200,
        ..HttpHead::default()
    }));
    events(FetchEvent::End);
    assert!(flush(&engine));
    assert_eq!(
        shown(&rx, &mut last),
        "log: now@0 later@100 answered@100 here@100"
    );
}

#[test]
fn the_system_clock_ignores_moves() {
    let (engine, rx) = start(Clock::system());
    let mut last = String::new();
    advance(&engine, 10_000);
    let text = shown(&rx, &mut last);
    assert!(text.ends_with("ticks 0|"), "{text}");
}

#[test]
fn rust_tasks_wait_on_the_manual_clock_too() {
    let config = EngineConfig {
        clock: Clock::manual(start_time()),
        ..EngineConfig::new(App::Rust(Root::new::<Waiting>(|| ())))
    };
    let (tx, rx) = crossbeam_channel::unbounded();
    let engine = Engine::start(config, || ApproxTextEngine, Sink(tx)).unwrap();
    let mut last = String::new();
    advance(&engine, 0);
    std::thread::sleep(Duration::from_millis(300));
    advance(&engine, 0);
    assert_eq!(shown(&rx, &mut last), "waiting 0");
    advance(&engine, 450);
    assert_eq!(shown(&rx, &mut last), "waiting 2");
    advance(&engine, 50);
    assert_eq!(shown(&rx, &mut last), "done 2");
}

const FRAMES: &str = r#"import { mount, signal, Text } from "nsh";

const frames = signal(0);
const at = signal(0);
const loop = (time) => {
  frames.set(frames() + 1);
  at.set(Math.round(time));
  requestAnimationFrame(loop);
};
requestAnimationFrame(loop);
mount(() => <Text>{`${frames()} frames, the last at ${at()}`}</Text>);
"#;

#[test]
fn a_clock_that_moves_with_frames_runs_them_at_the_display_pace() {
    let dir = crate::temporary_root().join(format!("nsh-clock-frames-{}", std::process::id()));
    std::fs::create_dir_all(&dir).unwrap();
    let entry = dir.join("App.tsx");
    std::fs::write(&entry, FRAMES).unwrap();
    let bundle = nsh_bundler::bundle(&nsh_bundler::BundleOptions {
        entry,
        minify: false,
        split: false,
        ui_sets: Vec::new(),
        ..Default::default()
    })
    .unwrap();
    let bytecode = nsh_js::compile_bytecode(&bundle.code, false).unwrap();
    let config = EngineConfig {
        clock: Clock::manual(start_time()).with_frames(),
        // SAFETY: compiled just now by this runtime.
        ..EngineConfig::new(nsh_engine::App::Js(unsafe { Bundle::bytecode(bytecode) }))
    };
    let (tx, rx) = crossbeam_channel::unbounded();
    let engine = Engine::start(config, || ApproxTextEngine, Sink(tx)).unwrap();
    let mut last = String::new();
    // A second of the app's time: a frame every 1/60 s on the way, as
    // a headless run's display gives them.
    advance(&engine, 1000);
    assert_eq!(shown(&rx, &mut last), "59 frames, the last at 983");
    // The platform's display frame comes a frame after the last: the
    // clock moves to its time first.
    engine.control().frame();
    advance(&engine, 0);
    assert_eq!(shown(&rx, &mut last), "60 frames, the last at 1000");
}

/// A Rust root with nothing to wake the engine after its first frame.
#[component]
fn Still() -> impl IntoElement {
    view().flex_1().child(text("still"))
}

/// What the engine handed a [`Reports`] sink.
#[derive(Debug)]
enum Sent {
    Batch(MountBatch),
    Resized(Size<Points>),
}

/// Hands batches and resize reports to the test, in order.
struct Reports(crossbeam_channel::Sender<Sent>);

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

    fn resized(&self, size: Size<Points>) {
        let _ = self.0.send(Sent::Resized(size));
    }
}

#[test]
fn a_resize_sent_before_the_first_frame_is_reported_with_it() {
    // On a manual clock a Rust app has no timer, collection or resize
    // burst to end: after its first frame nothing wakes the engine, so the
    // report cannot ride on a later turn.
    let config = EngineConfig {
        clock: Clock::manual(start_time()),
        ..EngineConfig::new(App::Rust(Root::new::<Still>(|| ())))
    };
    let size = Size::new(px(500.0), px(700.0));
    let (tx, rx) = crossbeam_channel::unbounded();
    // Sent before the engine starts, the resize is handled while the app
    // starts, as one that comes while a slow app starts is.
    let _engine = Engine::start_with(
        config,
        || ApproxTextEngine,
        Reports(tx),
        |control| control.resize(size),
    )
    .unwrap();
    let next = || rx.recv_timeout(Duration::from_secs(5)).expect("a message");
    let Sent::Batch(batch) = next() else {
        panic!("the first frame first");
    };
    assert!(
        batch.items.iter().any(|i| matches!(
            i,
            MountItem::Frame { frame, .. } if frame.size.width.0 == 500.0 && frame.size.height.0 == 700.0
        )),
        "the first frame is laid out at the new size"
    );
    assert!(matches!(next(), Sent::Resized(s) if s == size));
}

/// A Rust root whose task waits on the clock, then on an answer the core
/// gives in the commit, then on the clock again.
#[component]
fn Answered() -> impl IntoElement {
    let done = signal("waiting");
    let node = element_ref();
    spawn(async move {
        timer(Duration::from_millis(100)).await;
        let _ = node.transition_time().await;
        timer(Duration::from_millis(150)).await;
        done.set("done");
    });
    view()
        .element_ref(&node)
        .child(text(move || done.get().to_string()))
}

#[test]
fn a_task_woken_on_the_way_runs_at_the_time_it_was_woken() {
    let config = EngineConfig {
        clock: Clock::manual(start_time()),
        ..EngineConfig::new(App::Rust(Root::new::<Answered>(|| ())))
    };
    let (tx, rx) = crossbeam_channel::unbounded();
    let engine = Engine::start(config, || ApproxTextEngine, Sink(tx)).unwrap();
    let mut last = String::new();
    advance(&engine, 0);
    assert_eq!(shown(&rx, &mut last), "waiting");
    // The answer comes at 100 ms, in the commit after the first timer: the
    // second timer runs from there, not from where the move ends.
    advance(&engine, 300);
    assert_eq!(shown(&rx, &mut last), "done");
}