@iobroker/db-base
Advanced tools
Comparing version 6.0.1-alpha.0-20240603-3deca26fa to 6.0.1-alpha.0-20240603-8378eb65c
@@ -10,3 +10,2 @@ /** | ||
import type { InternalLogger } from '@iobroker/js-controller-common-db/tools'; | ||
import type { RedisHandler } from '../lib/redisHandler.js'; | ||
export interface ConnectionOptions { | ||
@@ -68,14 +67,5 @@ pass?: string; | ||
} | ||
interface SubscriptionClient extends Partial<InstanceType<typeof RedisHandler>> { | ||
_subscribe?: Map<string, Subscription[]>; | ||
interface SubscriptionClient { | ||
_subscribe?: Record<string, Subscription[]>; | ||
} | ||
interface SubscriptionClientRegex { | ||
/** The RegExp object */ | ||
regex: RegExp; | ||
/** All clients for this regexp */ | ||
clients: { | ||
client: SubscriptionClient; | ||
pattern: string; | ||
}[]; | ||
} | ||
/** | ||
@@ -86,4 +76,2 @@ * The parent of the class structure, which provides basic JSON storage | ||
export declare class InMemoryFileDB { | ||
/** Store all clients per RegExp */ | ||
private regExps; | ||
private settings; | ||
@@ -117,17 +105,6 @@ private readonly change; | ||
initBackupDir(): void; | ||
/** | ||
* 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; | ||
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; | ||
deleteOldBackupFiles(baseFilename: string): void; | ||
@@ -152,3 +129,3 @@ getTimeStr(date: number): string; | ||
getStatus(): DbStatus; | ||
getClients(): Map<string, SubscriptionClient>; | ||
getClients(): Record<string, any>; | ||
publishAll(type: string, id: string, obj: any): number; | ||
@@ -155,0 +132,0 @@ destroy(): Promise<void>; |
@@ -35,3 +35,2 @@ "use strict"; | ||
class InMemoryFileDB { | ||
regExps = /* @__PURE__ */ new Map(); | ||
settings; | ||
@@ -156,54 +155,44 @@ change; | ||
} | ||
addSubscribeToRegex(options) { | ||
const { type, pattern, client } = options; | ||
const regExpsForType = this.regExps.get(type); | ||
const regexStr = import_js_controller_common_db.tools.pattern2RegEx(pattern); | ||
const regex = new RegExp(regexStr); | ||
if (!regExpsForType.has(regexStr)) { | ||
regExpsForType.set(regexStr, { regex, clients: [] }); | ||
handleSubscribe(client, type, pattern, options, cb) { | ||
if (typeof options === "function") { | ||
cb = options; | ||
options = void 0; | ||
} | ||
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()); | ||
} | ||
client._subscribe = client._subscribe || {}; | ||
client._subscribe[type] = client._subscribe[type] || []; | ||
const s = client._subscribe[type]; | ||
if (pattern instanceof Array) { | ||
for (const patternStr of pattern) { | ||
this.addSubscribeToRegex({ pattern: patternStr, client, type }); | ||
pattern.forEach((pattern2) => { | ||
if (s.find((sub) => sub.pattern === pattern2)) { | ||
return; | ||
} | ||
s.push({ pattern: pattern2, regex: new RegExp(import_js_controller_common_db.tools.pattern2RegEx(pattern2)), options }); | ||
}); | ||
} else { | ||
if (!s.find((sub) => sub.pattern === pattern)) { | ||
s.push({ pattern, regex: new RegExp(import_js_controller_common_db.tools.pattern2RegEx(pattern)), options }); | ||
} | ||
} else { | ||
this.addSubscribeToRegex({ pattern, client, type }); | ||
} | ||
typeof cb === "function" && cb(); | ||
} | ||
removeSubscribeFromRegex(options) { | ||
const { type, pattern, client } = options; | ||
const regExpsForType = this.regExps.get(type); | ||
const regexStr = import_js_controller_common_db.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 }); | ||
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); | ||
} | ||
} else { | ||
this.removeSubscribeFromRegex({ pattern, client, type }); | ||
} | ||
return import_js_controller_common_db.tools.maybeCallback(cb); | ||
} | ||
publishPattern(_patternInformation, _type, _id, _obj) { | ||
publishToClients(_client, _type, _id, _obj) { | ||
throw new Error("no communication handling implemented"); | ||
@@ -338,3 +327,3 @@ } | ||
getClients() { | ||
return /* @__PURE__ */ new Map(); | ||
return {}; | ||
} | ||
@@ -346,11 +335,11 @@ publishAll(type, id, obj) { | ||
} | ||
const clients = this.getClients(); | ||
let publishCount = 0; | ||
const patternInfo = this.regExps.get(type); | ||
if (patternInfo) { | ||
for (const regex of patternInfo.values()) { | ||
publishCount += this.publishPattern(regex, type, id, obj); | ||
if (clients && typeof clients === "object") { | ||
for (const i of Object.keys(clients)) { | ||
publishCount += this.publishToClients(clients[i], type, id, obj); | ||
} | ||
} | ||
if (this.change && this.callbackSubscriptionClient._subscribe && this.callbackSubscriptionClient._subscribe.has(type)) { | ||
for (const entry of this.callbackSubscriptionClient._subscribe.get(type)) { | ||
if (this.change && this.callbackSubscriptionClient._subscribe && this.callbackSubscriptionClient._subscribe[type]) { | ||
for (const entry of this.callbackSubscriptionClient._subscribe[type]) { | ||
if (entry.regex.test(id)) { | ||
@@ -357,0 +346,0 @@ setImmediate(() => this.change(id, obj)); |
@@ -10,3 +10,2 @@ /** | ||
import type { InternalLogger } from '@iobroker/js-controller-common-db/tools'; | ||
import type { RedisHandler } from '../lib/redisHandler.js'; | ||
export interface ConnectionOptions { | ||
@@ -68,14 +67,5 @@ pass?: string; | ||
} | ||
interface SubscriptionClient extends Partial<InstanceType<typeof RedisHandler>> { | ||
_subscribe?: Map<string, Subscription[]>; | ||
interface SubscriptionClient { | ||
_subscribe?: Record<string, Subscription[]>; | ||
} | ||
interface SubscriptionClientRegex { | ||
/** The RegExp object */ | ||
regex: RegExp; | ||
/** All clients for this regexp */ | ||
clients: { | ||
client: SubscriptionClient; | ||
pattern: string; | ||
}[]; | ||
} | ||
/** | ||
@@ -86,4 +76,2 @@ * The parent of the class structure, which provides basic JSON storage | ||
export declare class InMemoryFileDB { | ||
/** Store all clients per RegExp */ | ||
private regExps; | ||
private settings; | ||
@@ -117,17 +105,6 @@ private readonly change; | ||
initBackupDir(): void; | ||
/** | ||
* 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; | ||
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; | ||
deleteOldBackupFiles(baseFilename: string): void; | ||
@@ -152,3 +129,3 @@ getTimeStr(date: number): string; | ||
getStatus(): DbStatus; | ||
getClients(): Map<string, SubscriptionClient>; | ||
getClients(): Record<string, any>; | ||
publishAll(type: string, id: string, obj: any): number; | ||
@@ -155,0 +132,0 @@ destroy(): Promise<void>; |
@@ -18,4 +18,2 @@ /** | ||
export class InMemoryFileDB { | ||
/** Store all clients per RegExp */ | ||
regExps = new Map(); | ||
settings; | ||
@@ -171,66 +169,46 @@ change; | ||
} | ||
/** | ||
* 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: [] }); | ||
handleSubscribe(client, type, pattern, options, cb) { | ||
if (typeof options === 'function') { | ||
cb = options; | ||
options = undefined; | ||
} | ||
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()); | ||
} | ||
client._subscribe = client._subscribe || {}; | ||
client._subscribe[type] = client._subscribe[type] || []; | ||
const s = client._subscribe[type]; | ||
if (pattern instanceof Array) { | ||
for (const patternStr of pattern) { | ||
this.addSubscribeToRegex({ pattern: patternStr, client, type }); | ||
} | ||
pattern.forEach(pattern => { | ||
if (s.find(sub => sub.pattern === pattern)) { | ||
return; | ||
} | ||
s.push({ pattern, regex: new RegExp(tools.pattern2RegEx(pattern)), options }); | ||
}); | ||
} | ||
else { | ||
this.addSubscribeToRegex({ pattern, client, type }); | ||
if (!s.find(sub => sub.pattern === pattern)) { | ||
s.push({ pattern, regex: new RegExp(tools.pattern2RegEx(pattern)), options }); | ||
} | ||
} | ||
typeof cb === 'function' && cb(); | ||
} | ||
/** | ||
* 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 }); | ||
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); | ||
} | ||
} | ||
else { | ||
this.removeSubscribeFromRegex({ pattern, client, type }); | ||
} | ||
return tools.maybeCallback(cb); | ||
} | ||
publishPattern(_patternInformation, _type, _id, _obj) { | ||
publishToClients(_client, _type, _id, _obj) { | ||
throw new Error('no communication handling implemented'); | ||
@@ -398,3 +376,3 @@ } | ||
getClients() { | ||
return new Map(); | ||
return {}; | ||
} | ||
@@ -406,7 +384,7 @@ publishAll(type, id, obj) { | ||
} | ||
const clients = this.getClients(); | ||
let publishCount = 0; | ||
const patternInfo = this.regExps.get(type); | ||
if (patternInfo) { | ||
for (const regex of patternInfo.values()) { | ||
publishCount += this.publishPattern(regex, type, id, obj); | ||
if (clients && typeof clients === 'object') { | ||
for (const i of Object.keys(clients)) { | ||
publishCount += this.publishToClients(clients[i], type, id, obj); | ||
} | ||
@@ -417,4 +395,4 @@ } | ||
this.callbackSubscriptionClient._subscribe && | ||
this.callbackSubscriptionClient._subscribe.has(type)) { | ||
for (const entry of this.callbackSubscriptionClient._subscribe.get(type)) { | ||
this.callbackSubscriptionClient._subscribe[type]) { | ||
for (const entry of this.callbackSubscriptionClient._subscribe[type]) { | ||
if (entry.regex.test(id)) { | ||
@@ -421,0 +399,0 @@ // @ts-expect-error we have checked 3 lines above |
{ | ||
"name": "@iobroker/db-base", | ||
"type": "module", | ||
"version": "6.0.1-alpha.0-20240603-3deca26fa", | ||
"version": "6.0.1-alpha.0-20240603-8378eb65c", | ||
"engines": { | ||
@@ -9,3 +9,3 @@ "node": ">=18.0.0" | ||
"dependencies": { | ||
"@iobroker/js-controller-common-db": "6.0.1-alpha.0-20240603-3deca26fa", | ||
"@iobroker/js-controller-common-db": "6.0.1-alpha.0-20240603-8378eb65c", | ||
"deep-clone": "^3.0.3", | ||
@@ -31,3 +31,3 @@ "fs-extra": "^11.1.0", | ||
"scripts": { | ||
"build": "tsc -b tsconfig.build.json && tsc-alias", | ||
"build": "tsc -b tsconfig.build.json", | ||
"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": "59c191f7ef1995375ade77815af6d3cf5b6efadf" | ||
"gitHead": "8e55b654ff30dec37857c4495d27022d48055a29" | ||
} |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
221644
2227