Skip to content

Commit

Permalink
Upgraded the lock management
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed May 20, 2017
1 parent a76f555 commit 299e307
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 28 deletions.
8 changes: 6 additions & 2 deletions lib/resource/lock/Lock.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { XMLElement } from '../../helper/XML';
import { LockKind } from './LockKind';
import { IUser } from '../../user/IUser';
export declare type LockOwner = string | XMLElement | XMLElement[];
export declare class Lock {
static generateUUID(expirationDate: number): string;
lockKind: LockKind;
expirationDate: number;
owner: string;
owner: LockOwner;
uuid: string;
constructor(lockKind: LockKind, owner: string);
user: IUser;
constructor(lockKind: LockKind, user: IUser, owner: LockOwner);
expired(): boolean;
}
3 changes: 2 additions & 1 deletion lib/resource/lock/Lock.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Lock = (function () {
function Lock(lockKind, owner) {
function Lock(lockKind, user, owner) {
this.expirationDate = Date.now() + lockKind.timeout;
this.lockKind = lockKind;
this.owner = owner;
this.uuid = Lock.generateUUID(this.expirationDate);
this.user = user;
}
Lock.generateUUID = function (expirationDate) {
var rnd1 = Math.ceil(Math.random() * 0x3FFF) + 0x8000;
Expand Down
8 changes: 5 additions & 3 deletions lib/resource/lock/LockBag.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { LockKind } from './LockKind';
import { LockType } from './LockType';
import { Lock } from './Lock';
export declare class LockBag {
locks: Lock[];
constructor();
getLocks(lockKind: LockKind): Lock[];
getLocks(lockType?: LockType): Lock[];
getLock(uuid: string): Lock;
setLock(lock: Lock): boolean;
removeLock(uuid: string, owner: string): void;
canRemoveLock(uuid: string, owner: string): boolean;
removeLock(uuid: string): void;
canRemoveLock(uuid: string): boolean;
canLock(lockKind: LockKind): boolean;
private notExpired(l);
private cleanLocks();
Expand Down
27 changes: 18 additions & 9 deletions lib/resource/lock/LockBag.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,38 @@ var LockBag = (function () {
function LockBag() {
this.locks = [];
}
LockBag.prototype.getLocks = function (lockKind) {
LockBag.prototype.getLocks = function (lockType) {
this.cleanLocks();
return this.locks.filter(function (l) { return l.lockKind.isSimilar(lockKind); });
if (lockType)
return this.locks.filter(function (l) { return l.lockKind.type.isSame(lockType); });
else
return this.locks;
};
LockBag.prototype.getLock = function (uuid) {
for (var _i = 0, _a = this.locks; _i < _a.length; _i++) {
var lock = _a[_i];
if (lock.uuid === uuid)
return lock;
}
return null;
};
LockBag.prototype.setLock = function (lock) {
if (!this.canLock(lock.lockKind))
return false;
this.locks.push(lock);
return true;
};
LockBag.prototype.removeLock = function (uuid, owner) {
LockBag.prototype.removeLock = function (uuid) {
var _this = this;
this.locks = this.locks.filter(function (l) { return _this.notExpired(l) && (l.uuid !== uuid || l.owner !== owner); });
this.locks = this.locks.filter(function (l) { return _this.notExpired(l) && l.uuid !== uuid; });
};
LockBag.prototype.canRemoveLock = function (uuid, owner) {
LockBag.prototype.canRemoveLock = function (uuid) {
this.cleanLocks();
return this.locks.some(function (l) { return l.uuid === uuid && l.owner !== owner; });
return this.locks.some(function (l) { return l.uuid === uuid; });
};
LockBag.prototype.canLock = function (lockKind) {
this.cleanLocks();
return !this.locks.some(function (l) {
return l.lockKind.scope === LockScope_1.LockScope.Exclusive;
});
return !(lockKind.scope.isSame(LockScope_1.LockScope.Exclusive) && this.locks.length > 0) && !this.locks.some(function (l) { return l.lockKind.scope.isSame(LockScope_1.LockScope.Exclusive); });
};
LockBag.prototype.notExpired = function (l) {
return !l.expired();
Expand Down
2 changes: 1 addition & 1 deletion lib/resource/lock/LockKind.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var LockKind = (function () {
this.timeout = timeout;
}
LockKind.prototype.isSimilar = function (lockKind) {
return this.scope === lockKind.scope && this.type === lockKind.type;
return this.scope.isSame(lockKind.scope) && this.type.isSame(lockKind.type);
};
return LockKind;
}());
Expand Down
1 change: 1 addition & 0 deletions lib/resource/lock/LockScope.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export declare class LockScope {
static Exclusive: LockScope;
constructor(value: string);
toString(): string;
isSame(scope: LockScope): boolean;
}
3 changes: 3 additions & 0 deletions lib/resource/lock/LockScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ var LockScope = (function () {
LockScope.prototype.toString = function () {
return this.value;
};
LockScope.prototype.isSame = function (scope) {
return scope.value.toLowerCase() === this.value.toLowerCase();
};
return LockScope;
}());
LockScope.Shared = new LockScope('shared');
Expand Down
1 change: 1 addition & 0 deletions lib/resource/lock/LockType.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export declare class LockType {
static Write: LockType;
constructor(value: string);
toString(): string;
isSame(scope: LockType): boolean;
}
3 changes: 3 additions & 0 deletions lib/resource/lock/LockType.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ var LockType = (function () {
LockType.prototype.toString = function () {
return this.value;
};
LockType.prototype.isSame = function (scope) {
return scope.value.toLowerCase() === this.value.toLowerCase();
};
return LockType;
}());
LockType.Write = new LockType('write');
Expand Down
10 changes: 8 additions & 2 deletions src/resource/lock/Lock.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { XMLElement } from '../../helper/XML'
import { LockKind } from './LockKind'
import { IUser } from '../../user/IUser'

export type LockOwner = string | XMLElement | XMLElement[];

export class Lock
{
Expand Down Expand Up @@ -34,15 +38,17 @@ export class Lock

lockKind : LockKind
expirationDate : number
owner : string
owner : LockOwner
uuid : string
user : IUser

constructor(lockKind : LockKind, owner : string)
constructor(lockKind : LockKind, user : IUser, owner : LockOwner)
{
this.expirationDate = Date.now() + lockKind.timeout;
this.lockKind = lockKind;
this.owner = owner;
this.uuid = Lock.generateUUID(this.expirationDate);
this.user = user;
}

expired() : boolean
Expand Down
28 changes: 19 additions & 9 deletions src/resource/lock/LockBag.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LockScope } from './LockScope'
import { LockKind } from './LockKind'
import { LockType } from './LockType'
import { Lock } from './Lock'

export class LockBag
Expand All @@ -11,10 +12,21 @@ export class LockBag
this.locks = [];
}

getLocks(lockKind : LockKind) : Lock[]
getLocks(lockType ?: LockType) : Lock[]
{
this.cleanLocks();
return this.locks.filter((l) => l.lockKind.isSimilar(lockKind))
if(lockType)
return this.locks.filter((l) => l.lockKind.type.isSame(lockType));
else
return this.locks;
}
getLock(uuid : string) : Lock
{
for(const lock of this.locks)
if(lock.uuid === uuid)
return lock;

return null;
}

setLock(lock : Lock) : boolean
Expand All @@ -26,22 +38,20 @@ export class LockBag
return true;
}

removeLock(uuid : string, owner : string) : void
removeLock(uuid : string) : void
{
this.locks = this.locks.filter((l) => this.notExpired(l) && (l.uuid !== uuid || l.owner !== owner));
this.locks = this.locks.filter((l) => this.notExpired(l) && l.uuid !== uuid);
}
canRemoveLock(uuid : string, owner : string) : boolean
canRemoveLock(uuid : string) : boolean
{
this.cleanLocks();
return this.locks.some((l) => l.uuid === uuid && l.owner !== owner);
return this.locks.some((l) => l.uuid === uuid);
}

canLock(lockKind : LockKind) : boolean
{
this.cleanLocks();
return !this.locks.some((l) => {
return l.lockKind.scope === LockScope.Exclusive;
});
return !(lockKind.scope.isSame(LockScope.Exclusive) && this.locks.length > 0) && !this.locks.some((l) => l.lockKind.scope.isSame(LockScope.Exclusive));
}

private notExpired(l : Lock)
Expand Down
2 changes: 1 addition & 1 deletion src/resource/lock/LockKind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ export class LockKind

isSimilar(lockKind : LockKind)
{
return this.scope === lockKind.scope && this.type === lockKind.type;
return this.scope.isSame(lockKind.scope) && this.type.isSame(lockKind.type);
}
}
5 changes: 5 additions & 0 deletions src/resource/lock/LockScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ export class LockScope
{
return this.value;
}

isSame(scope : LockScope) : boolean
{
return scope.value.toLowerCase() === this.value.toLowerCase();
}
}
5 changes: 5 additions & 0 deletions src/resource/lock/LockType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ export class LockType
{
return this.value;
}

isSame(scope : LockType) : boolean
{
return scope.value.toLowerCase() === this.value.toLowerCase();
}
}

0 comments on commit 299e307

Please sign in to comment.