graphix-package-sys 0.9.0

A dataflow language for UIs and network programming, sys package
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
use anyhow::{anyhow, bail, Result};
use arcstr::{literal, ArcStr};
use compact_str::format_compact;
use graphix_compiler::{
    deref_typ, err, errf,
    expr::ExprId,
    node::genn,
    typ::{FnType, Type},
    Apply, BindId, BuiltIn, Event, ExecCtx, LambdaId, Node, PrintFlag, Rt, Scope,
    TypecheckPhase, UserEvent,
};
use graphix_package_core::{arity1, arity2, extract_cast_type, CachedVals};
use netidx::{
    path::Path,
    publisher::{Typ, Val},
    subscriber::{self, Dval, UpdatesFlags, Value},
};
use netidx_core::utils::Either;
use netidx_protocols::rpc::server::{self, ArgSpec};
use netidx_value::ValArray;
use smallvec::{smallvec, SmallVec};
use std::collections::VecDeque;
use triomphe::Arc as TArc;

fn is_null_type(t: &Type) -> bool {
    matches!(t, Type::Primitive(flags) if flags.iter().count() == 1 && flags.contains(Typ::Null))
}

fn as_path(v: Value) -> Option<Path> {
    match v.cast_to::<String>() {
        Err(_) => None,
        Ok(p) => {
            if Path::is_absolute(&p) {
                Some(Path::from(p))
            } else {
                None
            }
        }
    }
}

#[derive(Debug)]
pub(crate) struct Write {
    args: CachedVals,
    top_id: ExprId,
    dv: Either<(Path, Dval), Vec<Value>>,
}

impl<R: Rt, E: UserEvent> BuiltIn<R, E> for Write {
    const NAME: &str = "sys_net_write";
    const NEEDS_CALLSITE: bool = false;

    fn init<'a, 'b, 'c, 'd>(
        _ctx: &'a mut ExecCtx<R, E>,
        _typ: &'a FnType,
        _resolved: Option<&'d FnType>,
        _scope: &'b Scope,
        from: &'c [Node<R, E>],
        top_id: ExprId,
    ) -> Result<Box<dyn Apply<R, E>>> {
        Ok(Box::new(Write {
            args: CachedVals::new(from),
            dv: Either::Right(vec![]),
            top_id,
        }))
    }
}

impl<R: Rt, E: UserEvent> Apply<R, E> for Write {
    fn update(
        &mut self,
        ctx: &mut ExecCtx<R, E>,
        from: &mut [Node<R, E>],
        event: &mut Event<E>,
    ) -> Option<Value> {
        fn set(dv: &mut Either<(Path, Dval), Vec<Value>>, val: &Value) {
            match dv {
                Either::Right(q) => q.push(val.clone()),
                Either::Left((_, dv)) => {
                    dv.write(val.clone());
                }
            }
        }
        let mut up = [false; 2];
        self.args.update_diff(&mut up, ctx, from, event);
        let ((path, value), (path_up, value_up)) = arity2!(self.args.0, &up);
        match ((path, value), (path_up, value_up)) {
            ((_, _), (false, false)) => (),
            ((_, Some(val)), (false, true)) => set(&mut self.dv, val),
            ((_, None), (false, true)) => (),
            ((None, Some(val)), (true, true)) => set(&mut self.dv, val),
            ((Some(path), Some(val)), (true, true)) if self.same_path(path) => {
                set(&mut self.dv, val)
            }
            ((Some(path), _), (true, false)) if self.same_path(path) => (),
            ((None, _), (true, false)) => (),
            ((None, None), (_, _)) => (),
            ((Some(path), val), (true, _)) => match as_path(path.clone()) {
                None => {
                    if let Either::Left(_) = &self.dv {
                        self.dv = Either::Right(vec![]);
                    }
                    let e = errf!(literal!("WriteError"), "invalid path {path:?}");
                    return Some(Value::Error(TArc::new(e)));
                }
                Some(path) => {
                    let dv = ctx.rt.subscribe(
                        UpdatesFlags::empty(),
                        path.clone(),
                        self.top_id,
                    );
                    match &mut self.dv {
                        Either::Left(_) => (),
                        Either::Right(q) => {
                            for v in q.drain(..) {
                                dv.write(v);
                            }
                        }
                    }
                    self.dv = Either::Left((path, dv));
                    if let Some(val) = val {
                        set(&mut self.dv, val)
                    }
                }
            },
        }
        None
    }

    fn delete(&mut self, ctx: &mut ExecCtx<R, E>) {
        if let Either::Left((path, dv)) = &self.dv {
            ctx.rt.unsubscribe(path.clone(), dv.clone(), self.top_id)
        }
        self.dv = Either::Right(vec![])
    }

    fn sleep(&mut self, ctx: &mut ExecCtx<R, E>) {
        self.args.clear();
        match &mut self.dv {
            Either::Left((path, dv)) => {
                ctx.rt.unsubscribe(path.clone(), dv.clone(), self.top_id);
                self.dv = Either::Right(vec![])
            }
            Either::Right(_) => (),
        }
    }
}

impl Write {
    fn same_path(&self, new_path: &Value) -> bool {
        match (new_path, &self.dv) {
            (Value::String(p0), Either::Left((p1, _))) => &**p0 == &**p1,
            _ => false,
        }
    }
}

#[derive(Debug)]
pub(crate) struct Subscribe {
    args: CachedVals,
    cur: Option<(Path, Dval)>,
    top_id: ExprId,
    cast_typ: Option<Type>,
}

impl<R: Rt, E: UserEvent> BuiltIn<R, E> for Subscribe {
    const NAME: &str = "sys_net_subscribe";
    const NEEDS_CALLSITE: bool = true;

    fn init<'a, 'b, 'c, 'd>(
        _ctx: &'a mut ExecCtx<R, E>,
        _typ: &'a FnType,
        resolved: Option<&'d FnType>,
        _scope: &'b Scope,
        from: &'c [Node<R, E>],
        top_id: ExprId,
    ) -> Result<Box<dyn Apply<R, E>>> {
        Ok(Box::new(Subscribe {
            args: CachedVals::new(from),
            cur: None,
            top_id,
            cast_typ: extract_cast_type(resolved),
        }))
    }
}

impl<R: Rt, E: UserEvent> Apply<R, E> for Subscribe {
    fn update(
        &mut self,
        ctx: &mut ExecCtx<R, E>,
        from: &mut [Node<R, E>],
        event: &mut Event<E>,
    ) -> Option<Value> {
        static ERR_TAG: ArcStr = literal!("SubscribeError");
        let mut up = [false; 1];
        self.args.update_diff(&mut up, ctx, from, event);
        let (path, path_up) = arity1!(self.args.0, &up);
        match (path, path_up) {
            (Some(_), false) | (None, false) => (),
            (None, true) => {
                if let Some((path, dv)) = self.cur.take() {
                    ctx.rt.unsubscribe(path, dv, self.top_id)
                }
                return None;
            }
            (Some(Value::String(path)), true)
                if self.cur.as_ref().map(|(p, _)| &**p) != Some(&*path) =>
            {
                if let Some((path, dv)) = self.cur.take() {
                    ctx.rt.unsubscribe(path, dv, self.top_id)
                }
                let path = Path::from(path);
                if !Path::is_absolute(&path) {
                    return Some(err!(ERR_TAG, "expected absolute path"));
                }
                let dval = ctx.rt.subscribe(
                    UpdatesFlags::BEGIN_WITH_LAST,
                    path.clone(),
                    self.top_id,
                );
                self.cur = Some((path, dval));
            }
            (Some(Value::String(_)), true) => (),
            (Some(v), true) => {
                return Some(errf!(ERR_TAG, "invalid path {v}, expected string"))
            }
        }
        self.cur.as_ref().and_then(|(_, dv)| {
            event.netidx.get(&dv.id()).map(|e| match e {
                subscriber::Event::Unsubscribed => Value::error(literal!("unsubscribed")),
                subscriber::Event::Update(v) => match &self.cast_typ {
                    Some(typ) => typ.cast_value(&ctx.env, v.clone()),
                    None => v.clone(),
                },
            })
        })
    }

    fn typecheck(
        &mut self,
        _ctx: &mut ExecCtx<R, E>,
        _from: &mut [Node<R, E>],
        phase: TypecheckPhase<'_>,
    ) -> Result<()> {
        match phase {
            TypecheckPhase::Lambda => Ok(()),
            TypecheckPhase::CallSite(resolved) => {
                self.cast_typ = extract_cast_type(Some(resolved));
                if self.cast_typ.is_none() {
                    bail!("sys::net::subscribe requires a concrete return type")
                }
                Ok(())
            }
        }
    }

