@chevre/domain
Version:
Chevre Domain Library for Node.js
49 lines (48 loc) • 2 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConcurrentLockRepo = void 0;
const factory = require("../factory");
/**
* 同時実行ロックリポジトリ
*/
class ConcurrentLockRepo {
constructor(params) {
const { redisClient } = params;
this.redisClient = redisClient;
}
static CREATE_REDIS_KEY(params) {
return params.about.identifier;
}
lock(params) {
return __awaiter(this, void 0, void 0, function* () {
const key = ConcurrentLockRepo.CREATE_REDIS_KEY(params);
const value = params.audience.identifier;
const results = yield this.redisClient.multi()
.setNX(key, value)
.expireAt(key, params.expires)
.exec();
if (Array.isArray(results) && (results[0] === 1 || results[0] === true)) {
return;
}
else {
throw new factory.errors.AlreadyInUse(params.about.identifier, []);
}
});
}
unlock(params) {
return __awaiter(this, void 0, void 0, function* () {
const key = ConcurrentLockRepo.CREATE_REDIS_KEY(params);
yield this.redisClient.del([key]);
});
}
}
exports.ConcurrentLockRepo = ConcurrentLockRepo;
;