idb_sys/request/mod.rs
1mod database_request;
2mod request_ready_state;
3mod store_request;
4
5pub use self::{
6 database_request::DatabaseRequest, request_ready_state::RequestReadyState,
7 store_request::StoreRequest,
8};
9
10use js_sys::Object;
11use wasm_bindgen::JsValue;
12use web_sys::{DomException, Event};
13
14use crate::{Error, Transaction};
15
16/// Specifies all the functions supported by request objects.
17pub trait Request {
18 /// When a request is completed, returns the `result`, or `undefined` if the request failed. Returns an [`Error`] if
19 /// the request is still pending.
20 fn result(&self) -> Result<JsValue, Error>;
21
22 /// When a request is completed, returns the `error` (a `DOMException`), or `None` if the request succeeded. Returns
23 /// an [`Error`] if the request is still pending.
24 fn error(&self) -> Result<Option<DomException>, Error>;
25
26 /// Returns the `ObjectStore`, `Index`, or `Cursor` the request was made against, or `null` if it was an open
27 /// request.
28 fn source(&self) -> Result<Object, Error>; // TODO: make return type as enum: (IDBObjectStore or IDBIndex or IDBCursor)
29
30 /// Returns the `Transaction` the request was made within. If this as an open request, then it returns an upgrade
31 /// transaction while it is running, or `None` otherwise.
32 fn transaction(&self) -> Option<Transaction>;
33
34 /// Returns `RequestReadyState::Pending` until a request is complete, then returns `RequestReadyState::Done`.
35 fn ready_state(&self) -> Result<RequestReadyState, Error>;
36
37 /// Adds an event handler for `success` event.
38 fn on_success<F>(&mut self, callback: F)
39 where
40 F: FnOnce(Event) + 'static;
41
42 /// Adds an event handler for `error` event.
43 fn on_error<F>(&mut self, callback: F)
44 where
45 F: FnOnce(Event) + 'static;
46}