@witchcraft/spellcraft
    Preparing search index...

    Function setManagerProp

    • Safely sets a settable manager property.

      You should not use this to set properties the manager manages (those tagged with

      Type Parameters

      • TEntries extends ManagerSetEntries
      • TProp extends keyof ManagerSetEntries
      • TEntry extends
            | {
                error: "UNKNOWN_COMMAND"
                | ChainError;
                hooks: GetManagerHooks<"shortcuts">;
                manager: Manager;
                val: Shortcuts;
            }
            | {
                error: "UNKNOWN_COMMAND";
                hooks: GetManagerHooks<"commands">;
                manager: Manager;
                val: Commands;
            }
            | {
                error: ChainError;
                hooks: GetManagerHooks<"keys">;
                manager: Manager;
                val: Keys;
            }
            | {
                error: "UNKNOWN_KEY";
                hooks: GetManagerHooks<"state.chain">;
                manager: Manager;
                val: string[][];
            }
            | ManagerStateHook<"isRecording">
            | ManagerStateHook<"isAwaitingKeyup">
            | ManagerStateHook<"nextIsChord">
            | ManagerStateHook<"untrigger">
      • THooks extends Partial<Hooks>
      • TCheck extends boolean | "only" = true

      Parameters

      • manager: TEntry["manager"] & { hooks?: THooks }

        Manager is mutate if check is not "only"

      • prop: TProp
      • val: TEntry["val"]
      • __namedParameters: { check?: TCheck } = {}

      Returns Result<
          TCheck extends "only" ? true : Manager,

              | MultipleErrors<TEntry["error"]>
              | CanHookErrors<THooks, "canSetManagerProp">,
      >

      in the docs) unless you've forgone using the manager.

      If you need to set the chain, use safeSetManagerChain.

      If enabling/disabling Manager.state.isRecording it's suggested you use safeSetManagerChain just before.

      How to set multiple manager properties safely (i.e. batch replace shortcuts/commands/keys)

      This can be an issue because there isn't a way to tell the manager you want to replace multiple properties and it might be impossible to, for example, replace commands with a smaller subset but not have it error even if you're planning to replace the shortcuts so they don't contain missing commands.

      To achieve this, you can shallow clone the manager, change all the properties you want directly, then validate it's state by using isValidManager which is what this function does internally.

      Once you know it's valid, detach the old manager and attach the new one.

      const clone = {...manager, keys: newKeys, shortcuts: newShortcuts}

      if (isValidManager(manager)) {
      detach(manager, ...)
      attach(clone, ...)
      }