* Locks a path for writing. While the lock is in place, it's value cannot be changed by other transactions.
* @param {string} path path being locked
* @param {string} tid a unique value to identify your transaction
* @param {boolean} forWriting if the record will be written to. Multiple read locks can be granted access at the same time if there is no write lock. Once a write lock is granted, no others can read from or write to it.
* @returns {Promise<NodeLock>} returns a promise with the lock object once it is granted. It's .release method can be used as a shortcut to .unlock(path, tid) to release the lock
DEBUG_MODE&&console.error(`${lock.forWriting ? "write" : "read"} lock ALLOWED on "${lock.path}" by tid ${lock.tid} (${lock.comment})`);
lock.state=LOCK_STATE.LOCKED;
if(typeoflock.granted==="number"){
//debug.warn(`lock :: ALLOWING ${lock.forWriting ? "write" : "read" } lock on path "/${lock.path}" by tid ${lock.tid}; ${lock.comment}`);
}
else{
lock.granted=Date.now();
if(options.noTimeout!==true){
lock.expires=Date.now()+LOCK_TIMEOUT;
//debug.warn(`lock :: GRANTED ${lock.forWriting ? "write" : "read" } lock on path "/${lock.path}" by tid ${lock.tid}; ${lock.comment}`);
lettimeoutCount=0;
consttimeoutHandler=()=>{
// Autorelease timeouts must only fire when there is something wrong in the
// executing (AceBase) code, eg an unhandled promise rejection causing a lock not
// to be released. To guard against programming errors, we will issue 3 warning
// messages before releasing the lock.
if(lock.state!==LOCK_STATE.LOCKED){return;}
timeoutCount++;
if(timeoutCount<=3){
// Warn first.
console.warn(`${lock.forWriting ? "write" : "read"} lock on path "/${lock.path}" by tid ${lock.tid} (${lock.comment}) is taking a long time to complete [${timeoutCount}]`);
DEBUG_MODE&&console.error(`moveToParent ALLOWED for ${this.forWriting ? 'write' : 'read'} lock on "${this.path}" by tid ${this.tid} (${this.comment})`);
this.history.push({path: this.path,forWriting: this.forWriting,action: 'moving to parent'});
this.waitingFor=null;
this.path=parentPath;
// this.comment = `moved to parent: ${this.comment}`;
returnthis;
}
else{
// Unlock without processing the queue
DEBUG_MODE&&console.error(`moveToParent QUEUED for ${this.forWriting ? 'write' : 'read'} lock on "${this.path}" by tid ${this.tid} (${this.comment})`);
DEBUG_MODE&&console.error(`QUEUED moveToParent ALLOWED for ${this.forWriting ? 'write' : 'read'} lock on "${this.path}" by tid ${this.tid} (${this.comment})`);
newLock.history=this.history;
newLock.history.push({path: this.path,forWriting: this.forWriting,action: 'moving to parent through queue (priority)'});