Cumulo: Flux like architechture on server
Cumulo is a realtime framework for small apps. It's designed like Flux to work with React.
Action Schema
This is the schema of actions used to connect server and clients:
action = name: 'scope/behavior' data: "JSON Data"
Usage
npm i --save cumulo
Here are the methods and constructors exposed:
exports.router = require './router'exports.database = require './database' exports.State = require './state'exports.Store = require './store'exports.Scene = require './scene'exports.View = require './view' = action = namedata raw = JSONstringify actionnull2 statewssend raw
State
State needs to be intrdoces first, it's plain Object.
.userId
will be used to detect if used logined..cache
is a place for holding cache, and removed with state.ws
is also important, cumulo usesstate.ws
to push data to client
wss = port: 3000wsson 'connection' state = # states will dispatch based on userId? userId: null id: shortidgenerate cache: ws: ws userId: null page: message: 1 thread: 1 step: 5
Router
cumulo = require 'cumulo'router = cumulorouter # for debugging = consolelog stateuserIdaction # events in router are mostly actions from websocket wson 'message' data = JSONparse raw routerdispatch statedata # and we will do this in a store routerregister 'account/login'
Database
It's actually a JSON in memory, with frequently writing to disk:
cumulo = require 'cumulo' cumulodatabaseinit # how long does it saves to disk duration: 100000 dbPath: pathjoin __dirname'../data/database.json' initialData: # JSON object : -> # collect data before saving : # get JSON data, and dispatch to stores
State
And example of state, actually it's states of all websockets.
First you need to define a file named states
:
cumulo = require 'cumulo'lodash = require 'lodash' router = cumulorouterstates = module.exports = states routerregister 'state/morePage' # it's also a dispatcher listened by scenes statesdispatch state
and with .record
.unrecord
to keep track of all states:
statesrecord stateconsoleinfo 'state record'stateidwson 'close'-> statesunrecord state consoleinfo 'state unrecord'stateid state = null
Store
cumulo = require 'cumulo'router = cumulorouter # initial data will be used in patching # and it's mostly [] or {} store = data: # store responds to router, and it's a dispatcher listend by scene too routerregister 'profile/add' storedatapush data storedispatch
Scene
cumulo = require 'cumulo' states = require '../states'client = require '../view/client' module.exports = # default value data: # duration of the looper to check .changed duration: 400 : -> # define how to notify all clients stateseach clientpatch stateworld: @get : -> # when store changes, mark this scene changed messagesStoreregister => @changed = yes # when state changes, call view patching statesregister clientpatch stateworld: @get : -> # returns an object representing the whole world
View
View represents a single user's perspective of a scene:
cumulo = require 'cumulo'lodash = require 'lodash' module.exports = # a name to caching data in state.cache cacheName: 'client' # here we use comulo.push to send data : cumulopush state'client/sync'data : cumulopush state'client/patch'data # renderer when state.userId is null : -> # JSON data, will be diffed # renderer when state.userId is defined, meaning loginged : # JSON data, will be diffed
See chat-distract for Details.
License
MIT