Skip to content

Commit

Permalink
start implementing transaction logic
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbenington committed Dec 2, 2024
1 parent 4c1f27f commit c92dd86
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 75 deletions.
20 changes: 9 additions & 11 deletions src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::io::{Error, ErrorKind, Read, Write};
use std::path::PathBuf;
use std::time::SystemTime;

use crate::state::{self, AppState};
use crate::util;

#[tauri::command]
Expand Down Expand Up @@ -85,18 +86,15 @@ pub fn delete_storage_files(
return result;
}

// #[derive(Default)]
// struct MyState {
// s: std::sync::Mutex<String>,
// t: std::sync::Mutex<std::collections::HashMap<String, String>>,
// }
// remember to call `.manage(MyState::default())`
// #[tauri::command]
// async fn command_name(state: tauri::State<'_, MyState>) -> Result<(), String> {
// *state.s.lock().unwrap() = "new string".into();
// state.t.lock().unwrap().insert("key".into(), "value".into());
// Ok(())
// }]
#[tauri::command]
pub fn start_transaction(state: tauri::State<'_, AppState>) -> Result<(), String> {
if *state.open_transaction.lock().unwrap() {
return Err("Previous transaction is still open".to_owned());
}
*state.open_transaction.lock().unwrap() = true;
Ok(())
}

