Easy to use file system queue with locking and events. Provide Asynchronous utilities for Directories and File
This library's goal is to provide nothing than a easy way to wait for the asynchronous execution of a FS operation in a lock environment.
npm install fslock
mkdir myproject
cd myproject
npm init
npm install fslock
touch index.js
And there just use that snipets to start playing ! :
const{FSLock, File, Directory}=require('fslockjs');constqueue=newFSLock();conststart=asyncfunction(){constdoc={_id:'507f1f77bcf86cd799439011',name:"Alex",age:28};constdoc2={_id:'507f1f77bcf86cd799439011',name:"Alex",age:27};constjob=queue.add('File.create','.db/507f1f77bcf86cd799439011.json',doc);constjob2=queue.add('File.overwrite','.db/507f1f77bcf86cd799439011.json',doc2);constjob3=queue.add('File.read','.db/507f1f77bcf86cd799439011.json');awaitjob3.execution();console.log(job3.results===doc2)//trueconstisExisting=awaitFile.exists('.db/507f1f77bcf86cd799439011.json')//true}start();Alternatively, you could suppress the autoexec, and deal with it this way
constqueue=newFSLock({autoexec:false});constdoc={_id:'507f1f77bcf86cd799439011',name:"Alex",age:28};queue.add('File.create','.db/507f1f77bcf86cd799439011.json',doc);constjob=queue.add('File.read','.db/507f1f77bcf86cd799439011.json');awaitqueue.processNext();awaitqueue.processNext();console.log(job.results===doc)//trueAlso, we provide events on job
constqueue=newFSLock({autoexec:false});constdoc={_id:'507f1f77bcf86cd799439011',name:"Alex",age:28};queue.add('File.create','.db/507f1f77bcf86cd799439011.json',doc);constjob=queue.add('File.read','.db/507f1f77bcf86cd799439011.json');job.on('executed',()=>{console.log(job.results);console.log(job.results===doc)//true});queue.start();Because of being able to do await job.execution()
Which end up being a nice way to wait for the resolvement of a FS Job in concurrent conditions
constjob=awaitthis.queue.add('File.exists',path).execution();constisExistingFile=job.results;Alternatively equal to this below, that will only returns results when having it.
constresults=awaitthis.queue.add('File.exists',path).getResults();Right now, an instance of FSLock only deal locally with the locks. If needed, we can make two instance working together with using real os lock.
Submit issue if you need such features.