1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.LocalStorageService = exports.StorageService = void 0;
|
19 | const tslib_1 = require("tslib");
|
20 | const inversify_1 = require("inversify");
|
21 | const logger_1 = require("../common/logger");
|
22 | const message_service_1 = require("../common/message-service");
|
23 | const window_service_1 = require("./window/window-service");
|
24 | const environment_1 = require("@theia/application-package/lib/environment");
|
25 | exports.StorageService = Symbol('IStorageService');
|
26 | let LocalStorageService = class LocalStorageService {
|
27 | init() {
|
28 | if (typeof window !== 'undefined' && window.localStorage) {
|
29 | this.storage = window.localStorage;
|
30 | this.testLocalStorage();
|
31 | }
|
32 | else {
|
33 | this.logger.warn(log => log("The browser doesn't support localStorage. state will not be persisted across sessions."));
|
34 | this.storage = {};
|
35 | }
|
36 | }
|
37 | setData(key, data) {
|
38 | if (data !== undefined) {
|
39 | try {
|
40 | this.storage[this.prefix(key)] = JSON.stringify(data);
|
41 | }
|
42 | catch (e) {
|
43 | this.showDiskQuotaExceededMessage();
|
44 | }
|
45 | }
|
46 | else {
|
47 | delete this.storage[this.prefix(key)];
|
48 | }
|
49 | return Promise.resolve();
|
50 | }
|
51 | getData(key, defaultValue) {
|
52 | const result = this.storage[this.prefix(key)];
|
53 | if (result === undefined) {
|
54 | return Promise.resolve(defaultValue);
|
55 | }
|
56 | return Promise.resolve(JSON.parse(result));
|
57 | }
|
58 | prefix(key) {
|
59 | if (environment_1.environment.electron.is()) {
|
60 | return `theia:${key}`;
|
61 | }
|
62 | const pathname = typeof window === 'undefined' ? '' : window.location.pathname;
|
63 | return `theia:${pathname}:${key}`;
|
64 | }
|
65 | async showDiskQuotaExceededMessage() {
|
66 | const READ_INSTRUCTIONS_ACTION = 'Read Instructions';
|
67 | const CLEAR_STORAGE_ACTION = 'Clear Local Storage';
|
68 | const ERROR_MESSAGE = `Your preferred browser's local storage is almost full.
|
69 | To be able to save your current workspace layout or data, you may need to free up some space.
|
70 | You can refer to Theia's documentation page for instructions on how to manually clean
|
71 | your browser's local storage or choose to clear all.`;
|
72 | this.messageService.warn(ERROR_MESSAGE, READ_INSTRUCTIONS_ACTION, CLEAR_STORAGE_ACTION).then(async (selected) => {
|
73 | if (READ_INSTRUCTIONS_ACTION === selected) {
|
74 | this.windowService.openNewWindow('https://github.com/eclipse-theia/theia/wiki/Cleaning-Local-Storage', { external: true });
|
75 | }
|
76 | else if (CLEAR_STORAGE_ACTION === selected) {
|
77 | this.clearStorage();
|
78 | }
|
79 | });
|
80 | }
|
81 | |
82 |
|
83 |
|
84 |
|
85 | testLocalStorage() {
|
86 | const keyTest = this.prefix('Test');
|
87 | try {
|
88 | this.storage[keyTest] = JSON.stringify(new Array(60000));
|
89 | }
|
90 | catch (error) {
|
91 | this.showDiskQuotaExceededMessage();
|
92 | }
|
93 | finally {
|
94 | this.storage.removeItem(keyTest);
|
95 | }
|
96 | }
|
97 | clearStorage() {
|
98 | this.storage.clear();
|
99 | }
|
100 | };
|
101 | (0, tslib_1.__decorate)([
|
102 | (0, inversify_1.inject)(logger_1.ILogger),
|
103 | (0, tslib_1.__metadata)("design:type", Object)
|
104 | ], LocalStorageService.prototype, "logger", void 0);
|
105 | (0, tslib_1.__decorate)([
|
106 | (0, inversify_1.inject)(message_service_1.MessageService),
|
107 | (0, tslib_1.__metadata)("design:type", message_service_1.MessageService)
|
108 | ], LocalStorageService.prototype, "messageService", void 0);
|
109 | (0, tslib_1.__decorate)([
|
110 | (0, inversify_1.inject)(window_service_1.WindowService),
|
111 | (0, tslib_1.__metadata)("design:type", Object)
|
112 | ], LocalStorageService.prototype, "windowService", void 0);
|
113 | (0, tslib_1.__decorate)([
|
114 | (0, inversify_1.postConstruct)(),
|
115 | (0, tslib_1.__metadata)("design:type", Function),
|
116 | (0, tslib_1.__metadata)("design:paramtypes", []),
|
117 | (0, tslib_1.__metadata)("design:returntype", void 0)
|
118 | ], LocalStorageService.prototype, "init", null);
|
119 | LocalStorageService = (0, tslib_1.__decorate)([
|
120 | (0, inversify_1.injectable)()
|
121 | ], LocalStorageService);
|
122 | exports.LocalStorageService = LocalStorageService;
|
123 |
|
\ | No newline at end of file |