1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use js_sys::Reflect;
use wasm_bindgen::{JsCast, JsValue};
use web_sys::IdbFactory;

use crate::{DatabaseRequest, Error};

/// Lets applications asynchronously access the indexed databases.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Factory {
    inner: IdbFactory,
}

impl Factory {
    /// Gets an instance of [Factory] from `global` scope.
    pub fn new() -> Result<Factory, Error> {
        let inner = Reflect::get(&js_sys::global(), &JsValue::from("indexedDB"))
            .map_err(Error::IndexedDbNotFound)?
            .dyn_into()
            .map_err(Error::IndexedDbNotFound)?;
        Ok(Self { inner })
    }

    /// Attempts to open a connection to the named database with the specified version. If the database already exists
    /// with a lower version and there are open connections that don’t close in response to a `versionchange` event, the
    /// request will be blocked until they all close, then an upgrade will occur. If the database already exists with a
    /// higher version the request will fail.
    pub fn open(&self, name: &str, version: u32) -> Result<DatabaseRequest, Error> {
        self.inner
            .open_with_u32(name, version)
            .map(Into::into)
            .map_err(Error::IndexedDbOpenFailed)
    }

    /// Attempts to delete the named database. If the database already exists and there are open connections that don’t
    /// close in response to a `versionchange` event, the request will be blocked until they all close.
    pub fn delete(&self, name: &str) -> Result<DatabaseRequest, Error> {
        self.inner
            .delete_database(name)
            .map(Into::into)
            .map_err(Error::IndexedDbDeleteFailed)
    }
}

impl From<IdbFactory> for Factory {
    fn from(inner: IdbFactory) -> Self {
        Self { inner }
    }
}

impl From<Factory> for IdbFactory {
    fn from(factory: Factory) -> Self {
        factory.inner
    }
}

impl TryFrom<JsValue> for Factory {
    type Error = Error;

    fn try_from(value: JsValue) -> Result<Self, Self::Error> {
        value
            .dyn_into::<IdbFactory>()
            .map(Into::into)
            .map_err(|value| Error::UnexpectedJsType("IdbFactory", value))
    }
}

impl From<Factory> for JsValue {
    fn from(value: Factory) -> Self {
        value.inner.into()
    }
}