Skip to content

Commit 6a40228

Browse files
committed
Add session lifecycle truapi interface
1 parent ae53964 commit 6a40228

7 files changed

Lines changed: 134 additions & 1 deletion

File tree

docs/rfcs/0016-product-session-restore.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ enum SessionLifecycleEvent {
194194
struct SessionLifecycleRequest {
195195
event_id: SessionLifecycleEventId,
196196
reason: SessionLifecycleReason,
197-
deadline_ms: Option<u32>,
197+
deadline_ms: Option<TimestampMs>,
198198
}
199199

200200
enum SessionLifecycleReason {

rust/crates/truapi/src/api/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub mod payment;
1010
pub mod permissions;
1111
pub mod preimage;
1212
pub mod resource_allocation;
13+
pub mod session;
1314
pub mod signing;
1415
pub mod statement_store;
1516
pub mod system;
@@ -25,6 +26,7 @@ pub use payment::Payment;
2526
pub use permissions::Permissions;
2627
pub use preimage::Preimage;
2728
pub use resource_allocation::ResourceAllocation;
29+
pub use session::Session;
2830
pub use signing::Signing;
2931
pub use statement_store::StatementStore;
3032
pub use system::System;
@@ -42,6 +44,7 @@ pub trait TrUApi:
4244
+ Permissions
4345
+ Preimage
4446
+ ResourceAllocation
47+
+ Session
4548
+ Signing
4649
+ StatementStore
4750
+ System
@@ -62,6 +65,7 @@ impl<T> TrUApi for T where
6265
+ Permissions
6366
+ Preimage
6467
+ ResourceAllocation
68+
+ Session
6569
+ Signing
6670
+ StatementStore
6771
+ System
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//! Unified [`Session`] trait.
2+
3+
use crate::versioned::session::{
4+
HostSessionLifecycleSubscribeError, HostSessionLifecycleSubscribeItem,
5+
HostSessionLifecycleSubscribeRequest,
6+
};
7+
use crate::wire;
8+
use crate::{CallContext, CallError, Subscription};
9+
10+
/// Product session lifecycle operations.
11+
///
12+
/// Default methods return [`CallError::HostFailure`] with an `unavailable`
13+
/// reason. Hosts override when they support product session restore.
14+
pub trait Session: Send + Sync {
15+
/// Subscribe to host lifecycle signals so a product can checkpoint semantic
16+
/// session state through scoped local storage before suspend, eviction, or
17+
/// close.
18+
///
19+
/// ```truapi-client-example
20+
/// import {
21+
/// type Client,
22+
/// type HostSessionLifecycleSubscribeError,
23+
/// type HostSessionLifecycleSubscribeItem,
24+
/// type Subscription,
25+
/// type SubscriptionError,
26+
/// } from "@parity/truapi";
27+
///
28+
/// export function watchSessionLifecycle(truapi: Client): Subscription {
29+
/// return truapi.session
30+
/// .sessionLifecycleSubscribe({
31+
/// request: { replayCurrentState: true },
32+
/// })
33+
/// .subscribe({
34+
/// next: (event: HostSessionLifecycleSubscribeItem) =>
35+
/// console.log(event),
36+
/// error: (error: SubscriptionError<HostSessionLifecycleSubscribeError>) =>
37+
/// console.error(error),
38+
/// complete: () => console.log("completed"),
39+
/// });
40+
/// }
41+
/// ```
42+
#[wire(start_id = 162)]
43+
async fn lifecycle_subscribe(
44+
&self,
45+
_cx: &CallContext,
46+
_request: HostSessionLifecycleSubscribeRequest,
47+
) -> Result<
48+
Subscription<HostSessionLifecycleSubscribeItem>,
49+
CallError<HostSessionLifecycleSubscribeError>,
50+
> {
51+
Err(CallError::unavailable())
52+
}
53+
}

rust/crates/truapi/src/v01/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod payment;
1111
mod permissions;
1212
mod preimage;
1313
mod resource_allocation;
14+
mod session;
1415
mod signing;
1516
mod statement_store;
1617
mod system;
@@ -28,6 +29,7 @@ pub use payment::*;
2829
pub use permissions::*;
2930
pub use preimage::*;
3031
pub use resource_allocation::*;
32+
pub use session::*;
3133
pub use signing::*;
3234
pub use statement_store::*;
3335
pub use system::*;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use parity_scale_codec::{Decode, Encode};
2+
3+
use super::GenericErr;
4+
5+
/// Milliseconds since the Unix epoch.
6+
pub type TimestampMs = u64;
7+
8+
/// Host-assigned stable identifier for one lifecycle event.
9+
pub type SessionLifecycleEventId = String;
10+
11+
/// Request to subscribe to host session lifecycle events.
12+
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
13+
pub struct HostSessionLifecycleSubscribeRequest {
14+
/// Ask the host to replay the current lifecycle state when one is active.
15+
pub replay_current_state: bool,
16+
}
17+
18+
/// Lifecycle event emitted by the host before a product transition.
19+
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
20+
pub enum HostSessionLifecycleSubscribeItem {
21+
/// The product should checkpoint state before it is suspended.
22+
WillSuspend(SessionLifecycleRequest),
23+
/// The product should checkpoint state before its WebView may be evicted.
24+
WillEvict(SessionLifecycleRequest),
25+
/// The product should checkpoint state before it is closed.
26+
WillClose(SessionLifecycleRequest),
27+
}
28+
29+
/// Details for a single lifecycle checkpoint request.
30+
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
31+
pub struct SessionLifecycleRequest {
32+
/// Host-assigned event id for de-duplicating repeated notifications.
33+
pub event_id: SessionLifecycleEventId,
34+
/// Reason the host is asking the product to checkpoint state.
35+
pub reason: SessionLifecycleReason,
36+
/// Best-effort deadline for checkpoint completion.
37+
pub deadline_ms: Option<TimestampMs>,
38+
}
39+
40+
/// Reason for a lifecycle checkpoint request.
41+
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
42+
pub enum SessionLifecycleReason {
43+
/// User switched away from this product in the host app switcher.
44+
AppSwitcher,
45+
/// Host application moved to the background.
46+
HostBackgrounded,
47+
/// Host application is terminating or restarting.
48+
HostTerminating,
49+
/// Platform memory pressure may evict the product WebView.
50+
MemoryPressure,
51+
/// User explicitly closed this product.
52+
UserClosedProduct,
53+
/// Host policy requires a checkpoint.
54+
HostPolicy,
55+
}
56+
57+
/// Error from session lifecycle subscription setup.
58+
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
59+
pub enum HostSessionLifecycleSubscribeError {
60+
/// The host does not support product session lifecycle events.
61+
Unsupported,
62+
/// Catch-all.
63+
Unknown(GenericErr),
64+
}

rust/crates/truapi/src/versioned/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ pub mod payment;
9090
pub mod permissions;
9191
pub mod preimage;
9292
pub mod resource_allocation;
93+
pub mod session;
9394
pub mod signing;
9495
pub mod statement_store;
9596
pub mod system;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//! Versioned wrappers for [`Session`](crate::api::Session) methods.
2+
3+
use crate::v01;
4+
5+
versioned_type! {
6+
pub enum HostSessionLifecycleSubscribeRequest { V1 => v01::HostSessionLifecycleSubscribeRequest }
7+
pub enum HostSessionLifecycleSubscribeItem { V1 => v01::HostSessionLifecycleSubscribeItem }
8+
pub enum HostSessionLifecycleSubscribeError { V1 => v01::HostSessionLifecycleSubscribeError }
9+
}

0 commit comments

Comments
 (0)