channel.setState @@SPLICE Tag Usage
Note: The following is also wholly applicable to global store.setState
channel.setState({
stateKey0: { // where `state.stateKey0` is an array
'@@SPLICE': [ // performs array splice on `state.stateKey0`.
-/+fromIndex,
deleteCount, // integer >= 0
...newInserts?
]
}
});Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import { SPLICE_TAG } from '@webkrafters/eagleeye'; // SPLICE_TAG = "@@SPLICE"
const state = {
a: { b: [{ x: 7, y: 8, z: 9 }, { x: 17, y: 18, z: 19 }] },
j: 10,
q: [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
};
channel.setState({ a: { [ SPLICE_TAG ]: [ 0, 1 ] } }); // assigning a '@@SPLICE' command to a non-array property has no effect.
/* removes state.a.b[0]; leaving state.a.b = [{ x: 17, y: 18, z: 19 }] */
channel.setState({ a: { b: { [ SPLICE_TAG ]: [ 0, 1 ] } } }) // or channel.setState({ a: { b: { [ SPLICE_TAG ]: [ -2, -1 ] } } });
/* replaces state.q[4] - [7] with 2 items; leaving state.q = [ 1, 2, 3, 4, 33, 88, 9 ] */
channel.setState({ a: { q: { [ SPLICE_TAG ]: [ 4, 4, 33, 88 ] } } }) // or channel.setState({ a: { q: { [ SPLICE_TAG ]: [ -5, 4, 33, 88 ] } } });