1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | const CLMemory_1 = require("../CLMemory");
|
9 | const CLDebug_1 = require("../CLDebug");
|
10 | const MAX_BROWSER_SLOTS = 10;
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | class 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 |
|
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 |
|
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 |
|
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 |
|
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 | }
|
80 | exports.BrowserSlot = BrowserSlot;
|
81 |
|
\ | No newline at end of file |