-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature swate refactore local storage usage #602
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
35ca202
Release v1.0.0-beta.19 :bookmark:
Etschbeijer d647cc5
Store data in indexedDB
Etschbeijer 65ecff6
Enable storing and retryfing data from local storage of newly compres…
Etschbeijer fe0cd8d
Enable creation, updating and reading from indexedDB
Etschbeijer 49dd0d6
Enable undo / redo funktionalität mit indexedDB
Etschbeijer 380074b
Implement most review suggestions
Etschbeijer 2287755
Implement last of pr review
Etschbeijer 2f017de
Fixed parameter that was too much
Etschbeijer 2e6da4a
Implement pr review suggestion
Etschbeijer 456965c
Moved promise start outside of function
Etschbeijer 4e60af4
Implement requested review changes
Etschbeijer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
module IndexedDB | ||
|
||
open Fable.Core | ||
open JsInterop | ||
|
||
open System | ||
|
||
/// <summary> | ||
/// Create or update an indexed database | ||
/// </summary> | ||
/// <param name="dbName"></param> | ||
/// <param name="version"></param> | ||
let createDatabase (dbName: string) version tableKey = | ||
promise { | ||
let indexedDB = emitJsExpr<obj>("globalThis.indexedDB") "globalThis.indexedDB" | ||
let request: obj = indexedDB?``open``(dbName, version) | ||
let! db = | ||
Async.FromContinuations(fun (resolve, reject, _) -> | ||
request?onsuccess <- fun _ -> resolve(request?result) | ||
request?onerror <- fun _ -> reject(new Exception(request?error?message)) | ||
request?onupgradeneeded <- fun e -> | ||
let resultDb = e?target?result | ||
if resultDb?objectStoreNames?contains(tableKey) |> not then | ||
let _ = resultDb?createObjectStore(tableKey) | ||
resolve(request?result) | ||
else | ||
resolve(request?result) | ||
) | ||
|> Async.StartAsPromise | ||
return db | ||
} | ||
|
||
/// <summary> | ||
/// Create or update an indexed database | ||
/// </summary> | ||
/// <param name="dbName"></param> | ||
/// <param name="version"></param> | ||
let rec openDatabase (dbName: string) (tableKey: string) = | ||
promise { | ||
let indexedDB = emitJsExpr<obj>("globalThis.indexedDB") "globalThis.indexedDB" | ||
let request: obj = indexedDB?``open``(dbName) | ||
let! db = | ||
Async.FromContinuations(fun (resolve, reject, _) -> | ||
request?onsuccess <- fun e -> | ||
let resultDb = e?target?result | ||
let version = resultDb?version | ||
if resultDb?objectStoreNames?contains(tableKey) |> not then | ||
resultDb?close() // Close the current db instance to avoid transaction problems | ||
let _ = | ||
createDatabase dbName (version + 1) tableKey | ||
|> Promise.start | ||
resolve(None) | ||
else | ||
resolve(Some request?result) | ||
request?onerror <- fun _ -> reject(new Exception(request?error?message)) | ||
) | ||
|> Async.StartAsPromise | ||
match db with | ||
| Some db -> return db | ||
| None -> return! openDatabase dbName tableKey | ||
} | ||
|
||
/// <summary> | ||
/// Closes the given db and associated transactions | ||
/// </summary> | ||
/// <param name="db"></param> | ||
let closeDatabase db = | ||
db?close() | ||
|
||
/// <summary> | ||
/// Initializes the indexedDB and its associated tables | ||
/// </summary> | ||
/// <param name="dbName"></param> | ||
/// <param name="tableKeys"></param> | ||
let initInidexedDB (dbName: string) (tableKeys: string []) = | ||
promise { | ||
for tableKey in tableKeys do | ||
let! db = openDatabase dbName tableKey | ||
closeDatabase db | ||
} | ||
|
||
let clearTable db (tableKey: string) = | ||
promise { | ||
if db?objectStoreNames?contains(tableKey) then | ||
let transaction = db?transaction(tableKey, "readwrite") | ||
let store = transaction?objectStore(tableKey) | ||
let storeRequest = store?clear() | ||
do! Async.FromContinuations(fun (resolve, reject, _) -> | ||
storeRequest?onsuccess <- fun _ -> resolve(storeRequest?result) | ||
storeRequest?onerror <- fun _ -> reject(new Exception(storeRequest?error?message)) | ||
) | ||
|> Async.StartAsPromise | ||
} | ||
|
||
let clearInidexedDB (dbName: string) (tableKeys: string []) = | ||
promise { | ||
for tableKey in tableKeys do | ||
let! db = openDatabase dbName tableKey | ||
do! clearTable db tableKey | ||
closeDatabase db | ||
} | ||
|
||
/// <summary> | ||
/// Add item to indexedDB | ||
/// </summary> | ||
/// <param name="db"></param> | ||
/// <param name="tableKey"></param> | ||
/// <param name="item"></param> | ||
/// <param name="key"></param> | ||
let addItem db (tableKey: string) (item: obj) (key: string) = | ||
promise { | ||
if db?objectStoreNames?contains(tableKey) then | ||
let transaction = db?transaction(tableKey, "readwrite") | ||
let store = transaction?objectStore(tableKey) | ||
let storeRequest = store?add(item, key) | ||
do! Async.FromContinuations(fun (resolve, reject, _) -> | ||
storeRequest?onsuccess <- fun _ -> resolve(storeRequest?result) | ||
storeRequest?onerror <- fun _ -> reject(new Exception(storeRequest?error?message)) | ||
) | ||
|> Async.StartAsPromise | ||
} | ||
|
||
/// <summary> | ||
/// Delete item from indexedDB | ||
/// </summary> | ||
/// <param name="db"></param> | ||
/// <param name="tableKey"></param> | ||
/// <param name="key"></param> | ||
let deleteItem db (tableKey: string) (key: string) = | ||
promise { | ||
if db?objectStoreNames?contains(tableKey) then | ||
let transaction = db?transaction(tableKey, "readwrite") | ||
let store = transaction?objectStore(tableKey) | ||
let storeRequest = store?delete(key) | ||
do! Async.FromContinuations(fun (resolve, reject, _) -> | ||
storeRequest?onsuccess <- fun _ -> resolve(storeRequest?result) | ||
storeRequest?onerror <- fun _ -> reject(new Exception(storeRequest?error?message)) | ||
) | ||
|> Async.StartAsPromise | ||
} | ||
|
||
/// <summary> | ||
/// Update an existing item in the IndexedDB object store | ||
/// </summary> | ||
/// <param name="db"></param> | ||
/// <param name="storeName"></param> | ||
/// <param name="data"></param> | ||
let updateItem db (tableKey: string) (item: obj) (key: string) = | ||
promise { | ||
if db?objectStoreNames?contains(tableKey) then | ||
let transaction = db?transaction(tableKey, "readwrite") | ||
let store = transaction?objectStore(tableKey) | ||
let storeRequest = store?put(item, key) | ||
do! Async.FromContinuations(fun (resolve, reject, _) -> | ||
storeRequest?onsuccess <- fun _ -> resolve(storeRequest?result) | ||
storeRequest?onerror <- fun _ -> reject(new Exception(storeRequest?error?message)) | ||
) | ||
|> Async.StartAsPromise | ||
} | ||
|
||
/// <summary> | ||
/// Retrive a specific item from the database | ||
/// </summary> | ||
/// <param name="db"></param> | ||
/// <param name="localStorage"></param> | ||
/// <param name="key"></param> | ||
let tryGetItem (db: obj) (tableKey: string) (key: string) = | ||
promise { | ||
if db?objectStoreNames?contains(tableKey) then | ||
let transaction = db?transaction(tableKey, "readonly") | ||
let store = transaction?objectStore(tableKey) | ||
let storeRequest = store?get(key) | ||
let! item = | ||
Async.FromContinuations(fun (resolve, reject, _) -> | ||
storeRequest?onsuccess <- fun _ -> resolve(storeRequest?result) | ||
storeRequest?onerror <- fun _ -> reject(new Exception(storeRequest?error?message)) | ||
) | ||
|> Async.StartAsPromise | ||
|
||
if isNullOrUndefined item then | ||
return None | ||
else | ||
let result = item.ToString() | ||
if String.IsNullOrEmpty result then | ||
return None | ||
else return Some result | ||
else | ||
return None | ||
} | ||
|
||
/// <summary> | ||
/// Retrive all items from the database | ||
/// </summary> | ||
/// <param name="db"></param> | ||
/// <param name="localStorage"></param> | ||
let getAllItems (db: obj) (tableKey: string) = | ||
promise { | ||
if db?objectStoreNames?contains(tableKey) then | ||
let transaction = db?transaction(tableKey, "readonly") | ||
let store = transaction?objectStore(tableKey) | ||
let storeRequest = store?getAll() | ||
|
||
let! item = | ||
Async.FromContinuations(fun (resolve, reject, _) -> | ||
storeRequest?onsuccess <- fun _ -> resolve(storeRequest?result) | ||
storeRequest?onerror <- fun _ -> reject(new Exception(storeRequest?error?message "Failed to open database")) | ||
) | ||
|> Async.StartAsPromise | ||
|
||
if isNullOrUndefined item then | ||
return None | ||
else | ||
let result = item.ToString() | ||
if String.IsNullOrEmpty result then | ||
return None | ||
else return Some result | ||
else | ||
return None | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At this point it might be a good idea to write a function
UpdateAnd of Messages.Msg * Cmd<Msg>