Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
edsilv committed Mar 13, 2023
1 parent ee5930b commit a9b3baa
Show file tree
Hide file tree
Showing 9 changed files with 840 additions and 540 deletions.
136 changes: 105 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
"@iiif/iiif-gallery-component": "^1.1.21",
"@iiif/iiif-metadata-component": "^1.1.19",
"@iiif/iiif-tree-component": "^2.0.3",
"@iiif/manifold": "2.0.23",
"@iiif/manifold": "2.0.31",
"@iiif/presentation-3": "^1.0.5",
"@iiif/vocabulary": "^1.0.23",
"@openseadragon-imaging/openseadragon-viewerinputhook": "^2.2.1",
Expand All @@ -107,7 +107,7 @@
"jquery": "3.5.0",
"jsviews": "0.9.83",
"less-plugin-clean-css": "^1.5.1",
"manifesto.js": "4.2.16",
"manifesto.js": "4.2.17",
"mediaelement": "4.2.15",
"mediaelement-plugins": "2.5.1",
"openseadragon": "2.4.2",
Expand Down
186 changes: 186 additions & 0 deletions src/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,189 @@ export const defaultLocale = {
export const getUUID = () => {
return URL.createObjectURL(new Blob()).substr(-36);
};

export class Storage {
private static _memoryStorage: any = {};

public static clear(storageType: StorageType = StorageType.MEMORY): void {
switch (storageType) {
case StorageType.MEMORY:
this._memoryStorage = {};
break;
case StorageType.SESSION:
sessionStorage.clear();
break;
case StorageType.LOCAL:
localStorage.clear();
break;
}
}

public static clearExpired(
storageType: StorageType = StorageType.MEMORY
): void {
const items: StorageItem[] = this.getItems(storageType);

for (let i = 0; i < items.length; i++) {
var item = items[i];

if (this._isExpired(item)) {
this.remove(item.key);
}
}
}

public static get(
key: string,
storageType: StorageType = StorageType.MEMORY
): StorageItem | null {
let data: string | null = null;

switch (storageType) {
case StorageType.MEMORY:
data = this._memoryStorage[key];
break;
case StorageType.SESSION:
data = sessionStorage.getItem(key);
break;
case StorageType.LOCAL:
data = localStorage.getItem(key);
break;
}

if (!data) return null;

let item: StorageItem | null = null;

try {
item = JSON.parse(data);
} catch (error) {
return null;
}

if (!item) return null;

if (this._isExpired(item)) return null;

// useful reference
item.key = key;

return item;
}

private static _isExpired(item: StorageItem): boolean {
if (new Date().getTime() < item.expiresAt) {
return false;
}

return true;
}

public static getItems(
storageType: StorageType = StorageType.MEMORY
): StorageItem[] {
const items: StorageItem[] = [];

switch (storageType) {
case StorageType.MEMORY:
const keys: string[] = Object.keys(this._memoryStorage);

for (let i = 0; i < keys.length; i++) {
const item: StorageItem | null = this.get(
keys[i],
StorageType.MEMORY
);

if (item) {
items.push(item);
}
}

break;
case StorageType.SESSION:
for (let i = 0; i < sessionStorage.length; i++) {
const key: string | null = sessionStorage.key(i);

if (key) {
const item: StorageItem | null = this.get(key, StorageType.SESSION);

if (item) {
items.push(item);
}
}
}
break;
case StorageType.LOCAL:
for (let i = 0; i < localStorage.length; i++) {
const key: string | null = localStorage.key(i);

if (key) {
const item: StorageItem | null = this.get(key, StorageType.LOCAL);

if (item) {
items.push(item);
}
}
}
break;
}

return items;
}

public static remove(
key: string,
storageType: StorageType = StorageType.MEMORY
) {
switch (storageType) {
case StorageType.MEMORY:
delete this._memoryStorage[key];
break;
case StorageType.SESSION:
sessionStorage.removeItem(key);
break;
case StorageType.LOCAL:
localStorage.removeItem(key);
break;
}
}

public static set(
key: string,
value: any,
expirationSecs: number,
storageType: StorageType = StorageType.MEMORY
): StorageItem {
const expirationMS: number = expirationSecs * 1000;

const record: StorageItem = new StorageItem();
record.value = value;
record.expiresAt = new Date().getTime() + expirationMS;

switch (storageType) {
case StorageType.MEMORY:
this._memoryStorage[key] = JSON.stringify(record);
break;
case StorageType.SESSION:
sessionStorage.setItem(key, JSON.stringify(record));
break;
case StorageType.LOCAL:
localStorage.setItem(key, JSON.stringify(record));
break;
}

return record;
}
}

export class StorageItem {
public key: string;
public value: any;
public expiresAt: number;
}

export enum StorageType {
MEMORY = "memory",
SESSION = "session",
LOCAL = "local",
}
Loading

0 comments on commit a9b3baa

Please sign in to comment.