    fn delete(&mut self, ctx: &mut ExecCtx<R, E>) {
        if let Some((path, dv)) = self.cur.take() {
            ctx.rt.unsubscribe(path, dv, self.top_id)
        }
    }

    fn sleep(&mut self, ctx: &mut ExecCtx<R, E>) {
        self.args.clear();
        if let Some((path, dv)) = self.cur.take() {
            ctx.rt.unsubscribe(path, dv, self.top_id);
        }
    }
}

#[derive(Debug)]
pub(crate) struct RpcCall {
    args: CachedVals,
    top_id: ExprId,
    id: BindId,
    cast_typ: Option<Type>,
}

impl<R: Rt, E: UserEvent> BuiltIn<R, E> for RpcCall {
    const NAME: &str = "sys_net_call";
    const NEEDS_CALLSITE: bool = true;

    fn init<'a, 'b, 'c, 'd>(
        ctx: &'a mut ExecCtx<R, E>,
        _typ: &'a FnType,
        resolved: Option<&'d FnType>,
        _scope: &'b Scope,
        from: &'c [Node<R, E>],
        top_id: ExprId,
    ) -> Result<Box<dyn Apply<R, E>>> {
        let id = BindId::new();
        ctx.rt.ref_var(id, top_id);
        Ok(Box::new(RpcCall {
            args: CachedVals::new(from),
            top_id,
            id,
            cast_typ: extract_cast_type(resolved),
        }))
    }
}

impl<R: Rt, E: UserEvent> Apply<R, E> for RpcCall {
    fn update(
        &mut self,
        ctx: &mut ExecCtx<R, E>,
        from: &mut [Node<R, E>],
        event: &mut Event<E>,
    ) -> Option<Value> {
        fn parse_args(
            path: &Value,
            args: &Value,
        ) -> Result<(Path, Vec<(ArcStr, Value)>)> {
            let path = as_path(path.clone()).ok_or_else(|| anyhow!("invalid path"))?;
            let args = match args {
                Value::Null => vec![],
                Value::Array(args) => args
                    .iter()
                    .map(|v| match v {
                        Value::Array(p) => match &**p {
                            [Value::String(name), value] => {
                                Ok((name.clone(), value.clone()))
                            }
                            _ => Err(anyhow!("rpc args expected [name, value] pair")),
                        },
                        _ => Err(anyhow!("rpc args expected [name, value] pair")),
                    })
                    .collect::<Result<Vec<_>>>()?,
                _ => bail!("rpc args expected to be a struct or null"),
            };
            Ok((path, args))
        }
        let mut up = [false; 2];
        self.args.update_diff(&mut up, ctx, from, event);
        let ((path, args), (path_up, args_up)) = arity2!(self.args.0, &up);
        match ((path, args), (path_up, args_up)) {
            ((Some(path), Some(args)), (_, true))
            | ((Some(path), Some(args)), (true, _)) => match parse_args(path, args) {
                Err(e) => return Some(errf!(literal!("RpcError"), "{e}")),
                Ok((path, args)) => ctx.rt.call_rpc(path, args, self.id),
            },
            ((None, _), (_, _)) | ((_, None), (_, _)) | ((_, _), (false, false)) => (),
        }
        event.variables.get(&self.id).map(|v| match &self.cast_typ {
            Some(typ) => typ.cast_value(&ctx.env, v.clone()),
            None => v.clone(),
        })
    }

    fn typecheck(
        &mut self,
        ctx: &mut ExecCtx<R, E>,
        _from: &mut [Node<R, E>],
        phase: TypecheckPhase<'_>,
    ) -> Result<()> {
        match phase {
            TypecheckPhase::Lambda => Ok(()),
            TypecheckPhase::CallSite(resolved) => {
                self.cast_typ = extract_cast_type(Some(resolved));
                if self.cast_typ.is_none() {
                    bail!("sys::net::call requires a concrete return type")
                }
                // validate args type: must be a struct or null
                if let Some(args_arg) = resolved.args.get(1) {
                    deref_typ!("struct, null, or Any", ctx, &args_arg.typ,
                        Some(Type::Struct(_)) => Ok(()),
                        Some(Type::Any) => Ok(()),
                        Some(t @ Type::Primitive(_)) => {
                            if is_null_type(t) { Ok(()) }
                            else { bail!("sys::net::call args must be a struct or null") }
                        }
                    )?;
                }
                Ok(())
            }
        }
    }

