npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rosmaro-react

v0.0.7

Published

Visual automata-based programming for React

Downloads

5

Readme

Rosmaro for React

Leverage the power of visual automata-based programming to build React components!

This package lets you build a React component which Rosmaro state lives in Redux and effects are managed in a declarative way with Redux-saga.


Using Rosmaro-React

Installing dependencies

$ npm install --save rosmaro rosmaro-redux redux-saga redux rosmaro-react
$ mkdir -p src/bindings && touch src/graph.json

The index.js file

// IMPORTING DEPENDENCIES:

// This is the component factory function exported by this package.
import rosmaroComponent from 'rosmaro-react';

// The content of this file should be generated using the Rosmaro visual editor (https://rosmaro.js.org/editor/). For more information on drawing graphs please visit https://rosmaro.js.org/doc/#graphs
import graph from './graph.json';

// It's convenient to generate the bindings/index.js file using https://github.com/lukaszmakuch/rosmaro-tools
import makeBindings from './bindings';

// We need Rosmaro and React.
import rosmaro from 'rosmaro';
import React from 'react';
import ReactDOM from 'react-dom';

// Redux itself.
import {createStore, applyMiddleware} from 'redux';

// This will gives us a Rosmaro driven reducer and integrate it with Redux Saga.
import {makeReducer, effectDispatcher} from 'rosmaro-redux';

// We need to provide the Redux state.
import {Provider} from 'react-redux';

// Side-effects are handled by Redux Saga.
import createSagaMiddleware from 'redux-saga';

// For more information on Redux Saga please refer to the official documentation at https://redux-saga.js.org 
import rootSaga from './sagas';

// Writing handlers is easier with https://github.com/lukaszmakuch/rosmaro-binding-utils
import {typeHandler, partialReturns, defaultHandler} from 'rosmaro-binding-utils';

// SETTING THINGS UP:

// Makes writing handlers convenient.
const makeHandler = opts => partialReturns(typeHandler({defaultHandler})(opts));

// Rosmaro bindings are built with the handler factory defined above.
const bindings = makeBindings({makeHandler});

// This is the Rosmaro model.
const model = rosmaro({graph, bindings});

// The reducer is based on the Rosmaro model.
const rootReducer = makeReducer(model);

// For more information on connecting Rosmaro and Redux please check https://github.com/lukaszmakuch/rosmaro-redux 
const sagaMiddleware = createSagaMiddleware();
const store = createStore(
  rootReducer,
  applyMiddleware(effectDispatcher, sagaMiddleware)
);
sagaMiddleware.run(rootSaga);

// This component is driven by the `model` Rosmaro model.
// Its state lives in the provided Redux store.
// The selector function defines the exact location of the Rosmaro model state.
const App = rosmaroComponent({
  model,
  selector: state => state
});

// Rendering the component.
ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('root')
);

Bindings

The most important thing is that every handler must react to the RENDER action. It is supposed to return a React component.

How the bindings are built doesn't really matter as long as they are correctly interpreted by Rosmaro. However, it's highly recommended to use rosmaro-binding-utils and rosmaro-tools most of the time, as they make most of the things a lot easier and provide a directory structure.

Below is an example of a simple On/Off component.

We're going to focus on the src/bindings directory we created at the very beginning.

Let's give it the following structure:

$ tree -U
.
└── main
    ├── index.js
    ├── On
    │   └── index.js
    └── Off
        └── index.js

The src/bindings/main/index.js file contains the main node binding. We don't need anything fancy here, so it may use the default handler provided by rosmaro-binding-utils:

// src/bindings/main/index.js
import {defaultHandler} from 'rosmaro-binding-utils';

export default () => ({handler: defaultHandler});

The src/bindings/main/On/index.js file is the main:On node binding. It's a simple button which when clicked makes the node follow the clicked arrow.

// src/bindings/main/On/index.js
import React from 'react';
import {connect} from 'react-redux';

// This component is connected to the redux store, 
// so it can dispatch actions.
const View = connect()(({dispatch}) => 
  <button
    id="on-button"
    onClick={() => dispatch({type: 'CLICK'})}
    >on</button>
);

// The makeHandler function is passed to the makeBindings function
// in the src/index.js file.
export default ({makeHandler}) => ({
  handler: makeHandler({

    CLICK: () => ({arrow: 'clicked'}),

    RENDER: () => <View />,
  
  })
});

The src/bindings/main/Off/index.js file is a very similar button:

// src/bindings/main/Off/index.js
import React from 'react';
import {connect} from 'react-redux';

const View = connect()(({dispatch}) => 
  <button
    id="off-button"
    onClick={() => dispatch({type: 'CLICK'})}
    >off</button>
);

export default ({makeHandler}) => ({
  handler: makeHandler({

    CLICK: () => ({arrow: 'clicked'}),

    RENDER: () => <View />,
  
  })
});

Now the src/bindings/index.js file, which is imported in the src/index.js file may be generated:

$ cd src/bindings
$ npx rosmaro-tools bindings:build .
Generated index.js!

The whole code of this example can be found in the example directory.


Links: