@dative-gpi/foundation-shared-services 0.0.53 → 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.
@@ -3,9 +3,8 @@ import { ComposableFactory, ServiceFactory } from "@dative-gpi/bones-ui";
3
3
 
4
4
  import { APPLICATION_CURRENT_URL } from "../../config/urls";
5
5
 
6
- const ApplicationServiceFactory = new ServiceFactory<ApplicationDetailsDTO, ApplicationDetails>("application", ApplicationDetails).create(factory => factory.build(
7
- factory.addCustom("getCurrent", (axios) => axios.get(APPLICATION_CURRENT_URL())),
8
- factory.addNotify()
9
- ));
6
+ const ApplicationServiceFactory = {
7
+ ...ServiceFactory.addCustom("getCurrent", (axios) => axios.get(APPLICATION_CURRENT_URL()), (dto: ApplicationDetailsDTO) => new ApplicationDetails(dto))
8
+ };
10
9
 
11
- export const useCurrentApplication = ComposableFactory.custom(ApplicationServiceFactory, ApplicationServiceFactory.getCurrent);
10
+ export const useCurrentApplication = ComposableFactory.custom(ApplicationServiceFactory.getCurrent);
@@ -1,11 +1,10 @@
1
1
  import { ComposableFactory, ServiceFactory } from "@dative-gpi/bones-ui";
2
- import { BlurHash } from "@dative-gpi/foundation-shared-domain/models";
2
+ import { BlurHash, BlurHashDTO } from "@dative-gpi/foundation-shared-domain/models";
3
3
 
4
4
  import { IMAGE_BLURHASH_URL } from "../../config/urls";
5
5
 
6
- const ImageServiceFactory = new ServiceFactory("image", BlurHash).create(factory => factory.build(
7
- factory.addCustom("getBlurHash", (axios, imageId: string) => axios.get(IMAGE_BLURHASH_URL(imageId))),
8
- factory.addNotify()
9
- ));
6
+ const ImageServiceFactory = {
7
+ ...ServiceFactory.addCustom("getBlurHash", (axios, imageId: string) => axios.get(IMAGE_BLURHASH_URL(imageId)), (dto: BlurHashDTO) => new BlurHash(dto))
8
+ };
10
9
 
11
- export const useImageBlurHash = ComposableFactory.custom(ImageServiceFactory, ImageServiceFactory.getBlurHash);
10
+ export const useImageBlurHash = ComposableFactory.custom(ImageServiceFactory.getBlurHash);
@@ -1,41 +1,10 @@
1
- import { ref } from "vue";
2
-
3
- import { TranslationInfos, TranslationInfosDTO, TranslationDetails, TranslationDetailsDTO } from "@dative-gpi/foundation-shared-domain/models";
4
- import { ServiceFactory } from "@dative-gpi/bones-ui";
1
+ import { TranslationInfos, TranslationInfosDTO } from "@dative-gpi/foundation-shared-domain/models";
2
+ import { ComposableFactory, ServiceFactory } from "@dative-gpi/bones-ui";
5
3
 
6
4
  import { TRANSLATIONS_LANGUAGE_URL } from "../../config/urls";
7
5
 
8
- const TranslationServiceFactory = new ServiceFactory<TranslationDetailsDTO, TranslationDetails>("translation", TranslationDetails).create(factory => factory.build(
9
- factory.addNotify(() => ({
10
- getMany: async (languageCode: string): Promise<TranslationInfos[]> => {
11
- const response = await ServiceFactory.http.get(TRANSLATIONS_LANGUAGE_URL(languageCode));
12
- const result = response.data.map((dto: TranslationInfosDTO) => new TranslationInfos(dto));
13
-
14
- return result;
15
- }
16
- }))
17
- ));
18
-
19
- export const useTranslations = () => {
20
- const service = TranslationServiceFactory();
21
-
22
- const fetching = ref(false);
23
- const entities = ref<TranslationInfos[]>([]);
24
-
25
- const getMany = async (languageCode: string) => {
26
- fetching.value = true;
27
- try {
28
- entities.value = await service.getMany(languageCode);
29
- }
30
- finally {
31
- fetching.value = false;
32
- }
33
- return entities;
34
- }
6
+ const TranslationServiceFactory = {
7
+ ...ServiceFactory.addCustom("getMany", (axios, languageCode: string) => axios.get(TRANSLATIONS_LANGUAGE_URL(languageCode)), (dtos: TranslationInfosDTO[]) => dtos.map(dto => new TranslationInfos(dto)))
8
+ };
35
9
 
36
- return {
37
- fetching,
38
- getMany,
39
- entities
40
- }
41
- }
10
+ export const useTranslations = ComposableFactory.custom(TranslationServiceFactory.getMany);
@@ -1,13 +1,30 @@
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
 
6
7
  const UserServiceFactory = new ServiceFactory<UserDetailsDTO, UserDetails>("user", UserDetails).create(factory => factory.build(
7
- factory.addCustom("getCurrent", (axios) => axios.get(USER_CURRENT_URL())),
8
- factory.addCustom("updateCurrent", (axios, payload: UpdateUserDTO) => axios.post(USER_CURRENT_URL(), payload)),
9
- factory.addNotify()
8
+ ServiceFactory.addCustom("getCurrent", (axios) => axios.get(USER_CURRENT_URL()), (dto: UserDetailsDTO) => new UserDetails(dto)),
9
+ factory.addNotify(notify => ({
10
+ ...ServiceFactory.addCustom("updateCurrent", (axios, payload: UpdateUserDTO) => axios.post(USER_CURRENT_URL(), payload), (dto: UserDetailsDTO) => {
11
+ const result = new UserDetails(dto);
12
+ notify.notify("update", result);
13
+ return result;
14
+ })
15
+ }))
10
16
  ));
11
17
 
12
- export const useCurrentUser = ComposableFactory.custom(UserServiceFactory, UserServiceFactory.getCurrent);
13
- export const useUpdateCurrentUser = ComposableFactory.custom(UserServiceFactory, UserServiceFactory.updateCurrent);
18
+ export const useTrackUser = ComposableFactory.track(UserServiceFactory);
19
+ export const useTrackUserRef = ComposableFactory.trackRef(UserServiceFactory);
20
+
21
+ const trackUser = () => {
22
+ const { track } = useTrackUserRef();
23
+
24
+ return (user: Ref<UserDetails>) => {
25
+ track(user);
26
+ }
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.53",
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.67",
14
- "@dative-gpi/foundation-shared-domain": "0.0.53",
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": "4ecd4c47590cb49ab9f480e12202cde9af5396e1"
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";