#[tauri::command]
pub fn write_file_bytes(absolute_path: PathBuf, bytes: Vec<u8>) -> Result<(), String> {
Expand Down
5 changes: 4 additions & 1 deletion src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod commands;
mod menu;
mod state;
mod util;
use std::{
env,
Expand Down Expand Up @@ -32,6 +33,7 @@ pub fn run() {
.plugin(tauri_plugin_os::init())
.plugin(tauri_plugin_dialog::init())
.plugin(tauri_plugin_shell::init())
.manage(state::AppState::default())
.invoke_handler(tauri::generate_handler![
commands::get_file_bytes,
commands::get_file_created,
Expand All @@ -40,7 +42,8 @@ pub fn run() {
commands::write_file_bytes,
commands::write_storage_file_bytes,
commands::get_ohpkm_files,
commands::delete_storage_files
commands::delete_storage_files,
commands::start_transaction,
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
Expand Down
7 changes: 7 additions & 0 deletions src-tauri/src/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use std::path::PathBuf;

#[derive(Default)]
pub struct AppState {
pub open_transaction: std::sync::Mutex<bool>,
pub temp_files: std::sync::Mutex<Vec<PathBuf>>,
}
106 changes: 44 additions & 62 deletions src/backend/tauri/tauriInvoker.tsx
Original file line number Diff line number Diff line change
@@ -1,113 +1,95 @@
import { invoke } from "@tauri-apps/api/core";
import * as E from "fp-ts/lib/Either";
import { Errorable, JSONArray, JSONObject, JSONValue } from "src/types/types";
import { RustResult } from "./types";
import { invoke } from '@tauri-apps/api/core'
import * as E from 'fp-ts/lib/Either'
import { Errorable, JSONArray, JSONObject, JSONValue } from 'src/types/types'
import { RustResult } from './types'

function rustResultToEither<T, E>(result: RustResult<T, E>): E.Either<E, T> {
return "Ok" in result ? E.right(result.Ok) : E.left(result.Err);
return 'Ok' in result ? E.right(result.Ok) : E.left(result.Err)
}

export const TauriInvoker = {
getFileBytes(absolutePath: string): Promise<Errorable<Uint8Array>> {
const promise: Promise<number[]> = invoke("get_file_bytes", {
const promise: Promise<number[]> = invoke('get_file_bytes', {
absolutePath,
});
return promise.then((u8s) => E.right(new Uint8Array(u8s))).catch(E.left);
})
return promise.then((u8s) => E.right(new Uint8Array(u8s))).catch(E.left)
},

getFileCreated(absolutePath: string): Promise<Errorable<Date>> {
const promise: Promise<number> = invoke("get_file_created", {
const promise: Promise<number> = invoke('get_file_created', {
absolutePath,
});
return promise
.then((unixMillis) => E.right(new Date(unixMillis)))
.catch(E.left);
})
return promise.then((unixMillis) => E.right(new Date(unixMillis))).catch(E.left)
},

getStorageFileJSON(
relativePath: string
): Promise<Errorable<JSONObject | JSONArray>> {
const promise: Promise<JSONObject | JSONArray> = invoke(
"get_storage_file_json",
{
relativePath,
}
);
return promise.then(E.right).catch(E.left);
getStorageFileJSON(relativePath: string): Promise<Errorable<JSONObject | JSONArray>> {
const promise: Promise<JSONObject | JSONArray> = invoke('get_storage_file_json', {
relativePath,
})
return promise.then(E.right).catch(E.left)
},

writeStorageFileJSON(
relativePath: string,
data: JSONValue
): Promise<Errorable<null>> {
writeStorageFileJSON(relativePath: string, data: JSONValue): Promise<Errorable<null>> {
console.log({
relativePath,
data,
});
const promise: Promise<null> = invoke("write_storage_file_json", {
})
const promise: Promise<null> = invoke('write_storage_file_json', {
relativePath,
data,
});
return promise.then(E.right).catch(E.left);
})
return promise.then(E.right).catch(E.left)
},

writeFileBytes(
absolutePath: string,
bytes: Uint8Array
): Promise<Errorable<null>> {
const promise: Promise<null> = invoke("write_file_bytes", {
writeFileBytes(absolutePath: string, bytes: Uint8Array): Promise<Errorable<null>> {
const promise: Promise<null> = invoke('write_file_bytes', {
absolutePath,
bytes,
});
return promise.then(E.right).catch(E.left);
})
return promise.then(E.right).catch(E.left)
},

writeStorageFileBytes(
relativePath: string,
bytes: Uint8Array
): Promise<Errorable<null>> {
const promise: Promise<null> = invoke("write_storage_file_bytes", {
writeStorageFileBytes(relativePath: string, bytes: Uint8Array): Promise<Errorable<null>> {
const promise: Promise<null> = invoke('write_storage_file_bytes', {
relativePath,
bytes,
});
return promise.then(E.right).catch(E.left);
})
return promise.then(E.right).catch(E.left)
},

async getOHPKMFiles(): Promise<Errorable<Record<string, Uint8Array>>> {
const promise: Promise<Record<string, number[]>> =
invoke("get_ohpkm_files");
const promise: Promise<Record<string, number[]>> = invoke('get_ohpkm_files')
return promise
.then((result) => {
return E.right(
Object.fromEntries(
Object.entries(result).map(([filename, bytes]) => [
filename,
new Uint8Array(bytes),
])
Object.entries(result).map(([filename, bytes]) => [filename, new Uint8Array(bytes)])
)
);
)
})
.catch(E.left);
.catch(E.left)
},

async deleteStorageFiles(
relativePaths: string[]
): Promise<Errorable<Record<string, Errorable<null>>>> {
const promise: Promise<Record<string, RustResult<null, string>>> = invoke(
"delete_storage_files",
'delete_storage_files',
{ relativePaths }
);
)
return promise
.then((result) => {
return E.right(
Object.fromEntries(
Object.entries(result).map(([file, result]) => [
file,
rustResultToEither(result),
])
Object.entries(result).map(([file, result]) => [file, rustResultToEither(result)])
)
);
)
})
.catch(E.left);
.catch(E.left)
},
};

startTransaction(): Promise<Errorable<null>> {
const promise: Promise<null> = invoke('start_transaction')
return promise.then(E.right).catch(E.left)
},
}
2 changes: 1 addition & 1 deletion src/backend/tauriBackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export const TauriBackend: BackendInterface = {

/* transactions */
startTransaction: async (): Promise<Errorable<null>> => {
return E.left('Not implemented')
return TauriInvoker.startTransaction()
},
commitTransaction: async (): Promise<Errorable<null>> => {
return E.left('Not implemented')
Expand Down

0 comments on commit c92dd86

Please sign in to comment.