mirror of
https://github.com/bower/bower.git
synced 2026-01-23 05:07:55 -05:00
A unit of work is a simple storage with write lock/unlock. The manager/package now share a unit of work instance. The unit of work is used to prevent shared dependencies from being cloned/copied "at the same time" fixing issue #81. The prune and version resolving algorithm was also not correct. It now resolves versions correctly, fixing issue #57. - Fix I/O errors caused by copying/clone repos simultaneously to the same dest. - Optimize the clone/copy step by avoiding it if the last resolved resource is the same. - Fix failing test (at least on my windows machine) - Add some more tests - Fix CS.
64 lines
1.8 KiB
JavaScript
64 lines
1.8 KiB
JavaScript
// ==========================================
|
|
// BOWER: Package Object Definition
|
|
// ==========================================
|
|
// Copyright 2012 Twitter, Inc
|
|
// Licensed under The MIT License
|
|
// http://opensource.org/licenses/MIT
|
|
// ==========================================
|
|
// Events:
|
|
// - lock: fired when a lock write over a key is acquired
|
|
// - unlock: fired when an unlock write over a key is acquired
|
|
// ==========================================
|
|
|
|
var events = require('events');
|
|
|
|
var UnitWork = function () {
|
|
this.locks = [];
|
|
this.data = [];
|
|
|
|
this.setMaxListeners(100); // Increase the number of listeners because this is a central storage
|
|
};
|
|
|
|
UnitWork.prototype = Object.create(events.EventEmitter.prototype);
|
|
|
|
UnitWork.prototype.constructor = UnitWork;
|
|
|
|
UnitWork.prototype.lock = function (key, owner) {
|
|
if (this.locks[key]) throw new Error('A write lock for "' + key + '" was already acquired.');
|
|
if (!owner) throw new Error('A lock requires an owner.');
|
|
this.locks[key] = owner;
|
|
|
|
return this.emit('lock', key);
|
|
};
|
|
|
|
UnitWork.prototype.unlock = function (key, owner) {
|
|
if (!owner) throw new Error('A write lock requires an owner.');
|
|
if (this.locks[key]) {
|
|
if (this.locks[key] !== owner) throw new Error('Lock owner for "' + key + '" mismatch.');
|
|
delete this.locks[key];
|
|
this.emit('unlock', key);
|
|
}
|
|
|
|
return this;
|
|
};
|
|
|
|
UnitWork.prototype.isLocked = function (key) {
|
|
return !!this.locks[key];
|
|
};
|
|
|
|
UnitWork.prototype.store = function (key, data, owner) {
|
|
if (this.locks[key] && owner !== this.locks[key]) throw new Error('A write lock for "' + key + '" is acquired therefore only its owner can write to it.');
|
|
this.data[key] = data;
|
|
|
|
return this;
|
|
};
|
|
|
|
UnitWork.prototype.retrieve = function (key) {
|
|
return this.data[key];
|
|
};
|
|
|
|
UnitWork.prototype.keys = function () {
|
|
return Object.keys(this.data);
|
|
};
|
|
|
|
module.exports = UnitWork; |