mirror of
https://github.com/VOREStation/VOREStation.git
synced 2026-08-30 23:49:21 +01:00
storage to typescript (#17144)
* storage to typescript * fix * keys * keys
This commit is contained in:
@@ -10,6 +10,11 @@ export const IMPL_MEMORY = 0;
|
||||
export const IMPL_HUB_STORAGE = 1;
|
||||
export const IMPL_INDEXED_DB = 2;
|
||||
|
||||
type StorageImplementation =
|
||||
| typeof IMPL_MEMORY
|
||||
| typeof IMPL_HUB_STORAGE
|
||||
| typeof IMPL_INDEXED_DB;
|
||||
|
||||
const INDEXED_DB_VERSION = 1;
|
||||
const INDEXED_DB_NAME = 'virgo';
|
||||
const INDEXED_DB_STORE_NAME = 'storage-v1';
|
||||
@@ -17,7 +22,15 @@ const INDEXED_DB_STORE_NAME = 'storage-v1';
|
||||
const READ_ONLY = 'readonly';
|
||||
const READ_WRITE = 'readwrite';
|
||||
|
||||
const testGeneric = (testFn) => () => {
|
||||
type StorageBackend = {
|
||||
impl: StorageImplementation;
|
||||
get(key: string): Promise<any>;
|
||||
set(key: string, value: any): Promise<void>;
|
||||
remove(key: string): Promise<void>;
|
||||
clear(): Promise<void>;
|
||||
};
|
||||
|
||||
const testGeneric = (testFn: () => boolean) => (): boolean => {
|
||||
try {
|
||||
return Boolean(testFn());
|
||||
} catch {
|
||||
@@ -26,30 +39,33 @@ const testGeneric = (testFn) => () => {
|
||||
};
|
||||
|
||||
const testHubStorage = testGeneric(
|
||||
() => window.hubStorage && window.hubStorage.getItem,
|
||||
() => window.hubStorage && !!window.hubStorage.getItem,
|
||||
);
|
||||
|
||||
class HubStorageBackend {
|
||||
class HubStorageBackend implements StorageBackend {
|
||||
public impl: StorageImplementation;
|
||||
|
||||
constructor() {
|
||||
this.impl = IMPL_HUB_STORAGE;
|
||||
}
|
||||
|
||||
async get(key) {
|
||||
const value = await window.hubStorage.getItem('virgo-' + key);
|
||||
async get(key: string): Promise<any> {
|
||||
const value = await window.hubStorage.getItem(key);
|
||||
if (typeof value === 'string') {
|
||||
return JSON.parse(value);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
set(key, value) {
|
||||
window.hubStorage.setItem('virgo-' + key, JSON.stringify(value));
|
||||
async set(key: string, value: any): Promise<void> {
|
||||
window.hubStorage.setItem(key, JSON.stringify(value));
|
||||
}
|
||||
|
||||
remove(key) {
|
||||
window.hubStorage.removeItem('virgo-' + key);
|
||||
async remove(key: string): Promise<void> {
|
||||
window.hubStorage.removeItem(key);
|
||||
}
|
||||
|
||||
clear() {
|
||||
async clear(): Promise<void> {
|
||||
window.hubStorage.clear();
|
||||
}
|
||||
}
|
||||
@@ -58,7 +74,10 @@ class HubStorageBackend {
|
||||
* Web Storage Proxy object, which selects the best backend available
|
||||
* depending on the environment.
|
||||
*/
|
||||
class StorageProxy {
|
||||
class StorageProxy implements StorageBackend {
|
||||
private backendPromise: Promise<StorageBackend>;
|
||||
public impl: StorageImplementation = IMPL_MEMORY;
|
||||
|
||||
constructor() {
|
||||
this.backendPromise = (async () => {
|
||||
if (!Byond.TRIDENT) {
|
||||
@@ -74,25 +93,25 @@ class StorageProxy {
|
||||
}
|
||||
return new HubStorageBackend();
|
||||
}
|
||||
})();
|
||||
})() as Promise<StorageBackend>;
|
||||
}
|
||||
|
||||
async get(key) {
|
||||
async get(key: string): Promise<any> {
|
||||
const backend = await this.backendPromise;
|
||||
return backend.get(key);
|
||||
}
|
||||
|
||||
async set(key, value) {
|
||||
async set(key: string, value: any): Promise<void> {
|
||||
const backend = await this.backendPromise;
|
||||
return backend.set(key, value);
|
||||
}
|
||||
|
||||
async remove(key) {
|
||||
async remove(key: string): Promise<void> {
|
||||
const backend = await this.backendPromise;
|
||||
return backend.remove(key);
|
||||
}
|
||||
|
||||
async clear() {
|
||||
async clear(): Promise<void> {
|
||||
const backend = await this.backendPromise;
|
||||
return backend.clear();
|
||||
}
|
||||
@@ -51,11 +51,11 @@ export function TguiSay() {
|
||||
const [lightMode, setLightMode] = useState(false);
|
||||
const [value, setValue] = useState('');
|
||||
|
||||
function handleArrowKeys(direction: KEY.Up | KEY.Down): void {
|
||||
function handleArrowKeys(direction: KEY.PageUp | KEY.PageDown): void {
|
||||
const chat = chatHistory.current;
|
||||
const iterator = channelIterator.current;
|
||||
|
||||
if (direction === KEY.Up) {
|
||||
if (direction === KEY.PageUp) {
|
||||
if (chat.isAtLatest() && value) {
|
||||
// Save current message to temp history if at the most recent message
|
||||
chat.saveTemp(value);
|
||||
@@ -234,8 +234,8 @@ export function TguiSay() {
|
||||
event.currentTarget.selectionEnd = selectionEnd + 2;
|
||||
}
|
||||
break;
|
||||
case KEY.Up:
|
||||
case KEY.Down:
|
||||
case KEY.PageUp:
|
||||
case KEY.PageDown:
|
||||
event.preventDefault();
|
||||
handleArrowKeys(event.key);
|
||||
break;
|
||||
|
||||
@@ -48,7 +48,7 @@ function setWindowVisibility(visible: boolean): void {
|
||||
});
|
||||
}
|
||||
|
||||
const CHANNEL_REGEX = /^[:.]\w|,b\s/;
|
||||
const CHANNEL_REGEX = /^[:.]\w\s|^,b\s/;
|
||||
|
||||
/** Tests for a channel prefix, returning it or none */
|
||||
export function getPrefix(
|
||||
|
||||
Reference in New Issue
Block a user