Code Monkey home page Code Monkey logo

metaflux's Introduction

metaflux logo metaflux

Simplifying the flux/redux pattern for vanilla (es6) javascript

Metaflux is a lightweight library that provides a store to your application.

Getting Started

Installation:

npm install @rebelstack-io/metaflux

Usage:

Step 1 (Require/Import).

const { Store } = require('@rebelstack-io/metaflux');

Or when import is enabled:

import { Store } from '@rebelstack-io/metaflux';

Step 2 (Initialize).

const storage = new Store(
	{Counter: 1},
	{INCREMENT: (action, state) => {
		state.Counter = state.Counter + 1;
		return {newState: state};
	}
});

Step 3 (Dispatch actions).

The Store object is also an event emitter, when an action is dispatched an event is emitted.

storage.dispatch({
	type: 'INCREMENT'
});

Step 4 (Listen for changes).

storage.on('INCREMENT', action => {
	// GET THE UPDATED VALUE
	const newValue = storage.getState().Counter;
	// UPDATE DOM.
});

MetaComponents

MetaComponents are WebComponents that support Metaflux's storage.

Usage

import { MetaComponent } from '@rebelstack-io/metaflux';

class MyComponent extends MetaComponent {
	render () {
		this.content = document.createElement('div');
		this.content.innerHtml = `
			<div>
				<h1>Hello World</h1>
			</div>
		`;
		return this.content;
	}
}

window.customElements.define('my-component', MyComponent);

Bind MetaComponents to Metaflux Storage

Althought Storage is an event emitter by itself it is more organize to use Metaflux's handleStoreEvents method to bind itself to the actions dispatched.

import { MetaComponent, Store } from '@rebelstack-io/metaflux';

const storage = new Store(
	{Counter: 1},
	{INCREMENT: (action, state) => {
		state.Counter = state.Counter + 1;
		return {newState: state};
	}
});

class MyComponent extends MetaComponent {
	// Pass storage to MetaComponent parent
	constructor () {
		super(storage);
	}
	render () {
		const content = document.createElement('div');
		const incrementButton = document.createElement('button');
		content.appendChild(incrementButton);
		// Dispatch Increment action when button is clicked.
		this.incrementButton.addEventListener('click', () => {
			this.storage.dispatch({
				type: 'INCREMENT'
			});
		});
		this.counter = document.createElement('div');
		// Here we get the initial state of Counter and assign it to the counter element.
		this.counter.textContent = this.storage.getState().Counter;
		content.appendChild(this.counter);
		return content;
	}
	// Handle Metaflux Storage events
	handleStoreEvents () {
		return {
			'INCREMENT': action => {
				// When INCREMENT action is dispatch we update the counter element
				this.counter.textContent = this.storage.getState().Counter;
			}
		}
	}
}

window.customElements.define('my-component', MyComponent);

Using MetaComponent inside a MetaContainer

A MetaContainer is a webcomponent with a render method that works like the MetaComponent's render method but instead you're not supposed to bind it to the storage, it doesn't have a handleStoreEvents method in order to avoid that, you can however put MetaComponents inside it.

This object was tought like a not complex element where developers can import stylesheets and organize their layout.

import { MetaContainer } from '@rebelstack-io/metaflux';
import '../components/my-component'; // Here we import our previously defined MetaComponent.

class MyContainer extends MetaContainer {
	render () {
		return `
			<h2>Simple Counter</h2>
			<my-component></my-component>
		`;
	}
}

window.customElements.define('my-container', MyContainer);

The MetaContainer is also a good place to declare global variables like the storage.

metaflux's People

Contributors

pablolimo avatar jegj avatar oreyes1991 avatar reinsbrain avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.