Skip to content
This repository has been archived by the owner on Jul 27, 2021. It is now read-only.

[MIRROR] Ports Important TGUI Fix #594

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 52 additions & 14 deletions tgui/packages/common/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,45 +44,44 @@ class MemoryBackend {
this.store = {};
}

async get(key) {
get(key) {
return this.store[key];
}

async set(key, value) {
set(key, value) {
this.store[key] = value;
}

async remove(key) {
remove(key) {
this.store[key] = undefined;
}

async clear() {
clear() {
this.store = {};
}
}

class LocalStorageBackend {
constructor() {
this.impl = IMPL_LOCAL_STORAGE;
this.store = {};
}

async get(key) {
get(key) {
const value = localStorage.getItem(key);
if (typeof value === 'string') {
return JSON.parse(value);
}
}

async set(key, value) {
set(key, value) {
localStorage.setItem(key, JSON.stringify(value));
}

async remove(key) {
remove(key) {
localStorage.removeItem(key);
}

async clear() {
clear() {
localStorage.clear();
}
}
Expand Down Expand Up @@ -150,8 +149,47 @@ class IndexedDbBackend {
}
}

export const storage = (
testIndexedDb() && new IndexedDbBackend()
|| testLocalStorage() && new LocalStorageBackend()
|| new MemoryBackend()
);
/**
* Web Storage Proxy object, which selects the best backend available
* depending on the environment.
*/
class StorageProxy {
constructor() {
this.backendPromise = (async () => {
if (testIndexedDb()) {
try {
const backend = new IndexedDbBackend();
await backend.dbPromise;
return backend;
}
catch {}
}
if (testLocalStorage()) {
return new LocalStorageBackend();
}
return new MemoryBackend();
})();
}

async get(key) {
const backend = await this.backendPromise;
return backend.get(key);
}

async set(key, value) {
const backend = await this.backendPromise;
return backend.set(key, value);
}

async remove(key) {
const backend = await this.backendPromise;
return backend.remove(key);
}

async clear() {
const backend = await this.backendPromise;
return backend.clear();
}
}

export const storage = new StorageProxy();
1 change: 1 addition & 0 deletions tgui/public/tgui-common.bundle.js

Large diffs are not rendered by default.