yai

YEH (Yai Event Hub)

A lightweight, flexible event handler for modern web applications. Simplifies event management by centralizing listeners and providing advanced routing options.

YEH is fundamentally designed around event delegation - the concept of using a single listener to handle multiple, or even dynamically added elements efficiently and lossless. This isn’t just an optimization; it’s the entire architectural foundation. Works on file:// with zero build tools.


✨ Features


πŸš€ Quick Start

No setup, no build step, no server, just include the file.

Get started in 30 seconds – try it live on JSFiddle

<!DOCTYPE html>
<html>
<head><title>YEH Demo</title></head>
<body>
  <div id="app">
    <button data-action="save">Save</button>
    <button data-action="delete">Delete</button>
  </div>

  <script type="module">
    import { YEH } from 'https://cdn.jsdelivr.net/npm/@yaijs/core@latest/dist/yai-bundle.js';

    class MyHandler extends YEH {
      constructor() {
        super({ '#app': ['click'] }); // Falls back to handleClick() = "handle + click"
      }

      handleClick(event, target, container) {
        const action = target.dataset.action;
        if (action && this[action]) this[action](target, event, container);
      }

      save(target)   { console.log('Saving...'); }
      delete(target) { console.log('Deleting...'); }
    }

    new MyHandler(); // Adding listeners Done
  </script>
</body>
</html>

30-second setup: Create app.html, copy & paste the above code, then double-click to run.

πŸ’‘ Universal Delegation Pattern

One listener on parent + custom-selector = handles unlimited elements within the parent


Installation

CDN (Instant Setup)

<script type="module">
  import { YEH } from 'https://cdn.jsdelivr.net/npm/@yaijs/core@latest/dist/yai-bundle.js';
  new YEH({ '#app': ['click'] });
</script>

NPM (Build Tools)

npm install @yaijs/core
// Import from main bundle
import { YEH } from '@yaijs/core';

// Or import directly
import { YEH } from '@yaijs/core/yeh';

Note: Package is ESM-only. Use import, not require().

Works with file:// protocol β€” no server needed.


βš™οΈ Configuration Options

Pass a third argument to the constructor to enable advanced features:

Option Type Default Description
enableStats boolean false Track performance metrics like event count and distance cache hits.
methods object null External method map for organizing handlers by event type.
enableGlobalFallback boolean false Fallback to global window functions when no method is found.
methodsFirst boolean false Check methods object before class methods during handler resolution.
passiveEvents array auto Override default passive events (scroll, touch, wheel, pointer).
abortController boolean false Enable AbortController support for programmatic listener removal.
enableDistanceCache boolean true Cache DOM distance calculations for performance (multi-handler scenarios).

Example: new YEH(events, aliases, { enableStats: true });


πŸ”— Fluent Chaining API

Chain operations for complex event orchestration:

App.on('data-ready', 'handleData')
    .on('user-login', 'handleLogin')
    .emit('init-complete', { loaded: true });

🧹 Cleanup

handler.destroy();
// Or with AbortController enabled
handler.abort();

πŸ“Š Performance Metrics

With enableStats: true:

console.log(handler.getStats());

🌐 Browser Support

Opera Chrome Firefox Safari Edge - all modern versions

Works with legacy browsers via Webpack + Babel.


Feature YEH EventEmitter3 Redux Toolkit jQuery
Bundle Size 5kB gzipped 7kB gzipped 12kB+ gzipped 30kB+ gzipped
Dependencies βœ… Zero βœ… Zero ❌ Many βœ… Zero
Event Delegation βœ… Advanced ❌ None ❌ None βœ… Basic
Multi-Handler System βœ… Unique ❌ None ❌ None ❌ None
Throttle/Debounce βœ… Built-in ❌ None ❌ None ❌ None
Native Browser API βœ… Yes ❌ No ❌ No ❌ No
Dynamic Element Support βœ… Zero-config ❌ None ❌ None βœ… Re-bind
TypeScript Support βœ… Full βœ… Partial βœ… Full ⚠️ Community
Memory Leak Prevention βœ… Automatic ⚠️ Manual βœ… Automatic ⚠️ Manual
Performance βœ… Native ⚠️ Synthetic ⚠️ Virtual ⚠️ Abstraction
Custom Event Dispatch βœ… Built-in βœ… Yes βœ… Yes βœ… Yes
Learning Curve βœ… Low βœ… Low ❌ Steep βœ… Familiar

Why YEH Stands Out

πŸš€ See It In Action

YaiTabs Live Demo Advanced tab system built on YEH with 20+ nested components

Performance Benchmark Stress test with 400+ nesting levels demonstrating YEH’s O(1) delegation


πŸ“¦ Resources


License

MIT License – free to use in personal or commercial projects.

πŸ‘₯ Authors