    fn delete(&mut self, ctx: &mut ExecCtx<R, E>) {
        ctx.rt.unref_var(self.id, self.top_id)
    }

    fn sleep(&mut self, ctx: &mut ExecCtx<R, E>) {
        ctx.rt.unref_var(self.id, self.top_id);
        self.id = BindId::new();
        ctx.rt.ref_var(self.id, self.top_id);
        self.args.clear()
    }
}

macro_rules! list {
    ($name:ident, $builtin:literal, $method:ident, $typ:literal) => {
        #[derive(Debug)]
        pub(crate) struct $name {
            args: CachedVals,
            current: Option<Path>,
            id: BindId,
            top_id: ExprId,
        }

        impl<R: Rt, E: UserEvent> BuiltIn<R, E> for $name {
            const NAME: &str = $builtin;
            const NEEDS_CALLSITE: bool = false;

            fn init<'a, 'b, 'c, 'd>(
                ctx: &'a mut ExecCtx<R, E>,
                _typ: &'a FnType,
                _resolved: Option<&'d FnType>,
                _scope: &'b Scope,
                from: &'c [Node<R, E>],
                top_id: ExprId,
            ) -> Result<Box<dyn Apply<R, E>>> {
                let id = BindId::new();
                ctx.rt.ref_var(id, top_id);
                Ok(Box::new($name {
                    args: CachedVals::new(from),
                    current: None,
                    top_id,
                    id,
                }))
            }
        }

        impl<R: Rt, E: UserEvent> Apply<R, E> for $name {
            fn update(
                &mut self,
                ctx: &mut ExecCtx<R, E>,
                from: &mut [Node<R, E>],
                event: &mut Event<E>,
            ) -> Option<Value> {
                let mut up = [false; 2];
                self.args.update_diff(&mut up, ctx, from, event);
                let ((_, path), (trigger_up, path_up)) = arity2!(self.args.0, &up);
                match (path, path_up, trigger_up) {
                    (Some(Value::String(path)), true, _)
                        if self
                            .current
                            .as_ref()
                            .map(|p| &**p != &**path)
                            .unwrap_or(true) =>
                    {
                        let path = Path::from(path);
                        self.current = Some(path.clone());
                        ctx.rt.$method(self.id, path);
                    }
                    (Some(Value::String(path)), _, true) => {
                        ctx.rt.$method(self.id, Path::from(path));
                    }
                    _ => (),
                }
                event.variables.get(&self.id).and_then(|v| match v {
                    Value::Null => None,
                    Value::Error(e) => Some(errf!(literal!("ListError"), "{e}")),
                    v => Some(v.clone()),
                })
            }

            fn delete(&mut self, ctx: &mut ExecCtx<R, E>) {
                ctx.rt.unref_var(self.id, self.top_id);
                ctx.rt.stop_list(self.id);
            }

            fn sleep(&mut self, ctx: &mut ExecCtx<R, E>) {
                ctx.rt.unref_var(self.id, self.top_id);
                ctx.rt.stop_list(self.id);
                self.id = BindId::new();
                ctx.rt.ref_var(self.id, self.top_id);
                self.current = None;
                self.args.clear();
            }
        }
    };
}

list!(
    List,
    "sys_net_list",
    list,
    "fn(?#update:Any, string) -> Result<Array<string>, `ListError(string)>"
);

list!(
    ListTable,
    "sys_net_list_table",
    list_table,
    "fn(?#update:Any, string) -> Result<Table, `ListError(string)>"
);

fn extract_publish_cast_type(resolved: Option<&FnType>) -> Option<Type> {
    let resolved = resolved?;
    resolved.args.first().and_then(|a| match &a.typ {
        Type::Fn(cb_ft) if !cb_ft.args.is_empty() => {
            let t = &cb_ft.args[0].typ;
            if format!("{t}").contains('\'') {
                None
            } else {
                Some(t.clone())
            }
        }
        _ => None,
    })
}

#[derive(Debug)]
pub(crate) struct Publish<R: Rt, E: UserEvent> {
    args: CachedVals,
    current: Option<(Path, Val)>,
    top_id: ExprId,
    x: BindId,
    pid: BindId,
    on_write: Node<R, E>,
    cast_typ: Option<Type>,
}

