xcraft-core-shredder

5.3.1 • Public • Published

Xcraft Shredder

State management

In a goblin, the state is always a shredder:

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:

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:

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.

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])

const nameOfId1 = state.get('id1.name', null);

Delete property value

state.del (path)

If we want delete the full state:

const logicHandlers = {
  delete: (state) => {
    return state.del ('');
  },
  ...
};

If we want delete an entry:

const logicHandlers = {
  removeById: (state, action) => {
    const id = action.get ('id');
    return state.del (id);
  },
  ...
};

Using shredder in a widget (React side)

this.shred (data)

const person = this.shred(this.props.person);

Package Sidebar

Install

npm i xcraft-core-shredder

Weekly Downloads

30

Version

5.3.1

License

MIT

Unpacked Size

25.7 kB

Total Files

10

Last publish

Collaborators

  • yvessaz
  • nocturnight
  • quarta
  • samlebarbare
  • schroeterm