forked from LNReader/lnreader
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Plugin storages (LNReader#986)
* test * fix * Update pluginManager.ts * test * fix lint * test * test webview * sync * / * Update WebviewScreen.tsx * CookieManager * Update storage.ts * Update сookie.ts * fix title * MarqueeText... * Slow * Even slower * Squashed commit of the following: commit da4d7e8 Author: Rider21 <58046032+Rider21@users.noreply.github.com> Date: Fri Mar 8 13:21:19 2024 +0300 / commit 592c4db Author: Rider21 <58046032+Rider21@users.noreply.github.com> Date: Fri Mar 8 13:20:34 2024 +0300 test commit 6324050 Author: Rider21 <58046032+Rider21@users.noreply.github.com> Date: Fri Mar 8 12:38:13 2024 +0300 / commit 591d259 Author: Rider21 <58046032+Rider21@users.noreply.github.com> Date: Fri Mar 8 11:42:03 2024 +0300 fix progressbar commit 3d34cfb Author: Rider21 <58046032+Rider21@users.noreply.github.com> Date: Thu Mar 7 21:13:49 2024 +0300 forgot commit c36d090 Author: Rider21 <58046032+Rider21@users.noreply.github.com> Date: Thu Mar 7 20:51:31 2024 +0300 / commit 3c4e43b Author: Rider21 <58046032+Rider21@users.noreply.github.com> Date: Thu Mar 7 20:15:38 2024 +0300 / commit 0a20a48 Author: Rider21 <58046032+Rider21@users.noreply.github.com> Date: Thu Mar 7 19:46:31 2024 +0300 add zIndex commit 7d44901 Author: Rider21 <58046032+Rider21@users.noreply.github.com> Date: Thu Mar 7 19:27:32 2024 +0300 test commit b585d5e Author: Rider21 <58046032+Rider21@users.noreply.github.com> Date: Thu Mar 7 18:28:49 2024 +0300 / commit cb7241c Author: Rider21 <58046032+Rider21@users.noreply.github.com> Date: Thu Mar 7 18:06:30 2024 +0300 Update Appbar.tsx commit 5fab766 Author: Rider21 <58046032+Rider21@users.noreply.github.com> Date: Thu Mar 7 17:55:24 2024 +0300 Update WebviewScreen.tsx commit aec9b92 Author: Rider21 <58046032+Rider21@users.noreply.github.com> Date: Thu Mar 7 17:15:16 2024 +0300 xd commit 1fa385b Author: Rider21 <58046032+Rider21@users.noreply.github.com> Date: Thu Mar 7 17:11:23 2024 +0300 2 commit 589870d Author: Rider21 <58046032+Rider21@users.noreply.github.com> Date: Thu Mar 7 16:28:24 2024 +0300 1 commit 343d2dd Author: Rider21 <58046032+Rider21@users.noreply.github.com> Date: Thu Mar 7 16:11:08 2024 +0300 test commit f2dbf34 Author: Rider21 <58046032+Rider21@users.noreply.github.com> Date: Thu Mar 7 14:36:48 2024 +0300 test commit 04c04d9 Author: Rider21 <58046032+Rider21@users.noreply.github.com> Date: Wed Mar 6 22:37:45 2024 +0300 test zoom commit 6b80559 Author: Rider21 <58046032+Rider21@users.noreply.github.com> Date: Wed Mar 6 22:33:55 2024 +0300 fix commit 287ce94 Author: Rider21 <58046032+Rider21@users.noreply.github.com> Date: Wed Mar 6 19:00:01 2024 +0300 test commit 67bdddc Author: Rider21 <58046032+Rider21@users.noreply.github.com> Date: Wed Mar 6 11:10:22 2024 +0300 Test newArch commit 7623537 Author: Rider21 <58046032+Rider21@users.noreply.github.com> Date: Tue Mar 5 20:49:29 2024 +0300 Update Appbar.tsx commit 2a876cb Author: Rider21 <58046032+Rider21@users.noreply.github.com> Date: Tue Mar 5 20:42:28 2024 +0300 modal commit ba4cade Author: Rider21 <58046032+Rider21@users.noreply.github.com> Date: Tue Mar 5 16:50:38 2024 +0300 1 commit 6c14a18 Author: Rider21 <58046032+Rider21@users.noreply.github.com> Date: Tue Mar 5 14:54:27 2024 +0300 test * fix * fix * Update pluginManager.ts * test * fix * fix lint * -marquee * fix defaultCover * test * Update src/plugins/helpers/storage.ts Co-authored-by: nyagami <hoangquan05112002@gmail.com> * test * Typescript fixes * private pluginID --------- Co-authored-by: nyagami <hoangquan05112002@gmail.com>
- Loading branch information
Showing
18 changed files
with
401 additions
and
145 deletions.
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const defaultCover = | ||
'https://github.com/LNReader/lnreader-plugins/blob/master/icons/coverNotAvailable.webp?raw=true'; |
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,123 @@ | ||
import { MMKV } from 'react-native-mmkv'; | ||
|
||
const store = new MMKV({ id: 'plugin_db' }); | ||
|
||
const PLUGIN_STORAGE = '_DB_'; | ||
const WEBVIEW_LOCAL_STORAGE = '_LocalStorage'; | ||
const WEBVIEW_SESSION_STORAGE = '_SessionStorage'; | ||
|
||
interface StoredItem { | ||
created: Date; | ||
value: any; | ||
expires?: number; // timestamp (miliseconds) | ||
} | ||
|
||
class Storage { | ||
#pluginID: string; | ||
|
||
constructor(pluginID: string) { | ||
this.#pluginID = pluginID; | ||
} | ||
|
||
/** | ||
* Sets a key-value pair in storage. | ||
* | ||
* @param {string} key - The key to set. | ||
* @param {any} value - The value to set. | ||
* @param {Date | number} [expires] - Optional expiry date or time in milliseconds. | ||
*/ | ||
set(key: string, value: any, expires?: Date | number): void { | ||
const item: StoredItem = { | ||
created: new Date(), | ||
value, | ||
expires: expires instanceof Date ? expires.getTime() : expires, | ||
}; | ||
store.set(this.#pluginID + PLUGIN_STORAGE + key, JSON.stringify(item)); | ||
} | ||
|
||
/** | ||
* Retrieves the value for a given key from storage. | ||
* | ||
* @param {string} key - The key to retrieve the value for. | ||
* @param {boolean} [raw] - Optional flag to return the raw stored item. | ||
* @returns {any} The stored value or undefined if key is not found. | ||
*/ | ||
get(key: string, raw?: boolean): any { | ||
const storedItem = store.getString(this.#pluginID + PLUGIN_STORAGE + key); | ||
if (storedItem) { | ||
const item: StoredItem = JSON.parse(storedItem); | ||
if (item.expires) { | ||
if (Date.now() > item.expires) { | ||
this.delete(key); | ||
return undefined; | ||
} | ||
if (raw) { | ||
item.expires = new Date(item.expires).getTime(); | ||
} | ||
} | ||
return raw ? item : item.value; | ||
} | ||
return undefined; | ||
} | ||
|
||
/** | ||
* Deletes a key from storage. | ||
* | ||
* @param {string} key - The key to delete. | ||
*/ | ||
delete(key: string): void { | ||
store.delete(this.#pluginID + PLUGIN_STORAGE + key); | ||
} | ||
|
||
/** | ||
* Clears all stored items from storage. | ||
*/ | ||
clearAll(): void { | ||
const keysToRemove = this.getAllKeys(); | ||
keysToRemove.forEach(key => this.delete(key)); | ||
} | ||
|
||
/** | ||
* Retrieves all keys set by the `set` method. | ||
* | ||
* @returns {string[]} An array of keys. | ||
*/ | ||
getAllKeys(): string[] { | ||
const keys = store | ||
.getAllKeys() | ||
.filter(key => key.startsWith(this.#pluginID + PLUGIN_STORAGE)) | ||
.map(key => key.replace(this.#pluginID + PLUGIN_STORAGE, '')); | ||
return keys; | ||
} | ||
} | ||
|
||
class LocalStorage { | ||
#pluginID: string; | ||
|
||
constructor(pluginID: string) { | ||
this.#pluginID = pluginID; | ||
} | ||
|
||
get(): StoredItem['value'] | undefined { | ||
const data = store.getString(this.#pluginID + WEBVIEW_LOCAL_STORAGE); | ||
return data ? JSON.parse(data) : undefined; | ||
} | ||
} | ||
|
||
class SessionStorage { | ||
#pluginID: string; | ||
|
||
constructor(pluginID: string) { | ||
this.#pluginID = pluginID; | ||
} | ||
|
||
get(): StoredItem['value'] | undefined { | ||
const data = store.getString(this.#pluginID + WEBVIEW_SESSION_STORAGE); | ||
return data ? JSON.parse(data) : undefined; | ||
} | ||
} | ||
|
||
export { Storage, LocalStorage, SessionStorage }; | ||
|
||
//to record data from the web view | ||
export { WEBVIEW_LOCAL_STORAGE, WEBVIEW_SESSION_STORAGE, store }; |
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
Oops, something went wrong.