impl<R: Rt, E: UserEvent> BuiltIn<R, E> for Publish<R, E> {
    const NAME: &str = "sys_net_publish";
    const NEEDS_CALLSITE: bool = true;

    fn init<'a, 'b, 'c, 'd>(
        ctx: &'a mut ExecCtx<R, E>,
        typ: &'a FnType,
        resolved: Option<&'d FnType>,
        scope: &'b Scope,
        from: &'c [Node<R, E>],
        top_id: ExprId,
    ) -> Result<Box<dyn Apply<R, E>>> {
        match from {
            [_, _, _] => {
                let typ = resolved.unwrap_or(typ);
                let scope =
                    scope.append(&format_compact!("fn{}", LambdaId::new().inner()));
                let pid = BindId::new();
                let mftyp = match &typ.args[0].typ {
                    Type::Fn(ft) => ft.clone(),
                    t => bail!("expected function not {t}"),
                };
                let (x, xn) = genn::bind(
                    ctx,
                    &scope.lexical,
                    "x",
                    mftyp.args[0].typ.clone(),
                    top_id,
                );
                let fnode = genn::reference(ctx, pid, Type::Fn(mftyp.clone()), top_id);
                let on_write = genn::apply(fnode, scope, vec![xn], &mftyp, top_id);
                Ok(Box::new(Publish {
                    args: CachedVals::new(from),
                    current: None,
                    top_id,
                    pid,
                    x,
                    on_write,
                    cast_typ: extract_publish_cast_type(resolved),
                }))
            }
            _ => bail!("expected three arguments"),
        }
    }
}

impl<R: Rt, E: UserEvent> Apply<R, E> for Publish<R, E> {
    fn update(
        &mut self,
        ctx: &mut ExecCtx<R, E>,
        from: &mut [Node<R, E>],
        event: &mut Event<E>,
    ) -> Option<Value> {
        macro_rules! publish {
            ($path:expr, $v:expr) => {{
                let path = Path::from($path.clone());
                match ctx.rt.publish(path.clone(), $v.clone(), self.top_id) {
                    Err(e) => {
                        let msg: ArcStr = format_compact!("{e:?}").as_str().into();
                        let e: Value = (literal!("PublishError"), msg).into();
                        return Some(Value::Error(TArc::new(e)));
                    }
                    Ok(id) => {
                        self.current = Some((path, id));
                    }
                }
            }};
        }
        let mut up = [false; 3];
        self.args.update_diff(&mut up, ctx, from, event);
        if up[0] {
            if let Some(v) = self.args.0[0].clone() {
                ctx.cached.insert(self.pid, v.clone());
                event.variables.insert(self.pid, v);
            }
        }
        match (&up[1..], &self.args.0[1..]) {
            ([true, _], [Some(Value::String(path)), Some(v)])
                if self.current.as_ref().map(|(p, _)| &**p != path).unwrap_or(true) =>
            {
                if let Some((_, id)) = self.current.take() {
                    ctx.rt.unpublish(id, self.top_id);
                }
                publish!(path, v)
            }
            ([_, true], [Some(Value::String(path)), Some(v)]) => match &self.current {
                Some((_, val)) => ctx.rt.update(val, v.clone()),
                None => publish!(path, v),
            },
            _ => (),
        }
        let mut reply = None;
        if let Some((_, val)) = &self.current {
            if let Some(req) = event.writes.remove(&val.id()) {
                let v = match &self.cast_typ {
                    Some(typ) => typ.cast_value(&ctx.env, req.value.clone()),
                    None => req.value.clone(),
                };
                ctx.cached.insert(self.x, v.clone());
                event.variables.insert(self.x, v);
                reply = req.send_result;
            }
        }
        if let Some(v) = self.on_write.update(ctx, event) {
            if let Some(reply) = reply {
                reply.send(v)
            }
        }
        None
    }

    fn typecheck(
        &mut self,
        ctx: &mut ExecCtx<R, E>,
        _from: &mut [Node<R, E>],
        phase: TypecheckPhase<'_>,
    ) -> Result<()> {
        match phase {
            TypecheckPhase::Lambda => {
                self.on_write.typecheck(ctx)?;
                Ok(())
            }
            TypecheckPhase::CallSite(resolved) => {
                self.cast_typ = extract_publish_cast_type(Some(resolved));
                Ok(())
            }
        }
    }

