@@SET Tag Usage
Signature 1:{ '@@SET': <any> }
{ '@@SET': ( currentValue: <any> ) => <any> }
This tag is mainly for handling edge cases.
Please use sparingly. In most cases, calling the consumer.set(...) method with or without any of the other tags is sufficient and most efficient.
This and the '@@REPLACE' tag are functionally equivalent when used with a replacement value argument.
Be aware that the compute function argument may be
undefined
for properties which do not yet exist in the AutoImmutable data.Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import AutoImmutable, { Tag } from 'auto-immutable';
const protectedData = {
a: {
b: [
{ x: 7, y: 8, z: 9 },
{ x: 17, y: 18, z: 19 }
]
},
j: 10
};
const aImmutable = new AutoImmutable( protectedData );
const consumer = aImmutable.connect();
/* rewrites aImmutable data to { ...aImmutable data, a: 'Demo', j: 17 } */
consumer.set({
[ Tag.SET ]: currentValue => ({
...currentValue,
a: 'Demo',
j: 17
})
});
/* rewrites aImmutable data.a to { ...aImmutable data, message: 'Testing...' } */
consumer.set({
a: {
[ Tag.SET ]: currentValue => ({
...currentValue,
message: 'Testing...'
})
}
});
// rewrites aImmutable data.a.b[1] to { x: 97, y: 98, z: 99 };
// leaving aImmutable data.a.b = [
// { x: 7, y: 8, z: 9 },
// { x: 97, y: 98, z: 99 }
// ]
consumer.set({
a: {
b: [
aImmutable data.a.b[ 0 ],
{
[ Tag.SET ]: currentValue => ({
...currentValue,
x: 97,
y: 98,
z: 99
})
}
]
}
});
// rewrites aImmutable data.a.b[1] to { x: 97, y: 98, z: 99 };
// leaving aImmutable data.a.b = [
// { x: 7, y: 8, z: 9 },
// { x: 97, y: 98, z: 99 }
// ]
// uses indexing (RECOMMENDED)
consumer.set({
a: {
b: {
1: {
[ Tag.SET ]: currentValue => ({
...currentValue,
x: 97,
y: 98,
z: 99
})
}
}
}
});