UNPKG

5.86 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2017 TypeFox and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16// *****************************************************************************
17var __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};
23var __metadata = (this && this.__metadata) || function (k, v) {
24 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
25};
26Object.defineProperty(exports, "__esModule", { value: true });
27exports.LocalStorageService = exports.StorageService = void 0;
28const inversify_1 = require("inversify");
29const logger_1 = require("../common/logger");
30const message_service_1 = require("../common/message-service");
31const window_service_1 = require("./window/window-service");
32const environment_1 = require("@theia/application-package/lib/environment");
33exports.StorageService = Symbol('IStorageService');
34let 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 * Verify if there is still some spaces left to save another workspace configuration into the local storage of your browser.
91 * If we are close to the limit, use a dialog to notify the user.
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);
127LocalStorageService = __decorate([
128 (0, inversify_1.injectable)()
129], LocalStorageService);
130exports.LocalStorageService = LocalStorageService;
131//# sourceMappingURL=storage-service.js.map
\No newline at end of file