@iobroker/db-states-file 7.2.3 → 8.0.0-alpha.1-20260921-e5941ca8a
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.
- package/build/cjs/index.js +5 -3
- package/build/cjs/index.js.map +1 -1
- package/build/cjs/lib/states/statesInMemFileDB.d.ts +171 -42
- package/build/cjs/lib/states/statesInMemFileDB.js +144 -53
- package/build/cjs/lib/states/statesInMemFileDB.js.map +4 -4
- package/build/cjs/lib/states/statesInMemServerClass.d.ts +27 -5
- package/build/cjs/lib/states/statesInMemServerClass.js +16 -2
- package/build/cjs/lib/states/statesInMemServerClass.js.map +3 -3
- package/build/cjs/lib/states/statesInMemServerRedis.d.ts +53 -25
- package/build/cjs/lib/states/statesInMemServerRedis.js +48 -30
- package/build/cjs/lib/states/statesInMemServerRedis.js.map +3 -3
- package/build/esm/lib/states/statesInMemFileDB.d.ts +171 -42
- package/build/esm/lib/states/statesInMemFileDB.d.ts.map +1 -1
- package/build/esm/lib/states/statesInMemFileDB.js +142 -54
- package/build/esm/lib/states/statesInMemFileDB.js.map +1 -1
- package/build/esm/lib/states/statesInMemServerClass.d.ts +27 -5
- package/build/esm/lib/states/statesInMemServerClass.d.ts.map +1 -1
- package/build/esm/lib/states/statesInMemServerClass.js +13 -0
- package/build/esm/lib/states/statesInMemServerClass.js.map +1 -1
- package/build/esm/lib/states/statesInMemServerRedis.d.ts +53 -25
- package/build/esm/lib/states/statesInMemServerRedis.d.ts.map +1 -1
- package/build/esm/lib/states/statesInMemServerRedis.js +52 -53
- package/build/esm/lib/states/statesInMemServerRedis.js.map +1 -1
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +6 -6
package/build/cjs/index.js
CHANGED
|
@@ -3,6 +3,7 @@ var __defProp = Object.defineProperty;
|
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
7
|
var __export = (target, all) => {
|
|
7
8
|
for (var name in all)
|
|
8
9
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -16,20 +17,21 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
17
|
return to;
|
|
17
18
|
};
|
|
18
19
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var
|
|
20
|
-
__export(
|
|
20
|
+
var index_exports = {};
|
|
21
|
+
__export(index_exports, {
|
|
21
22
|
Client: () => import_db_states_redis.Client,
|
|
22
23
|
Server: () => import_statesInMemServerClass.StatesInMemoryServerClass,
|
|
23
24
|
StatesInMemoryFileDB: () => import_statesInMemFileDB.StatesInMemoryFileDB,
|
|
24
25
|
getDefaultPort: () => getDefaultPort
|
|
25
26
|
});
|
|
26
|
-
module.exports = __toCommonJS(
|
|
27
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
28
|
var import_statesInMemFileDB = require("./lib/states/statesInMemFileDB.js");
|
|
28
29
|
var import_db_states_redis = require("@iobroker/db-states-redis");
|
|
29
30
|
var import_statesInMemServerClass = require("./lib/states/statesInMemServerClass.js");
|
|
30
31
|
function getDefaultPort() {
|
|
31
32
|
return 9e3;
|
|
32
33
|
}
|
|
34
|
+
__name(getDefaultPort, "getDefaultPort");
|
|
33
35
|
// Annotate the CommonJS export names for ESM import in node:
|
|
34
36
|
0 && (module.exports = {
|
|
35
37
|
Client,
|
package/build/cjs/index.js.map
CHANGED
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/index.ts"],
|
|
4
4
|
"sourcesContent": ["export { StatesInMemoryFileDB } from './lib/states/statesInMemFileDB.js';\nexport { Client } from '@iobroker/db-states-redis';\nexport { StatesInMemoryServerClass as Server } from './lib/states/statesInMemServerClass.js';\n\n/**\n * Get the default port of the objects db\n */\nexport function getDefaultPort(): number {\n return 9000;\n}\n"],
|
|
5
|
-
"mappings": "
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;AAAA,+BAAqC;AACrC,6BAAuB;AACvB,oCAAoD;AAK9C,SAAU,iBAAc;AAC1B,SAAO;AACX;AAFgB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,52 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* States DB in memory - Server
|
|
3
|
+
*
|
|
4
|
+
* Copyright 2013-2026 bluefox <dogafox@gmail.com>
|
|
5
|
+
*
|
|
6
|
+
* MIT License
|
|
7
|
+
*
|
|
8
|
+
*/
|
|
9
|
+
import { InMemoryFileDB, type FileDbSettings } from '@iobroker/db-base';
|
|
1
10
|
/**
|
|
2
11
|
* This class inherits InMemoryFileDB class and adds all relevant logic for states
|
|
3
12
|
* including the available methods for use by js-controller directly
|
|
4
13
|
*/
|
|
5
|
-
export class StatesInMemoryFileDB extends
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
export declare class StatesInMemoryFileDB<THandler extends {
|
|
15
|
+
_subscribe?: Record<string, {
|
|
16
|
+
pattern: string;
|
|
17
|
+
regex: RegExp;
|
|
18
|
+
}[]>;
|
|
19
|
+
}> extends InMemoryFileDB<ioBroker.State | ioBroker.Session, THandler> {
|
|
20
|
+
private readonly META_ID;
|
|
21
|
+
private session;
|
|
22
|
+
protected stateExpires: Record<string, NodeJS.Timeout>;
|
|
23
|
+
protected sessionExpires: Record<string, {
|
|
24
|
+
sessionEnd: number;
|
|
25
|
+
timeout: NodeJS.Timeout | null;
|
|
26
|
+
}>;
|
|
27
|
+
private readonly ONE_DAY_IN_SECS;
|
|
28
|
+
private readonly writeFileInterval;
|
|
29
|
+
/**
|
|
30
|
+
* @param settings Settings for the states database
|
|
31
|
+
*/
|
|
32
|
+
constructor(settings: FileDbSettings<ioBroker.State | ioBroker.Session>);
|
|
33
|
+
/**
|
|
34
|
+
* Expire all states that have an expiration set and (re)start the save timer
|
|
35
|
+
*/
|
|
16
36
|
_expireAll(): void;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
37
|
+
/**
|
|
38
|
+
* Remove an expired state and optionally publish the deletion
|
|
39
|
+
*
|
|
40
|
+
* @param id The ID of the state to expire
|
|
41
|
+
* @param dontPublish If true, do not publish the deletion to subscribers
|
|
42
|
+
*/
|
|
43
|
+
_expireState(id: string, dontPublish?: boolean): void;
|
|
44
|
+
/**
|
|
45
|
+
* Remove an expired session and clear its timeout
|
|
46
|
+
*
|
|
47
|
+
* @param id The ID of the session to expire
|
|
48
|
+
*/
|
|
49
|
+
_expireSession(id: string): void;
|
|
50
|
+
/**
|
|
51
|
+
* Destructor of the class. Called when shutting down to expire states and clear the save timer.
|
|
52
|
+
*/
|
|
53
|
+
destroy(): Promise<void>;
|
|
54
|
+
/**
|
|
55
|
+
* Get the values of the given state IDs (used by the server)
|
|
56
|
+
*
|
|
57
|
+
* @param keys The state IDs to read
|
|
58
|
+
*/
|
|
59
|
+
_getStates(keys: string[]): (ioBroker.State | ioBroker.Session | null)[];
|
|
60
|
+
/**
|
|
61
|
+
* Get the value of a single state (used by the server)
|
|
62
|
+
*
|
|
63
|
+
* @param id The state ID to read
|
|
64
|
+
*/
|
|
65
|
+
_getState(id: string): ioBroker.State;
|
|
66
|
+
/**
|
|
67
|
+
* Get the meta dictionary, creating it if it does not exist yet
|
|
68
|
+
*/
|
|
69
|
+
_ensureMetaDict(): Record<string, string>;
|
|
22
70
|
/**
|
|
23
71
|
* Get value of given meta id
|
|
24
72
|
*
|
|
25
|
-
* @param id
|
|
26
|
-
* @returns
|
|
27
|
-
*/
|
|
28
|
-
getMeta(id:
|
|
29
|
-
/**
|
|
30
|
-
* Sets given value to
|
|
31
|
-
*
|
|
32
|
-
* @param id
|
|
33
|
-
* @param value
|
|
34
|
-
*/
|
|
35
|
-
setMeta(id:
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
73
|
+
* @param id The meta ID to read
|
|
74
|
+
* @returns the stored meta value
|
|
75
|
+
*/
|
|
76
|
+
getMeta(id: string): string;
|
|
77
|
+
/**
|
|
78
|
+
* Sets given value to ID in metaNamespace
|
|
79
|
+
*
|
|
80
|
+
* @param id The meta ID to write
|
|
81
|
+
* @param value The value to store
|
|
82
|
+
*/
|
|
83
|
+
setMeta(id: string, value: string): void;
|
|
84
|
+
/**
|
|
85
|
+
* Directly set a state value and optionally schedule its expiration (used by the server)
|
|
86
|
+
*
|
|
87
|
+
* @param id The state ID to set
|
|
88
|
+
* @param obj The state object to store
|
|
89
|
+
* @param expire Optional expiration time in seconds
|
|
90
|
+
*/
|
|
91
|
+
_setStateDirect(id: string, obj: ioBroker.State, expire?: number): void;
|
|
92
|
+
/**
|
|
93
|
+
* Delete a state and publish the deletion (used by the server)
|
|
94
|
+
*
|
|
95
|
+
* @param id The state ID to delete
|
|
96
|
+
*/
|
|
97
|
+
_delState(id: string): void;
|
|
98
|
+
/**
|
|
99
|
+
* Get all state IDs matching the given pattern (used by the server)
|
|
100
|
+
*
|
|
101
|
+
* @param pattern The pattern to match state IDs against
|
|
102
|
+
*/
|
|
103
|
+
_getKeys(pattern: string): string[];
|
|
104
|
+
/**
|
|
105
|
+
* Subscribe a client to state changes (used by the server)
|
|
106
|
+
*
|
|
107
|
+
* @param client The client to subscribe
|
|
108
|
+
* @param pattern The pattern of state IDs to subscribe to
|
|
109
|
+
*/
|
|
110
|
+
_subscribeForClient(client: THandler, pattern: string): void;
|
|
111
|
+
/**
|
|
112
|
+
* Subscribe a client to meta changes
|
|
113
|
+
*
|
|
114
|
+
* @param client The client to subscribe
|
|
115
|
+
* @param pattern The pattern of meta IDs to subscribe to
|
|
116
|
+
*/
|
|
117
|
+
_subscribeMeta(client: THandler, pattern: string): void;
|
|
118
|
+
/**
|
|
119
|
+
* Unsubscribe a client from state changes (used by the server)
|
|
120
|
+
*
|
|
121
|
+
* @param client The client to unsubscribe
|
|
122
|
+
* @param pattern The pattern of state IDs to unsubscribe from
|
|
123
|
+
*/
|
|
124
|
+
_unsubscribeForClient(client: THandler, pattern: string): void;
|
|
125
|
+
/**
|
|
126
|
+
* Subscribe a client to its message box (used by the server)
|
|
127
|
+
*
|
|
128
|
+
* @param client The client to subscribe
|
|
129
|
+
* @param id The ID of the message box owner
|
|
130
|
+
*/
|
|
131
|
+
_subscribeMessageForClient(client: THandler, id: string): void;
|
|
132
|
+
/**
|
|
133
|
+
* Unsubscribe a client from its message box (used by the server)
|
|
134
|
+
*
|
|
135
|
+
* @param client The client to unsubscribe
|
|
136
|
+
* @param id The ID of the message box owner
|
|
137
|
+
*/
|
|
138
|
+
_unsubscribeMessageForClient(client: THandler, id: string): void;
|
|
139
|
+
/**
|
|
140
|
+
* Subscribe a client to log messages (used by the server)
|
|
141
|
+
*
|
|
142
|
+
* @param client The client to subscribe
|
|
143
|
+
* @param id The ID of the log owner
|
|
144
|
+
*/
|
|
145
|
+
_subscribeLogForClient(client: THandler, id: string): void;
|
|
146
|
+
/**
|
|
147
|
+
* Unsubscribe a client from log messages (used by the server)
|
|
148
|
+
*
|
|
149
|
+
* @param client The client to unsubscribe
|
|
150
|
+
* @param id The ID of the log owner
|
|
151
|
+
*/
|
|
152
|
+
_unsubscribeLogForClient(client: THandler, id: string): void;
|
|
153
|
+
/**
|
|
154
|
+
* Get a session by its ID (used by the server)
|
|
155
|
+
*
|
|
156
|
+
* @param id The session ID to read
|
|
157
|
+
*/
|
|
158
|
+
_getSession(id: string): ioBroker.Session;
|
|
159
|
+
/**
|
|
160
|
+
* Schedule the expiration of a session, splitting long delays into daily chunks
|
|
161
|
+
*
|
|
162
|
+
* @param id The session ID
|
|
163
|
+
* @param expireDate Timestamp in milliseconds when the session should expire
|
|
164
|
+
*/
|
|
165
|
+
_handleSessionExpire(id: string, expireDate: number): void;
|
|
166
|
+
/**
|
|
167
|
+
* Create or update a session and schedule its expiration (used by the server)
|
|
168
|
+
*
|
|
169
|
+
* @param id The session ID
|
|
170
|
+
* @param expire Expiration time in seconds from now
|
|
171
|
+
* @param sessionData The session data to store
|
|
172
|
+
*/
|
|
173
|
+
_setSession(id: string, expire: number, sessionData: ioBroker.Session): void;
|
|
174
|
+
/**
|
|
175
|
+
* Destroy a session immediately (used by the server)
|
|
176
|
+
*
|
|
177
|
+
* @param id The session ID to destroy
|
|
178
|
+
*/
|
|
179
|
+
_destroySession(id: string): void;
|
|
50
180
|
}
|
|
51
|
-
import { InMemoryFileDB } from '@iobroker/db-base';
|
|
52
181
|
//# sourceMappingURL=statesInMemFileDB.d.ts.map
|
|
@@ -3,6 +3,7 @@ var __defProp = Object.defineProperty;
|
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
7
|
var __export = (target, all) => {
|
|
7
8
|
for (var name in all)
|
|
8
9
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -22,45 +23,52 @@ __export(statesInMemFileDB_exports, {
|
|
|
22
23
|
});
|
|
23
24
|
module.exports = __toCommonJS(statesInMemFileDB_exports);
|
|
24
25
|
var import_db_base = require("@iobroker/db-base");
|
|
25
|
-
var import_db_base2 = require("@iobroker/db-base");
|
|
26
26
|
class StatesInMemoryFileDB extends import_db_base.InMemoryFileDB {
|
|
27
|
+
static {
|
|
28
|
+
__name(this, "StatesInMemoryFileDB");
|
|
29
|
+
}
|
|
30
|
+
META_ID = "**META**";
|
|
31
|
+
session = {};
|
|
32
|
+
stateExpires = {};
|
|
33
|
+
sessionExpires = {};
|
|
34
|
+
ONE_DAY_IN_SECS = 24 * 60 * 60 * 1e3;
|
|
35
|
+
writeFileInterval;
|
|
36
|
+
/**
|
|
37
|
+
* @param settings Settings for the states database
|
|
38
|
+
*/
|
|
27
39
|
constructor(settings) {
|
|
28
|
-
settings
|
|
29
|
-
settings.fileDB = settings.fileDB || {
|
|
40
|
+
settings.fileDB ??= {
|
|
30
41
|
fileName: "states.json",
|
|
31
42
|
backupDirName: "backup-objects"
|
|
32
43
|
};
|
|
33
44
|
super(settings);
|
|
34
|
-
this.
|
|
35
|
-
this.logs = {};
|
|
36
|
-
this.session = {};
|
|
37
|
-
this.globalMessageId = Math.round(Math.random() * 1e8);
|
|
38
|
-
this.globalLogId = Math.round(Math.random() * 1e8);
|
|
39
|
-
this.stateExpires = {};
|
|
40
|
-
this.sessionExpires = {};
|
|
41
|
-
this.ONE_DAY_IN_SECS = 24 * 60 * 60 * 1e3;
|
|
42
|
-
this.writeFileInterval = this.settings.connection && typeof this.settings.connection.writeFileInterval === "number" ? parseInt(this.settings.connection.writeFileInterval) : 3e4;
|
|
45
|
+
this.writeFileInterval = this.settings.connection && typeof this.settings.connection.writeFileInterval === "number" ? this.settings.connection.writeFileInterval : 3e4;
|
|
43
46
|
if (settings.jsonlDB) {
|
|
44
47
|
this.log.silly(`${this.namespace} States DB uses file write interval of ${this.writeFileInterval} ms`);
|
|
45
48
|
}
|
|
46
49
|
this._expireAll();
|
|
47
50
|
}
|
|
48
|
-
|
|
51
|
+
/**
|
|
52
|
+
* Expire all states that have an expiration set and (re)start the save timer
|
|
53
|
+
*/
|
|
49
54
|
_expireAll() {
|
|
50
55
|
Object.keys(this.stateExpires).forEach((id) => {
|
|
51
56
|
clearTimeout(this.stateExpires[id]);
|
|
52
57
|
this._expireState(id);
|
|
53
58
|
});
|
|
54
59
|
Object.entries(this.dataset).forEach(([id, obj]) => {
|
|
55
|
-
if (obj
|
|
60
|
+
if (obj?.expire) {
|
|
56
61
|
this._expireState(id, true);
|
|
57
62
|
}
|
|
58
63
|
});
|
|
59
|
-
|
|
60
|
-
this.stateTimer = setTimeout(() => this.saveState(), this.writeFileInterval);
|
|
61
|
-
}
|
|
64
|
+
this.stateTimer ||= setTimeout(() => this.saveState(), this.writeFileInterval);
|
|
62
65
|
}
|
|
63
|
-
|
|
66
|
+
/**
|
|
67
|
+
* Remove an expired state and optionally publish the deletion
|
|
68
|
+
*
|
|
69
|
+
* @param id The ID of the state to expire
|
|
70
|
+
* @param dontPublish If true, do not publish the deletion to subscribers
|
|
71
|
+
*/
|
|
64
72
|
_expireState(id, dontPublish) {
|
|
65
73
|
if (this.stateExpires[id] !== void 0) {
|
|
66
74
|
delete this.stateExpires[id];
|
|
@@ -73,9 +81,13 @@ class StatesInMemoryFileDB extends import_db_base.InMemoryFileDB {
|
|
|
73
81
|
this.stateTimer = setTimeout(() => this.saveState(), this.writeFileInterval);
|
|
74
82
|
}
|
|
75
83
|
}
|
|
76
|
-
|
|
84
|
+
/**
|
|
85
|
+
* Remove an expired session and clear its timeout
|
|
86
|
+
*
|
|
87
|
+
* @param id The ID of the session to expire
|
|
88
|
+
*/
|
|
77
89
|
_expireSession(id) {
|
|
78
|
-
if (this.sessionExpires[id]
|
|
90
|
+
if (this.sessionExpires[id]?.timeout) {
|
|
79
91
|
clearTimeout(this.sessionExpires[id].timeout);
|
|
80
92
|
delete this.sessionExpires[id];
|
|
81
93
|
}
|
|
@@ -83,8 +95,9 @@ class StatesInMemoryFileDB extends import_db_base.InMemoryFileDB {
|
|
|
83
95
|
delete this.session[id];
|
|
84
96
|
}
|
|
85
97
|
}
|
|
86
|
-
|
|
87
|
-
|
|
98
|
+
/**
|
|
99
|
+
* Destructor of the class. Called when shutting down to expire states and clear the save timer.
|
|
100
|
+
*/
|
|
88
101
|
async destroy() {
|
|
89
102
|
this._expireAll();
|
|
90
103
|
await super.destroy();
|
|
@@ -93,7 +106,11 @@ class StatesInMemoryFileDB extends import_db_base.InMemoryFileDB {
|
|
|
93
106
|
this.stateTimer = null;
|
|
94
107
|
}
|
|
95
108
|
}
|
|
96
|
-
|
|
109
|
+
/**
|
|
110
|
+
* Get the values of the given state IDs (used by the server)
|
|
111
|
+
*
|
|
112
|
+
* @param keys The state IDs to read
|
|
113
|
+
*/
|
|
97
114
|
_getStates(keys) {
|
|
98
115
|
if (!keys || !Array.isArray(keys)) {
|
|
99
116
|
throw new Error("no keys");
|
|
@@ -103,10 +120,17 @@ class StatesInMemoryFileDB extends import_db_base.InMemoryFileDB {
|
|
|
103
120
|
return obj !== void 0 ? obj : null;
|
|
104
121
|
});
|
|
105
122
|
}
|
|
106
|
-
|
|
123
|
+
/**
|
|
124
|
+
* Get the value of a single state (used by the server)
|
|
125
|
+
*
|
|
126
|
+
* @param id The state ID to read
|
|
127
|
+
*/
|
|
107
128
|
_getState(id) {
|
|
108
129
|
return this.dataset[id];
|
|
109
130
|
}
|
|
131
|
+
/**
|
|
132
|
+
* Get the meta dictionary, creating it if it does not exist yet
|
|
133
|
+
*/
|
|
110
134
|
_ensureMetaDict() {
|
|
111
135
|
let meta = this.dataset[this.META_ID];
|
|
112
136
|
if (!meta) {
|
|
@@ -118,18 +142,18 @@ class StatesInMemoryFileDB extends import_db_base.InMemoryFileDB {
|
|
|
118
142
|
/**
|
|
119
143
|
* Get value of given meta id
|
|
120
144
|
*
|
|
121
|
-
* @param id
|
|
122
|
-
* @returns
|
|
145
|
+
* @param id The meta ID to read
|
|
146
|
+
* @returns the stored meta value
|
|
123
147
|
*/
|
|
124
148
|
getMeta(id) {
|
|
125
149
|
const meta = this._ensureMetaDict();
|
|
126
150
|
return meta[id];
|
|
127
151
|
}
|
|
128
152
|
/**
|
|
129
|
-
* Sets given value to
|
|
153
|
+
* Sets given value to ID in metaNamespace
|
|
130
154
|
*
|
|
131
|
-
* @param id
|
|
132
|
-
* @param value
|
|
155
|
+
* @param id The meta ID to write
|
|
156
|
+
* @param value The value to store
|
|
133
157
|
*/
|
|
134
158
|
setMeta(id, value) {
|
|
135
159
|
const meta = this._ensureMetaDict();
|
|
@@ -139,21 +163,25 @@ class StatesInMemoryFileDB extends import_db_base.InMemoryFileDB {
|
|
|
139
163
|
this.log.silly(`${this.namespace} memory publish meta ${id} ${value}`);
|
|
140
164
|
this.publishAll("meta", id, value);
|
|
141
165
|
});
|
|
142
|
-
|
|
143
|
-
this.stateTimer = setTimeout(() => this.saveState(), this.writeFileInterval);
|
|
144
|
-
}
|
|
166
|
+
this.stateTimer ||= setTimeout(() => this.saveState(), this.writeFileInterval);
|
|
145
167
|
}
|
|
146
|
-
|
|
168
|
+
/**
|
|
169
|
+
* Directly set a state value and optionally schedule its expiration (used by the server)
|
|
170
|
+
*
|
|
171
|
+
* @param id The state ID to set
|
|
172
|
+
* @param obj The state object to store
|
|
173
|
+
* @param expire Optional expiration time in seconds
|
|
174
|
+
*/
|
|
147
175
|
_setStateDirect(id, obj, expire) {
|
|
148
176
|
if (this.stateExpires[id]) {
|
|
149
177
|
clearTimeout(this.stateExpires[id]);
|
|
150
178
|
delete this.stateExpires[id];
|
|
151
179
|
}
|
|
180
|
+
this.dataset[id] = obj;
|
|
152
181
|
if (expire) {
|
|
153
182
|
this.stateExpires[id] = setTimeout(() => this._expireState(id), expire * 1e3);
|
|
154
|
-
|
|
183
|
+
this.dataset[id].expire = 1;
|
|
155
184
|
}
|
|
156
|
-
this.dataset[id] = obj;
|
|
157
185
|
if (obj.val !== void 0) {
|
|
158
186
|
setImmediate(() => {
|
|
159
187
|
this.log.silly(`${this.namespace} memory publish ${id} ${JSON.stringify(obj)}`);
|
|
@@ -164,7 +192,11 @@ class StatesInMemoryFileDB extends import_db_base.InMemoryFileDB {
|
|
|
164
192
|
this.stateTimer = setTimeout(() => this.saveState(), this.writeFileInterval);
|
|
165
193
|
}
|
|
166
194
|
}
|
|
167
|
-
|
|
195
|
+
/**
|
|
196
|
+
* Delete a state and publish the deletion (used by the server)
|
|
197
|
+
*
|
|
198
|
+
* @param id The state ID to delete
|
|
199
|
+
*/
|
|
168
200
|
_delState(id) {
|
|
169
201
|
if (this.stateExpires[id]) {
|
|
170
202
|
clearTimeout(this.stateExpires[id]);
|
|
@@ -179,43 +211,92 @@ class StatesInMemoryFileDB extends import_db_base.InMemoryFileDB {
|
|
|
179
211
|
this.stateTimer = setTimeout(() => this.saveState(), this.writeFileInterval);
|
|
180
212
|
}
|
|
181
213
|
}
|
|
182
|
-
|
|
214
|
+
/**
|
|
215
|
+
* Get all state IDs matching the given pattern (used by the server)
|
|
216
|
+
*
|
|
217
|
+
* @param pattern The pattern to match state IDs against
|
|
218
|
+
*/
|
|
183
219
|
_getKeys(pattern) {
|
|
184
|
-
const r = new RegExp(
|
|
220
|
+
const r = new RegExp(import_db_base.tools.pattern2RegEx(pattern));
|
|
185
221
|
return Object.keys(this.dataset).filter((id) => r.test(id) && id !== this.META_ID);
|
|
186
222
|
}
|
|
187
|
-
|
|
223
|
+
/**
|
|
224
|
+
* Subscribe a client to state changes (used by the server)
|
|
225
|
+
*
|
|
226
|
+
* @param client The client to subscribe
|
|
227
|
+
* @param pattern The pattern of state IDs to subscribe to
|
|
228
|
+
*/
|
|
188
229
|
_subscribeForClient(client, pattern) {
|
|
189
230
|
this.handleSubscribe(client, "state", pattern);
|
|
190
231
|
}
|
|
232
|
+
/**
|
|
233
|
+
* Subscribe a client to meta changes
|
|
234
|
+
*
|
|
235
|
+
* @param client The client to subscribe
|
|
236
|
+
* @param pattern The pattern of meta IDs to subscribe to
|
|
237
|
+
*/
|
|
191
238
|
_subscribeMeta(client, pattern) {
|
|
192
239
|
this.handleSubscribe(client, "meta", pattern);
|
|
193
240
|
}
|
|
194
|
-
|
|
241
|
+
/**
|
|
242
|
+
* Unsubscribe a client from state changes (used by the server)
|
|
243
|
+
*
|
|
244
|
+
* @param client The client to unsubscribe
|
|
245
|
+
* @param pattern The pattern of state IDs to unsubscribe from
|
|
246
|
+
*/
|
|
195
247
|
_unsubscribeForClient(client, pattern) {
|
|
196
|
-
this.handleUnsubscribe(client, "state", pattern);
|
|
248
|
+
this.handleUnsubscribe(client, "state", pattern).catch((e) => this.log.error(`${this.namespace} Cannot unsubscribe client from states: ${e.message}`));
|
|
197
249
|
}
|
|
198
|
-
|
|
250
|
+
/**
|
|
251
|
+
* Subscribe a client to its message box (used by the server)
|
|
252
|
+
*
|
|
253
|
+
* @param client The client to subscribe
|
|
254
|
+
* @param id The ID of the message box owner
|
|
255
|
+
*/
|
|
199
256
|
_subscribeMessageForClient(client, id) {
|
|
200
257
|
this.handleSubscribe(client, "messagebox", `messagebox.${id}`);
|
|
201
258
|
}
|
|
202
|
-
|
|
259
|
+
/**
|
|
260
|
+
* Unsubscribe a client from its message box (used by the server)
|
|
261
|
+
*
|
|
262
|
+
* @param client The client to unsubscribe
|
|
263
|
+
* @param id The ID of the message box owner
|
|
264
|
+
*/
|
|
203
265
|
_unsubscribeMessageForClient(client, id) {
|
|
204
|
-
this.handleUnsubscribe(client, "messagebox", `messagebox.${id}`);
|
|
266
|
+
this.handleUnsubscribe(client, "messagebox", `messagebox.${id}`).catch((e) => this.log.error(`${this.namespace} Cannot unsubscribe client from messagebox: ${e.message}`));
|
|
205
267
|
}
|
|
206
|
-
|
|
268
|
+
/**
|
|
269
|
+
* Subscribe a client to log messages (used by the server)
|
|
270
|
+
*
|
|
271
|
+
* @param client The client to subscribe
|
|
272
|
+
* @param id The ID of the log owner
|
|
273
|
+
*/
|
|
207
274
|
_subscribeLogForClient(client, id) {
|
|
208
275
|
this.handleSubscribe(client, "log", `log.${id}`);
|
|
209
276
|
}
|
|
210
|
-
|
|
277
|
+
/**
|
|
278
|
+
* Unsubscribe a client from log messages (used by the server)
|
|
279
|
+
*
|
|
280
|
+
* @param client The client to unsubscribe
|
|
281
|
+
* @param id The ID of the log owner
|
|
282
|
+
*/
|
|
211
283
|
_unsubscribeLogForClient(client, id) {
|
|
212
|
-
this.handleUnsubscribe(client, "log", `log.${id}`);
|
|
284
|
+
this.handleUnsubscribe(client, "log", `log.${id}`).catch((e) => this.log.error(`${this.namespace} Cannot unsubscribe client from log: ${e.message}`));
|
|
213
285
|
}
|
|
214
|
-
|
|
286
|
+
/**
|
|
287
|
+
* Get a session by its ID (used by the server)
|
|
288
|
+
*
|
|
289
|
+
* @param id The session ID to read
|
|
290
|
+
*/
|
|
215
291
|
_getSession(id) {
|
|
216
292
|
return this.session[id];
|
|
217
293
|
}
|
|
218
|
-
|
|
294
|
+
/**
|
|
295
|
+
* Schedule the expiration of a session, splitting long delays into daily chunks
|
|
296
|
+
*
|
|
297
|
+
* @param id The session ID
|
|
298
|
+
* @param expireDate Timestamp in milliseconds when the session should expire
|
|
299
|
+
*/
|
|
219
300
|
_handleSessionExpire(id, expireDate) {
|
|
220
301
|
if (this.sessionExpires[id] && this.sessionExpires[id].timeout) {
|
|
221
302
|
clearTimeout(this.sessionExpires[id].timeout);
|
|
@@ -242,9 +323,15 @@ class StatesInMemoryFileDB extends import_db_base.InMemoryFileDB {
|
|
|
242
323
|
};
|
|
243
324
|
}
|
|
244
325
|
}
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
326
|
+
/**
|
|
327
|
+
* Create or update a session and schedule its expiration (used by the server)
|
|
328
|
+
*
|
|
329
|
+
* @param id The session ID
|
|
330
|
+
* @param expire Expiration time in seconds from now
|
|
331
|
+
* @param sessionData The session data to store
|
|
332
|
+
*/
|
|
333
|
+
_setSession(id, expire, sessionData) {
|
|
334
|
+
this.session[id] = sessionData || {};
|
|
248
335
|
if (this.sessionExpires[id] && this.sessionExpires[id].timeout) {
|
|
249
336
|
clearTimeout(this.sessionExpires[id].timeout);
|
|
250
337
|
delete this.sessionExpires[id];
|
|
@@ -252,7 +339,11 @@ class StatesInMemoryFileDB extends import_db_base.InMemoryFileDB {
|
|
|
252
339
|
this._handleSessionExpire(id, Date.now() + expire * 1e3);
|
|
253
340
|
this.session[id]._expire = true;
|
|
254
341
|
}
|
|
255
|
-
|
|
342
|
+
/**
|
|
343
|
+
* Destroy a session immediately (used by the server)
|
|
344
|
+
*
|
|
345
|
+
* @param id The session ID to destroy
|
|
346
|
+
*/
|
|
256
347
|
_destroySession(id) {
|
|
257
348
|
if (this.session[id]) {
|
|
258
349
|
delete this.session[id];
|