{{announcement.body}}
{{announcement.title}}

Storeon: An Event-Based State Manager for Corvid

DZone 's Guide to

Storeon: An Event-Based State Manager for Corvid

In this article, we explain how to manage an state in Corvid with a light-weight and robust solution: Storeon, an event-based state manager.

· Web Dev Zone ·
Free Resource

Motivation

In the article, “State management in Corvid,” Shahar Talmi brings up a question about controlling app states in Corvid. If you’re not familiar with Corvid, it’s a development platform running on Wix that allows you to quickly and easily develop web applications.

Accurately controlling the state of any app is a really big problem. If you have many component dependencies or need to handle constant user interactions, you're going to suffer a bit when you want to eventually add a new feature or scale your application.

In this article, I share my solution — a very tiny library called Storeon (it’s only 175 bytes) that features an easy interface. So, I wrote a wrapper for integration with Corvid. As a result, we have the state manager corvid-storeon, and it’s less than 90 lines of code.

You may also like: Angular Tutorial: State Management With NgRx.

How it Works

We will create a traditional study app with counters. I will use two counters to help provide a better demonstration.

At first, we need to install the library from Package Manager

Corvid Package Manager

Corvid Package Manager


and create one more file for store initialization in the public folder.

public

└── store.js

We will write our business logic in public/store.js .

Storeon's state is always an object; it canʼt be anything else. Itʼs a small limitation and not too important to us, but we have to remember it.

public/store.js

JavaScript


So, we created a store in the public folder and exported from there with four methods. In the second part, we will create our UI, and we will write logic to change the state. 

Letʼs add two text elements to display our counter value, and four buttons for event increments/decrements.

Creating two counter components

Creating two counter components


Of course, we have to import the store methods from the public file to the page's code.

import { dispatch, connect, connectPage } from 'public/store'; 

With connect("key", callback), we can subscribe to any store properties, and the callback function will be run when the page is loaded and each time when the listed property changes.

The connectPage(callback) is a wrapper around the $w.onReady(callback). With dispatch(event, [data]), we will emit events.

Page Code

JavaScript
xxxxxxxxxx
1
40
 
1
import { getState, dispatch, connect, connectPage } from 'public/store';
2
 
3
// Connect to property "x".
4
// The callback function will be run when the page loads ($w.onReady())
5
// and each time when property "x" would change.
6
connect('x', (state) => {
7
  console.log('counter X is changed', state);
8
9
  $w('#textX').text = String(state.x);
10
});
11
 
12
// Connect to "y"
13
connect('y', (state) => {
14
  console.log('counter Y is changed', state);
15
16
  $w('#textY').text = String(state.y);
17
});
18
 
19
// Wrapper around $w.onReady()
20
// The callback function will be run once.
21
connectPage((state) => {
22
  // Here we also have an object with initial state
23
  console.log('onReady runs', state);
24
25
  // X counter events
26
  $w('#buttonIncX').onClick(() => {
27
    dispatch('INCREMENT_X');
28
  });
29
  $w('#buttonDecX').onClick(() => {
30
    dispatch('DECREMENT_X');
31
  });
32
 
33
  // Y counter events
34
  $w('#buttonIncY').onClick(() => {
35
    dispatch('INCREMENT_Y');
36
  });
37
  $w('#buttonDecY').onClick(() => {
38
    dispatch('DECREMENT_Y');
39
  });
40
});


Demo

Modules

The function, createStore(modules), accepts a list of modules. We can create different functions to split business logic into our app. Letʼs see a few examples:

Synchronization the App state with the wix-storage memory API:

JavaScript
xxxxxxxxxx
1
10
 
1
// https://www.wix.com/corvid/reference/wix-storage.html#memory
2
import { memory } from 'wix-storage';
3
 
4
export function memoryModule(store) {
5
  // @changed will be fired every when event listeners changed the state.
6
  // It receives object with state changes.
7
  store.on('@changed', (state) => {
8
    memory.setItem('key', state);
9
  });
10
}


Tracking an event to external analytics tools with wixWindow.trackEvent():

JavaScript
xxxxxxxxxx
1
13
 
1
import wixWindow from 'wix-window';
2
 
3
export function trackEventModule(store) {
4
  // @dispatch will be fired on every `dispatch(event, [data])` call.
5
  // It receives an array with the event name and the event’s data.
6
  // Can be useful for debugging.
7
  store.on('@dispatch', (state, [event, data]) => {
8
    if (event !== '@changed' && event !== '@dispatch') {
9
      // Sends a tracking event to external analytics tools.
10
      wixWindow.trackEvent(event, data);
11
    }
12
  });
13
}


Combining modules:

JavaScript
xxxxxxxxxx
1
 
1
const store = createStore([
2
  coutnerModule,
3
  memoryModule,
4
  trackEventModule,
5
]);


Conclusion

As you can see, we were able to quickly implement our state management solution with a minimal amount of code. Of course, due to data binding in Corvid, you normally don’t have to worry about state management. However, in more complex applications, the issue can become more difficult, and state management will become more challenging to handle.

State management can be a tricky problem, but Storeon offers a simple, yet robust solution. In addition, Corvid allows us to quickly implement this in our application, all while focusing on code and not having to spend time dealing with other issues. 

Resources 

Demo 


Further Reading



This article originally appeared on Medium.

Topics:
state management ,web dev ,state ,corvid ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}