Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makes sure to return promise from setObject logic. … #2967

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
Placeholder for the next version (at the beginning of the line):
## __WORK IN PROGRESS__
-->

## __WORK IN PROGRESS__
* (Apollon77) Fixes async usage of extendObject
* (Apollon77) Makes setObject async save

## 7.0.3 (2024-11-13) - Lucy
* (@foxriver76) Introduce "Vendor Packages Workflow" (only relevant for vendors - see README.md)

Expand Down
53 changes: 38 additions & 15 deletions packages/adapter/src/lib/adapter/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2765,14 +2765,22 @@
this._intervals.delete(interval as NodeJS.Timeout);
}

setObject(id: string, obj: ioBroker.SettableObject, callback?: ioBroker.SetObjectCallback): Promise<void>;
setObject(
id: string,
obj: ioBroker.SettableObject,
callback?: ioBroker.SetObjectCallback,
): Promise<ioBroker.NonNullCallbackReturnTypeOf<ioBroker.SetObjectCallback> | void>;
setObject(
id: string,
obj: ioBroker.SettableObject,
options: unknown,
callback?: ioBroker.SetObjectCallback,
): Promise<void>;
setObject(id: string, obj: ioBroker.SettableObject, callback?: ioBroker.SetObjectCallback): Promise<void>;
): Promise<ioBroker.NonNullCallbackReturnTypeOf<ioBroker.SetObjectCallback> | void>;
setObject(
id: string,
obj: ioBroker.SettableObject,
callback?: ioBroker.SetObjectCallback,
): Promise<ioBroker.NonNullCallbackReturnTypeOf<ioBroker.SetObjectCallback> | void>;
Comment on lines +2779 to +2783

Check warning

Code scanning / CodeQL

Unreachable method overloads Warning

This overload of setObject() is unreachable, the
previous
overload will always be selected.
/**
* Creates or overwrites an object in objectDB.
*
Expand Down Expand Up @@ -2803,7 +2811,12 @@
* }
* ```
*/
setObject(id: unknown, obj: unknown, options: unknown, callback?: unknown): Promise<void> | void {
setObject(
id: unknown,
obj: unknown,
options: unknown,
callback?: unknown,
): Promise<ioBroker.NonNullCallbackReturnTypeOf<ioBroker.SetObjectCallback> | void> {
if (typeof options === 'function') {
callback = options;
options = null;
Expand All @@ -2819,7 +2832,9 @@
return this._setObject({ id, obj: obj as ioBroker.SettableObject, options, callback });
}

private async _setObject(options: InternalSetObjectOptions): Promise<void> {
private async _setObject(
options: InternalSetObjectOptions,
): Promise<ioBroker.NonNullCallbackReturnTypeOf<ioBroker.SetObjectCallback> | void> {
if (!this._defaultObjs) {
this._defaultObjs = (await import('./defaultObjs.js')).createDefaults();
}
Expand All @@ -2844,6 +2859,7 @@
this._utils.validateId(options.id, false, null);
} catch (e) {
this._logger.error(tools.appendStackTrace(`${this.namespaceLog} ${e.message}`));
// Error is logged and silently ignored to not break older adapters
return;
}
}
Expand Down Expand Up @@ -2921,11 +2937,11 @@
options.obj.user = options.obj.user || (options.options ? options.options.user : '') || SYSTEM_ADMIN_USER;
options.obj.ts = options.obj.ts || Date.now();

this._setObjectWithDefaultValue(options.id, options.obj, options.options, options.callback);
} else {
this._logger.error(`${this.namespaceLog} setObject ${options.id} mandatory property type missing!`);
return tools.maybeCallbackWithError(options.callback, 'mandatory property type missing!');
return this._setObjectWithDefaultValue(options.id, options.obj, options.options, options.callback);
}

this._logger.error(`${this.namespaceLog} setObject ${options.id} mandatory property type missing!`);
return tools.maybeCallbackWithError(options.callback, 'mandatory property type missing!');
}

/**
Expand Down Expand Up @@ -3357,7 +3373,12 @@
* }
* ```
*/
setForeignObject(id: unknown, obj: unknown, options: unknown, callback?: unknown): MaybePromise {
setForeignObject(
id: unknown,
obj: unknown,
options: unknown,
callback?: unknown,
): Promise<ioBroker.NonNullCallbackReturnTypeOf<ioBroker.SetObjectCallback> | void> | void {
if (typeof options === 'function') {
callback = options;
options = null;
Expand Down Expand Up @@ -3386,7 +3407,9 @@
return this._setForeignObject({ id, obj: obj as ioBroker.SettableObject, options, callback });
}

private _setForeignObject(_options: InternalSetObjectOptions): MaybePromise {
private _setForeignObject(
_options: InternalSetObjectOptions,
): Promise<ioBroker.NonNullCallbackReturnTypeOf<ioBroker.SetObjectCallback> | void> | void {
const { options, callback, obj } = _options;
let { id } = _options;

Expand Down Expand Up @@ -3426,7 +3449,7 @@
}
}

this._setObjectWithDefaultValue(id, obj, options, callback);
return this._setObjectWithDefaultValue(id, obj, options, callback);
}

// external signatures
Expand Down Expand Up @@ -9316,7 +9339,7 @@
return tools.maybeCallbackWithError(callback, e);
}
}
this.#states.delState(id, callback);
await this.#states.delState(id, callback);
}

// external signature
Expand Down Expand Up @@ -9784,7 +9807,7 @@

subs[pattern][this.namespace]++;
this.outputCount++;
this.#states.setState(`system.adapter.${autoSubEntry}.subscribes`, JSON.stringify(subs));
await this.#states.setState(`system.adapter.${autoSubEntry}.subscribes`, JSON.stringify(subs));
}
}

Expand Down Expand Up @@ -10013,7 +10036,7 @@
delete subs[pattern];
}
this.outputCount++;
this.#states.setState(`system.adapter.${autoSub}.subscribes`, JSON.stringify(subs));
await this.#states.setState(`system.adapter.${autoSub}.subscribes`, JSON.stringify(subs));
}
}
}
Expand Down
Loading