Navigator Prefabricating Marinates

    @beardedtim/pubsub

    0.3.0 • Public • Published

    PubSub

    Us suffer bad. Want justice. We want Thunderdome!

    Build Status codecov

    API

    PubSub

    Demo Usage:

    const { PubSub } = require('@beardedtim/pubsub);
    
    // emit is how we can observe the result of some action
    // in a uniformed way throughout the system.
    //
    // Think of `emit` as the `resolve` of a promise,
    // the callback of a, well, callback function
    //
    // This is how we emit changes inside of the PubSub
    // system into the outer scope
    const emit = (...args) => {
      console.log(...args);
    }
    
    // Each PubSub instance MUST have an emit 
    // function passed to its constructor.
    const pubsub = new PubSub(emit);
    
    // We want to subscribe to some event in the future
    // called 'EVENT_NAME', with default scope applied,
    // and when that even happens, I want to emit whatever
    // was passed with that event.
    pubsub.subscribe('EVENT_NAME', ({ emit }, ...data) => emit(...data));
    
    // At some point in the future we
    // publish that event into our system
    // and eventually our `emit` function
    // gets called.
    pubsub.publish('EVENT_NAME', 1, 2, 3);
    // 1, 2, 3
    

    API:

    • constructor: (emit: fn [, id: str, scoped: bool]) => void

      • Emit: Function to emit from the system
      • ID: The ID of this instance. Falls back to a guid
      • Scoped: If we are going to scope our event names
    • getEventName: (event: str, scoped: bool) => str

      • Returns an EventName based on scoped
      • Example:
          const unScopedName = pubsub.getEventName('event', false) // 'event'
          const scopedName = pubsub.getEventName('event', true) // '${pubsub._id}:event'
        
    • subscribe: (event: str, fn: function, scoped: bool) => fn

      • Adds an event listener to passed event

        • Example:
          const handleInput = ({ emit, publish, subscribe }, input) => {
            const massagedInput = sanitizeInput(input);
            publish('SANATIZED_INPUT', massagedInput);
          }
        
          const respondToUser = ({ emit }, payload) =>
            emit({
              type: 'RETURN_TO_USER',
              payload
            });
        
          const handleSanatizedInput = ({ emit }, input) =>
            emit({
              type: 'UPDATE_FORM',
              payload: input,
            });
          
          pubsub.subscribe('USER_NAME_UPDATE', handleInput);
          pubsub.subscribe('SANATIZED_INPUT', handleSanatizedInput);
        
    • publish: (event: str [, ...args: [*]]) => void

      • Publishes an event to the system
      • Example:
        pubsub.subscribe('GET_DB', handleDB);
        
        const asyncPublish = action => payload =>
          pubsub.publish(action, payload);
        
        findDB()
          .then(asyncPublish('GET_DB'));
      
    • __id: str

      • The ID of the instance
    • __emit: fn

      • The emit function passed to the constructor
    • __isScoped: bool

      • The scoped value passed to the constructor

    GUID

    This is a straight copy/paste from [stackOverflow](// https://stackoverflow.com/a/105074/3902127)

      // guid: () -> String
      guid() // some random string
    

    Running Local Example

    $ git clone [repo]
    
    $ cd [repo]
    
    $ yarn
    
    $ yarn example
    

    This will start a WebSocket server at ./.env's SOCKET_PORT= value or default to port 5001. You can send it the message { "type": "GET" } and get a response eventually. I use wscat for testing.

    You may also find a basic example here

    Keywords

    none

    Install

    npm i @beardedtim/pubsub

    DownloadsWeekly Downloads

    0

    Version

    0.3.0

    License

    MIT

    Last publish

    Collaborators

    • beardedtim