@dative-gpi/foundation-shared-services 0.0.54 → 0.0.55

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.
@@ -1,5 +1,6 @@
1
- import { UpdateUserDTO, UserDetails, UserDetailsDTO } from "@dative-gpi/foundation-shared-domain/models";
1
+ import { Ref } from "vue";
2
2
  import { ComposableFactory, ServiceFactory } from "@dative-gpi/bones-ui";
3
+ import { UpdateUserDTO, UserDetails, UserDetailsDTO } from "@dative-gpi/foundation-shared-domain/models";
3
4
 
4
5
  import { USER_CURRENT_URL } from "../../config/urls";
5
6
 
@@ -15,12 +16,15 @@ const UserServiceFactory = new ServiceFactory<UserDetailsDTO, UserDetails>("user
15
16
  ));
16
17
 
17
18
  export const useTrackUser = ComposableFactory.track(UserServiceFactory);
19
+ export const useTrackUserRef = ComposableFactory.trackRef(UserServiceFactory);
18
20
 
19
- export const useCurrentUser = ComposableFactory.custom(UserServiceFactory.getCurrent, () => {
20
- const { track } = useTrackUser();
21
-
22
- return (user) => {
21
+ const trackUser = () => {
22
+ const { track } = useTrackUserRef();
23
+
24
+ return (user: Ref<UserDetails>) => {
23
25
  track(user);
24
26
  }
25
- });
26
- export const useUpdateCurrentUser = ComposableFactory.custom(UserServiceFactory.updateCurrent);
27
+ }
28
+
29
+ export const useCurrentUser = ComposableFactory.custom(UserServiceFactory.getCurrent, trackUser);
30
+ export const useUpdateCurrentUser = ComposableFactory.custom(UserServiceFactory.updateCurrent, trackUser);
@@ -9,6 +9,6 @@ export function useFoundationShared() {
9
9
  const ready = computed(() => timeZoneReady.value && languageCodeReady.value);
10
10
 
11
11
  return {
12
- ready,
12
+ ready
13
13
  };
14
14
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@dative-gpi/foundation-shared-services",
3
3
  "sideEffects": false,
4
- "version": "0.0.54",
4
+ "version": "0.0.55",
5
5
  "description": "",
6
6
  "publishConfig": {
7
7
  "access": "public"
@@ -10,11 +10,11 @@
10
10
  "author": "",
11
11
  "license": "ISC",
12
12
  "dependencies": {
13
- "@dative-gpi/bones-ui": "^0.0.70",
14
- "@dative-gpi/foundation-shared-domain": "0.0.54",
13
+ "@dative-gpi/bones-ui": "^0.0.73",
14
+ "@dative-gpi/foundation-shared-domain": "0.0.55",
15
15
  "@microsoft/signalr": "^8.0.0",
16
16
  "vue": "^3.2.0",
17
17
  "vue-router": "^4.2.5"
18
18
  },
19
- "gitHead": "391503e595cd9d731fca14b5bb2d3a10b6efee95"
19
+ "gitHead": "c7ec3fb302319a1f49bb44f60c288eda9dec57d6"
20
20
  }
@@ -0,0 +1,116 @@
1
+ import { computed, onUnmounted, ref, watch } from "vue";
2
+ import * as signalR from "@microsoft/signalr";
3
+
4
+ export class HubFactory {
5
+ static create(
6
+ url: string | (() => string),
7
+ configureHooks: (connection: signalR.HubConnection,
8
+ { isWatched, hasWatchers }: { isWatched: (id: string) => boolean, hasWatchers: () => boolean }) => void,
9
+ init: (connection: signalR.HubConnection) => Promise<void>) {
10
+
11
+ let connection: signalR.HubConnection | null = null;
12
+ let subscribed = false;
13
+ let watchManySubscribers = 0;
14
+ let watcheds = ref<string[]>([]);
15
+
16
+ return () => {
17
+ const connect = async () => {
18
+ if (!connection) {
19
+ connection = new signalR.HubConnectionBuilder()
20
+ .withUrl(typeof url == "string" ? url : url())
21
+ .configureLogging(signalR.LogLevel.Warning)
22
+ .withAutomaticReconnect()
23
+ .build();
24
+
25
+ configureHooks(connection, {
26
+ isWatched: (id: string) => watcheds.value.includes(id),
27
+ hasWatchers: () => watchManySubscribers > 0,
28
+ })
29
+ }
30
+ if (connection.state !== signalR.HubConnectionState.Connected) {
31
+ await connection.start();
32
+ }
33
+ if (!subscribed) {
34
+ await init(connection);
35
+ subscribed = true;
36
+ }
37
+ }
38
+
39
+ const disconnect = async () => {
40
+ if (connection) {
41
+ await connection.stop();
42
+ connection = null;
43
+ }
44
+ }
45
+
46
+ const hasSubscribers = computed(() => watcheds.value.length > 0 || watchManySubscribers > 0);
47
+
48
+ watch(hasSubscribers, async (value) => {
49
+ if (value) {
50
+ await connect();
51
+ } else {
52
+ await disconnect();
53
+ }
54
+ }, { immediate: true });
55
+
56
+ const subscribeToOne = (alertId: string) => {
57
+ watcheds.value.push(alertId);
58
+ }
59
+
60
+ const unsubscribeFromOne = (alertId: string) => {
61
+ const index = watcheds.value.indexOf(alertId);
62
+ if (index > -1) watcheds.value = watcheds.value.splice(index, 1);
63
+ }
64
+
65
+ const subscribeToMany = () => {
66
+ watchManySubscribers++;
67
+ }
68
+
69
+ const unsubscribeFromMany = () => {
70
+ watchManySubscribers--;
71
+ }
72
+
73
+ return {
74
+ subscribeToOne,
75
+ unsubscribeFromOne,
76
+ subscribeToMany,
77
+ unsubscribeFromMany
78
+ }
79
+ }
80
+ }
81
+
82
+ static createWatcher(factory: ReturnType<typeof this.create>) {
83
+ return () => {
84
+ const { subscribeToOne, unsubscribeFromOne, subscribeToMany, unsubscribeFromMany } = factory();
85
+
86
+ const manySubscriptions = ref(false);
87
+ const oneSubscriptions = ref<string[]>([]);
88
+
89
+ onUnmounted(() => {
90
+ if (manySubscriptions.value) {
91
+ unsubscribeFromMany();
92
+ }
93
+ for (let subscription of oneSubscriptions.value) {
94
+ unsubscribeFromOne(subscription);
95
+ }
96
+ })
97
+
98
+ const watchOne = (id: string) => {
99
+ subscribeToOne(id);
100
+ oneSubscriptions.value.push(id);
101
+ }
102
+
103
+ const watchMany = () => {
104
+ if (manySubscriptions.value === false) {
105
+ subscribeToMany();
106
+ manySubscriptions.value = true;
107
+ }
108
+ }
109
+
110
+ return {
111
+ watchOne,
112
+ watchMany
113
+ }
114
+ }
115
+ }
116
+ }
package/tools/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./hubFactory";