All files / src/hooks/on on.ts

100% Statements 3/3
100% Branches 0/0
100% Functions 1/1
100% Lines 3/3

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 404x                                                                       4x 74x    
import { slot } from '../slot'
 
import type { Destructor, Event, Listener, Rune } from '../../RuneHub'
 
/**
 * Activates a rune by subscribing to it.
 * Keeps the rune and its dependencies active until unsubscribed.
 * @param rune - The rune to activate
 * @returns A destructor function to deactivate and unsubscribe
 * @example
 * ```ts
 * const count = () => 5
 * const effect = () => console.log('Count:', get(count))
 *
 * const off = on(effect) // logs: 'Count: 5'
 * set(count, 10) // logs: 'Count: 10'
 * off() // stop effect
 * ```
 */
export function on (rune: Rune): Destructor
/**
 * Subscribes to a specific event on a rune.
 * @param rune - The rune to subscribe to
 * @param event - The event to listen for ('init', 'update', 'call', 'change', 'clear', 'destroy', 'up', 'down')
 * @param listener - The listener function to call when the event occurs
 * @returns A destructor function to unsubscribe
 * @example
 * ```ts
 * const count = () => 5
 * const off = on(count, 'change', () => console.log('changed'))
 * set(count, 10) // logs: 'changed'
 * off() // unsubscribe
 * ```
 */
export function on (rune: Rune, event: Event, listener: Listener): Destructor
 
export function on (rune: Rune, event?: Event, listener?: Listener): Destructor {
  return slot(rune).on(event!, listener!)
}