UNPKG

3.15 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4/**
5 * Copyright (c) Microsoft Corporation. All rights reserved.
6 * Licensed under the MIT License.
7 */
8const CLMemory_1 = require("../CLMemory");
9const CLDebug_1 = require("../CLDebug");
10const MAX_BROWSER_SLOTS = 10;
11/**
12 * Used to keep track of memory slots used by open instances of the UI
13 * Each browser instance uses a different slot, with a max number of
14 * slots available
15 */
16class BrowserSlot {
17 constructor(browserId, offset) {
18 this.browserId = browserId;
19 this.lastUsed = new Date().getTime();
20 this.id = String.fromCharCode(offset + 65);
21 }
22 static GetSlot(browserId) {
23 return tslib_1.__awaiter(this, void 0, void 0, function* () {
24 let browserSlots = yield this.BrowserSlots();
25 // Check if browser already has a spot
26 let existingSlot = browserSlots.find(b => b.browserId === browserId);
27 if (existingSlot) {
28 existingSlot.lastUsed = new Date().getTime();
29 yield this.UpdateBrowserSlots(browserSlots);
30 return existingSlot.id;
31 }
32 // Add slot of spaces still availabled
33 if (browserSlots.length < MAX_BROWSER_SLOTS) {
34 let newSlot = new BrowserSlot(browserId, browserSlots.length);
35 browserSlots.push(newSlot);
36 yield this.UpdateBrowserSlots(browserSlots);
37 return newSlot.id;
38 }
39 // Use oldest slot
40 let oldestTime = browserSlots.reduce((min, b) => Math.min(min, b.lastUsed), browserSlots[0].lastUsed);
41 let oldestSlot = browserSlots.find(b => b.lastUsed === oldestTime);
42 if (!oldestSlot) {
43 throw new Error("Slot not found. This should never happen.");
44 }
45 // Claim this slot
46 oldestSlot.lastUsed = new Date().getTime();
47 oldestSlot.browserId = browserId;
48 yield this.UpdateBrowserSlots(browserSlots);
49 return oldestSlot.id;
50 });
51 }
52 static BrowserSlots() {
53 return tslib_1.__awaiter(this, void 0, void 0, function* () {
54 try {
55 let memory = CLMemory_1.CLMemory.GetMemory("BROWSER");
56 let data = yield memory.GetAsync("SLOTS");
57 if (data) {
58 return JSON.parse(data);
59 }
60 return [];
61 }
62 catch (err) {
63 CLDebug_1.CLDebug.Error(err, "BrowserSlots");
64 return [];
65 }
66 });
67 }
68 static UpdateBrowserSlots(browserSlots) {
69 return tslib_1.__awaiter(this, void 0, void 0, function* () {
70 try {
71 let memory = CLMemory_1.CLMemory.GetMemory("BROWSER");
72 yield memory.SetAsync("SLOTS", JSON.stringify(browserSlots));
73 }
74 catch (err) {
75 CLDebug_1.CLDebug.Error(err, "BrowserSlots");
76 }
77 });
78 }
79}
80exports.BrowserSlot = BrowserSlot;
81//# sourceMappingURL=BrowserSlot.js.map
\No newline at end of file