atomic-file
Advanced tools
Comparing version 2.0.1 to 2.1.0
@@ -1,2 +0,2 @@ | ||
var fs = require('fs') | ||
module.exports = function (file) { | ||
@@ -3,0 +3,0 @@ return { |
'use strict' | ||
var fs = require('fs') | ||
var mutexify = require('mutexify') | ||
function isFunction (f) { | ||
return 'function' === typeof f | ||
} | ||
function onceAtATime (fn, queue) { | ||
queue = queue || [] | ||
return function () { | ||
var args = [].slice.call(arguments) | ||
if(queue.length) | ||
return queue.push(function () { | ||
fn.apply(null, args) | ||
}) | ||
//should be a the callback... | ||
var cb = args.pop() | ||
if(!isFunction(cb)) throw new Error('cb must be provided') | ||
queue.push(cb) | ||
args.push(function done (err) { | ||
var _queue = queue | ||
queue = [] | ||
while(_queue.length) _queue.shift()(err) | ||
}) | ||
fn.apply(null, args) | ||
} | ||
} | ||
module.exports = function (store, _codec) { | ||
var codec = _codec || require('flumecodec/json') | ||
var queue = [] | ||
var lock = mutexify() | ||
var value | ||
@@ -50,15 +23,20 @@ | ||
//only allow one update at a time. | ||
set: onceAtATime(function put (_value, cb) { | ||
store.set(codec.encode(_value), function (err) { | ||
if(err) return cb(err) | ||
else cb(null, value=_value) | ||
set: function (_value, cb) { | ||
lock(function (unlock) { | ||
store.set(codec.encode(_value), function (err) { | ||
if(!err) value=_value | ||
unlock(cb, err, _value) | ||
}) | ||
}) | ||
}, queue), | ||
destroy: onceAtATime(function destroy (cb) { | ||
store.destroy(function (err) { | ||
cb(err, value=null) | ||
}, | ||
destroy: function (cb) { | ||
lock(function (unlock) { | ||
store.destroy(function (err) { | ||
value=null | ||
unlock(cb, err) | ||
}) | ||
}) | ||
}, queue) | ||
} | ||
} | ||
} | ||
{ | ||
"name": "atomic-file", | ||
"description": "", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"homepage": "https://github.com/dominictarr/atomic-file", | ||
@@ -12,3 +12,4 @@ "repository": { | ||
"flumecodec": "0.0.1", | ||
"idb-kv-store": "^4.4.0" | ||
"idb-kv-store": "^4.5.0", | ||
"mutexify": "^1.2.0" | ||
}, | ||
@@ -19,3 +20,3 @@ "devDependencies": { | ||
"scripts": { | ||
"test": "set -e; for t in test/*.js; do node $t; done" | ||
"test": "tape 'test/**/*.js'" | ||
}, | ||
@@ -22,0 +23,0 @@ "browser": { |
@@ -7,4 +7,3 @@ const IdbKvStore = require('idb-kv-store') | ||
const storename = parts.join('/') | ||
const store = new IdbKvStore(storename) | ||
var value | ||
const store = new IdbKvStore(storename, { disableBroadcast: true }) | ||
return { | ||
@@ -11,0 +10,0 @@ set: function (value, cb) { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9072
3
235
1
+ Addedmutexify@^1.2.0
+ Addedmutexify@1.4.0(transitive)
+ Addedqueue-tick@1.0.1(transitive)
Updatedidb-kv-store@^4.5.0