Module repository

Source
Expand description

To support clients implementing the Repository trait in a ::wasm_bindgen environment, we need to deal with an extern "C" interface, as that is what ::wasm_bindgen supports:

This looks something like this:

β“˜
#[wasm_bindgen]
extern "C" {
    pub type CipherRepository;

    #[wasm_bindgen(method, catch)]
    async fn get(this: &CipherRepository, id: String) -> Result<JsValue, JsValue>;
}

As you can see, this has a few limitations:

  • The type must be known at compile time, so we cannot use generics directly, which means we can’t use the existing Repository trait directly.
  • The return type must be JsValue, so we need to convert our types to and from JsValue.

To facilitate this, we provide some utilities:

MacrosΒ§

create_wasm_repository πŸ”’
This macro generates a ::wasm_bindgen interface for a repository type, and provides the implementation of WasmRepository and a way to convert it into something that implements the Repository trait.

StructsΒ§

WasmRepositoryChannel πŸ”’
This struct wraps a WasmRepository in a ThreadBoundRunner to allow it to be used as a Repository in a thread-safe manner. It implements the Repository trait directly, by converting the values as needed with [tsify::serde_wasm_bindgen].

ConstantsΒ§

UNIT πŸ”’

TraitsΒ§

WasmRepository πŸ”’
This trait defines the methods that a ::wasm_bindgen repository must implement. The trait itself exists to provide a generic way of handling the ::wasm_bindgen interface, which is !Send + !Sync, and only deals with JsValue.

FunctionsΒ§

convert_result πŸ”’
Converts a Result<JsValue, JsValue> to a typed Result<T, RepositoryError> using [tsify::serde_wasm_bindgen]
run_convert πŸ”’
Utility function that runs a closure in a thread-bound manner, and converts the Result from Result<JsValue, JsValue> to a typed Result<T, RepositoryError>.