11#![ doc = include_str ! ( "../README.md" ) ]
22// ^ if you are working on docs, go read the top comment of README.md please.
33
4+ use core:: cell:: { LazyCell , OnceCell , RefCell } ;
5+ use core:: ops:: Deref ;
6+ use spacetimedb_lib:: bsatn;
7+
48#[ cfg( feature = "unstable" ) ]
59mod client_visibility_filter;
610pub mod log_stopwatch;
@@ -12,16 +16,11 @@ pub mod rt;
1216#[ doc( hidden) ]
1317pub mod table;
1418
19+ #[ cfg( feature = "unstable" ) ]
20+ pub use client_visibility_filter:: Filter ;
1521pub use log;
1622#[ cfg( feature = "rand" ) ]
1723pub use rand08 as rand;
18- use spacetimedb_lib:: bsatn;
19- use std:: cell:: LazyCell ;
20- use std:: cell:: { OnceCell , RefCell } ;
21- use std:: ops:: Deref ;
22-
23- #[ cfg( feature = "unstable" ) ]
24- pub use client_visibility_filter:: Filter ;
2524#[ cfg( feature = "rand08" ) ]
2625pub use rng:: StdbRng ;
2726pub use sats:: SpacetimeType ;
@@ -721,24 +720,9 @@ pub use spacetimedb_bindings_macro::reducer;
721720/// Procedures are allowed to perform certain operations which take time.
722721/// During the execution of these operations, the procedure's execution will be suspended,
723722/// allowing other database operations to run in parallel.
724- /// The simplest (and least useful) of these operators is [`ProcedureContext::sleep_until`].
725723///
726724/// Procedures must not hold open a transaction while performing a blocking operation.
727- ///
728- /// ```no_run
729- /// # use std::time::Duration;
730- /// # use spacetimedb::{procedure, ProcedureContext};
731- /// #[procedure]
732- /// fn sleep_one_second(ctx: &mut ProcedureContext) {
733- /// let prev_time = ctx.timestamp;
734- /// let target = prev_time + Duration::from_secs(1);
735- /// ctx.sleep_until(target);
736- /// let new_time = ctx.timestamp;
737- /// let actual_delta = new_time.duration_since(prev_time).unwrap();
738- /// log::info!("Slept from {prev_time} to {new_time}, a total of {actual_delta:?}");
739- /// }
740- /// ```
741- // TODO(procedure-http): replace this example with an HTTP request.
725+ // TODO(procedure-http): add example with an HTTP request.
742726// TODO(procedure-transaction): document obtaining and using a transaction within a procedure.
743727///
744728/// # Scheduled procedures
@@ -1089,7 +1073,8 @@ impl ProcedureContext {
10891073 /// log::info!("Slept from {prev_time} to {new_time}, a total of {actual_delta:?}");
10901074 /// # }
10911075 /// ```
1092- // TODO(procedure-async): mark this method `async`.
1076+ // TODO(procedure-sleep-until): remove this method
1077+ #[ cfg( feature = "unstable" ) ]
10931078 pub fn sleep_until ( & mut self , timestamp : Timestamp ) {
10941079 let new_time = sys:: procedure:: sleep_until ( timestamp. to_micros_since_unix_epoch ( ) ) ;
10951080 let new_time = Timestamp :: from_micros_since_unix_epoch ( new_time) ;
@@ -1135,6 +1120,9 @@ impl DbContext for ReducerContext {
11351120#[ non_exhaustive]
11361121pub struct Local { }
11371122
1123+ /// The [JWT] of an [`AuthCtx`].
1124+ ///
1125+ /// [JWT]: https://en.wikipedia.org/wiki/JSON_Web_Token
11381126#[ non_exhaustive]
11391127pub struct JwtClaims {
11401128 payload : String ,
@@ -1145,7 +1133,8 @@ pub struct JwtClaims {
11451133/// Authentication information for the caller of a reducer.
11461134pub struct AuthCtx {
11471135 is_internal : bool ,
1148- // NOTE(jsdt): cannot directly use a LazyLock without making this struct generic.
1136+ // NOTE(jsdt): cannot directly use a `LazyCell` without making this struct generic,
1137+ // which would cause `ReducerContext` to become generic as well.
11491138 jwt : Box < dyn Deref < Target = Option < JwtClaims > > > ,
11501139}
11511140
@@ -1157,19 +1146,25 @@ impl AuthCtx {
11571146 }
11581147 }
11591148
1160- /// Create an [`AuthCtx`] for an internal call, with no JWT.
1149+ /// Creates an [`AuthCtx`] for an internal call, with no [ JWT] .
11611150 /// This represents a scheduled reducer.
1151+ ///
1152+ /// [JWT]: https://en.wikipedia.org/wiki/JSON_Web_Token
11621153 pub fn internal ( ) -> AuthCtx {
11631154 Self :: new ( true , || None )
11641155 }
11651156
1166- /// Creates an [`AuthCtx`] using the json claims from a JWT.
1157+ /// Creates an [`AuthCtx`] using the json claims from a [ JWT] .
11671158 /// This can be used to write unit tests.
1159+ ///
1160+ /// [JWT]: https://en.wikipedia.org/wiki/JSON_Web_Token
11681161 pub fn from_jwt_payload ( jwt_payload : String ) -> AuthCtx {
11691162 Self :: new ( false , move || Some ( JwtClaims :: new ( jwt_payload) ) )
11701163 }
11711164
1172- /// Creates an [`AuthCtx`] that reads the JWT for the given connection id.
1165+ /// Creates an [`AuthCtx`] that reads the [JWT] for the given connection id.
1166+ ///
1167+ /// [JWT]: https://en.wikipedia.org/wiki/JSON_Web_Token
11731168 fn from_connection_id ( connection_id : ConnectionId ) -> AuthCtx {
11741169 Self :: new ( false , move || rt:: get_jwt ( connection_id) . map ( JwtClaims :: new) )
11751170 }
@@ -1179,13 +1174,17 @@ impl AuthCtx {
11791174 self . is_internal
11801175 }
11811176
1182- /// Check if there is a JWT without loading it.
1183- /// If [`AuthCtx::is_internal`] is true, this will return false.
1177+ /// Checks if there is a [JWT] without loading it.
1178+ /// If [`AuthCtx::is_internal`] returns true, this will return false.
1179+ ///
1180+ /// [JWT]: https://en.wikipedia.org/wiki/JSON_Web_Token
11841181 pub fn has_jwt ( & self ) -> bool {
11851182 self . jwt . is_some ( )
11861183 }
11871184
1188- /// Load the jwt.
1185+ /// Loads the [JWT].
1186+ ///
1187+ /// [JWT]: https://en.wikipedia.org/wiki/JSON_Web_Token
11891188 pub fn jwt ( & self ) -> Option < & JwtClaims > {
11901189 self . jwt . as_ref ( ) . deref ( ) . as_ref ( )
11911190 }
@@ -1240,6 +1239,9 @@ impl JwtClaims {
12401239 }
12411240
12421241 /// Get the whole JWT payload as a json string.
1242+ ///
1243+ /// This method is intended for parsing custom claims,
1244+ /// beyond the methods offered by [`JwtClaims`].
12431245 pub fn raw_payload ( & self ) -> & str {
12441246 & self . payload
12451247 }
0 commit comments