bruce-models 0.9.4 → 0.9.6

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.
@@ -755,34 +755,35 @@
755
755
  (function (MessageBroker) {
756
756
  let EAction;
757
757
  (function (EAction) {
758
+ // Subscribes a client to topic messages on a server side.
758
759
  EAction["SUBSCRIBE"] = "subscribe";
760
+ // Subscribes a client to a topic without notifying other clients.
759
761
  EAction["SUBSCRIBE_SHADOW"] = "subscribeShadow";
762
+ // Unsubscribes clients from topic messages on a server side
760
763
  EAction["UNSUBSCRIBE"] = "unsubscribe";
764
+ // Sends a message to all topic subscribers.
761
765
  EAction["BROADCAST"] = "broadcast";
762
- EAction["GET_TOPIC_SUBSCRIBERS"] = "getTopicSubscribers"; // requests topic subscribers without subscribing
766
+ // Requests topic subscribers without subscribing.
767
+ EAction["GET_TOPIC_SUBSCRIBERS"] = "getTopicSubscribers";
763
768
  })(EAction = MessageBroker.EAction || (MessageBroker.EAction = {}));
764
- /** Events types any component may get from server */
765
- let EEvent;
766
- (function (EEvent) {
767
- EEvent["SAVED"] = "saved";
768
- EEvent["UPDATED"] = "updated";
769
- EEvent["DELETED"] = "deleted";
770
- EEvent["EDIT_STARTED"] = "editStarted";
771
- EEvent["EDIT_FINISHED"] = "editFinished";
772
- EEvent["USERS_UPDATE"] = "usersUpdate";
773
- })(EEvent = MessageBroker.EEvent || (MessageBroker.EEvent = {}));
774
769
  /**
775
770
  * Communicates with a server message broker.
776
771
  */
777
772
  class WebSocketBroker {
778
773
  constructor(uri, env) {
779
- this.uri = uri;
780
- this.env = env;
781
774
  this.subscriptions = new Map();
782
775
  this.reconnects = 0;
783
776
  this.maxReconnects = 5;
777
+ this.uri = uri;
778
+ this.env = env ? env : exports.Api.EEnv.PROD;
784
779
  this.connect();
785
780
  }
781
+ get IsConnected() {
782
+ return this.ws.readyState === WebSocket.OPEN;
783
+ }
784
+ SetUserId(userId) {
785
+ this.userId = this.userId ? this.userId : userId;
786
+ }
786
787
  connect() {
787
788
  try {
788
789
  if (!WebSocket) {
@@ -802,9 +803,6 @@
802
803
  const ws = document.location.protocol === "https:" ? "wss" : "ws";
803
804
  return uri.replace(/^(https|http)/, ws) + "websocket";
804
805
  }
805
- SetUser(user) {
806
- this.user = this.user ? this.user : user;
807
- }
808
806
  onOpen(ev) {
809
807
  if (this.env === exports.Api.EEnv.DEV) {
810
808
  console.log("MessageBroker connection opened: ", ev);
@@ -834,55 +832,45 @@
834
832
  }
835
833
  subscribers.push(callback);
836
834
  this.subscriptions.set(topic, subscribers);
837
- if (this.env === exports.Api.EEnv.DEV) {
838
- console.groupCollapsed("MessageBroker subscription added: ", topic);
839
- console.table(Array.from(this.subscriptions.entries()));
840
- console.groupEnd();
841
- }
842
835
  }
843
836
  /**
844
837
  * Send message to server.
845
- * @param msg
838
+ * @param base
846
839
  * @returns
847
840
  */
848
- sendMessage(msg) {
841
+ SendMessage(base) {
849
842
  if (this.ws.readyState !== WebSocket.OPEN) {
850
843
  return;
851
844
  }
852
- if (!this.user) {
853
- console.error("Set user first!");
854
- }
855
845
  try {
856
- const outgoingMessage = Object.assign(Object.assign({}, msg), { user: this.user, time: Date.now() });
846
+ const msg = base;
847
+ if (!msg.event) {
848
+ msg.event = EAction.BROADCAST;
849
+ }
850
+ let message = base.data ? base.data : {};
851
+ if (typeof message == "object") {
852
+ message = JSON.stringify(message);
853
+ }
854
+ msg.data = message;
855
+ const outgoingMessage = Object.assign(Object.assign({}, msg), { user: this.userId, time: Date.now() });
857
856
  this.ws.send(JSON.stringify(outgoingMessage));
858
857
  }
859
858
  catch (e) {
860
859
  console.warn(e);
861
860
  }
862
861
  }
863
- get IsConnected() {
864
- return this.ws.readyState === WebSocket.OPEN;
865
- }
866
862
  /**
867
863
  * Add a subscriber to a topic.
868
864
  * @param topic
869
865
  * @param callback
870
866
  * @param action
871
867
  */
872
- Subscribe(topic, callback, action = EAction.SUBSCRIBE) {
873
- this.addSubscriber(topic, callback);
874
- this.sendMessage({ topic, action });
875
- if (this.env === exports.Api.EEnv.DEV) {
876
- console.log(`Subscribe [${action}] called for topic`, topic);
868
+ Subscribe(topic, callback, action) {
869
+ if (!action) {
870
+ action = EAction.SUBSCRIBE;
877
871
  }
878
- }
879
- /**
880
- * Add a shadow subscriber to a topic.
881
- * @param topic
882
- * @param callback
883
- */
884
- SubscribeShadow(topic, callback) {
885
- this.Subscribe(topic, callback, EAction.SUBSCRIBE_SHADOW);
872
+ this.addSubscriber(topic, callback);
873
+ this.SendMessage({ topic, action });
886
874
  }
887
875
  /**
888
876
  * Remove a subscriber from a topic.
@@ -892,19 +880,11 @@
892
880
  Unsubscribe(topic, callback) {
893
881
  const subscribers = this.subscriptions.get(topic) || [];
894
882
  this.subscriptions.set(topic, subscribers.filter(fn => fn != callback));
895
- this.sendMessage({ topic, action: EAction.UNSUBSCRIBE });
896
- if (this.env === exports.Api.EEnv.DEV) {
897
- console.groupCollapsed("MessageBroker subscription removed: ", topic);
898
- console.table(Array.from(this.subscriptions.entries()));
899
- console.groupEnd();
900
- }
901
- }
902
- SendBroadcastMessage(msg) {
903
- this.sendMessage(Object.assign(Object.assign({}, msg), { action: EAction.BROADCAST }));
883
+ this.SendMessage({ topic, action: EAction.UNSUBSCRIBE });
904
884
  }
905
885
  RequestTopicUsers(topic, callback) {
906
886
  this.addSubscriber(topic, callback);
907
- this.sendMessage({ topic, action: EAction.GET_TOPIC_SUBSCRIBERS });
887
+ this.SendMessage({ topic, event: EAction.GET_TOPIC_SUBSCRIBERS });
908
888
  }
909
889
  }
910
890
  MessageBroker.WebSocketBroker = WebSocketBroker;