Skip to content
Functioncorev0.14.0

defineEffectGate

Builds an EffectGate from a check and a commit. check peeks and refuses by throwing, and commit applies the side effect, running only once the whole gate set passes so a later refusal never commits. In an or, seedcord rolls back the commit a refusing gate queued. This is how Cooldown peeks in check and charges the slot in commit.

See also:defineGate, the @Gated decorator from your transport package, Cooldown

defineEffectGate<Name, Ctx>(
    name: Name,
    check: (ctx: Ctx) => Promisable<void>,
    commit: (ctx: Ctx) => Promisable<void>
): EffectGate<Ctx, Name>
// an effect-gate factory that takes its own limit and closes over it
function UsesPerDay(max: number): EffectGate<GateContextBase, 'UsesPerDay'> {
    return defineEffectGate(
        'UsesPerDay',
        (ctx) => {
            // peek and refuse by throwing. do not mutate yet
            if (usedToday(ctx.userId) >= max) throw new OutOfUsesNotice(max);
        },
        (ctx) => {
            // runs only after the whole gate set passes
            recordUse(ctx.userId);
        }
    );
}

Name

The gate's literal name, which a mismatch error shows.

Name

The gate's literal name, which a mismatch error shows.

Ctx

The context both check and commit read, inferred from the ctx annotation.

Ctx

The context both check and commit read, inferred from the ctx annotation.

name

name: Name

The gate's name, used in mismatch errors.

check

check: (ctx: Ctx) => Promisable<void>

Peeks and refuses by throwing, without applying the side effect.

commit

commit: (ctx: Ctx) => Promisable<void>

Applies the side effect, running only after the whole gate set passes.