Created
April 5, 2019 13:09
-
-
Save Agroil/0fc30fa19202a53b20f1d18fecaa2835 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| export class MemoryStorage { | |
| privateBrowserMode = false; | |
| private storage = { | |
| memoryStorage: {} | |
| }; | |
| private saveData() { | |
| let buffer = {}; | |
| if (/memoryStorage/.test(window.name)) { | |
| buffer = JSON.parse(window.name); | |
| } | |
| window.name = JSON.stringify(Object.assign(buffer, this.storage)); | |
| } | |
| constructor(typeStorage: "localStorage" | "sessionStorage" = "localStorage") { | |
| const STORAGE_TEST = "__storage_test__"; | |
| try { | |
| window[typeStorage].setItem(STORAGE_TEST, STORAGE_TEST); | |
| window[typeStorage].removeItem(STORAGE_TEST); | |
| return (<any>window)[typeStorage]; | |
| } catch (e) { | |
| this.saveData(); | |
| this.privateBrowserMode = true; | |
| return this; | |
| } | |
| } | |
| setItem(name: string, data: any) { | |
| this.storage.memoryStorage[name] = data; | |
| this.saveData(); | |
| } | |
| getItem (name: string) { | |
| if (/memoryStorage/.test(window.name)) { | |
| this.storage = JSON.parse(window.name); | |
| } else { | |
| this.storage = { | |
| memoryStorage: {} | |
| }; | |
| } | |
| return this.storage.memoryStorage[name]; | |
| } | |
| removeItem (name: string) { | |
| delete this.storage.memoryStorage[name]; | |
| this.saveData(); | |
| } | |
| clear () { | |
| this.storage = { | |
| memoryStorage: {} | |
| }; | |
| this.saveData(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment