├── .travis.yml
├── .babelrc
├── .gitignore
├── radon
├── constants.js
├── virtualNode.js
├── constructorNode.js
├── combineNodes.js
└── siloNode.js
├── index.js
├── rollup.config.js
├── .github
└── ISSUE_TEMPLATE
│ └── bug_report.md
├── LICENSE.txt
├── package.json
├── tests
└── virtualSilo.test.js
└── README.md
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 | node_js:
3 | - "node"
--------------------------------------------------------------------------------
/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "presets": [
3 | "@babel/preset-env"
4 | ],
5 | "plugins": []
6 | }
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .vscode/
2 | node_modules
3 | npm-debug.log
4 | .DS_Store
5 | package-lock.json
6 | .github
--------------------------------------------------------------------------------
/radon/constants.js:
--------------------------------------------------------------------------------
1 | export const ARRAY = 'ARRAY';
2 | export const OBJECT = 'OBJECT';
3 | export const PRIMITIVE = 'PRIMITIVE';
4 | export const CONTAINER = 'CONTAINER';
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | import '@babel/polyfill';
2 | import combineNodes from './radon/combineNodes';
3 | import ConstructorNode from './radon/constructorNode';
4 |
5 | export const combineState = combineNodes;
6 | export const StateNode = ConstructorNode;
--------------------------------------------------------------------------------
/rollup.config.js:
--------------------------------------------------------------------------------
1 | import babel from 'rollup-plugin-babel';
2 | import { uglify } from 'rollup-plugin-uglify';
3 |
4 | export default {
5 | input: 'index.js',
6 | output: {
7 | file: 'build/bundle.js',
8 | format: 'cjs',
9 | sourcemap: 'inline',
10 | },
11 | plugins: [
12 | babel({
13 | exclude: 'node_modules/**',
14 | }),
15 | uglify()
16 | ],
17 | };
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/bug_report.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Bug report
3 | about: Create a report to help us improve
4 |
5 | ---
6 |
7 | **Describe the bug**
8 | A clear and concise description of what the bug is.
9 |
10 | **To Reproduce**
11 | Steps to reproduce the behavior:
12 | 1. Go to '...'
13 | 2. Click on '....'
14 | 3. Scroll down to '....'
15 | 4. See error
16 |
17 | **Expected behavior**
18 | A clear and concise description of what you expected to happen.
19 |
20 | **Screenshots**
21 | If applicable, add screenshots to help explain your problem.
22 |
23 | **Desktop (please complete the following information):**
24 | - OS: [e.g. iOS]
25 | - Browser [e.g. chrome, safari]
26 | - Version [e.g. 22]
27 |
28 | **Smartphone (please complete the following information):**
29 | - Device: [e.g. iPhone6]
30 | - OS: [e.g. iOS8.1]
31 | - Browser [e.g. stock browser, safari]
32 | - Version [e.g. 22]
33 |
34 | **Additional context**
35 | Add any other context about the problem here.
36 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) [2018] [Hannah Mitchell, Hayden Fithyan, Joshua Wright, Nicholas Smith]
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "radon-js",
3 | "version": "3.2.22",
4 | "description": "Radon is an object-oriented state management framework for JavaScript applications.",
5 | "main": "build/bundle.js",
6 | "files": [
7 | "build"
8 | ],
9 | "scripts": {
10 | "test": "jest",
11 | "test:siloNode": "jest siloNode.test.js",
12 | "watch": "jest constructorNode.test.js --watch",
13 | "test:virtualSilo": "jest virtualSilo.test.js --watch",
14 | "build": "rollup -c"
15 | },
16 | "keywords": [
17 | "npm",
18 | "state",
19 | "management",
20 | "oop",
21 | "object",
22 | "oriented"
23 | ],
24 | "repository": {
25 | "type": "git",
26 | "url": "git+https://github.com/radon-js/Radon.git"
27 | },
28 | "author": "Hannah Mitchell, Hayden Fithyan, Joshua Wright, Nicholas Smith",
29 | "license": "MIT",
30 | "bugs": {
31 | "url": "https://github.com/Joshua-Wright76/Radon/issues"
32 | },
33 | "homepage": "https://github.com/Joshua-Wright76/Radon#readme",
34 | "dependencies": {
35 | "@babel/polyfill": "^7.0.0"
36 | },
37 | "devDependencies": {
38 | "@babel/core": "^7.1.2",
39 | "@babel/preset-env": "^7.1.0",
40 | "babel-core": "^7.0.0-bridge.0",
41 | "babel-preset-es2015-rollup": "^3.0.0",
42 | "jest": "^23.6.0",
43 | "jest-cli": "^23.6.0",
44 | "rollup": "^0.66.2",
45 | "rollup-plugin-babel": "^4.0.3",
46 | "rollup-plugin-uglify": "^6.0.0"
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/tests/virtualSilo.test.js:
--------------------------------------------------------------------------------
1 | import ConstructorNode from '../radon/constructorNode';
2 | import combineNodes from '../radon/combineNodes';
3 |
4 | describe('Initialize State', () => {
5 | const PersonState = new ConstructorNode('PersonState', 'ColorState');
6 | PersonState.initializeState({
7 | name: ["Michael", "Laythe"],
8 | age: {19: 19}
9 | })
10 |
11 | const ColorState = new ConstructorNode('ColorState');
12 | ColorState.initializeState({
13 | red: ['crimson', 'ruby', 'scarlet', 'cherry'],
14 | blue: ['sapphire', 'navy', 'sky', 'baby'],
15 | green: ['chartreuse', 'ivy', 'teal', 'emerald']
16 | })
17 |
18 | ColorState.initializeModifiers({
19 | blue: {
20 | test: (payload, previous) => {
21 | console.log('running');
22 | }
23 | }
24 | });
25 |
26 | PersonState.initializeModifiers({
27 | age: {
28 | haveBirthday: (payload, previous) => {
29 | return previous + 1;
30 | }
31 | }
32 | })
33 |
34 | let silo = combineNodes(PersonState, ColorState/*BlimpState*/);
35 |
36 | test('ID\'s should represent the lineage of the node in the silo', () => {
37 | expect(silo['ColorState'].value['red'].value['red_2'].id).toBe('ColorState.red.red_2')
38 | expect(silo['ColorState'].value['blue'].value['blue_0'].id).toBe('ColorState.blue.blue_0')
39 | expect(silo['ColorState'].value['PersonState'].value['name'].id).toBe('ColorState.PersonState.name')
40 | })
41 | })
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/radon/virtualNode.js:
--------------------------------------------------------------------------------
1 | import * as types from './constants'
2 |
3 | class VirtualNode {
4 | constructor (node, modifiers) {
5 | this.parent = null;
6 | this.parents = {};
7 | if (node.parent){
8 |
9 | this.parent = node.parent.virtualNode;
10 | this.parents[this.parent.name] = this.parent;
11 | let ancestor = this.parent;
12 |
13 | while(ancestor.parent !== null){
14 | ancestor = ancestor.parent;
15 | this.parents[ancestor.name] = ancestor;
16 | }
17 | }
18 |
19 | this.name = node.name;
20 | this.type = node.type;
21 | this.id = node.id;
22 |
23 | if (this.type === types.PRIMITIVE){
24 | //value should just be an empty object.
25 | //when your children are being made
26 | //they'll just put themselves into your value.
27 | this.val = node.value;
28 | } else {
29 | this.val = {};
30 | if(this.type === types.ARRAY){ this.val = [] }
31 | }
32 |
33 | if (node.type !== types.CONTAINER){
34 | let name = node.name;
35 | if(name.includes('_')) name = name.split('_')[name.split('_').length - 1];
36 |
37 | node.parent.virtualNode.val[name] = this;
38 | if(this.parent.type === types.CONTAINER){
39 | this.parent[name] = this;
40 | }
41 | }
42 |
43 | if (node.modifiers){
44 | let modifierKeys = Object.keys(modifiers);
45 | modifierKeys.forEach(modifierKey => {
46 | this[modifierKey] = modifiers[modifierKey];
47 | })
48 | }
49 |
50 | }
51 | updateTo(data){
52 | this.val = data;
53 | }
54 | }
55 |
56 | export default VirtualNode;
57 |
--------------------------------------------------------------------------------
/radon/constructorNode.js:
--------------------------------------------------------------------------------
1 | class ConstructorNode {
2 | constructor(name, parentName = null) {
3 | this.name = name;
4 | this.state = {};
5 | this.parent = parentName;
6 |
7 | this.initializeState = this.initializeState.bind(this);
8 | this.initializeModifiers = this.initializeModifiers.bind(this);
9 | }
10 |
11 | /**
12 | * Adds variables to the state
13 | * @param {object} initialState - An object with keys as variable names and values of data
14 | */
15 |
16 | initializeState(initialState) {
17 | // make sure that the input is an object
18 | if (typeof initialState !== 'object' || Array.isArray(initialState)) throw new Error('Input must be an object');
19 | // loop through the state variables and create objects that hold the variable and any
20 | // associated modifiers
21 | Object.keys(initialState).forEach(newVariableInState => {
22 | this.state[newVariableInState] = {
23 | value: initialState[newVariableInState],
24 | // accounts for initializeModifers being called prior to initializeState
25 | // by checking to see if this object has already been created
26 | modifiers: this.state[newVariableInState] ? this.state[newVariableInState].modifiers : {}
27 | }
28 | });
29 | }
30 |
31 | /**
32 | * Stores modifiers in state
33 | * @param {object} initialModifiers - An object with keys associated with existing initialized variables and values that are objects containing modifiers to be bound to that specific variable
34 | */
35 |
36 | initializeModifiers(initialModifiers) {
37 | // make sure that the input is an object
38 | if (typeof initialModifiers !== 'object' || Array.isArray(initialModifiers)) throw new Error('Input must be an object');
39 | // loops through the state modifiers. The same object is created here as in initializeState and it
40 | // will overwrite the initializeState object. But it needs to be done this way in case the dev calls
41 | // initializeModifiers before they call initializeState. Now it works either way
42 | Object.keys(initialModifiers).forEach(newModifiersInState => {
43 | this.state[newModifiersInState] = {
44 | // accounts for initializeState being called prior to initializeModifiers.
45 | value: this.state[newModifiersInState] ? this.state[newModifiersInState].value : null,
46 | modifiers: initialModifiers[newModifiersInState]
47 | }
48 | });
49 | }
50 |
51 | set name(name) {
52 | if (typeof name !== 'string') throw new Error('Name must be a string');
53 | else this._name = name;
54 | }
55 |
56 | get name() {
57 | return this._name;
58 | }
59 |
60 | set parent(parent) {
61 | if (typeof parent !== 'string' && parent !== null) throw new Error('Parent must be a string');
62 | else this._parent = parent;
63 | }
64 |
65 | get parent() {
66 | return this._parent;
67 | }
68 |
69 | set state(state) {
70 | this._state = state;
71 | }
72 |
73 | get state() {
74 | return this._state;
75 | }
76 | }
77 |
78 | export default ConstructorNode;
--------------------------------------------------------------------------------
/radon/combineNodes.js:
--------------------------------------------------------------------------------
1 | // import state class for instanceof check
2 | import ConstructorNode from './constructorNode.js';
3 | import SiloNode from './siloNode.js';
4 | import * as types from './constants.js'
5 | import virtualNode from './virtualNode.js'
6 |
7 | const silo = {};
8 | const virtualSilo = {};
9 |
10 | /**
11 | * Takes all of the constructorNodes created by the developer and turns them into the silo
12 | * @param {...ConstructorNode} args - A list of constructor Nodes
13 | */
14 |
15 | function combineNodes(...args) {
16 | let devTool = null;
17 | if(args[0] && args[0].devTool === true) {
18 | devTool = args[0];
19 | args.shift();
20 | }
21 | if (args.length === 0) throw new Error('combineNodes function takes at least one constructorNode');
22 |
23 | // hastable accounts for passing in constructorNodes in any order.
24 | // hashtable organizes all nodes into parent-child relationships so the silo is easier to create
25 | const hashTable = {};
26 |
27 | // loop through the constructorNodes passed in as arguments
28 | args.forEach(constructorNode => {
29 | if (!constructorNode || constructorNode.constructor.name !== 'ConstructorNode') throw new Error('Only constructorNodes can be passed to combineNodes');
30 | // a node with a null parent will be the root node, and there can only be one
31 | else if (constructorNode.parent === null) {
32 | // we check to see if the root key already exists in the hashtable. If so, this means a root
33 | // has already been established
34 | if (!hashTable.root) hashTable.root = [constructorNode];
35 | else throw new Error('Only one constructor node can have null parent');
36 | }
37 | // if the parent isn't null, then the parent is another node
38 | else {
39 | // if the parent doesn't exist as a key yet, we will create the key and set it to an array
40 | // that can be filled with all possible children
41 | if (!hashTable[constructorNode.parent]) hashTable[constructorNode.parent] = [constructorNode];
42 | // if parent already exists, and node being added will append to the array of children
43 | else hashTable[constructorNode.parent].push(constructorNode);
44 | }
45 | })
46 |
47 | // ensure there is a defined root before continuing
48 | if (!hashTable.root) throw new Error('At least one constructor node must have a null parent');
49 |
50 | // a recursive function that will create siloNodes and return them to a parent
51 | function mapToSilo(constructorNode = 'root', parentConstructorNode = null) {
52 | // the very first pass will set the parent to root
53 | const constructorNodeName = (constructorNode === 'root') ? 'root' : constructorNode.name;
54 |
55 | // recursive base case, we only continue if the current node has any constructorNode children
56 | if (!hashTable[constructorNodeName]) return;
57 |
58 | const children = {};
59 |
60 | // loop through the children arrays in the hashtable
61 | hashTable[constructorNodeName].forEach(currConstructorNode => {
62 | const valuesOfCurrSiloNode = {};
63 | children[currConstructorNode.name] = new SiloNode(currConstructorNode.name, valuesOfCurrSiloNode, parentConstructorNode, {}, types.CONTAINER, devTool);
64 |
65 | // abstract some variables
66 | const currSiloNode = children[currConstructorNode.name];
67 | const stateOfCurrConstructorNode = currConstructorNode.state;
68 |
69 | // create SiloNodes for all the variables in the currConstructorNode
70 | Object.keys(stateOfCurrConstructorNode).forEach(varInConstructorNodeState => {
71 | // is the variable is an object/array, we need to deconstruct it into further siloNodes
72 | if (typeof stateOfCurrConstructorNode[varInConstructorNodeState].value === 'object') {
73 | valuesOfCurrSiloNode[varInConstructorNodeState] = currSiloNode.deconstructObjectIntoSiloNodes(varInConstructorNodeState, stateOfCurrConstructorNode[varInConstructorNodeState], currSiloNode, true);
74 | }
75 | // otherwise primitives can be stored in siloNodes and the modifiers run
76 | else {
77 | valuesOfCurrSiloNode[varInConstructorNodeState] = new SiloNode(varInConstructorNodeState, stateOfCurrConstructorNode[varInConstructorNodeState].value, currSiloNode, stateOfCurrConstructorNode[varInConstructorNodeState].modifiers, types.PRIMITIVE, devTool);
78 | valuesOfCurrSiloNode[varInConstructorNodeState].linkModifiers();
79 | }
80 | })
81 |
82 | // recursively check to see if the current constructorNode/siloNode has any children
83 | const siloNodeChildren = mapToSilo(currConstructorNode, currSiloNode);
84 | // if a Node did have children, we will add those returned siloNodes as values
85 | // into the current siloNode
86 | if (siloNodeChildren) {
87 | Object.keys(siloNodeChildren).forEach(siloNode => {
88 | valuesOfCurrSiloNode[siloNode] = siloNodeChildren[siloNode];
89 | })
90 | }
91 | })
92 | return children;
93 | }
94 |
95 | // here we will get the root siloNode with all its children added
96 | const wrappedRootSiloNode = mapToSilo();
97 |
98 | // add the siloNode root to the plain silo object
99 | // it will always only be a single key (the root) that is added into the silo
100 | Object.keys(wrappedRootSiloNode).forEach(rootSiloNode => {
101 | silo[rootSiloNode] = wrappedRootSiloNode[rootSiloNode];
102 | });
103 |
104 | function identify () {
105 | //each node's ID is a snake_case string that represents a
106 | //route to that node from the top of the silo by name
107 | forEachSiloNode(node => {
108 | node.issueID()
109 | });
110 | }
111 |
112 | identify();
113 |
114 | function virtualize () { //runs through each node in the tree, turns it into a virtual node in the vSilo
115 | forEachSiloNode(node => {
116 | if(!virtualSilo[node.id]){
117 | virtualSilo[node.id] = node.virtualNode;
118 | }
119 | })
120 | }
121 |
122 | virtualize();
123 |
124 | forEachSiloNode(node => {
125 | // apply keySubscribe only to object and array silo nodes
126 | if (node.type === 'OBJECT' || node.type === "ARRAY") {
127 | node.modifiers.keySubscribe = (key, renderFunc) => {
128 | const name = node.name + "_" + key;
129 | const subscribedAtIndex = node.value[name].pushToSubscribers(renderFunc);
130 | node.value[name].notifySubscribers();
131 | return () => {node.removeFromSubscribersAtIndex(subscribedAtIndex)}
132 | }
133 | }})
134 |
135 | silo.virtualSilo = virtualSilo;
136 | return silo;
137 | }
138 |
139 | /**
140 | * Applies the callback to every siloNode in the silo
141 | * @param {function} callback - A function that accepts a siloNode as its parameter
142 | */
143 |
144 | // callbacks have to accept a SILONODE
145 | function forEachSiloNode(callback) {
146 | // accessing the single root in the silo
147 | Object.keys(silo).forEach(siloNodeRootKey => {
148 | inner(silo[siloNodeRootKey], callback);
149 | })
150 |
151 | // recursively navigate to every siloNode
152 | function inner(head, callback) {
153 | if (head.constructor.name === 'SiloNode') {
154 | callback(head);
155 | if (head.type === types.PRIMITIVE) return; // recursive base case
156 |
157 | else {
158 | Object.keys(head.value).forEach(key => {
159 | if (head.value[key].constructor.name === 'SiloNode') {
160 | inner(head.value[key], callback);
161 | }
162 | })
163 | }
164 | }
165 | }
166 | }
167 |
168 | /**
169 | * Subscribes components to siloNodes in the silo
170 | * @param {function} renderFunction - Function to be appended to subscribers array
171 | * @param {string} name - Name of the relevant component with 'State' appended
172 | */
173 |
174 | silo.subscribe = (renderFunction, name) => {
175 | if (!name) {
176 | if (!!renderFunction.prototype) {
177 | name = renderFunction.prototype.constructor.name + 'State';
178 | } else {
179 | throw new Error('You can\'t use an anonymous function in subscribe without a name argument.');
180 | }
181 | }
182 |
183 | let foundNode;
184 | let subscribedAtIndex;
185 | const foundNodeChildren = [];
186 |
187 | forEachSiloNode(node => {
188 | if(node.name === name){
189 | subscribedAtIndex = node.pushToSubscribers(renderFunction)
190 | foundNode = node
191 | foundNodeChildren.push({node: foundNode, index: subscribedAtIndex});
192 | }
193 | })
194 |
195 | let unsubscribe;
196 |
197 | if (!!foundNode) {
198 | if (foundNode.value) {
199 | Object.keys(foundNode.value).forEach(key => {
200 | let node = foundNode.value[key];
201 | if(node.type !== 'CONTAINER'){
202 | subscribedAtIndex = node.pushToSubscribers(renderFunction);
203 | foundNodeChildren.push({node: node, index: subscribedAtIndex});
204 |
205 | }
206 | })
207 | }
208 |
209 | unsubscribe = () => {
210 | let ob;
211 | Object.keys(foundNodeChildren).forEach(key => {
212 | ob = foundNodeChildren[key];
213 | ob._subscribers.splice(ob.index, 1)
214 | })
215 | }
216 |
217 | foundNode.notifySubscribers();
218 | return unsubscribe;
219 |
220 | } else {
221 | console.error(new Error('You are trying to subscribe to something that isn\'t in the silo.'));
222 | return function errFunc () {
223 | console.error(new Error('You are trying to run unsubscribe from something that wasn\'t in the silo in the first place.'))
224 | }
225 | }
226 | }
227 |
228 | export default combineNodes;
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | #
2 |
3 | [](https://travis-ci.com/radonjs/Radon) [](https://npmjs.org/package/radon-js)
4 |
5 | [Radon](http://radonjs.org) is an object-oriented state management framework for JavaScript applications.
6 |
7 | Read our documentation at [radonjs.org](http://radonjs.org/docs/introduction)
8 |
9 | # Why?
10 |
11 | ## Data Encapsulation
12 |
13 | One of the first goals of Radon was to implement an object oriented state manager capable of data encapsulation. Many state managers allow pieces of state to be accessible by any component or module, and with that access follows modification allowances. This inherently conflicts with a ubiquitous object oriented programming practice: limiting scope. Limiting the scope of a variable or method provides better context for its purpose and makes it easier to reason about. Plus, there's the added bonus of protecting data from being modified by script that has several degrees of separation. Many programming languages have native features to handle data encapsulation such as privatized class attributes and methods. Unfortunately, Javascript doesn't have the same privatization features held by universal languages such as Java and C/C++. Therefore, the data encapsulation feature of Radon needed to be derived by other means.
14 |
15 | To understand encapsulation in Radon, it is first important to understand how the data is organized. Radon is built using a tree data structure. Pieces of state are stored in specially designed nodes and are organized in a way that parallels the component tree of many common frontend frameworks such as React or Vue. For example, if a developer created an initial App component that needed access to variables in state, a corresponding AppState node would be created that contained those specific variables and any accompanying modifier functions. Now let's say the App component renders two more components named Navbar and Main. If Navbar were to be stateful, it would need a corresponding node called NavbarState. If the same thing can be said for Main, then it would have a corresponding state node called MainState. If a frontend component is intended to be stateless, then there will be no corresponding state node. So now we can hopefully start to imagine that the App Component is at the top of a component tree (as the root), with NavbarState and MainState branching beneath it. The same can be said for the State Tree. AppState is our root, with NavbarState and MainState branching below.
16 |
17 | But what does this mean for data encapsulation? The intention for the State Tree is for state nodes to share their data and modifiers with corresponding frontend components. However, this implementation alone would be too constricting of the data. Therefore, frontend components are not only able to access the data from their corresponding state nodes, but also the data from its parent, grandparent, and any further parent tracing back to the root. Now there's a greater sense of flow that encourages commonly used and shared data to be stored near the root, and specialized data to be stored as leaves on the tree. In sum, frontend components will have access to parental lineage data, but will not have access to their sibling’s or children's data. Thus, varying pieces of state are exposed where they are needed, and hidden where they are not.
18 |
19 | ## Component Rendering Linked to Objects in State
20 |
21 | Another feature of Radon intends to remove unnecessary re-rendering that can emerge from modifying objects in state. In other state management systems, modifying a single key/value pair in a plain object or an index in an array will result in a re-render of any component subscribed to the object. The Radon State Tree solves this problem by deconstructing objects into state nodes by index or key/value pairs. The object deconstruction feature allows for direct modification of these indices/pairs and triggers a re-render of only the component listening to that particular data point.
22 |
23 | ## Asynchronous Modifications to State
24 |
25 | Modifiers are functions written by the developer that can only modify a single state variable. Developers have the option to create an asynchronous modifier which may seem problematic if multiple modifiers are called in tandem to edit the same piece of state. However, Radon ensures that all state changes, whether asynchronous or synchronous, occur in the order of initial invocation. This is accomplished with an asynchronous queue that awaits the completion of the most recently invoked modifier before progressing to the next. Hence, the developer does not need to worry about conflicting state changes or out of order updates.
26 |
27 | # Getting Started
28 |
29 | To install the stable version using npm as your package manager:
30 |
31 | ```npm install --save radon-js```
32 |
33 | The Radon source code is transpiled to ES2015 to work in any modern browser. You don't need to use Babel or a module bundler to get started with Radon.
34 |
35 | Most likely, you'll also need the React bindings and the developer tools.
36 |
37 | ```npm install --save react-radon```
38 |
39 | Unlike Radon, React doesn't provide UMD builds, so you will need to use a CommonJS module bundler like Webpack, Parcel, or Rollup to utilize Radon with React.
40 |
41 | ## How Radon Works
42 |
43 | ```javascript
44 | import { StateNode } from 'radon-js'
45 |
46 | /*
47 | StateNode is a class needed for creating instances of state. In Radon, StateNodes are created in
48 | tandem with frontend components. The naming convention is important here; if you have created
49 | a frontend component called App with the intent of statefulness, then an instance of StateNode must be
50 | declared and labeled as AppState. This will allow the App component to properly bind to AppState
51 | at compile time.
52 |
53 |
54 | The new instance of StateNode takes two arguments: the first argument is the name of the StateNode you
55 | are creating which must follow our naming convention. The second argument is the name of the parent
56 | node. One StateNode must be considered the root of the state tree. Therefore, at only one occasion can
57 | the parent argument be omitted. This instance of StateNode will be considered the root. Every other
58 | StateNode must take a parent argument.
59 | */
60 |
61 | const AppState = new StateNode('AppState');
62 | // or
63 | // const AppState = new StateNode('AppState', 'OtherState');
64 |
65 | /*
66 | To declare variables in state, the method initializeState must be called which takes an object
67 | as an argument. The variable names and their data should be listed in the object as key-value pairs.
68 | */
69 |
70 | AppState.initializeState({
71 | name: 'Radon',
72 | status: true,
73 | arrayOfNames: []
74 | })
75 |
76 | /*
77 | Modifiers are functions that modify a single variable in state. Modifiers are attached to variables by
78 | calling the method initializeModifiers which also takes an object as an argument. The keys of the
79 | argument object must correspond to variables that have already been declared in AppState. The values
80 | are objects that contain the modifier functions as key-value pairs. There are two types of modifiers
81 | in Radon. The first type, as seen below, can accept either 1 or 2 arguments. The 'current' argument
82 | will automatically be injected with the bound state variable. The 'payload' argument is any data that
83 | can be used to modify or replace the 'current' value of state. Even if the current value of state is
84 | not used in the modifier, it will still be passed in automatically.
85 | */
86 |
87 | AppState.initializeModifiers({
88 | name: {
89 | updateName: (current, payload) => {
90 | return payload;
91 | }
92 | },
93 | status: {
94 | toggleStatus: (current) => {
95 | return !current;
96 | }
97 | }
98 | })
99 |
100 | /*
101 | It is important to note that when these modifiers are called from a component, only the payload argument
102 | must be passed into the function as Radon will fill the 'current' parameter by default.
103 | */
104 |
105 |
106 |
107 |
108 | /*
109 | The second modifier type is what helps Radon eliminate unnecessary re-rendering of frontend components.
110 | This modifier type accepts three arguments and is used exclusively with objects. *Note that
111 | initializeModifiers should only be called once. It is shown again here for demonstration purposes only*.
112 | */
113 |
114 | AppState.initializeModifiers({
115 | arrayOfNames: {
116 | addNameToArray: (current, payload) => {
117 | current.push(payload);
118 | return current;
119 | },
120 | updateAName: (current, index, payload) => {
121 | return payload;
122 | }
123 | }
124 | })
125 |
126 | /*
127 | The modifier addNumberToArray is nothing new. Since the goal of the modifier is to edit the array as a
128 | whole, the entire array object is passed into the 'current' parameter. A modifier that edits the array
129 | will cause a re-render of any component that subscribes to the array. However, we may have
130 | circumstances in which we only want to edit a single index within an array. In this case we create a
131 | modifier that accepts an index. The 'current' value will always reflect arrayOfNumbers[index]. This
132 | will prevent a re-render of components listening to the entire array, and will instead only re-render
133 | components listening to the specified index.
134 |
135 | Again, it is important to note that the 'current' parameter will be injected with state automatically.
136 | */
137 |
138 |
139 |
140 | /*
141 | The same logic applies to plain objects. Instead of passing a numerical index into a modifier, the key
142 | of a key-value pair can be passed in instead.
143 |
144 | Objects can be nested and it is possible to create modifiers for deeply nested objects. Ultimately, the
145 | modifier will always be bound to the parent object. However, the key/index parameter will transform into
146 | a longer chain of 'addresses' to tell Radon exactly where the data is stored. For example:
147 | */
148 |
149 | names: {
150 | first: ['', 'Beth', 'Lisa'],
151 | last: {
152 | birth: ['Mitchell', 'Sanchez', 'Delaney'],
153 | married: ['Mitchell', 'Smith', 'Delaney']
154 | }
155 | }
156 |
157 | /*
158 | To inject the name 'Hannah' into index 0 of the 'first' array, the specified 'address' would be first_0.
159 | To change the value of index 2 of the 'married' array, the specified 'address' would be last_married_2.
160 | */
161 |
162 | /*
163 | Once all StateNodes have been declared, they should be combined in the function combineStateNodes. The
164 | returned object is known as the silo.
165 | */
166 |
167 | import AppState from './appState';
168 | import NarbarState from './navbarState';
169 | import mainState from './mainState';
170 |
171 | const silo = combineStateNodes(AppState, NavbarState, MainState);
172 |
173 | ```
174 |
175 |
176 | ### Bind the state
177 | In order to use the **Silo** state in a component, it must be passed to the same from the top of the application.
178 | That depends on the framework binding.
179 | Below you can find a working example of use of **Radon** on **React** via [react-radon](https://github.com/radonjs/React-Radon), the react binding for this library, as an example:
180 |
181 | ```javascript
182 | import {render} from 'react-dom';
183 | import {Provider} from 'react-radon';
184 |
185 | // Silo from Exported combineNodes from the example before
186 | import silo from './localSiloLocation';
187 |
188 | render(
189 |