All files / src/features form-events.js

92.86% Statements 13/14
50% Branches 4/8
100% Functions 5/5
100% Lines 12/12

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              2x 2x 2x 2x             2x 2x 2x     2x 2x 2x   2x       2x            
import { EventBus } from '../libs/event-bus'
 
export default {
  hooks: {
    schemaOptions: [
      {
        handler(options, { emit } = {}) {
          options.state = options.state || {}
          options.state.eventBus = new EventBus()
          options.state.eventBus.on('events', (payload) => emit('events', payload))
          return options
        },
      },
    ],
    schemaNode: [
      {
        handler(node, { state } = {}) {
          const { events = [], on = {}, ...attrs } = node.definition.attrs
          node.definition.attrs = attrs
          node.definition.on = {
            ...on,
            ...events.reduce((onEvents, eventName) => {
              onEvents[eventName] = function(payload) {
                Iif (on[eventName]) on[eventName](payload)
                state.eventBus.emit('events', { eventName, name: node.name, type: node.type, key: node.key, payload })
              }
              return onEvents
            }, {}),
          }
 
          return node
        },
      },
    ],
  },
}