|
1 | 1 | import back_schemas from "@geode/opengeodeweb-back/schemas.json" |
2 | 2 | import Status from "@ogw_f/utils/status.js" |
3 | 3 |
|
4 | | -export const use_geode_store = defineStore("geode", { |
5 | | - state: () => ({ |
6 | | - default_local_port: "5000", |
7 | | - request_counter: 0, |
8 | | - status: Status.NOT_CONNECTED, |
9 | | - }), |
10 | | - getters: { |
11 | | - protocol() { |
12 | | - if (use_infra_store().is_cloud) { |
13 | | - return "https" |
14 | | - } |
15 | | - return "http" |
16 | | - }, |
17 | | - port() { |
18 | | - if (use_infra_store().is_cloud) { |
19 | | - return "443" |
| 4 | +export const use_geode_store = defineStore("geode", () => { |
| 5 | + const default_local_port = "5000"; |
| 6 | + const request_counter = ref(0); |
| 7 | + const status = ref(Status.NOT_CONNECTED); |
| 8 | + |
| 9 | + function getProtocol() { |
| 10 | + if (use_infra_store().is_cloud) { |
| 11 | + return "https" |
| 12 | + } |
| 13 | + return "http" |
| 14 | + } |
| 15 | + |
| 16 | + function getPort() { |
| 17 | + if (use_infra_store().is_cloud) { |
| 18 | + return "443" |
| 19 | + } |
| 20 | + return default_local_port |
| 21 | + } |
| 22 | + |
| 23 | + function getBaseUrl() { |
| 24 | + const infra_store = use_infra_store() |
| 25 | + let geode_url = `${getProtocol()}://${infra_store.domain_name}:${getPort()}` |
| 26 | + if (infra_store.is_cloud) { |
| 27 | + if (infra_store.ID == "") { |
| 28 | + throw new Error("ID must not be empty in cloud mode") |
20 | 29 | } |
21 | | - return this.default_local_port |
22 | | - }, |
23 | | - base_url() { |
24 | | - const infra_store = use_infra_store() |
25 | | - let geode_url = `${this.protocol}://${infra_store.domain_name}:${this.port}` |
26 | | - if (infra_store.is_cloud) { |
27 | | - if (infra_store.ID == "") { |
28 | | - throw new Error("ID must not be empty in cloud mode") |
29 | | - } |
30 | | - geode_url += `/${infra_store.ID}/geode` |
| 30 | + geode_url += `/${infra_store.ID}/geode` |
| 31 | + } |
| 32 | + return geode_url |
| 33 | + } |
| 34 | + |
| 35 | + const is_busy = computed(() => { |
| 36 | + return request_counter.value > 0 |
| 37 | + }); |
| 38 | + |
| 39 | + function ping_task() { |
| 40 | + setInterval(() => { |
| 41 | + if (status.value == Status.CONNECTED) { |
| 42 | + do_ping() |
31 | 43 | } |
32 | | - return geode_url |
33 | | - }, |
34 | | - is_busy() { |
35 | | - return this.request_counter > 0 |
36 | | - }, |
37 | | - }, |
38 | | - actions: { |
39 | | - ping_task() { |
40 | | - setInterval(() => { |
41 | | - if (this.status == Status.CONNECTED) { |
42 | | - this.do_ping() |
| 44 | + }, 10 * 1000) |
| 45 | + } |
| 46 | + |
| 47 | + async function do_ping() { |
| 48 | + const geode_store = this |
| 49 | + const feedback_store = use_feedback_store() |
| 50 | + return useFetch(back_schemas.opengeodeweb_back.ping.$id, { |
| 51 | + baseURL: getBaseUrl(), |
| 52 | + method: back_schemas.opengeodeweb_back.ping.methods[0], |
| 53 | + body: {}, |
| 54 | + onRequestError({ error }) { |
| 55 | + feedback_store.server_error = true |
| 56 | + status.value = Status.NOT_CONNECTED |
| 57 | + }, |
| 58 | + onResponse({ response }) { |
| 59 | + if (response.ok) { |
| 60 | + feedback_store.server_error = false |
| 61 | + status.value = Status.CONNECTED |
43 | 62 | } |
44 | | - }, 10 * 1000) |
45 | | - }, |
46 | | - do_ping() { |
47 | | - const geode_store = this |
48 | | - const feedback_store = use_feedback_store() |
49 | | - return useFetch(back_schemas.opengeodeweb_back.ping.$id, { |
50 | | - baseURL: this.base_url, |
51 | | - method: back_schemas.opengeodeweb_back.ping.methods[0], |
52 | | - body: {}, |
53 | | - onRequestError({ error }) { |
54 | | - feedback_store.server_error = true |
55 | | - geode_store.status = Status.NOT_CONNECTED |
56 | | - }, |
57 | | - onResponse({ response }) { |
58 | | - if (response.ok) { |
59 | | - feedback_store.server_error = false |
60 | | - geode_store.status = Status.CONNECTED |
61 | | - } |
62 | | - }, |
63 | | - onResponseError({ response }) { |
64 | | - feedback_store.server_error = true |
65 | | - geode_store.status = Status.NOT_CONNECTED |
66 | | - }, |
67 | | - }) |
68 | | - }, |
69 | | - start_request() { |
70 | | - this.request_counter++ |
71 | | - }, |
72 | | - stop_request() { |
73 | | - this.request_counter-- |
74 | | - }, |
75 | | - }, |
76 | | - share: { |
77 | | - omit: ["status"], |
78 | | - }, |
| 63 | + }, |
| 64 | + onResponseError({ response }) { |
| 65 | + feedback_store.server_error = true |
| 66 | + status.value = Status.NOT_CONNECTED |
| 67 | + }, |
| 68 | + }) |
| 69 | + } |
| 70 | + |
| 71 | + function start_request() { |
| 72 | + request_counter.value++ |
| 73 | + } |
| 74 | + |
| 75 | + function stop_request() { |
| 76 | + request_counter.value-- |
| 77 | + } |
| 78 | + |
| 79 | + return { |
| 80 | + request_counter, |
| 81 | + status, |
| 82 | + getProtocol, |
| 83 | + getPort, |
| 84 | + getBaseUrl, |
| 85 | + is_busy, |
| 86 | + ping_task, |
| 87 | + do_ping, |
| 88 | + start_request, |
| 89 | + stop_request, |
| 90 | + } |
79 | 91 | }) |
0 commit comments