summaryrefslogtreecommitdiffstats
path: root/comm/chat/protocols/matrix/lib/matrix-sdk/store/indexeddb-store-worker.js
blob: 4708d589366efa572b2a392224892352d5d69f8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.IndexedDBStoreWorker = void 0;
var _indexeddbLocalBackend = require("./indexeddb-local-backend");
var _logger = require("../logger");
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } /*
                                                                                                                                                                                                                                                                                                                                                                                          Copyright 2017 - 2021 The Matrix.org Foundation C.I.C.
                                                                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                                                                                          Licensed under the Apache License, Version 2.0 (the "License");
                                                                                                                                                                                                                                                                                                                                                                                          you may not use this file except in compliance with the License.
                                                                                                                                                                                                                                                                                                                                                                                          You may obtain a copy of the License at
                                                                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                                                                                              http://www.apache.org/licenses/LICENSE-2.0
                                                                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                                                                                          Unless required by applicable law or agreed to in writing, software
                                                                                                                                                                                                                                                                                                                                                                                          distributed under the License is distributed on an "AS IS" BASIS,
                                                                                                                                                                                                                                                                                                                                                                                          WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
                                                                                                                                                                                                                                                                                                                                                                                          See the License for the specific language governing permissions and
                                                                                                                                                                                                                                                                                                                                                                                          limitations under the License.
                                                                                                                                                                                                                                                                                                                                                                                          */
/**
 * This class lives in the webworker and drives a LocalIndexedDBStoreBackend
 * controlled by messages from the main process.
 *
 * @example
 * It should be instantiated by a web worker script provided by the application
 * in a script, for example:
 * ```
 * import {IndexedDBStoreWorker} from 'matrix-js-sdk/lib/indexeddb-worker.js';
 * const remoteWorker = new IndexedDBStoreWorker(postMessage);
 * onmessage = remoteWorker.onMessage;
 * ```
 *
 * Note that it is advisable to import this class by referencing the file directly to
 * avoid a dependency on the whole js-sdk.
 *
 */
class IndexedDBStoreWorker {
  /**
   * @param postMessage - The web worker postMessage function that
   * should be used to communicate back to the main script.
   */
  constructor(postMessage) {
    this.postMessage = postMessage;
    _defineProperty(this, "backend", void 0);
    _defineProperty(this, "onClose", () => {
      this.postMessage.call(null, {
        command: "closed"
      });
    });
    /**
     * Passes a message event from the main script into the class. This method
     * can be directly assigned to the web worker `onmessage` variable.
     *
     * @param ev - The message event
     */
    _defineProperty(this, "onMessage", ev => {
      const msg = ev.data;
      let prom;
      switch (msg.command) {
        case "setupWorker":
          // this is the 'indexedDB' global (where global != window
          // because it's a web worker and there is no window).
          this.backend = new _indexeddbLocalBackend.LocalIndexedDBStoreBackend(indexedDB, msg.args[0]);
          prom = Promise.resolve();
          break;
        case "connect":
          prom = this.backend?.connect(this.onClose);
          break;
        case "isNewlyCreated":
          prom = this.backend?.isNewlyCreated();
          break;
        case "clearDatabase":
          prom = this.backend?.clearDatabase();
          break;
        case "getSavedSync":
          prom = this.backend?.getSavedSync(false);
          break;
        case "setSyncData":
          prom = this.backend?.setSyncData(msg.args[0]);
          break;
        case "syncToDatabase":
          prom = this.backend?.syncToDatabase(msg.args[0]);
          break;
        case "getUserPresenceEvents":
          prom = this.backend?.getUserPresenceEvents();
          break;
        case "getNextBatchToken":
          prom = this.backend?.getNextBatchToken();
          break;
        case "getOutOfBandMembers":
          prom = this.backend?.getOutOfBandMembers(msg.args[0]);
          break;
        case "clearOutOfBandMembers":
          prom = this.backend?.clearOutOfBandMembers(msg.args[0]);
          break;
        case "setOutOfBandMembers":
          prom = this.backend?.setOutOfBandMembers(msg.args[0], msg.args[1]);
          break;
        case "getClientOptions":
          prom = this.backend?.getClientOptions();
          break;
        case "storeClientOptions":
          prom = this.backend?.storeClientOptions(msg.args[0]);
          break;
        case "saveToDeviceBatches":
          prom = this.backend?.saveToDeviceBatches(msg.args[0]);
          break;
        case "getOldestToDeviceBatch":
          prom = this.backend?.getOldestToDeviceBatch();
          break;
        case "removeToDeviceBatch":
          prom = this.backend?.removeToDeviceBatch(msg.args[0]);
          break;
      }
      if (prom === undefined) {
        this.postMessage({
          command: "cmd_fail",
          seq: msg.seq,
          // Can't be an Error because they're not structured cloneable
          error: "Unrecognised command"
        });
        return;
      }
      prom.then(ret => {
        this.postMessage.call(null, {
          command: "cmd_success",
          seq: msg.seq,
          result: ret
        });
      }, err => {
        _logger.logger.error("Error running command: " + msg.command, err);
        this.postMessage.call(null, {
          command: "cmd_fail",
          seq: msg.seq,
          // Just send a string because Error objects aren't cloneable
          error: {
            message: err.message,
            name: err.name
          }
        });
      });
    });
  }
}
exports.IndexedDBStoreWorker = IndexedDBStoreWorker;