@hpcc-js/comms
Version:
hpcc-js - Communications
166 lines • 6.42 kB
JavaScript
import { __extends } from "tslib";
import { Cache, Dispatch, Message } from "@hpcc-js/util";
import { StoreService } from "../services/wsStore";
var StoreCache = /** @class */ (function (_super) {
__extends(StoreCache, _super);
function StoreCache() {
return _super.call(this, function (obj) {
return "".concat(obj.BaseUrl, "-").concat(obj.Name, ":").concat(obj.UserSpecific, "-").concat(obj.Namespace);
}) || this;
}
return StoreCache;
}(Cache));
export { StoreCache };
var _store = new StoreCache();
var ValueChangedMessage = /** @class */ (function (_super) {
__extends(ValueChangedMessage, _super);
function ValueChangedMessage(key, value, oldValue) {
var _this = _super.call(this) || this;
_this.key = key;
_this.value = value;
_this.oldValue = oldValue;
return _this;
}
Object.defineProperty(ValueChangedMessage.prototype, "canConflate", {
get: function () { return true; },
enumerable: false,
configurable: true
});
ValueChangedMessage.prototype.conflate = function (other) {
if (this.key === other.key) {
this.value = other.value;
return true;
}
return false;
};
ValueChangedMessage.prototype.void = function () {
return this.value === this.oldValue;
};
return ValueChangedMessage;
}(Message));
export { ValueChangedMessage };
var Store = /** @class */ (function () {
function Store(optsConnection, Name, Namespace, UserSpecific) {
this._dispatch = new Dispatch();
this._knownValues = {};
if (optsConnection instanceof StoreService) {
this.connection = optsConnection;
}
else {
this.connection = new StoreService(optsConnection);
}
this.Name = Name;
this.UserSpecific = UserSpecific;
this.Namespace = Namespace;
}
Object.defineProperty(Store.prototype, "BaseUrl", {
get: function () { return this.connection.baseUrl; },
enumerable: false,
configurable: true
});
Store.attach = function (optsConnection, Name, Namespace, UserSpecific) {
if (Name === void 0) { Name = "HPCCApps"; }
if (UserSpecific === void 0) { UserSpecific = true; }
var retVal = _store.get({ BaseUrl: optsConnection.baseUrl, Name: Name, UserSpecific: UserSpecific, Namespace: Namespace }, function () {
return new Store(optsConnection, Name, Namespace, UserSpecific);
});
return retVal;
};
Store.prototype.create = function () {
this.connection.CreateStore({ Name: this.Name, UserSpecific: this.UserSpecific, Type: "", Description: "" });
};
Store.prototype.set = function (key, value, broadcast) {
var _this = this;
if (broadcast === void 0) { broadcast = true; }
return this.connection.Set({
StoreName: this.Name,
UserSpecific: this.UserSpecific,
Namespace: this.Namespace,
Key: key,
Value: value
}).then(function (response) {
var oldValue = _this._knownValues[key];
_this._knownValues[key] = value;
if (broadcast) {
_this._dispatch.post(new ValueChangedMessage(key, value, oldValue));
}
}).catch(function (e) {
console.error("Store.set(\"".concat(key, "\", \"").concat(value, "\") failed:"), e);
});
};
Store.prototype.get = function (key, broadcast) {
var _this = this;
if (broadcast === void 0) { broadcast = true; }
return this.connection.Fetch({
StoreName: this.Name,
UserSpecific: this.UserSpecific,
Namespace: this.Namespace,
Key: key
}).then(function (response) {
var oldValue = _this._knownValues[key];
_this._knownValues[key] = response.Value;
if (broadcast) {
_this._dispatch.post(new ValueChangedMessage(key, response.Value, oldValue));
}
return response.Value;
}).catch(function (e) {
console.error("Store.get(".concat(key, ") failed:"), e);
return undefined;
});
};
Store.prototype.getAll = function (broadcast) {
var _this = this;
if (broadcast === void 0) { broadcast = true; }
return this.connection.FetchAll({
StoreName: this.Name,
UserSpecific: this.UserSpecific,
Namespace: this.Namespace
}).then(function (response) {
var retVal = {};
var deletedValues = _this._knownValues;
_this._knownValues = {};
response.Pairs.Pair.forEach(function (pair) {
var oldValue = _this._knownValues[pair.Key];
_this._knownValues[pair.Key] = pair.Value;
delete deletedValues[pair.Key];
retVal[pair.Key] = pair.Value;
if (broadcast) {
_this._dispatch.post(new ValueChangedMessage(pair.Key, pair.Value, oldValue));
}
});
if (broadcast) {
for (var key in deletedValues) {
_this._dispatch.post(new ValueChangedMessage(key, undefined, deletedValues[key]));
}
}
return retVal;
}).catch(function (e) {
console.error("Store.getAll failed:", e);
return {};
});
};
Store.prototype.delete = function (key, broadcast) {
var _this = this;
if (broadcast === void 0) { broadcast = true; }
return this.connection.Delete({
StoreName: this.Name,
UserSpecific: this.UserSpecific,
Namespace: this.Namespace,
Key: key
}).then(function (response) {
var oldValue = _this._knownValues[key];
delete _this._knownValues[key];
if (broadcast) {
_this._dispatch.post(new ValueChangedMessage(key, undefined, oldValue));
}
}).catch(function (e) {
console.error("Store.delete(".concat(key, ") failed:"), e);
});
};
Store.prototype.monitor = function (callback) {
return this._dispatch.attach(callback);
};
return Store;
}());
export { Store };
//# sourceMappingURL=store.js.map