    fn refs(&self, refs: &mut graphix_compiler::Refs) {
        self.on_write.refs(refs)
    }

    fn delete(&mut self, ctx: &mut ExecCtx<R, E>) {
        if let Some((_, val)) = self.current.take() {
            ctx.rt.unpublish(val, self.top_id);
        }
        ctx.cached.remove(&self.pid);
        ctx.cached.remove(&self.x);
        ctx.env.unbind_variable(self.x);
        self.on_write.delete(ctx);
    }

    fn sleep(&mut self, ctx: &mut ExecCtx<R, E>) {
        if let Some((_, val)) = self.current.take() {
            ctx.rt.unpublish(val, self.top_id);
        }
        self.args.clear();
        self.on_write.sleep(ctx);
    }
}

#[derive(Debug)]
pub(crate) struct PublishRpc<R: Rt, E: UserEvent> {
    args: CachedVals,
    id: BindId,
    top_id: ExprId,
    f: Node<R, E>,
    pid: BindId,
    x: BindId,
    queue: VecDeque<server::RpcCall>,
    argbuf: SmallVec<[(ArcStr, Value); 6]>,
    ready: bool,
    current: Option<Path>,
    cast_typ: Option<Type>,
}

impl<R: Rt, E: UserEvent> PublishRpc<R, E> {
    fn validate_spec(
        &mut self,
        ctx: &mut ExecCtx<R, E>,
        resolved: &FnType,
    ) -> Result<()> {
        // validate spec: must be a struct of RpcArg fields, or null (no args)
        let (spec_is_null, spec_fields) = if let Some(spec_arg) = resolved.args.get(2) {
            deref_typ!("struct or null", ctx, &spec_arg.typ,
                Some(Type::Struct(fields)) => Ok((false, fields.clone())),
                Some(t @ Type::Primitive(_)) => {
                    if is_null_type(t) { Ok((true, TArc::from_iter([]))) }
                    else { bail!("rpc #spec must be a struct or null") }
                }
            )?
        } else {
            bail!("rpc #spec type not available")
        };
        // validate each spec field is {default: T, doc: string}
        for (name, field_typ) in spec_fields.iter() {
            deref_typ!("RpcArg {{default: 'a, doc: string}}", ctx, field_typ,
                Some(Type::Struct(inner)) => {
                    if inner.len() == 2 {
                        let has_default = inner.iter().any(|(n, _)| n.as_str() == "default");
                        let has_doc = inner.iter().any(|(n, _)| n.as_str() == "doc");
                        if has_default && has_doc { Ok(()) }
                        else { bail!("rpc #spec field '{name}' must be {{default: 'a, doc: string}}") }
                    } else {
                        bail!("rpc #spec field '{name}' must be {{default: 'a, doc: string}}")
                    }
                }
            )?;
        }
        // validate callback arg
        let cb_fn = if let Some(f_arg) = resolved.args.get(3) {
            deref_typ!("fn", ctx, &f_arg.typ,
                Some(Type::Fn(ft)) => Ok(ft.clone())
            )?
        } else {
            bail!("rpc #f must be a function with an argument")
        };
        if cb_fn.args.is_empty() {
            bail!("rpc #f must be a function with an argument")
        }
        let cb_arg_typ = &cb_fn.args[0].typ;
        // if spec is null, callback arg must also be null
        if spec_is_null {
            deref_typ!("null", ctx, cb_arg_typ,
                Some(t @ Type::Primitive(_)) => {
                    if is_null_type(t) { Ok(()) }
                    else { bail!("rpc #f argument must be null when #spec is null") }
                }
            )?;
            self.cast_typ = Some(cb_arg_typ.clone());
            return Ok(());
        }
        let cb_fields = deref_typ!("struct", ctx, cb_arg_typ,
            Some(Type::Struct(fields)) => Ok(fields.clone())
        )?;
        // verify same fields and matching types.
        // the length check catches extra fields in either direction,
        // and the name check below catches mismatched names.
        if spec_fields.len() != cb_fields.len() {
            bail!(
                "rpc #spec has {} fields but #f argument has {}",
                spec_fields.len(),
                cb_fields.len()
            )
        }
        for (spec_name, spec_field_typ) in spec_fields.iter() {
            // extract the value type T from {default: T, doc: string}
            let value_typ = deref_typ!(
                "{{default: 'a, doc: string}}", ctx, spec_field_typ,
                Some(Type::Struct(inner)) => {
                    match inner.iter().find(|(n, _)| n.as_str() == "default") {
                        Some((_, t)) => Ok(t.clone()),
                        None => bail!("rpc #spec field '{spec_name}' missing 'default'"),
                    }
                }
            )?;
            let cb_field = cb_fields.iter().find(|(n, _)| n == spec_name);
            match cb_field {
                None => bail!("rpc #f argument missing field '{spec_name}'"),
                Some((_, cb_typ)) => {
                    let check = |t: &Type| -> Result<()> {
                        if !t.contains(&ctx.env, &value_typ)? {
                            bail!(
                                "rpc field '{spec_name}' type mismatch: \
                                 #f argument type {t} does not contain \
                                 #spec default type {value_typ}"
                            )
                        }
                        Ok(())
                    };
                    deref_typ!("type", ctx, cb_typ,
                        Some(Type::Any | Type::Bottom) => Ok(()),
                        Some(t @ (
                            Type::Primitive(_) | Type::Fn(_) | Type::Set(_)
                            | Type::Error(_) | Type::Array(_) | Type::ByRef(_)
                            | Type::Tuple(_) | Type::Struct(_) | Type::Variant(_, _)
                            | Type::Map { .. } | Type::Abstract { .. }
                        )) => check(t)
                    )?;
                }
            }
        }
        self.cast_typ = Some(cb_arg_typ.clone());
        Ok(())
    }
}

