Disconnecting from Source
Any AutoImmutable instance communicates with its environment through its own Connection class
(the consumer) instances.
The Connection's disconnect(...)
parameterless method of the Connection
instance provides the means for disconnecting itself from its AutoImmutable instance.
This method also prompts the source AutoImmutable instance to deallocate all resources dedicated to the disconnecting consumer.
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
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
// }
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`
Further Considerations
This method serves as the final association between a consumer and its source AutoImmutable instance.
Once issued, the instant consumer loses accessibility to its source AutoImmutable instance.
Note: calling an AutoImmutable close(...)
method summarily disconnects all its connected consumers. This instant consumer, if connected to the given AutoImmutable instance, will be disconnected as well.
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
import AutoImmutable from 'auto-immutable';
const protectedData = {
a: {
b: {
c: 22
d: 60
},
x: 99
}
};
type Data = typeof protectedData;
const aImmutable = new AutoImmutable<ProtectedData>( protectedData );
console.log( aImmutable.closed )
// prints `false`;
const consumers : Array<Connection<ProtectedData>> = [];
for( let c = 5; c--; ) {
consumers.push( aImmutable.connect();
}
consumers.forEach( c => console.log( c.disconnected ) );
// prints `false` 5 times
consumers.forEach( c => console.log( c.get( 'a.x' ) ) );
// prints `{'a.x': 99}` 5 times;
aImmutable.close();
console.log( aImmutable.closed )
// prints `true`;
consumers.forEach( c => console.log( c.disconnected ) );
// prints `true` 5 times
consumers.forEach( c => console.log( c.get( 'a.x' ) ) );
// prints `undefined` 5 times;