@svelte-lean/core 0.5.0 → 0.6.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # @svelte-lean/core
2
2
 
3
+ ## 0.6.0
4
+
5
+ ### Minor Changes
6
+
7
+ - dda6d05: Attribute behaviors (ADR 0009): a behavior registered with `attribute: 'data-slean-<name>'` gets
8
+ routed events for every element in the event path that carries the attribute, beside the
9
+ `data-slean="<name>"` roots, so it can sit on any element, including the root of another behavior.
10
+ Attribute roots are not skipped when disabled. Development validation now runs once per root and
11
+ behavior, so an element that is the root of two behaviors is validated by both.
12
+
3
13
  ## 0.5.0
4
14
 
5
15
  No changes in this release.
@@ -79,12 +79,17 @@ export function untrackRuntime(runtime) {
79
79
  if (DEV)
80
80
  live.delete(runtime);
81
81
  }
82
- let validated = new WeakSet();
82
+ // Per definition: one element can be the root of several behaviors (an attribute behavior on the
83
+ // root of another, ADR 0009), and each validates it once.
84
+ let validated = new WeakMap();
83
85
  /** @internal Runs `definition.validate` once per root and warns each message. */
84
86
  export function validateRoot(name, definition, root) {
85
- if (validated.has(root))
87
+ let roots = validated.get(definition);
88
+ if (!roots)
89
+ validated.set(definition, (roots = new WeakSet()));
90
+ if (roots.has(root))
86
91
  return;
87
- validated.add(root);
92
+ roots.add(root);
88
93
  const messages = definition.validate?.(root);
89
94
  if (messages)
90
95
  for (const message of messages)
@@ -92,6 +97,6 @@ export function validateRoot(name, definition, root) {
92
97
  }
93
98
  /** @internal Forgets validated roots so a replaced definition (HMR) is validated again. */
94
99
  export function resetValidation() {
95
- validated = new WeakSet();
100
+ validated = new WeakMap();
96
101
  }
97
102
  //# sourceMappingURL=diagnostics.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"diagnostics.js","sourceRoot":"","sources":["../src/diagnostics.ts"],"names":[],"mappings":"AACA,8FAA8F;AAC9F,gGAAgG;AAChG,OAAO,EAAE,GA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C,GAAG,IAAI,CAAC;YACzB,MAAM;SACN,CAAC;IACH,CAAC;AACF,CAAC;AAED,mGAAmG;AACnG,MAAM,UAAU,YAAY,CAAC,OAAgB;IAC5C,IAAI,GAAG;QAAE,OAAO,CAAC,OAAO,CAAC,CAAC;AAC3B,CAAC;AAED,gBAAgB;AAChB,MAAM,UAAU,cAAc,CAAC,OAAgB;IAC9C,IAAI,GAAG;QAAE,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;AAC/B,CAAC;AAED,IAAI,SAAS,GAAG,IAAI,OAAO,EAAe,CAAC;AAE3C,iFAAiF;AACjF,MAAM,UAAU,YAAY,CAC3B,IAAY,EACZ,UAA8B,EAC9B,IAAiB;IAEjB,IAAI,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC;QAAE,OAAO;IAChC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IACpB,MAAM,QAAQ,GAAG,UAAU,CAAC,QAAQ,EAAE,CAAC,IAAI,CAAC,CAAC;IAC7C,IAAI,QAAQ;QAAE,KAAK,MAAM,OAAO,IAAI,QAAQ;YAAE,IAAI,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;AACnE,CAAC;AAED,2FAA2F;AAC3F,MAAM,UAAU,eAAe;IAC9B,SAAS,GAAG,IAAI,OAAO,EAAE,CAAC;AAC3B,CAAC","sourcesContent":["import type { BehaviorDefinition, Runtime } from './types.js';\n// `#env` resolves through package.json `imports` to a literal `true`/`false` module under the\n// `development`/`production` conditions, so everything gated on DEV is dropped from production.\nimport { DEV } from '#env';\n\n// Shape rule for this file: a DEV gate is `if (DEV) helper(...)` with no declarations inside the\n// gate. A Vite (Rollup) production build removes the gates and the helpers they referenced;\n// scripts/size.mjs fails if a diagnostic survives that build. esbuild on its own folds the gates\n// but keeps referenced helpers, because it tree-shakes before it inlines cross-module constants,\n// and it drops only declaration-free dead blocks, hence the helpers.\n\n/** Prints a development warning as `[Svelte Lean: <behavior>] <message>`. No-op in production. */\nexport function warn(behavior: string, message: string): void {\n\tif (DEV) console.warn(`[Svelte Lean: ${behavior}] ${message}`);\n}\n\nconst controllers = new Map<string, number>();\n\nfunction decrement(behavior: string): void {\n\tconst next = (controllers.get(behavior) ?? 0) - 1;\n\tif (next > 0) controllers.set(behavior, next);\n\telse controllers.delete(behavior);\n}\n\n/**\n * Counter for Tier 2 scoped controllers. A behavior calls `add` when it creates an\n * `AbortController`-scoped session and `remove` when the session ends. Development only.\n */\nexport const scopedControllers = {\n\tadd(behavior: string): void {\n\t\tif (DEV) controllers.set(behavior, (controllers.get(behavior) ?? 0) + 1);\n\t},\n\tremove(behavior: string): void {\n\t\tif (DEV) decrement(behavior);\n\t},\n\tcount(behavior?: string): number {\n\t\tif (behavior) return controllers.get(behavior) ?? 0;\n\t\tlet total = 0;\n\t\tfor (const n of controllers.values()) total += n;\n\t\treturn total;\n\t}\n};\n\nexport interface SleanDebug {\n\truntimes(): Runtime[];\n\treport(): string;\n}\n\nconst live = new Set<Runtime>();\n\n/** Human-readable summary of live runtimes, their listeners and scoped controllers. */\nexport function report(): string {\n\tconst lines = ['Svelte Lean Runtime', '-------------------', `runtimes: ${live.size}`];\n\tlet i = 0;\n\tfor (const runtime of live) {\n\t\tconst installed = runtime.listeners();\n\t\tlines.push('', `[runtime ${++i}] root: ${runtime.root.nodeName}`, 'registered behaviors:');\n\t\tfor (const name of runtime.behaviors()) lines.push(` ${name}`);\n\t\tlines.push('shared listeners:');\n\t\tfor (const type of Object.keys(installed)) lines.push(` ${type}: ${installed[type]}`);\n\t}\n\tlines.push('', 'scoped controllers:');\n\tfor (const [name, n] of controllers) lines.push(` ${name}: ${n}`);\n\treturn lines.join('\\n');\n}\n\nfunction install(runtime: Runtime): void {\n\tlive.add(runtime);\n\tif (typeof window !== 'undefined') {\n\t\t(window as Window & { __SLEAN_DEBUG__?: SleanDebug }).__SLEAN_DEBUG__ ??= {\n\t\t\truntimes: () => [...live],\n\t\t\treport\n\t\t};\n\t}\n}\n\n/** @internal Called by `createRuntime`; installs `window.__SLEAN_DEBUG__` once, in development. */\nexport function trackRuntime(runtime: Runtime): void {\n\tif (DEV) install(runtime);\n}\n\n/** @internal */\nexport function untrackRuntime(runtime: Runtime): void {\n\tif (DEV) live.delete(runtime);\n}\n\nlet validated = new WeakSet<HTMLElement>();\n\n/** @internal Runs `definition.validate` once per root and warns each message. */\nexport function validateRoot(\n\tname: string,\n\tdefinition: BehaviorDefinition,\n\troot: HTMLElement\n): void {\n\tif (validated.has(root)) return;\n\tvalidated.add(root);\n\tconst messages = definition.validate?.(root);\n\tif (messages) for (const message of messages) warn(name, message);\n}\n\n/** @internal Forgets validated roots so a replaced definition (HMR) is validated again. */\nexport function resetValidation(): void {\n\tvalidated = new WeakSet();\n}\n"]}
1
+ {"version":3,"file":"diagnostics.js","sourceRoot":"","sources":["../src/diagnostics.ts"],"names":[],"mappings":"AACA,8FAA8F;AAC9F,gGAAgG;AAChG,OAAO,EAAE,GAAG,EAAE,MAAM,MAAM,CAAC;AAE3B,iGAAiG;AACjG,4FAA4F;AAC5F,iGAAiG;AACjG,iGAAiG;AACjG,qEAAqE;AAErE,kGAAkG;AAClG,MAAM,UAAU,IAAI,CAAC,QAAgB,EAAE,OAAe;IACrD,IAAI,GAAG;QAAE,OAAO,CAAC,IAAI,CAAC,iBAAiB,QAAQ,KAAK,OAAO,EAAE,CAAC,CAAC;AAChE,CAAC;AAED,MAAM,WAAW,GAAG,IAAI,GAAG,EAAkB,CAAC;AAE9C,SAAS,SAAS,CAAC,QAAgB;IAClC,MAAM,IAAI,GAAG,CAAC,WAAW,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC;IAClD,IAAI,IAAI,GAAG,CAAC;QAAE,WAAW,CAAC,GAAG,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;;QACzC,WAAW,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AACnC,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,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E,CAAC,CAAC,CAAC;IAC/D,IAAI,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC;QAAE,OAAO;IAC5B,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAChB,MAAM,QAAQ,GAAG,UAAU,CAAC,QAAQ,EAAE,CAAC,IAAI,CAAC,CAAC;IAC7C,IAAI,QAAQ;QAAE,KAAK,MAAM,OAAO,IAAI,QAAQ;YAAE,IAAI,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;AACnE,CAAC;AAED,2FAA2F;AAC3F,MAAM,UAAU,eAAe;IAC9B,SAAS,GAAG,IAAI,OAAO,EAAE,CAAC;AAC3B,CAAC","sourcesContent":["import type { BehaviorDefinition, Runtime } from './types.js';\n// `#env` resolves through package.json `imports` to a literal `true`/`false` module under the\n// `development`/`production` conditions, so everything gated on DEV is dropped from production.\nimport { DEV } from '#env';\n\n// Shape rule for this file: a DEV gate is `if (DEV) helper(...)` with no declarations inside the\n// gate. A Vite (Rollup) production build removes the gates and the helpers they referenced;\n// scripts/size.mjs fails if a diagnostic survives that build. esbuild on its own folds the gates\n// but keeps referenced helpers, because it tree-shakes before it inlines cross-module constants,\n// and it drops only declaration-free dead blocks, hence the helpers.\n\n/** Prints a development warning as `[Svelte Lean: <behavior>] <message>`. No-op in production. */\nexport function warn(behavior: string, message: string): void {\n\tif (DEV) console.warn(`[Svelte Lean: ${behavior}] ${message}`);\n}\n\nconst controllers = new Map<string, number>();\n\nfunction decrement(behavior: string): void {\n\tconst next = (controllers.get(behavior) ?? 0) - 1;\n\tif (next > 0) controllers.set(behavior, next);\n\telse controllers.delete(behavior);\n}\n\n/**\n * Counter for Tier 2 scoped controllers. A behavior calls `add` when it creates an\n * `AbortController`-scoped session and `remove` when the session ends. Development only.\n */\nexport const scopedControllers = {\n\tadd(behavior: string): void {\n\t\tif (DEV) controllers.set(behavior, (controllers.get(behavior) ?? 0) + 1);\n\t},\n\tremove(behavior: string): void {\n\t\tif (DEV) decrement(behavior);\n\t},\n\tcount(behavior?: string): number {\n\t\tif (behavior) return controllers.get(behavior) ?? 0;\n\t\tlet total = 0;\n\t\tfor (const n of controllers.values()) total += n;\n\t\treturn total;\n\t}\n};\n\nexport interface SleanDebug {\n\truntimes(): Runtime[];\n\treport(): string;\n}\n\nconst live = new Set<Runtime>();\n\n/** Human-readable summary of live runtimes, their listeners and scoped controllers. */\nexport function report(): string {\n\tconst lines = ['Svelte Lean Runtime', '-------------------', `runtimes: ${live.size}`];\n\tlet i = 0;\n\tfor (const runtime of live) {\n\t\tconst installed = runtime.listeners();\n\t\tlines.push('', `[runtime ${++i}] root: ${runtime.root.nodeName}`, 'registered behaviors:');\n\t\tfor (const name of runtime.behaviors()) lines.push(` ${name}`);\n\t\tlines.push('shared listeners:');\n\t\tfor (const type of Object.keys(installed)) lines.push(` ${type}: ${installed[type]}`);\n\t}\n\tlines.push('', 'scoped controllers:');\n\tfor (const [name, n] of controllers) lines.push(` ${name}: ${n}`);\n\treturn lines.join('\\n');\n}\n\nfunction install(runtime: Runtime): void {\n\tlive.add(runtime);\n\tif (typeof window !== 'undefined') {\n\t\t(window as Window & { __SLEAN_DEBUG__?: SleanDebug }).__SLEAN_DEBUG__ ??= {\n\t\t\truntimes: () => [...live],\n\t\t\treport\n\t\t};\n\t}\n}\n\n/** @internal Called by `createRuntime`; installs `window.__SLEAN_DEBUG__` once, in development. */\nexport function trackRuntime(runtime: Runtime): void {\n\tif (DEV) install(runtime);\n}\n\n/** @internal */\nexport function untrackRuntime(runtime: Runtime): void {\n\tif (DEV) live.delete(runtime);\n}\n\n// Per definition: one element can be the root of several behaviors (an attribute behavior on the\n// root of another, ADR 0009), and each validates it once.\nlet validated = new WeakMap<BehaviorDefinition, WeakSet<HTMLElement>>();\n\n/** @internal Runs `definition.validate` once per root and warns each message. */\nexport function validateRoot(\n\tname: string,\n\tdefinition: BehaviorDefinition,\n\troot: HTMLElement\n): void {\n\tlet roots = validated.get(definition);\n\tif (!roots) validated.set(definition, (roots = new WeakSet()));\n\tif (roots.has(root)) return;\n\troots.add(root);\n\tconst messages = definition.validate?.(root);\n\tif (messages) for (const message of messages) warn(name, message);\n}\n\n/** @internal Forgets validated roots so a replaced definition (HMR) is validated again. */\nexport function resetValidation(): void {\n\tvalidated = new WeakMap();\n}\n"]}
package/dist/runtime.js CHANGED
@@ -47,6 +47,9 @@ export function createRuntime(options = {}) {
47
47
  export function createRuntimeInternal(root, onDestroy) {
48
48
  const behaviors = new Map();
49
49
  const installed = new Set();
50
+ // Attribute behaviors (ADR 0009) as [name, definition]: few (usually none), checked on each
51
+ // path element.
52
+ let attributes = [];
50
53
  const route = (event) => {
51
54
  const type = event.type;
52
55
  const path = event.composedPath();
@@ -56,6 +59,19 @@ export function createRuntimeInternal(root, onDestroy) {
56
59
  const node = path[i];
57
60
  if (node.nodeType !== 1)
58
61
  continue;
62
+ for (let a = 0; a < attributes.length; a++) {
63
+ const [behavior, definition] = attributes[a];
64
+ const handler = definition.events?.[type];
65
+ if (!handler || !node.hasAttribute(definition.attribute))
66
+ continue;
67
+ // Not skipped when disabled: a hint explains a disabled control, and a behavior that
68
+ // must not act on one (copy) checks `isDisabled()` itself.
69
+ if (DEV)
70
+ validateRoot(behavior, definition, node);
71
+ handler({ root: node, target, event, runtime });
72
+ if (event.cancelBubble)
73
+ return;
74
+ }
59
75
  const name = node.getAttribute('data-slean');
60
76
  if (!name)
61
77
  continue;
@@ -73,6 +89,7 @@ export function createRuntimeInternal(root, onDestroy) {
73
89
  // Makes the installed set equal to the set of routed event types some registered behavior
74
90
  // handles. Unroutable types were reported once, at registration.
75
91
  const sync = () => {
92
+ attributes = [...behaviors].filter(([, definition]) => definition.attribute);
76
93
  const needed = new Set();
77
94
  for (const definition of behaviors.values()) {
78
95
  const events = definition.events;
@@ -1 +1 @@
1
- {"version":3,"file":"runtime.js","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,GAAG,EAAE,MAAM,MAAM,CAAC;AAC3B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C,IAAI,CAAC;gBAAE,IAAI,EAAE,CAAC;QACpC,CAAC;QACD,GAAG,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC;QAClC,SAAS,EAAE,GAAG,EAAE,CAAC,CAAC,GAAG,SAAS,CAAC,IAAI,EAAE,CAAC;QACtC,SAAS;YACR,MAAM,GAAG,GAA2B,EAAE,CAAC;YACvC,KAAK,MAAM,IAAI,IAAI,SAAS;gBAAE,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YAC5C,OAAO,GAAG,CAAC;QACZ,CAAC;QACD,OAAO;YACN,SAAS,CAAC,KAAK,EAAE,CAAC;YAClB,IAAI,EAAE,CAAC;YACP,cAAc,CAAC,OAAO,CAAC,CAAC;YACxB,SAAS,EAAE,EAAE,CAAC;QACf,CAAC;KACD,CAAC;IACF,YAAY,CAAC,OAAO,CAAC,CAAC;IACtB,OAAO,OAAO,CAAC;AAChB,CAAC","sourcesContent":["import type {\n\tBehaviorDefinition,\n\tBehaviorHandler,\n\tRuntime,\n\tRuntimeOptions,\n\tRuntimeRoot,\n\tSleanEventType\n} from './types.js';\nimport { DEV } from '#env';\nimport {\n\tresetValidation,\n\ttrackRuntime,\n\tuntrackRuntime,\n\tvalidateRoot,\n\twarn\n} from './diagnostics.js';\nimport { isDisabled } from './dom.js';\n\n/** Every event type the router can install, in report order. */\nexport const EVENT_TYPES: readonly SleanEventType[] = [\n\t'click',\n\t'dblclick',\n\t'keydown',\n\t'keyup',\n\t'focusin',\n\t'focusout',\n\t'input',\n\t'change',\n\t'pointerdown',\n\t'pointerup',\n\t'pointerover',\n\t'pointerout',\n\t'contextmenu',\n\t'dragenter',\n\t'dragleave',\n\t'drop',\n\t'toggle',\n\t'beforetoggle'\n];\n\n// `toggle` and `beforetoggle` do not bubble; a root listener only observes them in the capture\n// phase. Every other routed type bubbles, so it stays in the bubble phase and element-level\n// listeners keep their normal ordering.\nconst CAPTURE: Partial<Record<SleanEventType, true>> = { toggle: true, beforetoggle: true };\n\n/** Development check, run once per registration: every handled event type must be routable. */\nfunction checkEventTypes(name: string, definition: BehaviorDefinition): void {\n\tfor (const type in definition.events) {\n\t\tif (!definition.events[type as SleanEventType]) continue;\n\t\tif (!(EVENT_TYPES as readonly string[]).includes(type)) {\n\t\t\twarn(name, `\"${type}\" is not a routed event type; use a scoped listener instead.`);\n\t\t}\n\t}\n}\n\n/**\n * Creates a runtime whose shared listeners attach to `root` (`document` by default).\n * Nothing is installed until a behavior that needs an event type is registered.\n */\nexport function createRuntime(options: RuntimeOptions = {}): Runtime {\n\treturn createRuntimeInternal(options.root ?? document);\n}\n\n/** @internal `onDestroy` lets the default-runtime module forget a destroyed instance. */\nexport function createRuntimeInternal(root: RuntimeRoot, onDestroy?: () => void): Runtime {\n\tconst behaviors = new Map<string, BehaviorDefinition>();\n\tconst installed = new Set<SleanEventType>();\n\n\tconst route = (event: Event): void => {\n\t\tconst type = event.type as SleanEventType;\n\t\tconst path = event.composedPath();\n\t\tconst first = path[0] as Node | undefined;\n\t\tconst target = (first && first.nodeType === 1 ? first : event.target) as Element;\n\t\tfor (let i = 0; i < path.length; i++) {\n\t\t\tconst node = path[i] as HTMLElement;\n\t\t\tif (node.nodeType !== 1) continue;\n\t\t\tconst name = node.getAttribute('data-slean');\n\t\t\tif (!name) continue;\n\t\t\tconst definition = behaviors.get(name);\n\t\t\tconst handler = definition?.events?.[type];\n\t\t\tif (!handler || isDisabled(node)) continue;\n\t\t\tif (DEV) validateRoot(name, definition as BehaviorDefinition, node);\n\t\t\t(handler as BehaviorHandler)({ root: node, target, event, runtime });\n\t\t\tif (event.cancelBubble) return;\n\t\t}\n\t};\n\n\t// Makes the installed set equal to the set of routed event types some registered behavior\n\t// handles. Unroutable types were reported once, at registration.\n\tconst sync = (): void => {\n\t\tconst needed = new Set<SleanEventType>();\n\t\tfor (const definition of behaviors.values()) {\n\t\t\tconst events = definition.events;\n\t\t\tfor (const type in events) {\n\t\t\t\tif (!events[type as SleanEventType]) continue;\n\t\t\t\tif ((EVENT_TYPES as readonly string[]).includes(type)) needed.add(type as SleanEventType);\n\t\t\t}\n\t\t}\n\t\tfor (const type of needed) {\n\t\t\tif (installed.has(type)) continue;\n\t\t\troot.addEventListener(type, route, CAPTURE[type] === true);\n\t\t\tinstalled.add(type);\n\t\t}\n\t\tfor (const type of installed) {\n\t\t\tif (needed.has(type)) continue;\n\t\t\troot.removeEventListener(type, route, CAPTURE[type] === true);\n\t\t\tinstalled.delete(type);\n\t\t}\n\t};\n\n\tconst runtime: Runtime = {\n\t\troot,\n\t\tregister(name, definition) {\n\t\t\tbehaviors.set(name, definition);\n\t\t\tif (DEV) checkEventTypes(name, definition);\n\t\t\tif (DEV) resetValidation();\n\t\t\tsync();\n\t\t},\n\t\tunregister(name) {\n\t\t\tif (behaviors.delete(name)) sync();\n\t\t},\n\t\thas: (name) => behaviors.has(name),\n\t\tbehaviors: () => [...behaviors.keys()],\n\t\tlisteners() {\n\t\t\tconst out: Record<string, number> = {};\n\t\t\tfor (const type of installed) out[type] = 1;\n\t\t\treturn out;\n\t\t},\n\t\tdestroy() {\n\t\t\tbehaviors.clear();\n\t\t\tsync();\n\t\t\tuntrackRuntime(runtime);\n\t\t\tonDestroy?.();\n\t\t}\n\t};\n\ttrackRuntime(runtime);\n\treturn runtime;\n}\n"]}
1
+ {"version":3,"file":"runtime.js","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,GAAG,EAAE,MAAM,MAAM,CAAC;AAC3B,OAAO,EACN,eAAe,EACf,YAAY,EACZ,cAAc,EACd,YAAY,EACZ,IAAI,EACJ,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAEtC,gEAAgE;AAChE,MAAM,CAAC,MAAM,WAAW,GAA8B;IACrD,OAAO;IACP,UAAU;IACV,SAAS;IACT,OAAO;IACP,SAAS;IACT,UAAU;IACV,OAAO;I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\n\tRuntimeOptions,\n\tRuntimeRoot,\n\tSleanEventType\n} from './types.js';\nimport { DEV } from '#env';\nimport {\n\tresetValidation,\n\ttrackRuntime,\n\tuntrackRuntime,\n\tvalidateRoot,\n\twarn\n} from './diagnostics.js';\nimport { isDisabled } from './dom.js';\n\n/** Every event type the router can install, in report order. */\nexport const EVENT_TYPES: readonly SleanEventType[] = [\n\t'click',\n\t'dblclick',\n\t'keydown',\n\t'keyup',\n\t'focusin',\n\t'focusout',\n\t'input',\n\t'change',\n\t'pointerdown',\n\t'pointerup',\n\t'pointerover',\n\t'pointerout',\n\t'contextmenu',\n\t'dragenter',\n\t'dragleave',\n\t'drop',\n\t'toggle',\n\t'beforetoggle'\n];\n\n// `toggle` and `beforetoggle` do not bubble; a root listener only observes them in the capture\n// phase. Every other routed type bubbles, so it stays in the bubble phase and element-level\n// listeners keep their normal ordering.\nconst CAPTURE: Partial<Record<SleanEventType, true>> = { toggle: true, beforetoggle: true };\n\n/** Development check, run once per registration: every handled event type must be routable. */\nfunction checkEventTypes(name: string, definition: BehaviorDefinition): void {\n\tfor (const type in definition.events) {\n\t\tif (!definition.events[type as SleanEventType]) continue;\n\t\tif (!(EVENT_TYPES as readonly string[]).includes(type)) {\n\t\t\twarn(name, `\"${type}\" is not a routed event type; use a scoped listener instead.`);\n\t\t}\n\t}\n}\n\n/**\n * Creates a runtime whose shared listeners attach to `root` (`document` by default).\n * Nothing is installed until a behavior that needs an event type is registered.\n */\nexport function createRuntime(options: RuntimeOptions = {}): Runtime {\n\treturn createRuntimeInternal(options.root ?? document);\n}\n\n/** @internal `onDestroy` lets the default-runtime module forget a destroyed instance. */\nexport function createRuntimeInternal(root: RuntimeRoot, onDestroy?: () => void): Runtime {\n\tconst behaviors = new Map<string, BehaviorDefinition>();\n\tconst installed = new Set<SleanEventType>();\n\t// Attribute behaviors (ADR 0009) as [name, definition]: few (usually none), checked on each\n\t// path element.\n\tlet attributes: [string, BehaviorDefinition][] = [];\n\n\tconst route = (event: Event): void => {\n\t\tconst type = event.type as SleanEventType;\n\t\tconst path = event.composedPath();\n\t\tconst first = path[0] as Node | undefined;\n\t\tconst target = (first && first.nodeType === 1 ? first : event.target) as Element;\n\t\tfor (let i = 0; i < path.length; i++) {\n\t\t\tconst node = path[i] as HTMLElement;\n\t\t\tif (node.nodeType !== 1) continue;\n\t\t\tfor (let a = 0; a < attributes.length; a++) {\n\t\t\t\tconst [behavior, definition] = attributes[a]!;\n\t\t\t\tconst handler = definition.events?.[type];\n\t\t\t\tif (!handler || !node.hasAttribute(definition.attribute!)) continue;\n\t\t\t\t// Not skipped when disabled: a hint explains a disabled control, and a behavior that\n\t\t\t\t// must not act on one (copy) checks `isDisabled()` itself.\n\t\t\t\tif (DEV) validateRoot(behavior, definition, node);\n\t\t\t\t(handler as BehaviorHandler)({ root: node, target, event, runtime });\n\t\t\t\tif (event.cancelBubble) return;\n\t\t\t}\n\t\t\tconst name = node.getAttribute('data-slean');\n\t\t\tif (!name) continue;\n\t\t\tconst definition = behaviors.get(name);\n\t\t\tconst handler = definition?.events?.[type];\n\t\t\tif (!handler || isDisabled(node)) continue;\n\t\t\tif (DEV) validateRoot(name, definition as BehaviorDefinition, node);\n\t\t\t(handler as BehaviorHandler)({ root: node, target, event, runtime });\n\t\t\tif (event.cancelBubble) return;\n\t\t}\n\t};\n\n\t// Makes the installed set equal to the set of routed event types some registered behavior\n\t// handles. Unroutable types were reported once, at registration.\n\tconst sync = (): void => {\n\t\tattributes = [...behaviors].filter(([, definition]) => definition.attribute);\n\t\tconst needed = new Set<SleanEventType>();\n\t\tfor (const definition of behaviors.values()) {\n\t\t\tconst events = definition.events;\n\t\t\tfor (const type in events) {\n\t\t\t\tif (!events[type as SleanEventType]) continue;\n\t\t\t\tif ((EVENT_TYPES as readonly string[]).includes(type)) needed.add(type as SleanEventType);\n\t\t\t}\n\t\t}\n\t\tfor (const type of needed) {\n\t\t\tif (installed.has(type)) continue;\n\t\t\troot.addEventListener(type, route, CAPTURE[type] === true);\n\t\t\tinstalled.add(type);\n\t\t}\n\t\tfor (const type of installed) {\n\t\t\tif (needed.has(type)) continue;\n\t\t\troot.removeEventListener(type, route, CAPTURE[type] === true);\n\t\t\tinstalled.delete(type);\n\t\t}\n\t};\n\n\tconst runtime: Runtime = {\n\t\troot,\n\t\tregister(name, definition) {\n\t\t\tbehaviors.set(name, definition);\n\t\t\tif (DEV) checkEventTypes(name, definition);\n\t\t\tif (DEV) resetValidation();\n\t\t\tsync();\n\t\t},\n\t\tunregister(name) {\n\t\t\tif (behaviors.delete(name)) sync();\n\t\t},\n\t\thas: (name) => behaviors.has(name),\n\t\tbehaviors: () => [...behaviors.keys()],\n\t\tlisteners() {\n\t\t\tconst out: Record<string, number> = {};\n\t\t\tfor (const type of installed) out[type] = 1;\n\t\t\treturn out;\n\t\t},\n\t\tdestroy() {\n\t\t\tbehaviors.clear();\n\t\t\tsync();\n\t\t\tuntrackRuntime(runtime);\n\t\t\tonDestroy?.();\n\t\t}\n\t};\n\ttrackRuntime(runtime);\n\treturn runtime;\n}\n"]}
package/dist/types.d.ts CHANGED
@@ -26,6 +26,13 @@ export interface BehaviorDefinition {
26
26
  tier: 1 | 2;
27
27
  /** Handlers keyed by event type. Only the listed types get a shared listener. */
28
28
  events?: BehaviorEvents;
29
+ /**
30
+ * An attribute behavior (ADR 0009): its roots are the elements that carry this attribute
31
+ * (`data-slean-hint`, `data-slean-copy`) rather than `data-slean="<name>"`, so it can sit on
32
+ * any element, a root of another behavior included. Must start with `data-slean-`. The router
33
+ * checks it on each element of the event path, beside the `data-slean` marker.
34
+ */
35
+ attribute?: `data-slean-${string}`;
29
36
  /**
30
37
  * Development-only markup validation. Called once per root on the first routed event, in
31
38
  * development builds only; each returned string is printed through `warn()`. Primitives may
package/dist/types.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Event types the shared router is allowed to install. Nothing outside this list is ever\n * installed generically; in particular `pointermove`, `scroll` and `resize` belong to scoped\n * controllers (Tier 2) with an explicit lifetime. `pointerover` and `pointerout` are routed: they\n * bubble and fire when the pointer crosses an element boundary, not on every movement (tooltip,\n * toast). `contextmenu` is routed for the context menu: it bubbles and fires once per request\n * (secondary click, long press, the context-menu key or Shift+F10). `dragenter`, `dragleave` and\n * `drop` are routed for the file drop area; `dragover`, which fires continuously, is not.\n */\nexport type SleanEventType =\n\t| 'click'\n\t| 'dblclick'\n\t| 'keydown'\n\t| 'keyup'\n\t| 'focusin'\n\t| 'focusout'\n\t| 'input'\n\t| 'change'\n\t| 'pointerdown'\n\t| 'pointerup'\n\t| 'pointerover'\n\t| 'pointerout'\n\t| 'contextmenu'\n\t| 'dragenter'\n\t| 'dragleave'\n\t| 'drop'\n\t| 'toggle'\n\t| 'beforetoggle';\n\n/** Native event class per routed event type. */\nexport type SleanEventMap = Pick<HTMLElementEventMap, SleanEventType>;\n\n/** What a behavior handler receives. `target` is the deepest element of the composed path. */\nexport interface BehaviorContext<E extends Event = Event> {\n\troot: HTMLElement;\n\ttarget: Element;\n\tevent: E;\n\truntime: Runtime;\n}\n\nexport type BehaviorHandler<E extends Event = Event> = (context: BehaviorContext<E>) => void;\n\nexport type BehaviorEvents = {\n\t[K in SleanEventType]?: BehaviorHandler<SleanEventMap[K]>;\n};\n\nexport interface BehaviorDefinition {\n\t/** Runtime tier as declared in the primitive's contract (ADR 0003). Tier 0 never registers. */\n\ttier: 1 | 2;\n\t/** Handlers keyed by event type. Only the listed types get a shared listener. */\n\tevents?: BehaviorEvents;\n\t/**\n\t * Development-only markup validation. Called once per root on the first routed event, in\n\t * development builds only; each returned string is printed through `warn()`. Primitives may\n\t * strip it from production definitions.\n\t */\n\tvalidate?: (root: HTMLElement) => string[];\n}\n\nexport type RuntimeRoot = Document | ShadowRoot | HTMLElement;\n\nexport interface RuntimeOptions {\n\t/** Event target the shared listeners attach to. Defaults to `document`. */\n\troot?: RuntimeRoot;\n}\n\nexport interface Runtime {\n\treadonly root: RuntimeRoot;\n\t/** Idempotent: registering the same name again replaces the definition without adding listeners. */\n\tregister(name: string, definition: BehaviorDefinition): void;\n\tunregister(name: string): void;\n\thas(name: string): boolean;\n\tbehaviors(): string[];\n\t/** Installed shared listeners per event type; every installed type maps to exactly 1. */\n\tlisteners(): Record<string, number>;\n\t/** Removes every listener and definition. A destroyed runtime must not be reused. */\n\tdestroy(): void;\n}\n"]}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Event types the shared router is allowed to install. Nothing outside this list is ever\n * installed generically; in particular `pointermove`, `scroll` and `resize` belong to scoped\n * controllers (Tier 2) with an explicit lifetime. `pointerover` and `pointerout` are routed: they\n * bubble and fire when the pointer crosses an element boundary, not on every movement (tooltip,\n * toast). `contextmenu` is routed for the context menu: it bubbles and fires once per request\n * (secondary click, long press, the context-menu key or Shift+F10). `dragenter`, `dragleave` and\n * `drop` are routed for the file drop area; `dragover`, which fires continuously, is not.\n */\nexport type SleanEventType =\n\t| 'click'\n\t| 'dblclick'\n\t| 'keydown'\n\t| 'keyup'\n\t| 'focusin'\n\t| 'focusout'\n\t| 'input'\n\t| 'change'\n\t| 'pointerdown'\n\t| 'pointerup'\n\t| 'pointerover'\n\t| 'pointerout'\n\t| 'contextmenu'\n\t| 'dragenter'\n\t| 'dragleave'\n\t| 'drop'\n\t| 'toggle'\n\t| 'beforetoggle';\n\n/** Native event class per routed event type. */\nexport type SleanEventMap = Pick<HTMLElementEventMap, SleanEventType>;\n\n/** What a behavior handler receives. `target` is the deepest element of the composed path. */\nexport interface BehaviorContext<E extends Event = Event> {\n\troot: HTMLElement;\n\ttarget: Element;\n\tevent: E;\n\truntime: Runtime;\n}\n\nexport type BehaviorHandler<E extends Event = Event> = (context: BehaviorContext<E>) => void;\n\nexport type BehaviorEvents = {\n\t[K in SleanEventType]?: BehaviorHandler<SleanEventMap[K]>;\n};\n\nexport interface BehaviorDefinition {\n\t/** Runtime tier as declared in the primitive's contract (ADR 0003). Tier 0 never registers. */\n\ttier: 1 | 2;\n\t/** Handlers keyed by event type. Only the listed types get a shared listener. */\n\tevents?: BehaviorEvents;\n\t/**\n\t * An attribute behavior (ADR 0009): its roots are the elements that carry this attribute\n\t * (`data-slean-hint`, `data-slean-copy`) rather than `data-slean=\"<name>\"`, so it can sit on\n\t * any element, a root of another behavior included. Must start with `data-slean-`. The router\n\t * checks it on each element of the event path, beside the `data-slean` marker.\n\t */\n\tattribute?: `data-slean-${string}`;\n\t/**\n\t * Development-only markup validation. Called once per root on the first routed event, in\n\t * development builds only; each returned string is printed through `warn()`. Primitives may\n\t * strip it from production definitions.\n\t */\n\tvalidate?: (root: HTMLElement) => string[];\n}\n\nexport type RuntimeRoot = Document | ShadowRoot | HTMLElement;\n\nexport interface RuntimeOptions {\n\t/** Event target the shared listeners attach to. Defaults to `document`. */\n\troot?: RuntimeRoot;\n}\n\nexport interface Runtime {\n\treadonly root: RuntimeRoot;\n\t/** Idempotent: registering the same name again replaces the definition without adding listeners. */\n\tregister(name: string, definition: BehaviorDefinition): void;\n\tunregister(name: string): void;\n\thas(name: string): boolean;\n\tbehaviors(): string[];\n\t/** Installed shared listeners per event type; every installed type maps to exactly 1. */\n\tlisteners(): Record<string, number>;\n\t/** Removes every listener and definition. A destroyed runtime must not be reused. */\n\tdestroy(): void;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@svelte-lean/core",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "Svelte Lean runtime kernel: behavior registry, shared event router, DOM and keyboard helpers.",
5
5
  "license": "MIT",
6
6
  "author": "Ömer Say",