{"size": 1235, "ext": "js", "max_stars_repo_path": "examples/random-gif-nest-context/src/util/meiosis.js", "max_stars_repo_name": "foxdonut/meiosis-examples", "max_stars_repo_stars_event_min_datetime": "2016-05-16T23:12:59.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-20T19:48:45.000Z", "max_issues_repo_path": "examples/random-gif-nest-context/src/util/meiosis.js", "max_issues_repo_name": "foxdonut/meiosis-examples", "max_issues_repo_issues_event_min_datetime": "2016-07-31T23:33:22.000Z", "max_issues_repo_issues_event_max_datetime": "2020-12-13T17:38:01.000Z", "max_forks_repo_path": "examples/random-gif-nest-context/src/util/meiosis.js", "max_forks_repo_name": "foxdonut/meiosis-examples", "max_forks_repo_forks_event_min_datetime": "2016-11-03T21:22:30.000Z", "max_forks_repo_forks_event_max_datetime": "2021-03-03T02:11:15.000Z"}
{"max_stars_count": 67, "max_issues_count": 16, "max_forks_count": 16, "avg_line_length": 24.2156862745, "max_line_length": 88, "alphanum_fraction": 0.587854251}
export const meiosis = ({ stream, merge, app }) => {
  const update = stream()

  const states = stream.scan((state, patch) => merge(state, patch), app.initial, update)

  const pathGet = (object, path) =>
    path.reduce((obj, key) => (obj == undefined ? undefined : obj[key]), object)

  const nestPatch = (path, value) => ({
    [path[0]]: path.length === 1 ? value : nestPatch(path.slice(1), value)
  })

  const contextCache = {}

  const root = {
    getState: () => states(),
    update
  }

  const actions = app.Actions(root)
  root.actions = actions

  const nest = propOrPath => {
    if (propOrPath) {
      const path = [].concat(propOrPath)

      if (!contextCache[path]) {
        const getState = () => pathGet(states(), path)
        const localUpdate = patch => update(nestPatch(path, patch))

        const localContext = {
          getState,
          update: localUpdate,
          nest: next => nest(path.concat(next)),
          root
        }
        const localActions = app.Actions(localContext)
        localContext.actions = localActions

        contextCache[path] = localContext
      }
      return contextCache[path]
    }
    return root
  }

  root.nest = nest

  return { states, context: root }
}
