Sha256: 01097aeee8aba68b2dba6e1caf28ff2a3b0ae5b1f0587d66dc984ca416e9c1b5

Contents?: true

Size: 1.21 KB

Versions: 12

Compression:

Stored size: 1.21 KB

Contents

import _ from 'lodash'
import pluralize from 'pluralize'
import { v4 as uuidv4 } from 'uuid'

function enrich(type, payload) {
  if (type.split('/')[1] === 'upsert' && !(type.split('/')[0] === 'session')) {
    if (!payload.id) {
      return { ...payload, id: uuidv4() }
    }
  }
  return payload
}

export default function createOptDis(schema, dispatch, restClient, serverActionQueue) {
  const plurals = _.keys(schema).map(k => pluralize(k))

  function enqueueServerAction (action) {
    return serverActionQueue.addItem(action)
  }

  function dispatchServerAction() {
    const item = serverActionQueue.getItem()
    if (!item) return

    const { id, action } = item

    restClient.post('/jason/api/action', action)
    .then(({ data }) => serverActionQueue.itemProcessed(id, data))
    .catch(error => {
      dispatch({ type: 'jason/upsert', payload: { error } })
      serverActionQueue.itemFailed(id, error)
    })
  }

  setInterval(dispatchServerAction, 10)

  return function (action) {
    const { type, payload } = action
    const data = enrich(type, payload)

    dispatch({ type, payload: data })

    if (plurals.indexOf(type.split('/')[0]) > -1) {
      return enqueueServerAction({ type, payload: data })
    }
  }
}

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
jason-rails-0.8.7 client/src/createOptDis.ts
jason-rails-0.8.6 client/src/createOptDis.ts
jason-rails-0.8.4 client/src/createOptDis.ts
jason-rails-0.8.3 client/src/createOptDis.ts
jason-rails-0.8.2 client/src/createOptDis.ts
jason-rails-0.8.1 client/src/createOptDis.ts
jason-rails-0.8.0 client/src/createOptDis.ts
jason-rails-0.7.5 client/src/createOptDis.ts
jason-rails-0.7.3 client/src/createOptDis.ts
jason-rails-0.7.2 client/src/createOptDis.ts
jason-rails-0.7.1 client/src/createOptDis.ts
jason-rails-0.7.0 client/src/createOptDis.ts