@iobroker/db-base
Advanced tools
Comparing version 5.0.20-alpha.0-20240508-d36cddc8d to 5.0.20-alpha.0-20240510-819f1976e
@@ -10,2 +10,3 @@ /** | ||
import type { InternalLogger } from '@iobroker/js-controller-common/tools'; | ||
import type { RedisHandler } from '../lib/redisHandler.js'; | ||
export interface ConnectionOptions { | ||
@@ -67,5 +68,14 @@ pass?: string; | ||
} | ||
interface SubscriptionClient { | ||
_subscribe?: Record<string, Subscription[]>; | ||
interface SubscriptionClient extends Partial<InstanceType<typeof RedisHandler>> { | ||
_subscribe?: Map<string, Subscription[]>; | ||
} | ||
interface SubscriptionClientRegex { | ||
/** The RegExp object */ | ||
regex: RegExp; | ||
/** All clients for this regexp */ | ||
clients: { | ||
client: SubscriptionClient; | ||
pattern: string; | ||
}[]; | ||
} | ||
/** | ||
@@ -76,2 +86,4 @@ * The parent of the class structure, which provides basic JSON storage | ||
export declare class InMemoryFileDB { | ||
/** Store all clients per RegExp */ | ||
private regExps; | ||
private settings; | ||
@@ -105,6 +117,17 @@ private readonly change; | ||
initBackupDir(): void; | ||
handleSubscribe(client: SubscriptionClient, type: string, pattern: string | string[], cb?: () => void): void; | ||
handleSubscribe(client: SubscriptionClient, type: string, pattern: string | string[], options: any, cb?: () => void): void; | ||
handleUnsubscribe(client: SubscriptionClient, type: string, pattern: string | string[], cb?: () => void): void | Promise<void>; | ||
publishToClients(_client: SubscriptionClient, _type: string, _id: string, _obj: any): number; | ||
/** | ||
* Add client subscribe to a regex for given type | ||
* | ||
* @param options pattern, client and type information | ||
*/ | ||
private addSubscribeToRegex; | ||
handleSubscribe(client: SubscriptionClient, type: string, pattern: string | string[]): void; | ||
/** | ||
* Remove client subscribe from a regex for given type | ||
* | ||
* @param options pattern, client and type information | ||
*/ | ||
private removeSubscribeFromRegex; | ||
handleUnsubscribe(client: SubscriptionClient, type: string, pattern: string | string[]): void | Promise<void>; | ||
publishPattern(_patternInformation: SubscriptionClientRegex, _type: string, _id: string, _obj: unknown): number; | ||
deleteOldBackupFiles(baseFilename: string): void; | ||
@@ -129,3 +152,3 @@ getTimeStr(date: number): string; | ||
getStatus(): DbStatus; | ||
getClients(): Record<string, any>; | ||
getClients(): Map<string, SubscriptionClient>; | ||
publishAll(type: string, id: string, obj: any): number; | ||
@@ -132,0 +155,0 @@ destroy(): Promise<void>; |
@@ -35,2 +35,3 @@ "use strict"; | ||
class InMemoryFileDB { | ||
regExps = /* @__PURE__ */ new Map(); | ||
settings; | ||
@@ -155,44 +156,54 @@ change; | ||
} | ||
handleSubscribe(client, type, pattern, options, cb) { | ||
if (typeof options === "function") { | ||
cb = options; | ||
options = void 0; | ||
addSubscribeToRegex(options) { | ||
const { type, pattern, client } = options; | ||
const regExpsForType = this.regExps.get(type); | ||
const regexStr = import_js_controller_common.tools.pattern2RegEx(pattern); | ||
const regex = new RegExp(regexStr); | ||
if (!regExpsForType.has(regexStr)) { | ||
regExpsForType.set(regexStr, { regex, clients: [] }); | ||
} | ||
client._subscribe = client._subscribe || {}; | ||
client._subscribe[type] = client._subscribe[type] || []; | ||
const s = client._subscribe[type]; | ||
const regExs = regExpsForType.get(regexStr); | ||
const found = !!regExs.clients.find((cl) => cl === client); | ||
if (!found) { | ||
regExs.clients.push({ client, pattern }); | ||
} | ||
} | ||
handleSubscribe(client, type, pattern) { | ||
if (!this.regExps.has(type)) { | ||
this.regExps.set(type, /* @__PURE__ */ new Map()); | ||
} | ||
if (pattern instanceof Array) { | ||
pattern.forEach((pattern2) => { | ||
if (s.find((sub) => sub.pattern === pattern2)) { | ||
return; | ||
} | ||
s.push({ pattern: pattern2, regex: new RegExp(import_js_controller_common.tools.pattern2RegEx(pattern2)), options }); | ||
}); | ||
for (const patternStr of pattern) { | ||
this.addSubscribeToRegex({ pattern: patternStr, client, type }); | ||
} | ||
} else { | ||
if (!s.find((sub) => sub.pattern === pattern)) { | ||
s.push({ pattern, regex: new RegExp(import_js_controller_common.tools.pattern2RegEx(pattern)), options }); | ||
} | ||
this.addSubscribeToRegex({ pattern, client, type }); | ||
} | ||
typeof cb === "function" && cb(); | ||
} | ||
handleUnsubscribe(client, type, pattern, cb) { | ||
const s = client?._subscribe?.[type]; | ||
if (s) { | ||
const removeEntry = (p) => { | ||
const index = s.findIndex((sub) => sub.pattern === p); | ||
if (index > -1) { | ||
s.splice(index, 1); | ||
} | ||
}; | ||
if (pattern instanceof Array) { | ||
pattern.forEach((p) => { | ||
removeEntry(p); | ||
}); | ||
} else { | ||
removeEntry(pattern); | ||
removeSubscribeFromRegex(options) { | ||
const { type, pattern, client } = options; | ||
const regExpsForType = this.regExps.get(type); | ||
const regexStr = import_js_controller_common.tools.pattern2RegEx(pattern); | ||
const entry = regExpsForType.get(regexStr); | ||
if (!entry) { | ||
return; | ||
} | ||
const clientIndex = entry.clients.findIndex((cl) => cl.client === client); | ||
if (clientIndex > -1) { | ||
entry.clients.splice(clientIndex, 1); | ||
} | ||
if (entry.clients.length === 0) { | ||
regExpsForType.delete(regexStr); | ||
} | ||
} | ||
handleUnsubscribe(client, type, pattern) { | ||
if (pattern instanceof Array) { | ||
for (const p of pattern) { | ||
this.removeSubscribeFromRegex({ pattern: p, client, type }); | ||
} | ||
} else { | ||
this.removeSubscribeFromRegex({ pattern, client, type }); | ||
} | ||
return import_js_controller_common.tools.maybeCallback(cb); | ||
} | ||
publishToClients(_client, _type, _id, _obj) { | ||
publishPattern(_patternInformation, _type, _id, _obj) { | ||
throw new Error("no communication handling implemented"); | ||
@@ -327,3 +338,3 @@ } | ||
getClients() { | ||
return {}; | ||
return /* @__PURE__ */ new Map(); | ||
} | ||
@@ -335,11 +346,11 @@ publishAll(type, id, obj) { | ||
} | ||
const clients = this.getClients(); | ||
let publishCount = 0; | ||
if (clients && typeof clients === "object") { | ||
for (const i of Object.keys(clients)) { | ||
publishCount += this.publishToClients(clients[i], type, id, obj); | ||
const patternInfo = this.regExps.get(type); | ||
if (patternInfo) { | ||
for (const regex of patternInfo.values()) { | ||
publishCount += this.publishPattern(regex, type, id, obj); | ||
} | ||
} | ||
if (this.change && this.callbackSubscriptionClient._subscribe && this.callbackSubscriptionClient._subscribe[type]) { | ||
for (const entry of this.callbackSubscriptionClient._subscribe[type]) { | ||
if (this.change && this.callbackSubscriptionClient._subscribe && this.callbackSubscriptionClient._subscribe.has(type)) { | ||
for (const entry of this.callbackSubscriptionClient._subscribe.get(type)) { | ||
if (entry.regex.test(id)) { | ||
@@ -346,0 +357,0 @@ setImmediate(() => this.change(id, obj)); |
@@ -10,2 +10,3 @@ /** | ||
import type { InternalLogger } from '@iobroker/js-controller-common/tools'; | ||
import type { RedisHandler } from '../lib/redisHandler.js'; | ||
export interface ConnectionOptions { | ||
@@ -67,5 +68,14 @@ pass?: string; | ||
} | ||
interface SubscriptionClient { | ||
_subscribe?: Record<string, Subscription[]>; | ||
interface SubscriptionClient extends Partial<InstanceType<typeof RedisHandler>> { | ||
_subscribe?: Map<string, Subscription[]>; | ||
} | ||
interface SubscriptionClientRegex { | ||
/** The RegExp object */ | ||
regex: RegExp; | ||
/** All clients for this regexp */ | ||
clients: { | ||
client: SubscriptionClient; | ||
pattern: string; | ||
}[]; | ||
} | ||
/** | ||
@@ -76,2 +86,4 @@ * The parent of the class structure, which provides basic JSON storage | ||
export declare class InMemoryFileDB { | ||
/** Store all clients per RegExp */ | ||
private regExps; | ||
private settings; | ||
@@ -105,6 +117,17 @@ private readonly change; | ||
initBackupDir(): void; | ||
handleSubscribe(client: SubscriptionClient, type: string, pattern: string | string[], cb?: () => void): void; | ||
handleSubscribe(client: SubscriptionClient, type: string, pattern: string | string[], options: any, cb?: () => void): void; | ||
handleUnsubscribe(client: SubscriptionClient, type: string, pattern: string | string[], cb?: () => void): void | Promise<void>; | ||
publishToClients(_client: SubscriptionClient, _type: string, _id: string, _obj: any): number; | ||
/** | ||
* Add client subscribe to a regex for given type | ||
* | ||
* @param options pattern, client and type information | ||
*/ | ||
private addSubscribeToRegex; | ||
handleSubscribe(client: SubscriptionClient, type: string, pattern: string | string[]): void; | ||
/** | ||
* Remove client subscribe from a regex for given type | ||
* | ||
* @param options pattern, client and type information | ||
*/ | ||
private removeSubscribeFromRegex; | ||
handleUnsubscribe(client: SubscriptionClient, type: string, pattern: string | string[]): void | Promise<void>; | ||
publishPattern(_patternInformation: SubscriptionClientRegex, _type: string, _id: string, _obj: unknown): number; | ||
deleteOldBackupFiles(baseFilename: string): void; | ||
@@ -129,3 +152,3 @@ getTimeStr(date: number): string; | ||
getStatus(): DbStatus; | ||
getClients(): Record<string, any>; | ||
getClients(): Map<string, SubscriptionClient>; | ||
publishAll(type: string, id: string, obj: any): number; | ||
@@ -132,0 +155,0 @@ destroy(): Promise<void>; |
@@ -18,2 +18,4 @@ /** | ||
export class InMemoryFileDB { | ||
/** Store all clients per RegExp */ | ||
regExps = new Map(); | ||
settings; | ||
@@ -169,46 +171,66 @@ change; | ||
} | ||
handleSubscribe(client, type, pattern, options, cb) { | ||
if (typeof options === 'function') { | ||
cb = options; | ||
options = undefined; | ||
/** | ||
* Add client subscribe to a regex for given type | ||
* | ||
* @param options pattern, client and type information | ||
*/ | ||
addSubscribeToRegex(options) { | ||
const { type, pattern, client } = options; | ||
const regExpsForType = this.regExps.get(type); | ||
const regexStr = tools.pattern2RegEx(pattern); | ||
const regex = new RegExp(regexStr); | ||
if (!regExpsForType.has(regexStr)) { | ||
regExpsForType.set(regexStr, { regex, clients: [] }); | ||
} | ||
client._subscribe = client._subscribe || {}; | ||
client._subscribe[type] = client._subscribe[type] || []; | ||
const s = client._subscribe[type]; | ||
const regExs = regExpsForType.get(regexStr); | ||
const found = !!regExs.clients.find(cl => cl === client); | ||
if (!found) { | ||
regExs.clients.push({ client, pattern }); | ||
} | ||
} | ||
handleSubscribe(client, type, pattern) { | ||
if (!this.regExps.has(type)) { | ||
this.regExps.set(type, new Map()); | ||
} | ||
if (pattern instanceof Array) { | ||
pattern.forEach(pattern => { | ||
if (s.find(sub => sub.pattern === pattern)) { | ||
return; | ||
} | ||
s.push({ pattern, regex: new RegExp(tools.pattern2RegEx(pattern)), options }); | ||
}); | ||
for (const patternStr of pattern) { | ||
this.addSubscribeToRegex({ pattern: patternStr, client, type }); | ||
} | ||
} | ||
else { | ||
if (!s.find(sub => sub.pattern === pattern)) { | ||
s.push({ pattern, regex: new RegExp(tools.pattern2RegEx(pattern)), options }); | ||
} | ||
this.addSubscribeToRegex({ pattern, client, type }); | ||
} | ||
typeof cb === 'function' && cb(); | ||
} | ||
handleUnsubscribe(client, type, pattern, cb) { | ||
const s = client?._subscribe?.[type]; | ||
if (s) { | ||
const removeEntry = (p) => { | ||
const index = s.findIndex(sub => sub.pattern === p); | ||
if (index > -1) { | ||
s.splice(index, 1); | ||
} | ||
}; | ||
if (pattern instanceof Array) { | ||
pattern.forEach(p => { | ||
removeEntry(p); | ||
}); | ||
/** | ||
* Remove client subscribe from a regex for given type | ||
* | ||
* @param options pattern, client and type information | ||
*/ | ||
removeSubscribeFromRegex(options) { | ||
const { type, pattern, client } = options; | ||
const regExpsForType = this.regExps.get(type); | ||
const regexStr = tools.pattern2RegEx(pattern); | ||
const entry = regExpsForType.get(regexStr); | ||
if (!entry) { | ||
return; | ||
} | ||
const clientIndex = entry.clients.findIndex(cl => cl.client === client); | ||
if (clientIndex > -1) { | ||
entry.clients.splice(clientIndex, 1); | ||
} | ||
if (entry.clients.length === 0) { | ||
regExpsForType.delete(regexStr); | ||
} | ||
} | ||
handleUnsubscribe(client, type, pattern) { | ||
if (pattern instanceof Array) { | ||
for (const p of pattern) { | ||
this.removeSubscribeFromRegex({ pattern: p, client, type }); | ||
} | ||
else { | ||
removeEntry(pattern); | ||
} | ||
} | ||
return tools.maybeCallback(cb); | ||
else { | ||
this.removeSubscribeFromRegex({ pattern, client, type }); | ||
} | ||
} | ||
publishToClients(_client, _type, _id, _obj) { | ||
publishPattern(_patternInformation, _type, _id, _obj) { | ||
throw new Error('no communication handling implemented'); | ||
@@ -376,3 +398,3 @@ } | ||
getClients() { | ||
return {}; | ||
return new Map(); | ||
} | ||
@@ -384,7 +406,7 @@ publishAll(type, id, obj) { | ||
} | ||
const clients = this.getClients(); | ||
let publishCount = 0; | ||
if (clients && typeof clients === 'object') { | ||
for (const i of Object.keys(clients)) { | ||
publishCount += this.publishToClients(clients[i], type, id, obj); | ||
const patternInfo = this.regExps.get(type); | ||
if (patternInfo) { | ||
for (const regex of patternInfo.values()) { | ||
publishCount += this.publishPattern(regex, type, id, obj); | ||
} | ||
@@ -395,4 +417,4 @@ } | ||
this.callbackSubscriptionClient._subscribe && | ||
this.callbackSubscriptionClient._subscribe[type]) { | ||
for (const entry of this.callbackSubscriptionClient._subscribe[type]) { | ||
this.callbackSubscriptionClient._subscribe.has(type)) { | ||
for (const entry of this.callbackSubscriptionClient._subscribe.get(type)) { | ||
if (entry.regex.test(id)) { | ||
@@ -399,0 +421,0 @@ // @ts-expect-error we have checked 3 lines above |
{ | ||
"name": "@iobroker/db-base", | ||
"type": "module", | ||
"version": "5.0.20-alpha.0-20240508-d36cddc8d", | ||
"version": "5.0.20-alpha.0-20240510-819f1976e", | ||
"engines": { | ||
@@ -9,3 +9,3 @@ "node": ">=18.0.0" | ||
"dependencies": { | ||
"@iobroker/js-controller-common": "5.0.20-alpha.0-20240508-d36cddc8d", | ||
"@iobroker/js-controller-common": "5.0.20-alpha.0-20240510-819f1976e", | ||
"deep-clone": "^3.0.3", | ||
@@ -31,3 +31,3 @@ "fs-extra": "^11.1.0", | ||
"scripts": { | ||
"build": "tsc -b tsconfig.build.json", | ||
"build": "tsc -b tsconfig.build.json && tsc-alias", | ||
"postbuild": "esm2cjs --in build/esm --out build/cjs -l error -t node18 && cpy ./**/*.d.ts ./build/cjs/ --cwd=build/esm/" | ||
@@ -58,3 +58,3 @@ }, | ||
], | ||
"gitHead": "a1bf1c4c5ca460d8d1faaf73dcc7cfe1d93ed3b7" | ||
"gitHead": "9189372e5c7449f16964b737db9bbe85d7491619" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
225837
2306