diff --git a/CHANGELOG.md b/CHANGELOG.md index 4269e72d3..963508d32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,8 +5,9 @@ --> ## __WORK IN PROGRESS__ -* (Apollon77) Fixes async usage of extendObject -* (Apollon77) Makes setObject async save +* (@Apollon77) Fixes async usage of extendObject +* (@Apollon77) Makes setObject async save +* (@foxriver76) deprecated `set(Foreign)ObjectAsync` as the non async methods are now working correctly with promises ## 7.0.3 (2024-11-13) - Lucy * (@foxriver76) Introduce "Vendor Packages Workflow" (only relevant for vendors - see README.md) diff --git a/packages/adapter/src/lib/adapter/adapter.ts b/packages/adapter/src/lib/adapter/adapter.ts index 3a6b78982..1cf9d0c4e 100644 --- a/packages/adapter/src/lib/adapter/adapter.ts +++ b/packages/adapter/src/lib/adapter/adapter.ts @@ -319,9 +319,17 @@ export interface AdapterClass { commandsPermissions: CommandsPermissions, options?: unknown, ): Promise; - /** Creates or overwrites an object in the object db */ + /** + * Creates or overwrites an object in the object db + * + * @deprecated use `adapter.setObject` without a callback instead + */ setObjectAsync(id: string, obj: ioBroker.SettableObject, options?: unknown): ioBroker.SetObjectPromise; - /** Creates or overwrites an object (which might not belong to this adapter) in the object db */ + /** + * Creates or overwrites an object (which might not belong to this adapter) in the object db + * + * @deprecated use `adapter.setForeignObject` without a callback instead + */ setForeignObjectAsync( id: T, obj: ioBroker.SettableObject>, @@ -2768,19 +2776,14 @@ export class AdapterClass extends EventEmitter { setObject( id: string, obj: ioBroker.SettableObject, - callback?: ioBroker.SetObjectCallback, - ): Promise | void>; + ): Promise>; + setObject(id: string, obj: ioBroker.SettableObject, callback: ioBroker.SetObjectCallback): void; + setObject(id: string, obj: ioBroker.SettableObject, options: unknown, callback: 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>; /** * Creates or overwrites an object in objectDB. * @@ -2814,7 +2817,7 @@ export class AdapterClass extends EventEmitter { setObject( id: unknown, obj: unknown, - options: unknown, + options?: unknown, callback?: unknown, ): Promise | void> { if (typeof options === 'function') { @@ -3348,14 +3351,23 @@ export class AdapterClass extends EventEmitter { setForeignObject( id: T, obj: ioBroker.SettableObject>, - callback?: ioBroker.SetObjectCallback, + ): Promise>; + setForeignObject( + id: T, + obj: ioBroker.SettableObject>, + callback: ioBroker.SetObjectCallback, ): void; + setForeignObject( + id: T, + obj: ioBroker.SettableObject>, + options: unknown, + ): Promise>; setForeignObject( id: T, obj: ioBroker.SettableObject>, options: unknown, callback?: ioBroker.SetObjectCallback, - ): void; + ): void | Promise>; /** * Same as {@link AdapterClass.setObject}, but for any object. @@ -3376,7 +3388,7 @@ export class AdapterClass extends EventEmitter { setForeignObject( id: unknown, obj: unknown, - options: unknown, + options?: unknown, callback?: unknown, ): Promise | void> | void { if (typeof options === 'function') { @@ -3456,14 +3468,23 @@ export class AdapterClass extends EventEmitter { extendForeignObject( id: T, objPart: ioBroker.PartialObject>, - callback?: ioBroker.SetObjectCallback, + ): Promise>; + extendForeignObject( + id: T, + objPart: ioBroker.PartialObject>, + callback: ioBroker.SetObjectCallback, ): void; extendForeignObject( id: T, objPart: ioBroker.PartialObject>, options: ioBroker.ExtendObjectOptions, - callback?: ioBroker.SetObjectCallback, + callback: ioBroker.SetObjectCallback, ): void; + extendForeignObject( + id: T, + objPart: ioBroker.PartialObject>, + options: ioBroker.ExtendObjectOptions, + ): Promise>; /** * Same as {@link AdapterClass.extendObject}, but for any object. @@ -3484,7 +3505,7 @@ export class AdapterClass extends EventEmitter { extendForeignObject( id: unknown, obj: unknown, - options: unknown, + options?: unknown, callback?: unknown, ): Promise | void> | void { if (typeof options === 'function') { diff --git a/packages/controller/test/lib/testObjectsFunctions.ts b/packages/controller/test/lib/testObjectsFunctions.ts index c8590dbaf..f329095e0 100644 --- a/packages/controller/test/lib/testObjectsFunctions.ts +++ b/packages/controller/test/lib/testObjectsFunctions.ts @@ -84,7 +84,53 @@ export function register(it: Mocha.TestFunction, expect: Chai.ExpectStatic, cont }); }); - //extendObject + it(`${testName}setObject without callback is async`, async () => { + const id = `${gid}AsyncNoCb`; + + const res = await context.adapter.setObject(id, { + type: 'state', + common: { + name: 'test1', + type: 'number', + role: 'level', + read: true, + write: true, + }, + native: { + attr1: '1', + attr2: '2', + attr3: '3', + }, + }); + + expect(res).to.be.ok; + expect(res.id).to.be.equal(`${context.adapter.namespace}.${id}`); + }); + + it(`${testName}setForeignObject without callback is async`, async () => { + const id = `${context.adapterShortName}.0.${gid}ForeignAsyncNoCb`; + + const res = await context.adapter.setForeignObject(id, { + type: 'state', + common: { + name: 'test1', + type: 'number', + role: 'level', + read: true, + write: true, + }, + native: { + attr1: '1', + attr2: '2', + attr3: '3', + }, + }); + + expect(res).to.be.ok; + expect(res.id).to.be.equal(id); + }); + + // extendObject it(`${testName}Check if objects will be extended`, function (done) { context.adapter.extendObject( gid, @@ -1363,7 +1409,7 @@ export function register(it: Mocha.TestFunction, expect: Chai.ExpectStatic, cont // should use def as default state value on extendObject when obj non-existing it(`${testName}Check extendObject state with def`, async function () { this.timeout(3_000); - let obj = await context.adapter.extendObjectAsync('testDefaultValExtend', { + let obj = await context.adapter.extendObject('testDefaultValExtend', { type: 'state', common: { type: 'string', @@ -1392,7 +1438,7 @@ export function register(it: Mocha.TestFunction, expect: Chai.ExpectStatic, cont // delete state but not object await context.adapter.delStateAsync('testDefaultValExtend'); // extend it again - def should be created again, because state has been removed - now we set a def object - obj = await context.adapter.extendObjectAsync('testDefaultValExtend', { + obj = await context.adapter.extendObject('testDefaultValExtend', { common: { def: { hello: 'world' }, }, @@ -1408,7 +1454,8 @@ export function register(it: Mocha.TestFunction, expect: Chai.ExpectStatic, cont // should use def as default state value on extendForeignObject when obj non-existing it(`${testName}Check extendForeignObject state with def`, async () => { - let obj = await context.adapter.extendForeignObjectAsync('foreign.0.testDefaultValExtend', { + const id = 'foreign.0.testDefaultValExtend'; + let obj = await context.adapter.extendForeignObject(id, { type: 'state', common: { type: 'string', @@ -1417,13 +1464,14 @@ export function register(it: Mocha.TestFunction, expect: Chai.ExpectStatic, cont }); expect(obj).to.be.ok; + expect(obj?.id).to.be.equal(id); - let state = await context.adapter.getForeignStateAsync('foreign.0.testDefaultValExtend'); + let state = await context.adapter.getForeignStateAsync(id); expect(state!.val).to.equal('Run Forrest, Run!'); expect(state!.ack).to.equal(true); // when state already exists def should not override - obj = await context.adapter.extendForeignObjectAsync('foreign.0.testDefaultValExtend', { + obj = await context.adapter.extendForeignObjectAsync(id, { common: { def: 'Please, do not set me up', }, @@ -1431,13 +1479,13 @@ export function register(it: Mocha.TestFunction, expect: Chai.ExpectStatic, cont expect(obj).to.be.ok; - state = await context.adapter.getForeignStateAsync('foreign.0.testDefaultValExtend'); + state = await context.adapter.getForeignStateAsync(id); expect(state!.val).to.equal('Run Forrest, Run!'); // delete state but not object - await context.adapter.delForeignStateAsync('foreign.0.testDefaultValExtend'); + await context.adapter.delForeignStateAsync(id); // extend it again - def should be created again, because state has been removed - now we set a def object - obj = await context.adapter.extendForeignObjectAsync('foreign.0.testDefaultValExtend', { + obj = await context.adapter.extendForeignObjectAsync(id, { common: { def: { hello: 'world' }, }, @@ -1445,7 +1493,7 @@ export function register(it: Mocha.TestFunction, expect: Chai.ExpectStatic, cont expect(obj).to.be.ok; - state = await context.adapter.getForeignStateAsync('foreign.0.testDefaultValExtend'); + state = await context.adapter.getForeignStateAsync(id); // @ts-expect-error TODO do we want this auto parsing? expect(state!.val!.hello).to.equal('world'); expect(state!.ack).to.equal(true);