https://github.com/xcraft-inc/xcraft-core-shredder
Xcraft shredder
https://github.com/xcraft-inc/xcraft-core-shredder
Last synced: 4 months ago
JSON representation
Xcraft shredder
- Host: GitHub
- URL: https://github.com/xcraft-inc/xcraft-core-shredder
- Owner: Xcraft-Inc
- Created: 2017-06-01T07:31:19.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2024-09-02T14:23:57.000Z (10 months ago)
- Last Synced: 2025-01-14T05:35:14.696Z (5 months ago)
- Language: JavaScript
- Size: 120 KB
- Stars: 0
- Watchers: 6
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Xcraft Shredder
## State management
In a goblin, the `state` is always a shredder:
```js
const logicHandlers = {
type: (state, action) => {
return state;
},
};
```### Exemple state
If we manage a collection of some entity, we encourage to use entity `id` as
key:```js
const exempleCollection = {
id1: {
id: 'id1',
name: 'Shredder 1000',
version: 1,
hp: '1000CH',
},
id2: {
id: 'id2',
name: 'Shredder 2000',
version: 2,
hp: '2000CH',
},
id3: {
id: 'id3',
name: 'Mega Shredder 6000',
version: 3,
hp: '6000CH',
},
};
```### Set property value
#### state.set (path, value)
If we want set our example as state:
```js
const logicHandlers = {
create: (state) => {
return state.set ('', exempleCollection);
},
...
};
```Note that we use `''` path (empty) for indicating the root of the the state.
Then, in other handlers we can set specific properties, using `'id.property'`
path.```js
const logicHandlers = {
...
'set-name': (state, action) => {
const id = action.get ('id');
const name = action.get ('name');return state.set (`${id}.name`, name);
},
};
```Tips: use JS template for building the path.
### Get property value
#### state.get (path, [optionalfallbackValue])
```js
const nameOfId1 = state.get('id1.name', null);
```### Delete property value
#### state.del (path)
If we want delete the full state:
```js
const logicHandlers = {
delete: (state) => {
return state.del ('');
},
...
};
```If we want delete an entry:
```js
const logicHandlers = {
removeById: (state, action) => {
const id = action.get ('id');
return state.del (id);
},
...
};
```## Using shredder in a widget (React side)
#### this.shred (data)
```js
const person = this.shred(this.props.person);
```