API
AutoImmutable Instance
is a source object holding the immutable data.
exposes its underlying data through its
connect(...)
method.provides a
close(...)
method which can be called before deallocating the instance to immediately release longer-lived connections; Otherwise, this task is delegated to the GC.also offers interactivity with its
exit
process through its onClose(...)
method. Handlers registered by this method are invokes during the close(...)
method call.communicates its availability through its
closed
property.Example:
1
2
3
4
5
6
7
import AutoImmutable from 'auto-immutable';
const ProtectedData : {} = {};
const aImmutable = new AutoImmutable( ProtectedData ); // creates an AutoImmutable instance bearing the ProtectedData
aImmutable.connect(); // returns a consumer for this Immutable instance
An AutoImmutable instance, once closed, remains permanently unavailable.
All operations on a closed AutoImmutable instance is a
noop
.When in doubt, please check its
closed
property.Connection Instance
is a consumer for an
AutoImmutable
instance.exposes
get(...)
, set(...)
and disconnect(...)
methods for interaction with its AutoImmutable instance.its
disconnect(...)
method can be called before deallocating this connection to immediately release longer-lived resources held by it; Otherwise, this task is delegated to the GC.communicates its availability through its
disconnected
property.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
import AutoImmutable from 'auto-immutable';
const aImmutable = new AutoImmutable({
a: {
b: {
c: 22
d: 60
},
x: 99
}
});
const consumer = aImmutable.connect(); // returns a consumer for this AutoImmutable instance
consumer.get( 'a.b.d', 'a.x' );
// returns {
// 'a.b.d': 60,
// 'a.x': 99
// }
consumer.set({
a: {
b: {
d: {
m: 70
},
g: 506
}
}
});
// updates immutable data to:
// {
// a: {
// b: {
// c: 22
// d: {
// m: 70
// }
// g: 506
// },
// x: 99
// }
// }
consumer.get( 'a.b.d', 'a.x' );
// returns {
// 'a.b.d': { m: 70 },
// 'a.x': 99
// }
console.log( consumer.instanceId ); // prints this consumer's unique id
console.log( consumer.disconnected ); // prints `false`
consumer.disconnect(); // severs connection to the AutoImmutable instance.
console.log( consumer.disconnected ); // prints `true`
consumer.get( 'a.b.d', 'a.x' ); // returns `undefined`
consumer.set({ a: { x: 1000 } }); // cannot update the immutable data
/* ----------------------------------------------------- */
const consumer1 = aImmutable.connect();
consumer1.get( 'a.b.d', 'a.x' );
// returns {
// 'a.b.d': { m: 70 },
// 'a.x': 99
// }
console.log( consumer.disconnected ); // prints `false`
Similar to the AutoImmutable instance
Connection, once disconnected, loses all access to its AutoImmutable instance permanently.
To regain access, simply obtain a fresh connection from the Immutable instance.
All operations on a disconnected connection is a
noop
.When in doubt, please check its
disconnected
property.