Skip to content

Commit

Permalink
Refactor Effect Action Code
Browse files Browse the repository at this point in the history
  • Loading branch information
timoclsn committed Aug 21, 2024
1 parent b1ecaea commit ca0ef04
Showing 1 changed file with 26 additions and 32 deletions.
58 changes: 26 additions & 32 deletions src/data/smarthome/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export const turnOnBalcony = createAction({
input: zfd.formData({
color: zfd.text(colorSchema),
}),
action: async ({ input }) => {
const action = Effect.gen(function* () {
action: async ({ input }) =>
Effect.gen(function* () {
const { color } = input;
yield* Effect.logInfo(`Trying to turn on balcony light ${color}`);

Expand All @@ -38,36 +38,30 @@ export const turnOnBalcony = createAction({
yield* Effect.sleep("2 seconds");
yield* incrementBalconyCounter(color);

yield* Effect.logInfo(`Balcony light turned on ${color}`);

revalidateTag("control-count");
});

await Effect.runPromise(
action.pipe(
Effect.provide(DevToolsLive),
Effect.provide(NodeSdkLive),
Effect.provide(HomeeService.Live),
Effect.provide(DatabaseService.Live),
Effect.mapError((error) => {
let message = "";

switch (error._tag) {
case "PlayHomeegramError":
message = "Balkonlampe konnte nicht eingeschaltet werden.";
break;
case "IncrementBalconyCounterError":
message = `Der ${error.color} Zähler konnte nicht erhöht werden.`;
break;
default:
message = "Unbekannter Fehler";
break;
}

return new ActionError({ message, cause: error.cause });
}),
Effect.orDie,
),
);
},
yield* Effect.logInfo(`Balcony light turned on ${color}`);
}).pipe(
Effect.provide(DevToolsLive),
Effect.provide(NodeSdkLive),
Effect.provide(HomeeService.Live),
Effect.provide(DatabaseService.Live),
Effect.mapError((error) => {
let message = "";
switch (error._tag) {
case "PlayHomeegramError":
message = "Balkonlampe konnte nicht eingeschaltet werden.";
break;
case "IncrementBalconyCounterError":
message = `Der ${error.color} Zähler konnte nicht erhöht werden.`;
break;
default:
message = "Unbekannter Fehler";
break;
}
return new ActionError({ message, cause: error.cause });
}),
Effect.orDie,
Effect.runPromise,
),
});

0 comments on commit ca0ef04

Please sign in to comment.