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.
@@ -917,6 +917,150 @@ var CamApi;
917
917
  CamApi.Api = Api$$1;
918
918
  })(CamApi || (CamApi = {}));
919
919
 
920
+ var MessageBroker;
921
+ (function (MessageBroker) {
922
+ var Action;
923
+ (function (Action) {
924
+ Action["SUBSCRIBE"] = "subscribe";
925
+ Action["SUBSCRIBE_SHADOW"] = "subscribeShadow";
926
+ Action["UNSUBSCRIBE"] = "unsubscribe";
927
+ Action["BROADCAST"] = "broadcast";
928
+ Action["GET_TOPIC_SUBSCRIBERS"] = "getTopicSubscribers"; // requests topic subscribers without subscribing
929
+ })(Action = MessageBroker.Action || (MessageBroker.Action = {}));
930
+ /** Events types any component may get from server */
931
+ var Event;
932
+ (function (Event) {
933
+ Event["SAVED"] = "saved";
934
+ Event["UPDATED"] = "updated";
935
+ Event["DELETED"] = "deleted";
936
+ Event["EDIT_STARTED"] = "editStarted";
937
+ Event["EDIT_FINISHED"] = "editFinished";
938
+ Event["USERS_UPDATE"] = "usersUpdate";
939
+ })(Event = MessageBroker.Event || (MessageBroker.Event = {}));
940
+ /** Communicates with a server message broker */
941
+ var WebSocketBroker = /** @class */ (function () {
942
+ function WebSocketBroker(uri, env) {
943
+ this.uri = uri;
944
+ this.env = env;
945
+ this.subscriptions = new Map();
946
+ this.reconnects = 0;
947
+ this.maxReconnects = 5;
948
+ this.connect();
949
+ }
950
+ WebSocketBroker.prototype.connect = function () {
951
+ try {
952
+ this.ws = new WebSocket(this.formatApiUri(this.uri));
953
+ this.ws.onopen = this.onOpen.bind(this);
954
+ this.ws.onmessage = this.onMessage.bind(this);
955
+ this.ws.onerror = this.onError.bind(this);
956
+ this.ws.onclose = this.onClose.bind(this);
957
+ }
958
+ catch (e) {
959
+ console.error(e);
960
+ }
961
+ };
962
+ WebSocketBroker.prototype.formatApiUri = function (uri) {
963
+ var ws = document.location.protocol === "https:" ? "wss" : "ws";
964
+ return uri.replace(/^(https|http)/, ws) + "websocket";
965
+ };
966
+ WebSocketBroker.prototype.setUser = function (user) {
967
+ this.user = this.user ? this.user : user;
968
+ };
969
+ WebSocketBroker.prototype.onOpen = function (ev) {
970
+ if (this.env === Api.EEnv.DEV) {
971
+ console.log("MessageBroker connection opened: ", ev);
972
+ }
973
+ };
974
+ WebSocketBroker.prototype.onClose = function (ev) {
975
+ var _this = this;
976
+ if (this.env === Api.EEnv.DEV) {
977
+ console.log("MessageBroker connection closed, trying to reconnect: ", ev);
978
+ }
979
+ if (this.reconnects < this.maxReconnects) {
980
+ this.reconnects += 1;
981
+ setTimeout(function () { return _this.connect(); }, 5000 * this.reconnects);
982
+ }
983
+ };
984
+ WebSocketBroker.prototype.onMessage = function (ev) {
985
+ var _a;
986
+ var data = JSON.parse(ev.data);
987
+ (_a = this.subscriptions.get(data.topic)) === null || _a === void 0 ? void 0 : _a.forEach(function (cb) { return cb(data); });
988
+ };
989
+ WebSocketBroker.prototype.onError = function (ev) {
990
+ console.error("MessageBroker connection error: ", ev);
991
+ };
992
+ WebSocketBroker.prototype.addSubscriber = function (topic, callback) {
993
+ var subscribers = this.subscriptions.get(topic) || [];
994
+ if (subscribers.includes(callback)) {
995
+ return;
996
+ }
997
+ subscribers.push(callback);
998
+ this.subscriptions.set(topic, subscribers);
999
+ if (this.env === Api.EEnv.DEV) {
1000
+ console.groupCollapsed("MessageBroker subscription added: ", topic);
1001
+ console.table(Array.from(this.subscriptions.entries()));
1002
+ console.groupEnd();
1003
+ }
1004
+ };
1005
+ /** Send message to server */
1006
+ WebSocketBroker.prototype.sendMessage = function (msg) {
1007
+ if (this.ws.readyState !== WebSocket.OPEN) {
1008
+ return;
1009
+ }
1010
+ if (!this.user) {
1011
+ console.error("Set user first!");
1012
+ }
1013
+ try {
1014
+ var outgoingMessage = __assign(__assign({}, msg), { user: this.user, time: Date.now() });
1015
+ this.ws.send(JSON.stringify(outgoingMessage));
1016
+ }
1017
+ catch (e) {
1018
+ console.warn(e);
1019
+ }
1020
+ };
1021
+ Object.defineProperty(WebSocketBroker.prototype, "connected", {
1022
+ get: function () {
1023
+ return this.ws.readyState === WebSocket.OPEN;
1024
+ },
1025
+ enumerable: false,
1026
+ configurable: true
1027
+ });
1028
+ /** Add a subscriber to a topic */
1029
+ WebSocketBroker.prototype.subscribe = function (topic, callback, action) {
1030
+ if (action === void 0) { action = Action.SUBSCRIBE; }
1031
+ this.addSubscriber(topic, callback);
1032
+ this.sendMessage({ topic: topic, action: action });
1033
+ if (this.env === Api.EEnv.DEV) {
1034
+ console.log("Subscribe [" + action + "] called for topic", topic);
1035
+ }
1036
+ };
1037
+ /** Add a shadow subscriber to a topic */
1038
+ WebSocketBroker.prototype.subscribeShadow = function (topic, callback) {
1039
+ this.subscribe(topic, callback, Action.SUBSCRIBE_SHADOW);
1040
+ };
1041
+ /** Remove a subscriber from a topic */
1042
+ WebSocketBroker.prototype.unsubscribe = function (topic, callback) {
1043
+ var subscribers = this.subscriptions.get(topic) || [];
1044
+ this.subscriptions.set(topic, subscribers.filter(function (fn) { return fn != callback; }));
1045
+ this.sendMessage({ topic: topic, action: Action.UNSUBSCRIBE });
1046
+ if (this.env === Api.EEnv.DEV) {
1047
+ console.groupCollapsed("MessageBroker subscription removed: ", topic);
1048
+ console.table(Array.from(this.subscriptions.entries()));
1049
+ console.groupEnd();
1050
+ }
1051
+ };
1052
+ WebSocketBroker.prototype.sendBroadcastMessage = function (msg) {
1053
+ this.sendMessage(__assign(__assign({}, msg), { action: Action.BROADCAST }));
1054
+ };
1055
+ WebSocketBroker.prototype.requestTopicUsers = function (topic, callback) {
1056
+ this.addSubscriber(topic, callback);
1057
+ this.sendMessage({ topic: topic, action: Action.GET_TOPIC_SUBSCRIBERS });
1058
+ };
1059
+ return WebSocketBroker;
1060
+ }());
1061
+ MessageBroker.WebSocketBroker = WebSocketBroker;
1062
+ })(MessageBroker || (MessageBroker = {}));
1063
+
920
1064
  var BruceApi;
921
1065
  (function (BruceApi) {
922
1066
  /**
@@ -936,19 +1080,31 @@ var BruceApi;
936
1080
  _this.loadCancelled = false;
937
1081
  _this.accountId = accountId;
938
1082
  _this.env = env !== null && env !== void 0 ? env : Api.EEnv.PROD;
939
- _this.loadProm = _this.setBaseUrl(cam);
1083
+ _this.loadProm = _this.init(cam);
940
1084
  return _this;
941
1085
  }
1086
+ Object.defineProperty(Api$$1.prototype, "MessageBroker", {
1087
+ get: function () {
1088
+ return this.messageBroker;
1089
+ },
1090
+ enumerable: false,
1091
+ configurable: true
1092
+ });
942
1093
  Object.defineProperty(Api$$1.prototype, "Loading", {
943
1094
  // Indicates if the init process has finished loading.
944
- // This means the regional base url has been set.
1095
+ // This means the regional base url has been set and message broker is ready.
945
1096
  get: function () {
946
1097
  return this.loadProm;
947
1098
  },
948
1099
  enumerable: false,
949
1100
  configurable: true
950
1101
  });
951
- Api$$1.prototype.setBaseUrl = function (cam) {
1102
+ /**
1103
+ * Loads regional base url and sets up message broker.
1104
+ * @param cam
1105
+ * @returns
1106
+ */
1107
+ Api$$1.prototype.init = function (cam) {
952
1108
  var _a;
953
1109
  return __awaiter(this, void 0, void 0, function () {
954
1110
  var prefix, url, env, camApi, settings, endpoint, e_1;
@@ -997,7 +1153,9 @@ var BruceApi;
997
1153
  e_1 = _b.sent();
998
1154
  console.error(e_1);
999
1155
  return [3 /*break*/, 4];
1000
- case 4: return [2 /*return*/];
1156
+ case 4:
1157
+ this.messageBroker = new MessageBroker.WebSocketBroker(this.baseUrl, this.env);
1158
+ return [2 /*return*/];
1001
1159
  }
1002
1160
  });
1003
1161
  });
@@ -1894,7 +2052,8 @@ var Calculator;
1894
2052
  if (typeof value == "string") {
1895
2053
  try {
1896
2054
  value = BruceVariable.SwapValues(value, entity);
1897
- return eval(value);
2055
+ var eval2 = eval; // https://rollupjs.org/guide/en/#avoiding-eval
2056
+ return eval2(value);
1898
2057
  }
1899
2058
  catch (exception) {
1900
2059
  var e = exception;
@@ -6699,5 +6858,5 @@ var ImportedFile;
6699
6858
  ImportedFile.Get = Get;
6700
6859
  })(ImportedFile || (ImportedFile = {}));
6701
6860
 
6702
- export { AnnDocument, CustomForm, AbstractApi, Api, BruceApi, CamApi, IdmApi, GlobalApi, Calculator, BruceEvent, CacheControl, Camera, Cartes, Carto, Color, DelayQueue, Geometry, UTC, EntityAttachmentType, EntityAttachment, EntityComment, EntityLink, EntityLod, EntityRelationType, EntityRelation, EntitySource, EntityTag, EntityType, Entity, EntityGlobe, EntityFilterGetter, BatchedDataGetter, EntityCoords, ClientFile, ProgramKey, ZoomControl, MenuItem, ProjectViewBookmark, ProjectView, ProjectViewLegacyTile, ProjectViewTile, PendingAction, Style, TilesetEntitiesMapTiles, TilesetExtMapTiles, Tileset, Permission, Session, UserGroup, User, Account, EncryptUtils, MathUtils, ObjectUtils, PathUtils, UrlUtils, DataLab, ImportCad, ImportCsv, ImportJson, ImportKml, ImportedFile };
6861
+ export { AnnDocument, CustomForm, AbstractApi, Api, BruceApi, CamApi, IdmApi, GlobalApi, Calculator, BruceEvent, CacheControl, Camera, Cartes, Carto, Color, DelayQueue, Geometry, UTC, EntityAttachmentType, EntityAttachment, EntityComment, EntityLink, EntityLod, EntityRelationType, EntityRelation, EntitySource, EntityTag, EntityType, Entity, EntityGlobe, EntityFilterGetter, BatchedDataGetter, EntityCoords, ClientFile, ProgramKey, ZoomControl, MenuItem, ProjectViewBookmark, ProjectView, ProjectViewLegacyTile, ProjectViewTile, PendingAction, MessageBroker, Style, TilesetEntitiesMapTiles, TilesetExtMapTiles, Tileset, Permission, Session, UserGroup, User, Account, EncryptUtils, MathUtils, ObjectUtils, PathUtils, UrlUtils, DataLab, ImportCad, ImportCsv, ImportJson, ImportKml, ImportedFile };
6703
6862
  //# sourceMappingURL=bruce-models.es5.js.map