Skip to content

Commit

Permalink
refactor: remove ts-ignore statements and fix code
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpeintner committed Aug 31, 2023
1 parent 1bd4c5e commit 511602d
Showing 1 changed file with 38 additions and 43 deletions.
81 changes: 38 additions & 43 deletions packages/examples/src/quickstart/simple-coffee-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import { Servient } from "@node-wot/core";
import { HttpServer } from "@node-wot/binding-http";
import { InteractionOutput } from "wot-typescript-definitions";

// create Servient add HTTP binding with port configuration
const servient = new Servient();
Expand Down Expand Up @@ -82,52 +81,48 @@ servient.start().then((WoT) => {
};
});

thing.setActionHandler("brew", async (params) => {
thing.setActionHandler("brew", async (params, options) => {
const coffeeType = await params.value();
console.info("received coffee order of ", coffeeType);
return new Promise<InteractionOutput>((resolve, reject) => {
if (coffeeType === "espresso") {
console.log("here");
if (waterAmount <= 10 || beansAmount <= 10) {
console.log("here4");
reject(new Error("Not enough water or beans"));
} else {
console.log("here2");
setTimeout(() => {
console.log("here3");
waterAmount = waterAmount - 10;
beansAmount = beansAmount - 10;
// @ts-ignore
resolve();
}, 1000);
}
} else if (coffeeType === "cappuccino") {
if (waterAmount <= 20 || beansAmount <= 25 || milkAmount <= 15) {
reject(new Error("Not enough water or beans"));
} else {
setTimeout(() => {
waterAmount = waterAmount - 15;
beansAmount = beansAmount - 20;
milkAmount = milkAmount - 10;
// @ts-ignore
resolve();
}, 2000);
}
} else if (coffeeType === "americano") {
if (waterAmount <= 35 || beansAmount <= 10) {
reject(new Error("Not enough water or beans"));
} else {
setTimeout(() => {
waterAmount = waterAmount - 30;
beansAmount = beansAmount - 10;
// @ts-ignore
resolve();
}, 2000);
}
if (coffeeType === "espresso") {
console.log("here");
if (waterAmount <= 10 || beansAmount <= 10) {
console.log("here4");
throw new Error("Not enough water or beans");
} else {
reject(new Error("Wrong coffee input"));
console.log("here2");
setTimeout(() => {
console.log("here3");
waterAmount = waterAmount - 10;
beansAmount = beansAmount - 10;
return undefined;
}, 1000);
}
});
} else if (coffeeType === "cappuccino") {
if (waterAmount <= 20 || beansAmount <= 25 || milkAmount <= 15) {
throw new Error("Not enough water or beans");
} else {
setTimeout(() => {
waterAmount = waterAmount - 15;
beansAmount = beansAmount - 20;
milkAmount = milkAmount - 10;
return undefined;
}, 2000);
}
} else if (coffeeType === "americano") {
if (waterAmount <= 35 || beansAmount <= 10) {
throw new Error("Not enough water or beans");
} else {
setTimeout(() => {
waterAmount = waterAmount - 30;
beansAmount = beansAmount - 10;
return undefined;
}, 2000);
}
} else {
throw new Error("Wrong coffee input");
}
return undefined;

This comment has been minimized.

Copy link
@danielpeintner

danielpeintner Aug 31, 2023

Author Member

I think this is what causes to return "early"

});

// expose the thing
Expand Down

0 comments on commit 511602d

Please sign in to comment.