11pub mod entities;
2- use crate :: entities:: { ClientAPIConfig , ClientResponse } ;
2+
3+ use crate :: entities:: * ;
4+ use client_api:: entity:: QueryCollabParams ;
5+ use client_api:: notify:: TokenState ;
36use client_api:: { Client , ClientConfiguration } ;
7+ use std:: sync:: Arc ;
8+ use tracing;
49use wasm_bindgen:: prelude:: * ;
510
611#[ cfg( feature = "enable_wee_alloc" ) ]
@@ -27,11 +32,16 @@ extern "C" {
2732 #[ wasm_bindgen( js_namespace = console) ]
2833 fn trace ( msg : & str ) ;
2934
35+ #[ wasm_bindgen( js_namespace = window) ]
36+ fn refresh_token ( token : & str ) ;
37+
38+ #[ wasm_bindgen( js_namespace = window) ]
39+ fn invalid_token ( ) ;
3040}
3141
3242#[ wasm_bindgen]
3343pub struct ClientAPI {
34- client : Client ,
44+ client : Arc < Client > ,
3545}
3646
3747#[ wasm_bindgen]
@@ -55,54 +65,77 @@ impl ClientAPI {
5565 configuration,
5666 config. client_id . as_str ( ) ,
5767 ) ;
68+
5869 tracing:: debug!( "Client API initialized, config: {:?}" , config) ;
59- ClientAPI { client }
70+ ClientAPI {
71+ client : Arc :: new ( client) ,
72+ }
6073 }
6174
62- // pub async fn get_user(&self) -> ClientResponse {
63- // if let Err(err) = self.client.get_profile().await {
64- // log::error!("Get user failed: {:?}", err);
65- // return ClientResponse<bool> {
66- // code: ClientErrorCode::from(err.code),
67- // message: err.message.to_string(),
68- // data: None
69- // }
70- // }
71- //
72- // log::info!("Get user success");
73- // ClientResponse {
74- // code: ClientErrorCode::Ok,
75- // message: "Get user success".to_string(),
76- // }
77- // }
78-
79- pub async fn sign_up_email_verified (
80- & self ,
81- email : & str ,
82- password : & str ,
83- ) -> Result < bool , ClientResponse > {
84- if let Err ( err) = self . client . sign_up ( email, password) . await {
85- return Err ( ClientResponse {
86- code : err. code ,
87- message : err. message . to_string ( ) ,
88- } ) ;
75+ pub fn subscribe ( & self ) {
76+ let mut rx = self . client . subscribe_token_state ( ) ;
77+ let client = self . client . clone ( ) ;
78+
79+ wasm_bindgen_futures:: spawn_local ( async move {
80+ while let Ok ( state) = rx. recv ( ) . await {
81+ match state {
82+ TokenState :: Refresh => {
83+ if let Ok ( token) = client. get_token ( ) {
84+ refresh_token ( token. as_str ( ) ) ;
85+ } else {
86+ invalid_token ( ) ;
87+ }
88+ } ,
89+ TokenState :: Invalid => {
90+ invalid_token ( ) ;
91+ } ,
92+ }
93+ }
94+ } ) ;
95+ }
96+ pub async fn login ( & self , email : & str , password : & str ) -> Result < ( ) , ClientResponse > {
97+ match self . client . sign_in_password ( email, password) . await {
98+ Ok ( _) => Ok ( ( ) ) ,
99+ Err ( err) => Err ( ClientResponse :: from ( err) ) ,
89100 }
101+ }
90102
91- Ok ( true )
103+ pub async fn sign_up ( & self , email : & str , password : & str ) -> Result < ( ) , ClientResponse > {
104+ match self . client . sign_up ( email, password) . await {
105+ Ok ( _) => Ok ( ( ) ) ,
106+ Err ( err) => Err ( ClientResponse :: from ( err) ) ,
107+ }
92108 }
93109
94- pub async fn sign_in_password (
95- & self ,
96- email : & str ,
97- password : & str ,
98- ) -> Result < bool , ClientResponse > {
99- if let Err ( err) = self . client . sign_in_password ( email, password) . await {
100- return Err ( ClientResponse {
101- code : err. code ,
102- message : err. message . to_string ( ) ,
103- } ) ;
110+ pub async fn logout ( & self ) -> Result < ( ) , ClientResponse > {
111+ match self . client . sign_out ( ) . await {
112+ Ok ( _) => Ok ( ( ) ) ,
113+ Err ( err) => Err ( ClientResponse :: from ( err) ) ,
104114 }
115+ }
105116
106- Ok ( true )
117+ pub async fn get_user ( & self ) -> Result < User , ClientResponse > {
118+ match self . client . get_profile ( ) . await {
119+ Ok ( profile) => Ok ( User :: from ( profile) ) ,
120+ Err ( err) => Err ( ClientResponse :: from ( err) ) ,
121+ }
122+ }
123+
124+ pub fn restore_token ( & self , token : & str ) -> Result < ( ) , ClientResponse > {
125+ match self . client . restore_token ( token) {
126+ Ok ( _) => Ok ( ( ) ) ,
127+ Err ( err) => Err ( ClientResponse :: from ( err) ) ,
128+ }
129+ }
130+
131+ pub async fn get_collab (
132+ & self ,
133+ params : ClientQueryCollabParams ,
134+ ) -> Result < ClientEncodeCollab , ClientResponse > {
135+ tracing:: debug!( "get_collab: {:?}" , params) ;
136+ match self . client . get_collab ( params. into ( ) ) . await {
137+ Ok ( data) => Ok ( ClientEncodeCollab :: from ( data) ) ,
138+ Err ( err) => Err ( ClientResponse :: from ( err) ) ,
139+ }
107140 }
108141}
0 commit comments