impl<R: Rt, E: UserEvent> BuiltIn<R, E> for PublishRpc<R, E> {
    const NAME: &str = "sys_net_publish_rpc";
    const NEEDS_CALLSITE: bool = true;

    fn init<'a, 'b, 'c>(
        ctx: &'a mut ExecCtx<R, E>,
        typ: &'a graphix_compiler::typ::FnType,
        resolved: Option<&FnType>,
        scope: &'b Scope,
        from: &'c [Node<R, E>],
        top_id: ExprId,
    ) -> Result<Box<dyn Apply<R, E>>> {
        match from {
            [_, _, _, _] => {
                let typ = resolved.unwrap_or(typ);
                let scope =
                    scope.append(&format_compact!("fn{}", LambdaId::new().inner()));
                let id = BindId::new();
                ctx.rt.ref_var(id, top_id);
                let pid = BindId::new();
                let mftyp = match &typ.args[3].typ {
                    Type::Fn(ft) => ft.clone(),
                    t => bail!("expected a function not {t}"),
                };
                let (x, xn) = genn::bind(
                    ctx,
                    &scope.lexical,
                    "x",
                    mftyp.args[0].typ.clone(),
                    top_id,
                );
                let fnode = genn::reference(ctx, pid, Type::Fn(mftyp.clone()), top_id);
                let f = genn::apply(fnode, scope, vec![xn], &mftyp, top_id);
                let mut t = PublishRpc {
                    queue: VecDeque::new(),
                    args: CachedVals::new(from),
                    x,
                    id,
                    top_id,
                    f,
                    pid,
                    argbuf: smallvec![],
                    ready: true,
                    current: None,
                    cast_typ: None,
                };
                if let Some(resolved) = resolved {
                    let _ = t.validate_spec(ctx, resolved);
                }
                Ok(Box::new(t))
            }
            _ => bail!("expected four arguments"),
        }
    }
}

