Volatile
Volatile is a library for serial object mutation powered by es8 async/await.
By default in javascript, there is only one thread, even with async. so having a simple variable like a counter being accessed from different async threads is not a problem.
However, when working with something more compliacted, it may be beneficial to be able to lock an object's value while some operations are performed on it, then unlock it afterwards, allowing others to use it.
Api
Create Volatile Object
- Require the volatile package which returns a class.
- Create an instance of Volatile, giving it the initial value (if any) of your object.
const Volatile = ;let vObject = {};// This also works for strings, numbers, symbols, etclet vString = '';let vNumber = 4;let vSymbol = Symbol'customSymbol';
Lock the Object and perform Serial Operations
- call lock and pass it a function
- perform operations on the variable
- return back the new value
The variable is not immutable, however some types, for example number, it would be impossible to mutatue without using an object wrapper. so to make changes, the return value is used.
The side effect of this, is that if you forget to return, the variable will be set to undefined.
// despite the timeout, at no time will the variable have both// 1 and 5 accessible. all operations within a lock are guaranteed to finish// before moving on to the next lock.vObject;vObject;
toString Behavior
toString will return the value of invoking the internal variable's toString. If that isnt defined, toString will return 'no toString defined'
. You can create on by locking and adding one.
vObject;