Skip to content

Commit

Permalink
refactor: align with modbus
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpeintner committed Sep 6, 2023
1 parent a6c9f9f commit be3d6de
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions packages/binding-mbus/src/mbus-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export class MBusConnection {
}
}

async execute(op: PropertyOperation): Promise<Content | PromiseLike<Content>> {
async execute(op: PropertyOperation): Promise<Content | PromiseLike<Content> | undefined> {
this.trigger();
return op.execute();
}
Expand All @@ -182,7 +182,7 @@ export class MBusConnection {
// inform all the operations that the connection cannot be recovered
this.queue.forEach((transaction) => {
transaction.operations.forEach((op) => {
op.failed(`${error}`);
op.failed(error instanceof Error ? error : new Error(JSON.stringify(error)));
});
});
}
Expand Down Expand Up @@ -215,7 +215,9 @@ export class MBusConnection {
} catch (err) {
warn(`Read operation failed on unit ${transaction.unitId}. ${err}.`);
// inform all operations and the invoker
transaction.operations.forEach((op) => op.failed(`${error}`));
transaction.operations.forEach((op) =>
op.failed(error instanceof Error ? error : new Error(JSON.stringify(error)))
);

Check warning on line 220 in packages/binding-mbus/src/mbus-connection.ts

View check run for this annotation

Codecov / codecov/patch

packages/binding-mbus/src/mbus-connection.ts#L218-L220

Added lines #L218 - L220 were not covered by tests
throw err;
}
}
Expand Down Expand Up @@ -278,11 +280,13 @@ export class PropertyOperation {
* Trigger execution of this operation.
*
*/
async execute(): Promise<Content | PromiseLike<Content>> {
return new Promise<Content>((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
async execute(): Promise<(Content | PromiseLike<Content>) | undefined> {
return new Promise(
(resolve: (value?: Content | PromiseLike<Content>) => void, reject: (reason?: Error) => void) => {
this.resolve = resolve;
this.reject = reject;
}
);
}

/**
Expand Down Expand Up @@ -315,26 +319,24 @@ export class PropertyOperation {

const resp = new Content("application/json", Readable.from(JSON.stringify(payload)));

// resolve the Promise given to the invoking script
if (this.resolve) {
this.resolve(resp);
} else {
warn("resolve undefined");
if (!this.resolve) {
throw new Error("Function 'done' was invoked before executing the Mbus operation");
}

Check warning on line 324 in packages/binding-mbus/src/mbus-connection.ts

View check run for this annotation

Codecov / codecov/patch

packages/binding-mbus/src/mbus-connection.ts#L322-L324

Added lines #L322 - L324 were not covered by tests
// resolve the Promise given to the invoking script
this.resolve(resp);
}

/**
* Invoked by the MBusTransaction when it has failed.
*
* @param reason Reason of failure
*/
failed(reason: string): void {
warn("Operation failed:", reason);
// reject the Promise given to the invoking script
if (this.reject) {
this.reject(new Error(reason));
} else {
warn("reject undefined");
failed(reason: Error): void {
warn(`Operation failed: ${reason}`);
if (!this.reject) {
throw new Error("Function 'failed' was invoked before executing the Mbus operation");
}

Check warning on line 338 in packages/binding-mbus/src/mbus-connection.ts

View check run for this annotation

Codecov / codecov/patch

packages/binding-mbus/src/mbus-connection.ts#L337-L338

Added lines #L337 - L338 were not covered by tests
// reject the Promise given to the invoking script
this.reject(reason);
}
}

0 comments on commit be3d6de

Please sign in to comment.