impl<R: Rt, E: UserEvent> Apply<R, E> for PublishRpc<R, E> {
    fn update(
        &mut self,
        ctx: &mut ExecCtx<R, E>,
        from: &mut [Node<R, E>],
        event: &mut Event<E>,
    ) -> Option<Value> {
        let mut changed = [false; 4];
        self.args.update_diff(&mut changed, ctx, from, event);
        if changed[3] {
            if let Some(v) = self.args.0[3].clone() {
                ctx.cached.insert(self.pid, v.clone());
                event.variables.insert(self.pid, v);
            }
        }
        if changed[0] || changed[1] || changed[2] {
            if let Some(path) = self.current.take() {
                ctx.rt.unpublish_rpc(path);
            }
            if let (Some(Value::String(path)), Some(doc)) =
                (&self.args.0[0], &self.args.0[1])
            {
                let path = Path::from(path);
                let spec = match &self.args.0[2] {
                    Some(Value::Null) => vec![],
                    Some(Value::Array(spec)) => spec
                        .iter()
                        .map(|field| match field {
                            Value::Array(pair) if pair.len() == 2 => {
                                let name = match &pair[0] {
                                    Value::String(n) => n.clone(),
                                    _ => unreachable!(),
                                };
                                // pair[1] is {default: val, doc: docstr} struct
                                // fields sorted: "default" < "doc"
                                match &pair[1] {
                                    Value::Array(rpc_arg) if rpc_arg.len() == 2 => {
                                        let default_value = match &rpc_arg[0] {
                                            Value::Array(p) => p[1].clone(),
                                            _ => unreachable!(),
                                        };
                                        let doc = match &rpc_arg[1] {
                                            Value::Array(p) => p[1].clone(),
                                            _ => unreachable!(),
                                        };
                                        ArgSpec { name, doc, default_value }
                                    }
                                    _ => unreachable!(),
                                }
                            }
                            _ => unreachable!(),
                        })
                        .collect::<Vec<_>>(),
                    _ => vec![],
                };
                if let Err(e) =
                    ctx.rt.publish_rpc(path.clone(), doc.clone(), spec, self.id)
                {
                    let e: ArcStr = format_compact!("{e:?}").as_str().into();
                    let e: Value = (literal!("PublishRpcError"), e).into();
                    return Some(Value::Error(TArc::new(e)));
                }
                self.current = Some(path);
            }
        }
        macro_rules! set {
            ($c:expr) => {{
                self.ready = false;
                self.argbuf.extend($c.args.iter().map(|(n, v)| (n.clone(), v.clone())));
                self.argbuf.sort_by_key(|(n, _)| n.clone());
                let args =
                    ValArray::from_iter_exact(self.argbuf.drain(..).map(|(n, v)| {
                        Value::Array(ValArray::from([Value::String(n), v]))
                    }));
                let args = match &self.cast_typ {
                    Some(typ) => typ.cast_value(&ctx.env, Value::Array(args)),
                    None => Value::Array(args),
                };
                ctx.cached.insert(self.x, args.clone());
                event.variables.insert(self.x, args);
            }};
        }
        if let Some(c) = event.rpc_calls.remove(&self.id) {
            self.queue.push_back(c);
        }
        if self.ready && self.queue.len() > 0 {
            if let Some(c) = self.queue.front() {
                set!(c)
            }
        }
        loop {
            match self.f.update(ctx, event) {
                None => break None,
                Some(v) => {
                    self.ready = true;
                    if let Some(mut call) = self.queue.pop_front() {
                        call.reply.send(v);
                    }
                    match self.queue.front() {
                        Some(c) => set!(c),
                        None => break None,
                    }
                }
            }
        }
    }

    fn typecheck(
        &mut self,
        ctx: &mut ExecCtx<R, E>,
        _from: &mut [Node<R, E>],
        phase: TypecheckPhase<'_>,
    ) -> Result<()> {
        match phase {
            TypecheckPhase::Lambda => {
                self.f.typecheck(ctx)?;
                Ok(())
            }
            TypecheckPhase::CallSite(resolved) => {
                self.validate_spec(ctx, resolved)?;
                Ok(())
            }
        }
    }

    fn refs(&self, refs: &mut graphix_compiler::Refs) {
        self.f.refs(refs)
    }

    fn delete(&mut self, ctx: &mut ExecCtx<R, E>) {
        ctx.rt.unref_var(self.id, self.top_id);
        if let Some(path) = self.current.take() {
            ctx.rt.unpublish_rpc(path);
        }
        ctx.cached.remove(&self.x);
        ctx.env.unbind_variable(self.x);
        ctx.cached.remove(&self.pid);
        self.f.delete(ctx);
    }

    fn sleep(&mut self, ctx: &mut ExecCtx<R, E>) {
        ctx.rt.unref_var(self.id, self.top_id);
        self.id = BindId::new();
        ctx.rt.ref_var(self.id, self.top_id);
        if let Some(path) = self.current.take() {
            ctx.rt.unpublish_rpc(path);
        }
        self.args.clear();
        self.queue.clear();
        self.argbuf.clear();
        self.ready = true;
        self.f.sleep(ctx);
    }
}