consumerportal
Version:
mydna Custimised for you
53 lines (46 loc) • 1.41 kB
text/typescript
module services {
export interface IQLockFactory {
create(fn: Function): QLock
}
export interface IQLock {
call(...args): angular.IPromise<any>
reset(): void
}
export class QLock implements IQLock {
private busy: boolean = false;
private value: any;
constructor(
private fn: Function,
private defer: angular.IDeferred<any>) {
}
public call(... args) {
if (!this.value) {
if (!this.busy) {
this.busy = true;
this.fn.call(this, args).then((value: any) => {
this.busy = false;
this.defer.resolve(value);
});
}
} else {
this.defer.resolve(this.value);
}
return this.defer.promise;
}
public reset() {
this.value = null;
this.busy = false;
}
}
class QLockFactory implements IQLockFactory {
static $inject = ['$q'];
constructor(private $q: angular.IQService) {}
create(fn: Function): QLock {
return new QLock(fn, this.$q.defer());
}
}
/*angular
.module('app')
.service('$qLock', QLockFactory);
*/
}