Svuick

Packages Guides

@svuick/events API


createEventContext()

  • Type

    createEventContext(key?: string): EventEmitter
  • Details

    The key argument can be used to create different event contexts. It defaults to “EVENTS”.

    Event Listerners attach to the returned EventEmitter are removed on unMount.

  • Example

    <script lang="ts">
        import { createEventContext } from '@svuick/events'
        import MyNestedComponent from './MyNestedComponent';
    
        const events = createEventContext();
    
        let value = 0;
    
        events.on('update', newValue => value = newValue);
    </script>
    
    {value}
    <MyNestedComponent/>

getEventContext()

  • Type

    getEventContext(key?: string): EventEmitter
  • Details

    The key argument can be used to get different event contexts. It defaults to “EVENTS”.

    Event Listerners attach to the returned EventEmitter are removed on unMount.

  • Example

    <script lang="ts">
      import { getEventContext } from '@svuick/events'
    
      const events = getEventContext();
    
      let value = 0;
    
      function handleClick() {
          events.emit('update', value);
      }
    </script>
    
    <input type="number" bind:value={value}/>
    <button on:click={handleClick}>Send!</button>

new EventEmitter()

Created a new EventEmitter with typed events.

  • Type

    type EventMap = {
      [key: string]: Listener;
    };
    new EventEmitter<Events extends EventMap>(): EventEmitter<Events>
  • Example

    interface MyEvents {
    	update: (value: number) => void;
    	change: (num: number, str: string) => void;
    }
    
    const events = new EventEmitter<MyEvents>();

events.on()

attach the listener

  • Type

    on(type: string, listener: (...args: any[]) => void): this

events.once()

attach the listener and remove it after it is called once

  • Type

    once(type: string, listener: (...args: any[]) => void): this

events.off()

removes an event listener

  • Type

    off(type: string, listener: (...args: any[]) => void): this

events.emit()

emits an event

  • Type

    emit(type: string, ...args: any[]): this
Released under the MIT License.
Copyright © 2022-present David Plugge