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