bruce-models 0.9.5 → 0.9.7
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.
- package/dist/bruce-models.es5.js +33 -53
- package/dist/bruce-models.es5.js.map +1 -1
- package/dist/bruce-models.umd.js +33 -53
- package/dist/bruce-models.umd.js.map +1 -1
- package/dist/lib/server/message-broker.js +33 -53
- package/dist/lib/server/message-broker.js.map +1 -1
- package/dist/types/server/message-broker.d.ts +40 -81
- package/package.json +1 -1
package/dist/bruce-models.es5.js
CHANGED
|
@@ -767,34 +767,35 @@ var MessageBroker;
|
|
|
767
767
|
(function (MessageBroker) {
|
|
768
768
|
let EAction;
|
|
769
769
|
(function (EAction) {
|
|
770
|
+
// Subscribes a client to topic messages on a server side.
|
|
770
771
|
EAction["SUBSCRIBE"] = "subscribe";
|
|
772
|
+
// Subscribes a client to a topic without notifying other clients.
|
|
771
773
|
EAction["SUBSCRIBE_SHADOW"] = "subscribeShadow";
|
|
774
|
+
// Unsubscribes clients from topic messages on a server side
|
|
772
775
|
EAction["UNSUBSCRIBE"] = "unsubscribe";
|
|
776
|
+
// Sends a message to all topic subscribers.
|
|
773
777
|
EAction["BROADCAST"] = "broadcast";
|
|
774
|
-
|
|
778
|
+
// Requests topic subscribers without subscribing.
|
|
779
|
+
EAction["GET_TOPIC_SUBSCRIBERS"] = "getTopicSubscribers";
|
|
775
780
|
})(EAction = MessageBroker.EAction || (MessageBroker.EAction = {}));
|
|
776
|
-
/** Events types any component may get from server */
|
|
777
|
-
let EEvent;
|
|
778
|
-
(function (EEvent) {
|
|
779
|
-
EEvent["SAVED"] = "saved";
|
|
780
|
-
EEvent["UPDATED"] = "updated";
|
|
781
|
-
EEvent["DELETED"] = "deleted";
|
|
782
|
-
EEvent["EDIT_STARTED"] = "editStarted";
|
|
783
|
-
EEvent["EDIT_FINISHED"] = "editFinished";
|
|
784
|
-
EEvent["USERS_UPDATE"] = "usersUpdate";
|
|
785
|
-
})(EEvent = MessageBroker.EEvent || (MessageBroker.EEvent = {}));
|
|
786
781
|
/**
|
|
787
782
|
* Communicates with a server message broker.
|
|
788
783
|
*/
|
|
789
784
|
class WebSocketBroker {
|
|
790
785
|
constructor(uri, env) {
|
|
791
|
-
this.uri = uri;
|
|
792
|
-
this.env = env;
|
|
793
786
|
this.subscriptions = new Map();
|
|
794
787
|
this.reconnects = 0;
|
|
795
788
|
this.maxReconnects = 5;
|
|
789
|
+
this.uri = uri;
|
|
790
|
+
this.env = env ? env : Api.EEnv.PROD;
|
|
796
791
|
this.connect();
|
|
797
792
|
}
|
|
793
|
+
get IsConnected() {
|
|
794
|
+
return this.ws.readyState === WebSocket.OPEN;
|
|
795
|
+
}
|
|
796
|
+
SetUserId(userId) {
|
|
797
|
+
this.userId = this.userId ? this.userId : userId;
|
|
798
|
+
}
|
|
798
799
|
connect() {
|
|
799
800
|
try {
|
|
800
801
|
if (!WebSocket) {
|
|
@@ -814,9 +815,6 @@ var MessageBroker;
|
|
|
814
815
|
const ws = document.location.protocol === "https:" ? "wss" : "ws";
|
|
815
816
|
return uri.replace(/^(https|http)/, ws) + "websocket";
|
|
816
817
|
}
|
|
817
|
-
SetUser(user) {
|
|
818
|
-
this.user = this.user ? this.user : user;
|
|
819
|
-
}
|
|
820
818
|
onOpen(ev) {
|
|
821
819
|
if (this.env === Api.EEnv.DEV) {
|
|
822
820
|
console.log("MessageBroker connection opened: ", ev);
|
|
@@ -846,55 +844,45 @@ var MessageBroker;
|
|
|
846
844
|
}
|
|
847
845
|
subscribers.push(callback);
|
|
848
846
|
this.subscriptions.set(topic, subscribers);
|
|
849
|
-
if (this.env === Api.EEnv.DEV) {
|
|
850
|
-
console.groupCollapsed("MessageBroker subscription added: ", topic);
|
|
851
|
-
console.table(Array.from(this.subscriptions.entries()));
|
|
852
|
-
console.groupEnd();
|
|
853
|
-
}
|
|
854
847
|
}
|
|
855
848
|
/**
|
|
856
849
|
* Send message to server.
|
|
857
|
-
* @param
|
|
850
|
+
* @param base
|
|
858
851
|
* @returns
|
|
859
852
|
*/
|
|
860
|
-
|
|
853
|
+
SendMessage(base) {
|
|
861
854
|
if (this.ws.readyState !== WebSocket.OPEN) {
|
|
862
855
|
return;
|
|
863
856
|
}
|
|
864
|
-
if (!this.user) {
|
|
865
|
-
console.error("Set user first!");
|
|
866
|
-
}
|
|
867
857
|
try {
|
|
868
|
-
const
|
|
858
|
+
const msg = base;
|
|
859
|
+
if (!msg.event) {
|
|
860
|
+
msg.event = EAction.BROADCAST;
|
|
861
|
+
}
|
|
862
|
+
let message = base.data ? base.data : {};
|
|
863
|
+
if (typeof message == "object") {
|
|
864
|
+
message = JSON.stringify(message);
|
|
865
|
+
}
|
|
866
|
+
msg.data = message;
|
|
867
|
+
const outgoingMessage = Object.assign(Object.assign({}, msg), { user: this.userId, time: Date.now() });
|
|
869
868
|
this.ws.send(JSON.stringify(outgoingMessage));
|
|
870
869
|
}
|
|
871
870
|
catch (e) {
|
|
872
871
|
console.warn(e);
|
|
873
872
|
}
|
|
874
873
|
}
|
|
875
|
-
get IsConnected() {
|
|
876
|
-
return this.ws.readyState === WebSocket.OPEN;
|
|
877
|
-
}
|
|
878
874
|
/**
|
|
879
875
|
* Add a subscriber to a topic.
|
|
880
876
|
* @param topic
|
|
881
877
|
* @param callback
|
|
882
878
|
* @param action
|
|
883
879
|
*/
|
|
884
|
-
Subscribe(topic, callback, action
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
if (this.env === Api.EEnv.DEV) {
|
|
888
|
-
console.log(`Subscribe [${action}] called for topic`, topic);
|
|
880
|
+
Subscribe(topic, callback, action) {
|
|
881
|
+
if (!action) {
|
|
882
|
+
action = EAction.SUBSCRIBE;
|
|
889
883
|
}
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
* Add a shadow subscriber to a topic.
|
|
893
|
-
* @param topic
|
|
894
|
-
* @param callback
|
|
895
|
-
*/
|
|
896
|
-
SubscribeShadow(topic, callback) {
|
|
897
|
-
this.Subscribe(topic, callback, EAction.SUBSCRIBE_SHADOW);
|
|
884
|
+
this.addSubscriber(topic, callback);
|
|
885
|
+
this.SendMessage({ topic, action });
|
|
898
886
|
}
|
|
899
887
|
/**
|
|
900
888
|
* Remove a subscriber from a topic.
|
|
@@ -904,19 +892,11 @@ var MessageBroker;
|
|
|
904
892
|
Unsubscribe(topic, callback) {
|
|
905
893
|
const subscribers = this.subscriptions.get(topic) || [];
|
|
906
894
|
this.subscriptions.set(topic, subscribers.filter(fn => fn != callback));
|
|
907
|
-
this.
|
|
908
|
-
if (this.env === Api.EEnv.DEV) {
|
|
909
|
-
console.groupCollapsed("MessageBroker subscription removed: ", topic);
|
|
910
|
-
console.table(Array.from(this.subscriptions.entries()));
|
|
911
|
-
console.groupEnd();
|
|
912
|
-
}
|
|
913
|
-
}
|
|
914
|
-
SendBroadcastMessage(msg) {
|
|
915
|
-
this.sendMessage(Object.assign(Object.assign({}, msg), { action: EAction.BROADCAST }));
|
|
895
|
+
this.SendMessage({ topic, action: EAction.UNSUBSCRIBE });
|
|
916
896
|
}
|
|
917
897
|
RequestTopicUsers(topic, callback) {
|
|
918
898
|
this.addSubscriber(topic, callback);
|
|
919
|
-
this.
|
|
899
|
+
this.SendMessage({ topic, event: EAction.GET_TOPIC_SUBSCRIBERS });
|
|
920
900
|
}
|
|
921
901
|
}
|
|
922
902
|
MessageBroker.WebSocketBroker = WebSocketBroker;
|