⚙️ MOTOKO RUNE DIFFICULTY: Apprentice Behavioral

The Command Hex

Encapsulating an arcane imperative as an object, allowing for queued rituals and reversals.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Command
ARCANUM SCHOOL: Enchantment // Actor Model Hexes
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// MOTOKO PATTERN //
module Command {
  public type Command = {
    execute : () -> async Text;
  };

  public class Familiar() {
    public func fetchSoul() : async Text { "Soul fetched." };
    public func sweepTower() : async Text { "Tower swept." };
  };

  public class FetchSoulCommand(familiar : Familiar) {
    public func execute() : async Text {
      await familiar.fetchSoul();
    };
  };

  public class SweepTowerCommand(familiar : Familiar) {
    public func execute() : async Text {
      await familiar.sweepTower();
    };
  };

  public actor Archmage {
    let minion = Familiar();
    var ritualQueue : [Command] = [];

    public func queueFetch() : async () {
      ritualQueue := Array.append(ritualQueue, [FetchSoulCommand(minion)]);
    };

    public func queueSweep() : async () {
      ritualQueue := Array.append(ritualQueue, [SweepTowerCommand(minion)]);
    };

    public func executeRituals() : async [Text] {
      var results : [Text] = [];
      for (cmd in ritualQueue.vals()) {
        let res = await cmd.execute();
        results := Array.append(results, [res]);
      };
      ritualQueue := [];
      results;
    };
  };
}

The Command Hex turns a request into a stand-alone object containing all information about the imperative. The Archmage actor queues these incantations asynchronously without needing to immediately execute them, treating actions as data to be evaluated at the optimal astrological alignment.

Runic Tags: #motoko #behavioral #command #actor-model