Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@erickmerchant/framework

Package Overview
Dependencies
Maintainers
1
Versions
244
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@erickmerchant/framework

A simple data down, actions up framework.

  • 13.1.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
increased by300%
Maintainers
1
Weekly downloads
 
Created
Source

@erickmerchant/framework

This scoped package is my personal framework. I use it on a number of small projects.

An Example

This example uses bel with nanomorph, but you should be able to use diffHTML and possibly other solutions.

const framework = require('@erickmerchant/framework')
const html = require('bel')
const diff = require('nanomorph')
const target = document.querySelector('main')

framework({target, store, component, diff})()

function store (commit) {
  commit(() => 0)

  return function (action) {
    switch (action) {
      case 'increment':
        commit((state) => state + 1)
        break

      case 'decrement':
        commit((state) => state - 1)
        break
    }
  }
}

function component ({state, dispatch}) {
  return html`<main>
    <output>${state}</output>
    <br>
    <button onclick=${() => dispatch('decrement')}>--</button>
    <button onclick=${() => dispatch('increment')}>++</button>
  </main>`
}

API Reference

Framework Code

framework

framework({target, store, component, diff, raf})

The function exported by this module.

  • target: a DOM element. The place to render your application
  • store
  • component
  • diff
  • raf: optional. A replacement for window.requestAnimationFrame. It defaults to window.requestAnimationFrame

Returns a function. See init

state

This is what is passed to commit. It can be anything from simply a number like in the example, but a plain object makes sense in most cases.

commit

commit((current) => next)

It's passed a callback that receives the current state and should return the new state.

dispatch

dispatch(...arguments)

Initializes a change in state and causes a render. If the length of arguments is 0, the agent is not called, but a render is triggered.

  • arguments: all get passed to the agent
next

next((target) => { ... })

A convenient helper to pass a callback to process.nextTick. Can be used to manipulate the page in some way after a render. For example scrolling to a specific element or focusing an input element.

init

init((dispatch) => { ... })

Call init to do some initial work that may require dispatch.

Application Code

store

store(commit)

It should return the agent.

agent

agent(...arguments)

Anything returned will be ignored

  • arguments: all the arguments passed to dispatch
component

component({state, dispatch, next})

Should return the element to pass to diff

diff

diff(target, element)

  • target: the target passed to framework
  • element: the new element returned from the component

FAQs

Package last updated on 24 Dec 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc