bruce-models 0.7.7 → 0.7.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -902,6 +902,149 @@
902
902
  CamApi.Api = Api$$1;
903
903
  })(exports.CamApi || (exports.CamApi = {}));
904
904
 
905
+ (function (MessageBroker) {
906
+ var Action;
907
+ (function (Action) {
908
+ Action["SUBSCRIBE"] = "subscribe";
909
+ Action["SUBSCRIBE_SHADOW"] = "subscribeShadow";
910
+ Action["UNSUBSCRIBE"] = "unsubscribe";
911
+ Action["BROADCAST"] = "broadcast";
912
+ Action["GET_TOPIC_SUBSCRIBERS"] = "getTopicSubscribers"; // requests topic subscribers without subscribing
913
+ })(Action = MessageBroker.Action || (MessageBroker.Action = {}));
914
+ /** Events types any component may get from server */
915
+ var Event;
916
+ (function (Event) {
917
+ Event["SAVED"] = "saved";
918
+ Event["UPDATED"] = "updated";
919
+ Event["DELETED"] = "deleted";
920
+ Event["EDIT_STARTED"] = "editStarted";
921
+ Event["EDIT_FINISHED"] = "editFinished";
922
+ Event["USERS_UPDATE"] = "usersUpdate";
923
+ })(Event = MessageBroker.Event || (MessageBroker.Event = {}));
924
+ /** Communicates with a server message broker */
925
+ var WebSocketBroker = /** @class */ (function () {
926
+ function WebSocketBroker(uri, env) {
927
+ this.uri = uri;
928
+ this.env = env;
929
+ this.subscriptions = new Map();
930
+ this.reconnects = 0;
931
+ this.maxReconnects = 5;
932
+ this.connect();
933
+ }
934
+ WebSocketBroker.prototype.connect = function () {
935
+ try {
936
+ this.ws = new WebSocket(this.formatApiUri(this.uri));
937
+ this.ws.onopen = this.onOpen.bind(this);
938
+ this.ws.onmessage = this.onMessage.bind(this);
939
+ this.ws.onerror = this.onError.bind(this);
940
+ this.ws.onclose = this.onClose.bind(this);
941
+ }
942
+ catch (e) {
943
+ console.error(e);
944
+ }
945
+ };
946
+ WebSocketBroker.prototype.formatApiUri = function (uri) {
947
+ var ws = document.location.protocol === "https:" ? "wss" : "ws";
948
+ return uri.replace(/^(https|http)/, ws) + "websocket";
949
+ };
950
+ WebSocketBroker.prototype.setUser = function (user) {
951
+ this.user = this.user ? this.user : user;
952
+ };
953
+ WebSocketBroker.prototype.onOpen = function (ev) {
954
+ if (this.env === exports.Api.EEnv.DEV) {
955
+ console.log("MessageBroker connection opened: ", ev);
956
+ }
957
+ };
958
+ WebSocketBroker.prototype.onClose = function (ev) {
959
+ var _this = this;
960
+ if (this.env === exports.Api.EEnv.DEV) {
961
+ console.log("MessageBroker connection closed, trying to reconnect: ", ev);
962
+ }
963
+ if (this.reconnects < this.maxReconnects) {
964
+ this.reconnects += 1;
965
+ setTimeout(function () { return _this.connect(); }, 5000 * this.reconnects);
966
+ }
967
+ };
968
+ WebSocketBroker.prototype.onMessage = function (ev) {
969
+ var _a;
970
+ var data = JSON.parse(ev.data);
971
+ (_a = this.subscriptions.get(data.topic)) === null || _a === void 0 ? void 0 : _a.forEach(function (cb) { return cb(data); });
972
+ };
973
+ WebSocketBroker.prototype.onError = function (ev) {
974
+ console.error("MessageBroker connection error: ", ev);
975
+ };
976
+ WebSocketBroker.prototype.addSubscriber = function (topic, callback) {
977
+ var subscribers = this.subscriptions.get(topic) || [];
978
+ if (subscribers.includes(callback)) {
979
+ return;
980
+ }
981
+ subscribers.push(callback);
982
+ this.subscriptions.set(topic, subscribers);
983
+ if (this.env === exports.Api.EEnv.DEV) {
984
+ console.groupCollapsed("MessageBroker subscription added: ", topic);
985
+ console.table(Array.from(this.subscriptions.entries()));
986
+ console.groupEnd();
987
+ }
988
+ };
989
+ /** Send message to server */
990
+ WebSocketBroker.prototype.sendMessage = function (msg) {
991
+ if (this.ws.readyState !== WebSocket.OPEN) {
992
+ return;
993
+ }
994
+ if (!this.user) {
995
+ console.error("Set user first!");
996
+ }
997
+ try {
998
+ var outgoingMessage = __assign(__assign({}, msg), { user: this.user, time: Date.now() });
999
+ this.ws.send(JSON.stringify(outgoingMessage));
1000
+ }
1001
+ catch (e) {
1002
+ console.warn(e);
1003
+ }
1004
+ };
1005
+ Object.defineProperty(WebSocketBroker.prototype, "connected", {
1006
+ get: function () {
1007
+ return this.ws.readyState === WebSocket.OPEN;
1008
+ },
1009
+ enumerable: false,
1010
+ configurable: true
1011
+ });
1012
+ /** Add a subscriber to a topic */
1013
+ WebSocketBroker.prototype.subscribe = function (topic, callback, action) {
1014
+ if (action === void 0) { action = Action.SUBSCRIBE; }
1015
+ this.addSubscriber(topic, callback);
1016
+ this.sendMessage({ topic: topic, action: action });
1017
+ if (this.env === exports.Api.EEnv.DEV) {
1018
+ console.log("Subscribe [" + action + "] called for topic", topic);
1019
+ }
1020
+ };
1021
+ /** Add a shadow subscriber to a topic */
1022
+ WebSocketBroker.prototype.subscribeShadow = function (topic, callback) {
1023
+ this.subscribe(topic, callback, Action.SUBSCRIBE_SHADOW);
1024
+ };
1025
+ /** Remove a subscriber from a topic */
1026
+ WebSocketBroker.prototype.unsubscribe = function (topic, callback) {
1027
+ var subscribers = this.subscriptions.get(topic) || [];
1028
+ this.subscriptions.set(topic, subscribers.filter(function (fn) { return fn != callback; }));
1029
+ this.sendMessage({ topic: topic, action: Action.UNSUBSCRIBE });
1030
+ if (this.env === exports.Api.EEnv.DEV) {
1031
+ console.groupCollapsed("MessageBroker subscription removed: ", topic);
1032
+ console.table(Array.from(this.subscriptions.entries()));
1033
+ console.groupEnd();
1034
+ }
1035
+ };
1036
+ WebSocketBroker.prototype.sendBroadcastMessage = function (msg) {
1037
+ this.sendMessage(__assign(__assign({}, msg), { action: Action.BROADCAST }));
1038
+ };
1039
+ WebSocketBroker.prototype.requestTopicUsers = function (topic, callback) {
1040
+ this.addSubscriber(topic, callback);
1041
+ this.sendMessage({ topic: topic, action: Action.GET_TOPIC_SUBSCRIBERS });
1042
+ };
1043
+ return WebSocketBroker;
1044
+ }());
1045
+ MessageBroker.WebSocketBroker = WebSocketBroker;
1046
+ })(exports.MessageBroker || (exports.MessageBroker = {}));
1047
+
905
1048
  (function (BruceApi) {
906
1049
  /**
907
1050
  * This is the request handler for Bruce Api,
@@ -920,19 +1063,31 @@
920
1063
  _this.loadCancelled = false;
921
1064
  _this.accountId = accountId;
922
1065
  _this.env = env !== null && env !== void 0 ? env : exports.Api.EEnv.PROD;
923
- _this.loadProm = _this.setBaseUrl(cam);
1066
+ _this.loadProm = _this.init(cam);
924
1067
  return _this;
925
1068
  }
1069
+ Object.defineProperty(Api$$1.prototype, "MessageBroker", {
1070
+ get: function () {
1071
+ return this.messageBroker;
1072
+ },
1073
+ enumerable: false,
1074
+ configurable: true
1075
+ });
926
1076
  Object.defineProperty(Api$$1.prototype, "Loading", {
927
1077
  // Indicates if the init process has finished loading.
928
- // This means the regional base url has been set.
1078
+ // This means the regional base url has been set and message broker is ready.
929
1079
  get: function () {
930
1080
  return this.loadProm;
931
1081
  },
932
1082
  enumerable: false,
933
1083
  configurable: true
934
1084
  });
935
- Api$$1.prototype.setBaseUrl = function (cam) {
1085
+ /**
1086
+ * Loads regional base url and sets up message broker.
1087
+ * @param cam
1088
+ * @returns
1089
+ */
1090
+ Api$$1.prototype.init = function (cam) {
936
1091
  var _a;
937
1092
  return __awaiter(this, void 0, void 0, function () {
938
1093
  var prefix, url, env, camApi, settings, endpoint, e_1;
@@ -981,7 +1136,9 @@
981
1136
  e_1 = _b.sent();
982
1137
  console.error(e_1);
983
1138
  return [3 /*break*/, 4];
984
- case 4: return [2 /*return*/];
1139
+ case 4:
1140
+ this.messageBroker = new exports.MessageBroker.WebSocketBroker(this.baseUrl, this.env);
1141
+ return [2 /*return*/];
985
1142
  }
986
1143
  });
987
1144
  });
@@ -1862,7 +2019,8 @@
1862
2019
  if (typeof value == "string") {
1863
2020
  try {
1864
2021
  value = BruceVariable.SwapValues(value, entity);
1865
- return eval(value);
2022
+ var eval2 = eval; // https://rollupjs.org/guide/en/#avoiding-eval
2023
+ return eval2(value);
1866
2024
  }
1867
2025
  catch (exception) {
1868
2026
  var e = exception;