UNPKG

5.3 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-only WITH Classpath-exception-2.0
16// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.LocalStorageService = exports.StorageService = void 0;
19const tslib_1 = require("tslib");
20const inversify_1 = require("inversify");
21const logger_1 = require("../common/logger");
22const message_service_1 = require("../common/message-service");
23const window_service_1 = require("./window/window-service");
24const environment_1 = require("@theia/application-package/lib/environment");
25exports.StorageService = Symbol('IStorageService');
26let 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 * Verify if there is still some spaces left to save another workspace configuration into the local storage of your browser.
83 * If we are close to the limit, use a dialog to notify the user.
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);
119LocalStorageService = (0, tslib_1.__decorate)([
120 (0, inversify_1.injectable)()
121], LocalStorageService);
122exports.LocalStorageService = LocalStorageService;
123//# sourceMappingURL=storage-service.js.map
\No newline at end of file