Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

1.3 KB
Raw Blame
import { Duration, Effect } from "effect";
import { deleteLabelStep, type LabelDeletion } from "../api/labels.js";
import type { RateLimited } from "../errors/errors.js";
import { type DeletionOutcome, nextDeletionStep } from "./deletion-step.js";

type Attempt =
  | {
      readonly outcome: DeletionOutcome;
      readonly deletion: LabelDeletion;
    }
  | { readonly outcome: DeletionOutcome; readonly busy: RateLimited };

export const deleteLabelFully = Effect.fn("labels.deleteFully")(function* (
  id: string,
) {
  let waited = 0;
  for (;;) {
    const attempt: Attempt = yield* deleteLabelStep(id).pipe(
      Effect.map((deletion): Attempt => ({
        outcome: { kind: deletion.pendingDeletion ? "pending" : "deleted" },
        deletion,
      })),
      Effect.catchTag("RateLimited", (busy) =>
        Effect.succeed<Attempt>({
          outcome: { kind: "busy", retryAfterSeconds: busy.retryAfterSeconds },
          busy,
        }),
      ),
    );
    const step = nextDeletionStep(attempt.outcome, waited);
    if (step.kind === "done" && "deletion" in attempt) return attempt.deletion;
    if (step.kind === "give-up" && "busy" in attempt) {
      return yield* Effect.fail(attempt.busy);
    }
    if (step.kind === "wait") {
      waited += step.seconds;
      yield* Effect.sleep(Duration.seconds(step.seconds));
    }
  }
});