Skip to content

Finally, done #1

@tunnckoCore

Description

@tunnckoCore

Around 1.81kb min+gzip - without the diffing and html/jsx thing. You can choose how to work under the hood - with virtual dom or not. So if you choose virtual dom and jsx, then from the views return jsx, and pass Virtual dom diffing algorithm as app.diff(oldTree, newTree).

Otherwise, good combo is bel plus nanomorph but minified and gzipped with them is ~5.63kb

Main Parts:

index.js

const dush = require('dush')
const dushRouter = require('./dush-router-v2')
const dushChanges = require('./dush-changes')
const dushActions = require('./dush-actions')

/* eslint-disable prefer-destructuring */

const HISTORY_OBJECT = {}

module.exports = function Chika (model, _context) {
  _context = {}

  /**
   * Some internal plugins
   */
  const app = dush()
    .use(dushRouter())
    .use(dushChanges())
    .use(dushActions(model))

  let _tree = null

  app.off('route')
  app.on('route', (view, ctx) => {
    ctx = Object.assign({}, ctx, {
      state: app.state,
      actions: app.actions
    })

    const { state, actions, params, pathname, route } = ctx
    const match = { params, pathname, route }
    _context = { state, actions, params, match }

    const newTree = view(_context, _context.params, _context.match)

    // each route will have signature: ({ state, actions }, params, match)
    // so it is easy to destruct `params` and `match`
    return (_tree = app.diff(_tree, newTree))
  })

  /**
   * Re-render when state get updated
   */
  app.on('stateUpdate', (currentState, oldState) => {
    const view = app.getRoutes(_context.match.route)
    _context.state = currentState

    _tree = app.diff(_tree, view(_context, _context.params, _context.match))
  })

  /**
   * Start the application and get generated dom tree,
   * which you should append somewhere in the dom
   */
  app.start = () => {
    _tree = app.emit(window.location.pathname, app.state)

    app.on('render', (node, hist) => {
      if (hist) {
        window.history.replaceState(HISTORY_OBJECT, '', node.pathname)
      } else {
        window.history.pushState(HISTORY_OBJECT, '', node.pathname)
      }
      app.emit(node.pathname, app.state)
    })

    app.on('historyChange', (node) => {
      app.emit('render', node, true)
    })
    app.on('hrefChange', (node) => {
      if (node.href === window.location.href) return

      app.emit('render', node)
    })

    return _tree
  }

  return app
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions