@nu-art/ts-focused-object-backend 0.401.9 → 0.500.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { Module } from '@nu-art/ts-common';
|
|
2
2
|
import { OnPreLogout } from '@nu-art/user-account-backend';
|
|
3
|
+
import { API_FocusedObject } from '@nu-art/ts-focused-object-shared';
|
|
3
4
|
type Config = {};
|
|
4
5
|
export declare class ModuleBE_FocusedObject_Class extends Module<Config> implements OnPreLogout {
|
|
5
6
|
__onPreLogout: () => Promise<void>;
|
|
6
7
|
protected init(): void;
|
|
8
|
+
update(body: API_FocusedObject['update']['Body']): Promise<API_FocusedObject['update']['Response']>;
|
|
7
9
|
private updateFocusData;
|
|
8
10
|
private onAccountLogOut;
|
|
9
11
|
private getFocusMap;
|
|
@@ -1,136 +1,154 @@
|
|
|
1
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
2
|
+
var useValue = arguments.length > 2;
|
|
3
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
4
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
5
|
+
}
|
|
6
|
+
return useValue ? value : void 0;
|
|
7
|
+
};
|
|
8
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
9
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
10
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
11
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
12
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
13
|
+
var _, done = false;
|
|
14
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
15
|
+
var context = {};
|
|
16
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
17
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
18
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
19
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
20
|
+
if (kind === "accessor") {
|
|
21
|
+
if (result === void 0) continue;
|
|
22
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
23
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
24
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
25
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
26
|
+
}
|
|
27
|
+
else if (_ = accept(result)) {
|
|
28
|
+
if (kind === "field") initializers.unshift(_);
|
|
29
|
+
else descriptor[key] = _;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
33
|
+
done = true;
|
|
34
|
+
};
|
|
1
35
|
import { _keys, cloneObj, compare, currentTimeMillis, flatArray, Module } from '@nu-art/ts-common';
|
|
2
|
-
import {
|
|
36
|
+
import { ApiHandler } from '@nu-art/http-server';
|
|
3
37
|
import { Header_DeviceId, Header_TabId, MemKey_AccountId } from '@nu-art/user-account-backend';
|
|
4
38
|
import { ModuleBE_Firebase } from '@nu-art/firebase-backend';
|
|
5
39
|
import { ApiDef_FocusedObject, } from '@nu-art/ts-focused-object-shared';
|
|
6
40
|
import { DefaultTTL_FocusedObject, getRelationalPath } from '@nu-art/ts-focused-object-shared/consts';
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const newFocusMap = cloneObj(oldFocusMap);
|
|
22
|
-
//Make changes to newFocusMap
|
|
23
|
-
this.cleanTTL(newFocusMap);
|
|
24
|
-
this.clearFocusForTabId(newFocusMap, tabId);
|
|
25
|
-
this.setNewFocusData(newFocusMap, request.focusedEntities);
|
|
26
|
-
//Return if there are no changes
|
|
27
|
-
if (compare(oldFocusMap, newFocusMap))
|
|
28
|
-
return;
|
|
29
|
-
await this.setFocusMap(newFocusMap);
|
|
30
|
-
};
|
|
31
|
-
onAccountLogOut = async () => {
|
|
32
|
-
const deviceId = Header_DeviceId.get();
|
|
33
|
-
const oldFocusMap = await this.getFocusMap();
|
|
34
|
-
const newFocusMap = cloneObj(oldFocusMap);
|
|
35
|
-
this.clearFocusForDeviceId(newFocusMap, deviceId);
|
|
36
|
-
//Return if there are no changes
|
|
37
|
-
if (compare(oldFocusMap, newFocusMap))
|
|
38
|
-
return;
|
|
39
|
-
await this.setFocusMap(newFocusMap);
|
|
40
|
-
};
|
|
41
|
-
// ######################## Logic ########################
|
|
42
|
-
getFocusMap = async () => {
|
|
43
|
-
const ref = ModuleBE_Firebase.createAdminSession().getDatabase().ref(getRelationalPath());
|
|
44
|
-
return await ref.get({});
|
|
45
|
-
};
|
|
46
|
-
setFocusMap = async (map) => {
|
|
47
|
-
const ref = ModuleBE_Firebase.createAdminSession().getDatabase().ref(getRelationalPath());
|
|
48
|
-
return await ref.patch(map);
|
|
49
|
-
};
|
|
50
|
-
cleanTTL = (map) => {
|
|
51
|
-
const now = currentTimeMillis();
|
|
52
|
-
return this.processNodes(map, 'tabId', (dbKey, itemId, accountId, deviceId, tabId) => {
|
|
53
|
-
if (map[dbKey][itemId][accountId][deviceId][tabId] + DefaultTTL_FocusedObject >= now)
|
|
54
|
-
return false;
|
|
55
|
-
delete map[dbKey][itemId][accountId][deviceId][tabId];
|
|
56
|
-
return true;
|
|
57
|
-
});
|
|
58
|
-
};
|
|
59
|
-
clearFocusForTabId = (map, _tabId) => {
|
|
60
|
-
return this.processNodes(map, 'tabId', (dbKey, itemId, accountId, deviceId, tabId) => {
|
|
61
|
-
if (_tabId !== tabId)
|
|
62
|
-
return false;
|
|
63
|
-
delete map[dbKey][itemId][accountId][deviceId][tabId];
|
|
64
|
-
// //No more entries under this deviceId
|
|
65
|
-
// if (!_keys(map[dbKey][itemId][accountId][deviceId]).length)
|
|
66
|
-
// delete map[dbKey][itemId][accountId][deviceId];
|
|
67
|
-
//
|
|
68
|
-
// //No more entries under this accountId
|
|
69
|
-
// if (!_keys(map[dbKey][itemId][accountId]).length)
|
|
70
|
-
// delete map[dbKey][itemId][accountId];
|
|
71
|
-
//
|
|
72
|
-
// //If no more entries under this itemId
|
|
73
|
-
// if (_keys(map[dbKey][itemId]).length)
|
|
74
|
-
// delete map[dbKey][itemId];
|
|
75
|
-
//
|
|
76
|
-
// //If no more entries under this dbKey
|
|
77
|
-
// if (_keys(map[dbKey]).length)
|
|
78
|
-
// delete map[dbKey];
|
|
79
|
-
return true;
|
|
41
|
+
let ModuleBE_FocusedObject_Class = (() => {
|
|
42
|
+
let _classSuper = Module;
|
|
43
|
+
let _instanceExtraInitializers = [];
|
|
44
|
+
let _update_decorators;
|
|
45
|
+
return class ModuleBE_FocusedObject_Class extends _classSuper {
|
|
46
|
+
static {
|
|
47
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
|
|
48
|
+
_update_decorators = [ApiHandler(() => ApiDef_FocusedObject.update)];
|
|
49
|
+
__esDecorate(this, null, _update_decorators, { kind: "method", name: "update", static: false, private: false, access: { has: obj => "update" in obj, get: obj => obj.update }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
50
|
+
if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
51
|
+
}
|
|
52
|
+
__onPreLogout = (__runInitializers(this, _instanceExtraInitializers), async () => {
|
|
53
|
+
console.log('USER LOG OUT');
|
|
54
|
+
await this.onAccountLogOut();
|
|
80
55
|
});
|
|
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
|
-
// if
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
56
|
+
init() {
|
|
57
|
+
}
|
|
58
|
+
async update(body) {
|
|
59
|
+
await this.updateFocusData(body);
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
updateFocusData = async (request) => {
|
|
63
|
+
const tabId = Header_TabId.get();
|
|
64
|
+
const oldFocusMap = await this.getFocusMap();
|
|
65
|
+
const newFocusMap = cloneObj(oldFocusMap);
|
|
66
|
+
//Make changes to newFocusMap
|
|
67
|
+
this.cleanTTL(newFocusMap);
|
|
68
|
+
this.clearFocusForTabId(newFocusMap, tabId);
|
|
69
|
+
this.setNewFocusData(newFocusMap, request.focusedEntities);
|
|
70
|
+
//Return if there are no changes
|
|
71
|
+
if (compare(oldFocusMap, newFocusMap))
|
|
72
|
+
return;
|
|
73
|
+
await this.setFocusMap(newFocusMap);
|
|
74
|
+
};
|
|
75
|
+
onAccountLogOut = async () => {
|
|
76
|
+
const deviceId = Header_DeviceId.get();
|
|
77
|
+
const oldFocusMap = await this.getFocusMap();
|
|
78
|
+
const newFocusMap = cloneObj(oldFocusMap);
|
|
79
|
+
this.clearFocusForDeviceId(newFocusMap, deviceId);
|
|
80
|
+
//Return if there are no changes
|
|
81
|
+
if (compare(oldFocusMap, newFocusMap))
|
|
82
|
+
return;
|
|
83
|
+
await this.setFocusMap(newFocusMap);
|
|
84
|
+
};
|
|
85
|
+
getFocusMap = async () => {
|
|
86
|
+
const ref = ModuleBE_Firebase.createAdminSession().getDatabase().ref(getRelationalPath());
|
|
87
|
+
return await ref.get({});
|
|
88
|
+
};
|
|
89
|
+
setFocusMap = async (map) => {
|
|
90
|
+
const ref = ModuleBE_Firebase.createAdminSession().getDatabase().ref(getRelationalPath());
|
|
91
|
+
return await ref.patch(map);
|
|
92
|
+
};
|
|
93
|
+
cleanTTL = (map) => {
|
|
94
|
+
const now = currentTimeMillis();
|
|
95
|
+
return this.processNodes(map, 'tabId', (dbKey, itemId, accountId, deviceId, tabId) => {
|
|
96
|
+
if (map[dbKey][itemId][accountId][deviceId][tabId] + DefaultTTL_FocusedObject >= now)
|
|
97
|
+
return false;
|
|
98
|
+
delete map[dbKey][itemId][accountId][deviceId][tabId];
|
|
99
|
+
return true;
|
|
100
|
+
});
|
|
101
|
+
};
|
|
102
|
+
clearFocusForTabId = (map, _tabId) => {
|
|
103
|
+
return this.processNodes(map, 'tabId', (dbKey, itemId, accountId, deviceId, tabId) => {
|
|
104
|
+
if (_tabId !== tabId)
|
|
105
|
+
return false;
|
|
106
|
+
delete map[dbKey][itemId][accountId][deviceId][tabId];
|
|
107
|
+
return true;
|
|
108
|
+
});
|
|
109
|
+
};
|
|
110
|
+
setNewFocusData = (map, focusEntities) => {
|
|
111
|
+
const now = currentTimeMillis();
|
|
112
|
+
const tabId = Header_TabId.get();
|
|
113
|
+
const accountId = MemKey_AccountId.get();
|
|
114
|
+
const deviceId = Header_DeviceId.get();
|
|
115
|
+
focusEntities.forEach(focusEntity => {
|
|
116
|
+
map[focusEntity.dbKey] ??= {};
|
|
117
|
+
map[focusEntity.dbKey][focusEntity.itemId] ??= {};
|
|
118
|
+
map[focusEntity.dbKey][focusEntity.itemId][accountId] ??= {};
|
|
119
|
+
map[focusEntity.dbKey][focusEntity.itemId][accountId][deviceId] ??= {};
|
|
120
|
+
map[focusEntity.dbKey][focusEntity.itemId][accountId][deviceId][tabId] = now;
|
|
121
|
+
});
|
|
122
|
+
};
|
|
123
|
+
clearFocusForDeviceId = (map, _deviceId) => {
|
|
124
|
+
this.processNodes(map, 'deviceId', (dbKey, itemId, accountId, deviceId) => {
|
|
125
|
+
if (deviceId !== _deviceId)
|
|
126
|
+
return false;
|
|
127
|
+
delete map[dbKey][itemId][accountId][deviceId];
|
|
128
|
+
return true;
|
|
129
|
+
});
|
|
130
|
+
};
|
|
131
|
+
processNodes = (map, resolution, processor) => {
|
|
132
|
+
return flatArray(_keys(map).map(dbKey => {
|
|
133
|
+
if (resolution === 'dbKey')
|
|
134
|
+
return processor(dbKey);
|
|
135
|
+
return _keys(map[dbKey]).map(itemId => {
|
|
136
|
+
if (resolution === 'itemId')
|
|
137
|
+
return processor(dbKey, itemId);
|
|
138
|
+
return _keys(map[dbKey][itemId]).map(accountId => {
|
|
139
|
+
if (resolution === 'accountId')
|
|
140
|
+
return processor(dbKey, itemId, accountId);
|
|
141
|
+
return _keys(map[dbKey][itemId][accountId]).map(deviceId => {
|
|
142
|
+
if (resolution === 'deviceId')
|
|
143
|
+
return processor(dbKey, itemId, accountId, deviceId);
|
|
144
|
+
return _keys(map[dbKey][itemId][accountId][deviceId]).map(tabId => //For every tabId
|
|
145
|
+
processor(dbKey, itemId, accountId, deviceId, tabId));
|
|
146
|
+
});
|
|
130
147
|
});
|
|
131
148
|
});
|
|
132
|
-
});
|
|
133
|
-
}
|
|
149
|
+
})).some(i => i);
|
|
150
|
+
};
|
|
134
151
|
};
|
|
135
|
-
}
|
|
152
|
+
})();
|
|
153
|
+
export { ModuleBE_FocusedObject_Class };
|
|
136
154
|
export const ModuleBE_FocusedObject = new ModuleBE_FocusedObject_Class();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nu-art/ts-focused-object-backend",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.500.0",
|
|
4
4
|
"description": "ts-focused-object - Express & Typescript based backend framework Backend",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"TacB0sS",
|
|
@@ -31,18 +31,17 @@
|
|
|
31
31
|
"build": "tsc"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@nu-art/ts-focused-object-shared": "0.
|
|
35
|
-
"@nu-art/firebase-backend": "0.
|
|
36
|
-
"@nu-art/firebase-shared": "0.
|
|
37
|
-
"@nu-art/
|
|
38
|
-
"@nu-art/
|
|
39
|
-
"@nu-art/
|
|
40
|
-
"@nu-art/user-account-backend": "0.401.9",
|
|
41
|
-
"@nu-art/user-account-shared": "0.401.9",
|
|
34
|
+
"@nu-art/ts-focused-object-shared": "0.500.0",
|
|
35
|
+
"@nu-art/firebase-backend": "0.500.0",
|
|
36
|
+
"@nu-art/firebase-shared": "0.500.0",
|
|
37
|
+
"@nu-art/ts-common": "0.500.0",
|
|
38
|
+
"@nu-art/user-account-backend": "0.500.0",
|
|
39
|
+
"@nu-art/user-account-shared": "0.500.0",
|
|
42
40
|
"firebase": "^11.9.0",
|
|
43
41
|
"firebase-admin": "13.4.0",
|
|
44
42
|
"firebase-functions": "6.3.2",
|
|
45
|
-
"react": "^18.0.0"
|
|
43
|
+
"react": "^18.0.0",
|
|
44
|
+
"@nu-art/http-server": "{{THUNDERSTORM_VERSION}}"
|
|
46
45
|
},
|
|
47
46
|
"devDependencies": {
|
|
48
47
|
"@types/react": "^18.0.0",
|