Hacked together example of usage of Redux as a predictable in memory db with a node http frontend.
Start the server:
node server.js
Read from the store:
curl localhost:3000/
{"count":0}
curl localhost:3000/count
0
Dispatch an action:
curl localhost:3000/dispatch --data '{ "type": "INC" }'
Successfully dispatched
curl localhost:3000/dispatch --data '{ "type": "INC" }'
Successfully dispatched
Could be used to build a flux style firebase clone. Each client connects a websocket, then sends a path selector
const listeners = {
'/count': {
path: ['count'],
callback: function(count) {
console.log('count changed');
}
}
};
The server keeps track of the subscribers, then hooks the sockets to store.subscribe
store.subscribe(function(oldState, newState) {
for(let { id, path, socket } of subscribers) {
const oldFragment = selector(path, oldState);
const newFragment = selector(path, newState);
if(oldFragment !== newFragment) {
socket.send(id, newFragment);
}
}
});