@opentui/keymap 0.7.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.js CHANGED
@@ -1,4 +1,7 @@
1
1
  // @bun
2
+ import {
3
+ KEYMAP_EXTENSION_CONTEXT2
4
+ } from "../chunks/index-pjh5rp2p.js";
2
5
  import {
3
6
  normalizeBindingTokenName,
4
7
  cloneKeyStroke,
@@ -17,9 +20,8 @@ import {
17
20
  activeOptionsForCaptures,
18
21
  capturePriority,
19
22
  advanceSequenceCapture,
20
- collectRootSequenceCaptures,
21
- KEYMAP_EXTENSION_CONTEXT2
22
- } from "../chunks/index-7g381cth.js";
23
+ collectRootSequenceCaptures
24
+ } from "../chunks/index-0a7ve4a3.js";
23
25
 
24
26
  // src/lib/runtime-utils.ts
25
27
  function createRuntimeEmitter(onError) {
@@ -964,41 +966,118 @@ function getActiveCommandView(state, host, conditions, focused) {
964
966
  return collectActiveCommands(state.activeLayersCache, conditions, true);
965
967
  }
966
968
 
967
- // src/services/primitives/field-invariants.ts
968
- function mergeRequirement(target, name, value, source) {
969
- if (Object.prototype.hasOwnProperty.call(target, name) && !Object.is(target[name], value)) {
970
- throw new Error(`Conflicting keymap requirement for "${name}" from ${source}`);
969
+ // src/services/values.ts
970
+ function isPlainObject(value) {
971
+ const prototype = Object.getPrototypeOf(value);
972
+ return prototype === Object.prototype || prototype === null;
973
+ }
974
+ var DEPTH_MAX = 16;
975
+ var COMPILED_DEPTH_MAX = 32;
976
+ var PLAIN_FUNCTION_KEYS = new Set(["length", "name", "prototype"]);
977
+ function isPlainData(value, identityKey) {
978
+ try {
979
+ return checkPlainData(value, identityKey, 0);
980
+ } catch {
981
+ return false;
971
982
  }
972
- target[name] = value;
973
983
  }
974
- function mergeAttribute(target, name, value, source) {
975
- if (Object.prototype.hasOwnProperty.call(target, name) && !Object.is(target[name], value)) {
976
- throw new Error(`Conflicting keymap attribute for "${name}" from ${source}`);
984
+ function checkPlainData(value, identityKey, depth) {
985
+ if (typeof value === "function")
986
+ return isPlainFunction(value);
987
+ if (typeof value !== "object" || value === null)
988
+ return true;
989
+ const isArray = Array.isArray(value);
990
+ const plain = isArray ? Object.getPrototypeOf(value) === Array.prototype : isPlainObject(value);
991
+ if (!plain || !Object.isExtensible(value) || depth >= DEPTH_MAX)
992
+ return false;
993
+ return Reflect.ownKeys(value).every((key) => {
994
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
995
+ if (typeof key !== "string" || key === "__proto__" || !("value" in descriptor))
996
+ return false;
997
+ if (!descriptor.enumerable && !(isArray && key === "length"))
998
+ return false;
999
+ return depth === 0 && key === identityKey || checkPlainData(Reflect.get(value, key), undefined, depth + 1);
1000
+ });
1001
+ }
1002
+ var forwardTargets = new WeakMap;
1003
+ function createForwarder(target) {
1004
+ const cell = { target };
1005
+ const forward = function(...args) {
1006
+ return Reflect.apply(cell.target, this, args);
1007
+ };
1008
+ forwardTargets.set(forward, cell);
1009
+ return forward;
1010
+ }
1011
+ var external;
1012
+ function collectExternal(objects, compile) {
1013
+ const previous = external;
1014
+ external = objects;
1015
+ try {
1016
+ return compile();
1017
+ } finally {
1018
+ external = previous;
977
1019
  }
978
- target[name] = value;
979
1020
  }
980
- function createFieldCompilerContext(options) {
981
- const source = `field ${options.fieldName}`;
982
- return {
983
- require(name, value) {
984
- mergeRequirement(options.requirements, name, value, source);
985
- },
986
- attr(name, value) {
987
- if (!options.attrs) {
988
- throw new Error(`Keymap ${source} cannot publish attrs`);
989
- }
990
- mergeAttribute(options.attrs, name, value, source);
991
- },
992
- activeWhen(matcher) {
993
- options.matchers.push(options.conditions.buildRuntimeMatcher(matcher, source));
1021
+ function markSupplied(value) {
1022
+ if (typeof value === "function" || typeof value === "object" && value !== null)
1023
+ external?.add(value);
1024
+ }
1025
+ function markExternal(value, depth = 0) {
1026
+ if (!external || typeof value !== "object" || value === null || external.has(value))
1027
+ return;
1028
+ external.add(value);
1029
+ try {
1030
+ if (depth >= DEPTH_MAX || !(Array.isArray(value) || isPlainObject(value)))
1031
+ return;
1032
+ for (const key of Reflect.ownKeys(value)) {
1033
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
1034
+ if (descriptor && "value" in descriptor)
1035
+ markExternal(descriptor.value, depth + 1);
994
1036
  }
995
- };
1037
+ } catch {}
996
1038
  }
997
-
998
- // src/services/values.ts
999
- function isPlainObject(value) {
1000
- const prototype = Object.getPrototypeOf(value);
1001
- return prototype === Object.prototype || prototype === null;
1039
+ function sameCompiledData(left, right, external, moves, depth = 0) {
1040
+ if (Object.is(left, right))
1041
+ return true;
1042
+ if (external.has(left) || external.has(right))
1043
+ return false;
1044
+ if (typeof left === "function")
1045
+ return typeof right === "function" && isPlainFunction(left) && isPlainFunction(right);
1046
+ if (typeof left !== "object" || left === null || typeof right !== "object" || right === null)
1047
+ return false;
1048
+ if (!isContainer(left) || !isContainer(right) || Array.isArray(left) !== Array.isArray(right))
1049
+ return false;
1050
+ if (depth >= COMPILED_DEPTH_MAX)
1051
+ return false;
1052
+ const keys = Reflect.ownKeys(left);
1053
+ return keys.length === Reflect.ownKeys(right).length && keys.every((key) => {
1054
+ const before = Object.getOwnPropertyDescriptor(left, key);
1055
+ const after = Object.getOwnPropertyDescriptor(right, key);
1056
+ if (typeof key !== "string" || !before || !after || !("value" in before) || !("value" in after))
1057
+ return false;
1058
+ const enumerable = before.enumerable || Array.isArray(left) && key === "length";
1059
+ if (!enumerable || after.enumerable !== before.enumerable)
1060
+ return false;
1061
+ if (!sameCompiledData(before.value, after.value, external, moves, depth + 1))
1062
+ return false;
1063
+ if (typeof before.value !== "function" || before.value === after.value)
1064
+ return true;
1065
+ const cell = forwardTargets.get(before.value);
1066
+ const nextCell = forwardTargets.get(after.value);
1067
+ if (cell && nextCell)
1068
+ moves.push([cell, "target", nextCell.target]);
1069
+ else if (!cell && Object.isExtensible(left))
1070
+ moves.push([left, key, after.value]);
1071
+ else
1072
+ return false;
1073
+ return true;
1074
+ });
1075
+ }
1076
+ function isPlainFunction(value) {
1077
+ return Reflect.ownKeys(value).every((key) => PLAIN_FUNCTION_KEYS.has(key));
1078
+ }
1079
+ function isContainer(value) {
1080
+ return typeof value === "object" && value !== null && (Array.isArray(value) || isPlainObject(value));
1002
1081
  }
1003
1082
  function getErrorMessage(error, fallback) {
1004
1083
  if (error instanceof Error && error.message) {
@@ -1036,6 +1115,39 @@ function snapshotDataValue(value, options) {
1036
1115
  return value;
1037
1116
  }
1038
1117
 
1118
+ // src/services/primitives/field-invariants.ts
1119
+ function mergeRequirement(target, name, value, source) {
1120
+ if (Object.prototype.hasOwnProperty.call(target, name) && !Object.is(target[name], value)) {
1121
+ throw new Error(`Conflicting keymap requirement for "${name}" from ${source}`);
1122
+ }
1123
+ target[name] = value;
1124
+ }
1125
+ function mergeAttribute(target, name, value, source) {
1126
+ if (Object.prototype.hasOwnProperty.call(target, name) && !Object.is(target[name], value)) {
1127
+ throw new Error(`Conflicting keymap attribute for "${name}" from ${source}`);
1128
+ }
1129
+ target[name] = value;
1130
+ }
1131
+ function createFieldCompilerContext(options) {
1132
+ const source = `field ${options.fieldName}`;
1133
+ return {
1134
+ require(name, value) {
1135
+ markSupplied(value);
1136
+ mergeRequirement(options.requirements, name, value, source);
1137
+ },
1138
+ attr(name, value) {
1139
+ if (!options.attrs) {
1140
+ throw new Error(`Keymap ${source} cannot publish attrs`);
1141
+ }
1142
+ markSupplied(value);
1143
+ mergeAttribute(options.attrs, name, value, source);
1144
+ },
1145
+ activeWhen(matcher) {
1146
+ options.matchers.push(options.conditions.buildRuntimeMatcher(matcher, source));
1147
+ }
1148
+ };
1149
+ }
1150
+
1039
1151
  // src/services/command-catalog.ts
1040
1152
  var DEFAULT_COMMAND_SEARCH_FIELDS = ["name"];
1041
1153
  var EMPTY_COMMAND_FIELDS = Object.freeze({});
@@ -1053,9 +1165,10 @@ function createCommandCatalogService(state, host, notify, conditions, options) {
1053
1165
  let registeredBindingsByCommand;
1054
1166
  let registeredResolvedCacheVersion = -1;
1055
1167
  let registeredResolvedCache = new Map;
1056
- const normalizeLayerCommands = (commands) => {
1168
+ const normalizeLayerCommands = (commands, owned) => {
1057
1169
  return normalizeCommands({
1058
1170
  commands,
1171
+ owned,
1059
1172
  commandFields: state.commandFields,
1060
1173
  conditions,
1061
1174
  onError: (code, error, message) => {
@@ -1570,6 +1683,14 @@ function createCommandCatalogService(state, host, notify, conditions, options) {
1570
1683
  function getCommand(state) {
1571
1684
  return state.command;
1572
1685
  }
1686
+ function ownCommand(command, name) {
1687
+ const own = {};
1688
+ for (const [key, value] of Object.entries(command)) {
1689
+ own[key] = typeof value === "function" ? createForwarder(value) : value;
1690
+ }
1691
+ own.name = name;
1692
+ return own;
1693
+ }
1573
1694
  function normalizeCommands(options) {
1574
1695
  const normalizedCommands = [];
1575
1696
  const seen = new Set;
@@ -1584,7 +1705,10 @@ function normalizeCommands(options) {
1584
1705
  options.onError("duplicate-command", { command: normalizedName }, `Duplicate keymap command "${normalizedName}" in the same layer`);
1585
1706
  continue;
1586
1707
  }
1587
- command.name = normalizedName;
1708
+ const owned = options.owned && isPlainData(command);
1709
+ if (!owned)
1710
+ command.name = normalizedName;
1711
+ const exposed = owned ? ownCommand(command, normalizedName) : command;
1588
1712
  for (const [fieldName, value] of Object.entries(fields)) {
1589
1713
  if (value === undefined) {
1590
1714
  continue;
@@ -1602,8 +1726,8 @@ function normalizeCommands(options) {
1602
1726
  }));
1603
1727
  }
1604
1728
  const commandState = {
1605
- command,
1606
- fields,
1729
+ command: exposed,
1730
+ fields: owned ? getCommandFields(exposed) : fields,
1607
1731
  attrs: Object.keys(attrs).length === 0 ? undefined : attrs,
1608
1732
  requires: Object.entries(mergedRequires),
1609
1733
  matchers
@@ -2420,6 +2544,13 @@ function isReactiveMatcher(value) {
2420
2544
  return typeof candidate.get === "function" && typeof candidate.subscribe === "function";
2421
2545
  }
2422
2546
  function createConditionService(state, notify) {
2547
+ const seen = new WeakMap;
2548
+ let epoch = 0;
2549
+ notify.onStateEvent(() => void (epoch += 1));
2550
+ const seenResults = (target) => {
2551
+ const entry = seen.get(target) ?? 0;
2552
+ return entry - entry % 4 === epoch * 4 ? entry % 4 : 0;
2553
+ };
2423
2554
  const hasNoConditions = (target) => {
2424
2555
  return target.requires.length === 0 && target.matchers.length === 0;
2425
2556
  };
@@ -2458,7 +2589,12 @@ function createConditionService(state, notify) {
2458
2589
  return true;
2459
2590
  };
2460
2591
  const matchesConditions = (target) => {
2461
- return hasNoConditions(target) || matchRequirements(target.requires) && matchesRuntimeMatchers(target);
2592
+ if (hasNoConditions(target))
2593
+ return true;
2594
+ const result = matchRequirements(target.requires) && matchesRuntimeMatchers(target);
2595
+ if (target.matchers.length > 0)
2596
+ seen.set(target, epoch * 4 + (seenResults(target) | (result ? 2 : 1)));
2597
+ return result;
2462
2598
  };
2463
2599
  return {
2464
2600
  buildRuntimeMatcher(matcher, source) {
@@ -2474,7 +2610,8 @@ function createConditionService(state, notify) {
2474
2610
  }
2475
2611
  throw new Error(`Keymap ${source} expected a function or a reactive matcher`);
2476
2612
  },
2477
- matchesConditions
2613
+ matchesConditions,
2614
+ seenResults
2478
2615
  };
2479
2616
  }
2480
2617
 
@@ -3628,6 +3765,14 @@ function compareLayers(left, right) {
3628
3765
  const priorityDiff = right.priority - left.priority;
3629
3766
  return priorityDiff || right.order - left.order;
3630
3767
  }
3768
+ function compiledData(layer) {
3769
+ const { target: _target, order: _order, root: _root, offTargetDestroy: _off, ...data } = layer;
3770
+ const { activeKeyCacheBlocked: _keys, activeCommandViewCacheBlocked: _commands, ...compiled } = data;
3771
+ return compiled;
3772
+ }
3773
+ function conditionTargets(layer) {
3774
+ return [layer, ...layer.commands, ...layer.bindings];
3775
+ }
3631
3776
  function layerBlocksActiveKeyCache(layer) {
3632
3777
  if (layer.matchers.length > 0)
3633
3778
  return true;
@@ -3650,85 +3795,152 @@ function layerBlocksActiveCommandViewCache(layer) {
3650
3795
  return false;
3651
3796
  }
3652
3797
  function createLayerService(state, notify, conditions, activation, options) {
3798
+ const plainLayers = new WeakSet;
3799
+ const forwardBindingCommands = (bindings) => {
3800
+ for (const binding of bindings) {
3801
+ if (typeof binding.command !== "function")
3802
+ continue;
3803
+ binding.command = createForwarder(binding.command);
3804
+ if (binding.run)
3805
+ binding.run = binding.command;
3806
+ }
3807
+ };
3653
3808
  const registerLayer = (layer) => {
3654
3809
  return notify.runWithStateChangeBatch(() => {
3655
- const target = layer.target;
3656
- if (target && options.host.isTargetDestroyed(target)) {
3657
- notify.emitError("destroyed-layer-target", { target }, "Cannot register a keymap layer for a destroyed keymap target");
3658
- return NOOP2;
3659
- }
3660
- let sourceBindings;
3661
- let requires;
3662
- let matchers;
3663
- let fields;
3664
- let attrs;
3665
- let commands;
3666
- let targetMode;
3667
- try {
3668
- targetMode = normalizeTargetMode(layer);
3669
- sourceBindings = applyLayerBindingsTransformers(snapshotBindings(layer.bindings ?? []), layer);
3670
- const sourceCommands = applyCommandTransformers(layer.commands ?? [], layer);
3671
- commands = sourceCommands.length === 0 ? [] : options.commands.normalizeCommands(sourceCommands);
3672
- ({ requires, matchers, fields, attrs } = compileLayerRuntimeState(layer));
3673
- } catch (error) {
3674
- notify.emitError("register-layer-failed", error, getErrorMessage(error, "Failed to register keymap layer"));
3675
- return NOOP2;
3676
- }
3677
- const order = state.order++;
3678
- const bindingStates = options.compiler.compileBindings(sourceBindings, state.tokens, target, order, fields);
3679
- if (bindingStates.bindings.length === 0 && !bindingStates.hasTokenBindings && commands.length === 0) {
3810
+ const registeredLayer = compileLayer(layer, false);
3811
+ if (!registeredLayer)
3680
3812
  return NOOP2;
3681
- }
3682
- options.diagnostics?.analyzeLayer({
3683
- target,
3684
- order,
3685
- commands,
3686
- sourceBindings,
3687
- bindings: bindingStates.bindings,
3688
- hasTokenBindings: bindingStates.hasTokenBindings
3813
+ addLayer(registeredLayer);
3814
+ return () => unregisterLayer(registeredLayer);
3815
+ });
3816
+ };
3817
+ const replaceLayer = (slot, layer) => {
3818
+ return notify.runWithStateChangeBatch(() => {
3819
+ const kept = slot.layer && state.layers.has(slot.layer) ? slot.layer : undefined;
3820
+ const plain = layer !== undefined && isPlainData(layer, "target");
3821
+ const external = new WeakSet;
3822
+ const next = collectExternal(external, () => {
3823
+ markExternal(layer);
3824
+ return layer && compileLayer(layer, plain, slot.order);
3689
3825
  });
3690
- const registeredLayer = {
3691
- order,
3692
- target,
3693
- targetMode,
3694
- priority: layer.priority ?? 0,
3695
- requires,
3696
- matchers,
3697
- fields,
3698
- attrs,
3699
- commands,
3700
- sourceBindings,
3701
- bindings: bindingStates.bindings,
3702
- root: buildSequenceTree(bindingStates.bindings, state.patterns),
3703
- hasTokenBindings: bindingStates.hasTokenBindings,
3704
- activeKeyCacheBlocked: false,
3705
- activeCommandViewCacheBlocked: false
3706
- };
3707
- updateCacheBlockers(registeredLayer);
3708
- state.layers.add(registeredLayer);
3709
- state.sortedLayers = [...state.sortedLayers, registeredLayer].sort(compareLayers);
3710
- attachReactiveMatchers(registeredLayer);
3711
- for (const command of registeredLayer.commands) {
3712
- attachReactiveMatchers(command);
3713
- }
3714
- for (const binding of registeredLayer.bindings) {
3715
- attachReactiveMatchers(binding);
3716
- }
3717
- if (target) {
3718
- const onTargetDestroy = () => {
3719
- unregisterLayer(registeredLayer);
3720
- };
3721
- registeredLayer.offTargetDestroy = options.host.onTargetDestroy(target, onTargetDestroy);
3722
- }
3723
- if (registeredLayer.commands.length > 0) {
3724
- activation.ensureValidPendingSequence();
3725
- }
3726
- notify.queueStateChange();
3727
- return () => {
3728
- unregisterLayer(registeredLayer);
3729
- };
3826
+ const moves = kept && next && plain ? keptMoves(kept, next, external) : undefined;
3827
+ if (kept && moves) {
3828
+ refreshLayer(kept, moves);
3829
+ return kept;
3830
+ }
3831
+ if (slot.layer)
3832
+ unregisterLayer(slot.layer);
3833
+ slot.layer = next;
3834
+ if (!next)
3835
+ return;
3836
+ if (plain)
3837
+ plainLayers.add(next);
3838
+ addLayer(next);
3839
+ return next;
3730
3840
  });
3731
3841
  };
3842
+ const keptMoves = (kept, next, external) => {
3843
+ if (!plainLayers.has(kept) || kept.target !== next.target)
3844
+ return;
3845
+ if (conditionTargets(next).some((target) => target.matchers.some((matcher) => matcher.subscribe)))
3846
+ return;
3847
+ const moves = [];
3848
+ try {
3849
+ return sameCompiledData(compiledData(kept), compiledData(next), external, moves) ? moves : undefined;
3850
+ } catch {
3851
+ return;
3852
+ }
3853
+ };
3854
+ const compileLayer = (layer, ownCommands, slotOrder) => {
3855
+ const target = layer.target;
3856
+ if (target && options.host.isTargetDestroyed(target)) {
3857
+ notify.emitError("destroyed-layer-target", { target }, "Cannot register a keymap layer for a destroyed keymap target");
3858
+ return;
3859
+ }
3860
+ let sourceBindings;
3861
+ let requires;
3862
+ let matchers;
3863
+ let fields;
3864
+ let attrs;
3865
+ let commands;
3866
+ let targetMode;
3867
+ try {
3868
+ targetMode = normalizeTargetMode(layer);
3869
+ sourceBindings = applyLayerBindingsTransformers(snapshotBindings(layer.bindings ?? []), layer);
3870
+ const sourceCommands = applyCommandTransformers(layer.commands ?? [], layer);
3871
+ commands = sourceCommands.length === 0 ? [] : options.commands.normalizeCommands(sourceCommands, ownCommands);
3872
+ ({ requires, matchers, fields, attrs } = compileLayerRuntimeState(layer, ownCommands));
3873
+ } catch (error) {
3874
+ notify.emitError("register-layer-failed", error, getErrorMessage(error, "Failed to register keymap layer"));
3875
+ return;
3876
+ }
3877
+ const order = slotOrder ?? state.order++;
3878
+ const bindingStates = options.compiler.compileBindings(sourceBindings, state.tokens, target, order, fields);
3879
+ if (ownCommands)
3880
+ forwardBindingCommands(bindingStates.bindings);
3881
+ if (bindingStates.bindings.length === 0 && !bindingStates.hasTokenBindings && commands.length === 0) {
3882
+ return;
3883
+ }
3884
+ return {
3885
+ order,
3886
+ target,
3887
+ targetMode,
3888
+ priority: layer.priority ?? 0,
3889
+ requires,
3890
+ matchers,
3891
+ fields,
3892
+ attrs,
3893
+ commands,
3894
+ sourceBindings,
3895
+ bindings: bindingStates.bindings,
3896
+ root: buildSequenceTree(bindingStates.bindings, state.patterns),
3897
+ hasTokenBindings: bindingStates.hasTokenBindings,
3898
+ activeKeyCacheBlocked: false,
3899
+ activeCommandViewCacheBlocked: false
3900
+ };
3901
+ };
3902
+ const addLayer = (registeredLayer) => {
3903
+ options.diagnostics?.analyzeLayer(registeredLayer);
3904
+ updateCacheBlockers(registeredLayer);
3905
+ state.layers.add(registeredLayer);
3906
+ if (registeredLayer.order < state.order - 1) {
3907
+ state.layers = new Set([...state.layers].sort((left, right) => left.order - right.order));
3908
+ }
3909
+ state.sortedLayers = [...state.sortedLayers, registeredLayer].sort(compareLayers);
3910
+ attachReactiveMatchers(registeredLayer);
3911
+ for (const command of registeredLayer.commands) {
3912
+ attachReactiveMatchers(command);
3913
+ }
3914
+ for (const binding of registeredLayer.bindings) {
3915
+ attachReactiveMatchers(binding);
3916
+ }
3917
+ const target = registeredLayer.target;
3918
+ if (target) {
3919
+ registeredLayer.offTargetDestroy = options.host.onTargetDestroy(target, () => {
3920
+ unregisterLayer(registeredLayer);
3921
+ });
3922
+ }
3923
+ if (registeredLayer.commands.length > 0) {
3924
+ activation.ensureValidPendingSequence();
3925
+ }
3926
+ notify.queueStateChange();
3927
+ };
3928
+ const readResults = (layer, targets) => {
3929
+ const enabled = targets.length > 0 && conditions.matchesConditions(layer);
3930
+ return targets.map((target) => target === layer ? enabled ? 2 : 1 : enabled ? conditions.matchesConditions(target) ? 2 : 1 : 0);
3931
+ };
3932
+ const refreshLayer = (layer, moves) => {
3933
+ const targets = conditionTargets(layer).filter((target) => target.matchers.length > 0);
3934
+ const seen = targets.map((target) => conditions.seenResults(target));
3935
+ const before = readResults(layer, targets);
3936
+ for (const [container, key, value] of moves)
3937
+ container[key] = value;
3938
+ const after = readResults(layer, targets);
3939
+ if (!options.host.isDestroyed)
3940
+ activation.ensureValidPendingSequence();
3941
+ if (after.some((result, index) => ((seen[index] | before[index]) & ~result) !== 0))
3942
+ notify.queueStateChange();
3943
+ };
3732
3944
  const applyTokenState = (nextTokens) => {
3733
3945
  notify.runWithStateChangeBatch(() => {
3734
3946
  const nextCompilations = new Map;
@@ -3845,9 +4057,11 @@ function createLayerService(state, notify, conditions, activation, options) {
3845
4057
  }
3846
4058
  transformedCommands.push(...extraCommands);
3847
4059
  }
4060
+ for (const command of transformedCommands)
4061
+ Object.values(command).forEach((value) => markExternal(value));
3848
4062
  return transformedCommands;
3849
4063
  };
3850
- const compileLayerRuntimeState = (layer) => {
4064
+ const compileLayerRuntimeState = (layer, forward) => {
3851
4065
  const mergedRequires = {};
3852
4066
  const matchers = [];
3853
4067
  const fields = Object.create(null);
@@ -3859,7 +4073,7 @@ function createLayerService(state, notify, conditions, activation, options) {
3859
4073
  if (value === undefined) {
3860
4074
  continue;
3861
4075
  }
3862
- fields[fieldName] = snapshotDataValue(value);
4076
+ fields[fieldName] = forward && typeof value === "function" ? createForwarder(value) : snapshotDataValue(value);
3863
4077
  const compiler = state.layerFields.get(fieldName);
3864
4078
  if (!compiler) {
3865
4079
  options.warnUnknownField("layer", fieldName);
@@ -3897,6 +4111,8 @@ function createLayerService(state, notify, conditions, activation, options) {
3897
4111
  detachReactiveMatchers(binding);
3898
4112
  }
3899
4113
  layer.bindings = compilation.bindings;
4114
+ if (plainLayers.has(layer))
4115
+ forwardBindingCommands(layer.bindings);
3900
4116
  layer.root = buildSequenceTree(compilation.bindings, state.patterns);
3901
4117
  layer.hasTokenBindings = compilation.hasTokenBindings;
3902
4118
  updateCacheBlockers(layer);
@@ -3983,7 +4199,8 @@ function createLayerService(state, notify, conditions, activation, options) {
3983
4199
  matcher.dispose = undefined;
3984
4200
  }
3985
4201
  };
3986
- return { registerLayer, applyTokenState, recompileBindings, cleanup };
4202
+ const createSlot = () => ({ order: state.order++ });
4203
+ return { registerLayer, createSlot, replaceLayer, applyTokenState, recompileBindings, cleanup };
3987
4204
  }
3988
4205
 
3989
4206
  // src/services/notify.ts
@@ -4013,6 +4230,7 @@ function createNotificationService(state, events, hooks) {
4013
4230
  }
4014
4231
  events.emit("error", { code, message, error });
4015
4232
  };
4233
+ let beforeStateEvent;
4016
4234
  const flushStateChange = () => {
4017
4235
  if (!state.stateChangePending || state.stateChangeDepth > 0 || state.flushingStateChange) {
4018
4236
  return;
@@ -4028,6 +4246,7 @@ function createNotificationService(state, events, hooks) {
4028
4246
  }
4029
4247
  iterations += 1;
4030
4248
  state.stateChangePending = false;
4249
+ beforeStateEvent?.();
4031
4250
  hooks.emit("state");
4032
4251
  }
4033
4252
  } finally {
@@ -4059,6 +4278,9 @@ function createNotificationService(state, events, hooks) {
4059
4278
  flushStateChange();
4060
4279
  }
4061
4280
  },
4281
+ onStateEvent(listener) {
4282
+ beforeStateEvent = listener;
4283
+ },
4062
4284
  emitWarning,
4063
4285
  emitError,
4064
4286
  reportListenerError(name, error) {
@@ -4269,7 +4491,8 @@ class Keymap {
4269
4491
  host: this.#host,
4270
4492
  conditions: this.#conditions,
4271
4493
  catalog: this.#catalog,
4272
- activation: this.#activation
4494
+ activation: this.#activation,
4495
+ layers: this.#layers
4273
4496
  };
4274
4497
  }
4275
4498
  #cleanup() {
@@ -4502,5 +4725,5 @@ export {
4502
4725
  stringifyKeyStroke2 as stringifyKeyStroke
4503
4726
  };
4504
4727
 
4505
- //# debugId=A921C8E17706FE5864756E2164756E21
4728
+ //# debugId=E8FB5AFA99E6AEC564756E2164756E21
4506
4729
  //# sourceMappingURL=index.js.map