@@ -25,7 +25,7 @@ use anyhow::Context;
2525use bytes:: Bytes ;
2626use spacetimedb_datastore:: locking_tx_datastore:: { FuncCallType , MutTxId , ViewCallInfo } ;
2727use spacetimedb_lib:: { ConnectionId , Identity , RawModuleDef , Timestamp } ;
28- use spacetimedb_primitives:: { ColId , IndexId , ProcedureId , TableId } ;
28+ use spacetimedb_primitives:: { ColId , IndexId , ProcedureId , TableId , ViewFnPtr } ;
2929use spacetimedb_sats:: bsatn;
3030use spacetimedb_schema:: def:: ModuleDef ;
3131use spacetimedb_schema:: identifier:: Identifier ;
@@ -752,19 +752,22 @@ fn refresh_views(
752752
753753 for view_call in views_for_refresh {
754754 let res: SysCallResult < ( ) > = ( || {
755- let view_def = module_def
756- . get_view_by_id ( view_call. fn_ptr , view_call. sender . is_none ( ) )
757- . ok_or_else ( || {
758- TypeError ( format ! (
759- "view with fn_ptr `{}` not found while refreshing procedure transaction" ,
760- view_call. fn_ptr
761- ) )
762- . throw ( scope)
763- } ) ?;
755+ let resolved = crate :: host:: module_host:: resolve_view_for_refresh (
756+ tx. as_ref ( ) . expect ( "procedure tx missing during view refresh" ) ,
757+ module_def,
758+ & view_call,
759+ )
760+ . map_err ( |err| TypeError ( format ! ( "view refresh failed after procedure call: {err}" ) ) . throw ( scope) ) ?;
761+
762+ let table_id = resolved. table_id ;
763+ let view_def = resolved. view_def ;
764+ let view_name = & view_def. name ;
765+ let fn_ptr = view_def. fn_ptr ;
764766
765767 let current_tx = tx. take ( ) . expect ( "procedure tx missing during view refresh" ) ;
766- let ( next_tx, call_result) =
767- tx_slot. set ( current_tx, || call_view ( scope, hooks, & view_call, & view_def. name ) ) ;
768+ let ( next_tx, call_result) = tx_slot. set ( current_tx, || {
769+ call_view ( scope, hooks, & view_call, view_name, table_id, fn_ptr)
770+ } ) ;
768771 tx = Some ( next_tx) ;
769772 let return_data = call_result?;
770773
@@ -814,25 +817,14 @@ fn refresh_views(
814817 } ) ?,
815818 } ;
816819
817- match view_call. sender {
818- Some ( sender) => stdb
819- . materialize_view (
820- tx. as_mut ( )
821- . expect ( "procedure tx missing while materializing authenticated view" ) ,
822- view_call. table_id ,
823- sender,
824- rows,
825- )
826- . map_err ( NodesError :: from) ?,
827- None => stdb
828- . materialize_anonymous_view (
829- tx. as_mut ( )
830- . expect ( "procedure tx missing while materializing anonymous view" ) ,
831- view_call. table_id ,
832- rows,
833- )
834- . map_err ( NodesError :: from) ?,
835- }
820+ stdb. materialize_view_call (
821+ tx. as_mut ( )
822+ . expect ( "procedure tx missing while materializing refreshed view" ) ,
823+ table_id,
824+ view_call. clone ( ) ,
825+ rows,
826+ )
827+ . map_err ( NodesError :: from) ?;
836828
837829 Ok ( ( ) )
838830 } ) ( ) ;
@@ -857,6 +849,8 @@ fn call_view(
857849 hooks : & HookFunctions < ' _ > ,
858850 view_call : & ViewCallInfo ,
859851 view_name : & Identifier ,
852+ table_id : TableId ,
853+ fn_ptr : ViewFnPtr ,
860854) -> SysCallResult < ViewReturnData > {
861855 let prev_func_type = get_env ( scope) ?
862856 . instance_env
@@ -871,8 +865,8 @@ fn call_view(
871865 ViewOp {
872866 name : view_name,
873867 view_id : view_call. view_id ,
874- table_id : view_call . table_id ,
875- fn_ptr : view_call . fn_ptr ,
868+ table_id,
869+ fn_ptr,
876870 args : & args,
877871 sender : & sender,
878872 timestamp : Timestamp :: now ( ) ,
@@ -884,8 +878,8 @@ fn call_view(
884878 AnonymousViewOp {
885879 name : view_name,
886880 view_id : view_call. view_id ,
887- table_id : view_call . table_id ,
888- fn_ptr : view_call . fn_ptr ,
881+ table_id,
882+ fn_ptr,
889883 args : & args,
890884 timestamp : Timestamp :: now ( ) ,
891885 } ,
0 commit comments