Skip to content

Commit 0285efa

Browse files
Merge pull request #34 from Geode-solutions/feat_request_counter
feat(request_counter): use in api_fetch
2 parents a6a56d3 + 2ed6418 commit 0285efa

File tree

11 files changed

+139
-94
lines changed

11 files changed

+139
-94
lines changed

components/Launcher.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<vue-recaptcha ref="recaptcha" :sitekey="site_key" :loadRecaptchaScript="true"
99
@expired="is_captcha_validated = false" @verify="submit_recaptcha" align-self="center" />
1010
</v-col>
11-
<v-col v-if="!is_cloud_running && is_connexion_launched">
11+
<v-col v-if="!cloud_store.is_running">
1212
<Loading />
1313
</v-col>
1414
</v-row>
@@ -20,7 +20,7 @@ import { VueRecaptcha } from "vue-recaptcha"
2020
2121
const websocket_store = use_websocket_store()
2222
const cloud_store = use_cloud_store()
23-
const { is_cloud_running, is_captcha_validated, is_connexion_launched } = storeToRefs(cloud_store)
23+
const { is_captcha_validated } = storeToRefs(cloud_store)
2424
2525
const site_key = useRuntimeConfig().public.RECAPTCHA_SITE_KEY
2626

components/PackagesVersions.vue

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ const props = defineProps({
2222
const { route_prefix } = props
2323
2424
const cloud_store = use_cloud_store()
25-
const { is_cloud_running } = storeToRefs(cloud_store)
2625
2726
const packages_versions = ref([])
2827
@@ -34,20 +33,20 @@ async function get_packages_versions () {
3433
})
3534
}
3635
37-
watch(is_cloud_running, (value) => {
36+
watch(cloud_store.is_running, (value) => {
3837
if (value === true) {
3938
get_packages_versions()
4039
}
4140
})
4241
4342
onMounted(() => {
44-
if (is_cloud_running.value === true) {
43+
if (cloud_store.is_running === true) {
4544
get_packages_versions()
4645
}
4746
})
4847
4948
onActivated(() => {
50-
if (is_cloud_running.value === true) {
49+
if (cloud_store.is_running === true) {
5150
get_packages_versions()
5251
}
5352
})

components/RemoteRenderingView.client.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { useElementSize } from '@vueuse/core'
1414
const viewer_store = use_viewer_store()
1515
const { picking_mode } = storeToRefs(viewer_store)
1616
const websocket_store = use_websocket_store()
17-
const { client, is_client_created } = storeToRefs(websocket_store)
17+
const { client } = storeToRefs(websocket_store)
1818
1919
function get_x_y (event) {
2020
if (picking_mode.value === true) {
@@ -77,7 +77,7 @@ onMounted(async () => {
7777
})
7878
7979
function connect () {
80-
if (!is_client_created.value) { return }
80+
if (!websocket_store.is_running) { return }
8181
console.log('connecting', client.value)
8282
const session = client.value.getConnection().getSession()
8383
view.setSession(session)

components/Wrapper.vue

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
<v-col>
55
<Header :tool_name="tool_name" :cards_list="cards_list" />
66
</v-col>
7-
<v-col v-if="!is_cloud_running">
7+
<v-col v-if="!cloud_store.is_running">
88
<Launcher />
99
</v-col>
10-
<v-col v-if="is_cloud_running">
10+
<v-col v-if="cloud_store.is_running">
1111
<Stepper />
1212
</v-col>
13-
<v-col v-if="is_cloud_running">
13+
<v-col v-if="cloud_store.is_running">
1414
<PackagesVersions :route_prefix="route_prefix" />
1515
</v-col>
1616
</v-row>
@@ -19,7 +19,6 @@
1919

2020
<script setup>
2121
const cloud_store = use_cloud_store()
22-
const { is_cloud_running } = storeToRefs(cloud_store)
2322
2423
const props = defineProps({
2524
cards_list: { type: Array, required: true },

composables/api_fetch.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
export function api_fetch (request_url, request_options, { request_error_function, response_function, response_error_function } = {}) {
22
const errors_store = use_errors_store()
3-
const cloud_store = use_cloud_store()
3+
const geode_store = use_geode_store()
4+
5+
geode_store.start_request()
46
return useFetch(request_url,
57
{
6-
baseURL: cloud_store.geode_url,
8+
baseURL: geode_store.base_url,
79
...request_options,
810
onRequestError ({ error }) {
11+
geode_store.stop_request()
912
// MUST STAY HERE FOR EASIER DEBUG
1013
// console.log('onRequestError', error)
1114
errors_store.add_error({ "code": '', "route": request_url, 'name': error.message, 'description': error.stack })
1215
if (request_error_function) { request_error_function(error) }
1316
},
1417
onResponse ({ response }) {
1518
if (response.ok) {
19+
geode_store.stop_request()
1620
// MUST STAY HERE FOR EASIER DEBUG
1721
// console.log('onResponse', response)
1822
if (response_function) { response_function(response) }
1923
}
2024
},
2125
onResponseError ({ response }) {
26+
geode_store.stop_request()
2227
// MUST STAY HERE FOR EASIER DEBUG
2328
// console.log('onResponseError', response)
2429
errors_store.add_error({ "code": response.status, "route": request_url, 'name': response._data.name, 'description': response._data.description })

nuxt.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ export default defineNuxtConfig({
22
imports: {
33
dirs: ['stores']
44
},
5+
6+
modules: [['@pinia/nuxt', { autoImports: ['defineStore'] }]],
57
})

stores/cloud.js

Lines changed: 12 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,23 @@
1-
import { defineStore } from 'pinia'
21
import { useStorage } from '@vueuse/core'
32

43
export const use_cloud_store = defineStore('cloud', {
54
state: () => ({
65
ID: useStorage('ID', ''),
76
is_captcha_validated: false,
8-
is_cloud_running: false,
97
is_connexion_launched: false,
10-
request_counter: 0
118
}),
129
getters: {
13-
geode_url: (state) => {
14-
const public_runtime_config = useRuntimeConfig().public
15-
var geode_url = `${public_runtime_config.GEODE_PROTOCOL}://${public_runtime_config.API_URL}:${public_runtime_config.GEODE_PORT}`
16-
if (process.env.NODE_ENV == 'production') {
17-
geode_url = geode_url + `/${state.ID}/geode`
18-
}
19-
return geode_url
20-
},
21-
viewer_url: (state) => {
22-
const public_runtime_config = useRuntimeConfig().public
23-
var viewer_url = `${public_runtime_config.VIEWER_PROTOCOL}://${public_runtime_config.API_URL}:${public_runtime_config.VIEWER_PORT}`
24-
if (process.env.NODE_ENV == 'production') {
25-
viewer_url = viewer_url + `/${state.ID}/viewer`
26-
}
27-
viewer_url = viewer_url + '/ws'
28-
return viewer_url
10+
is_running: () => {
11+
return use_geode_store().is_running && use_websocket_store().is_running
2912
},
13+
is_busy: () => {
14+
return use_geode_store().is_busy || use_websocket_store().is_busy
15+
}
16+
3017
},
3118
actions: {
3219
async create_connexion () {
20+
const geode_store = use_geode_store()
3321
if (this.is_connexion_launched) { return }
3422
this.is_connexion_launched = true
3523
if (this.ID === '' || this.ID === null || typeof this.ID === 'undefined') {
@@ -38,50 +26,28 @@ export const use_cloud_store = defineStore('cloud', {
3826
const { data, error } = await useFetch(`${this.geode_url}/ping`, { method: 'POST' })
3927
console.log("error", error)
4028
if (data.value !== null) {
41-
this.is_cloud_running = true
42-
return this.ping_task()
29+
geode_store.is_running = true
30+
return geode_store.ping_task()
4331
} else {
4432
return this.create_backend()
4533
}
4634
}
4735
},
4836
async create_backend () {
37+
const geode_store = use_geode_store()
4938
const errors_store = use_errors_store()
5039
const config = useRuntimeConfig()
5140
const public_runtime_config = config.public
5241
const { data, error } = await useFetch(`${public_runtime_config.GEODE_PROTOCOL}://${public_runtime_config.API_URL}:${public_runtime_config.GEODE_PORT}${public_runtime_config.SITE_BRANCH}/${public_runtime_config.PROJECT}/createbackend`, { method: 'POST' })
5342
if (data.value !== null) {
5443
this.ID = data.value.ID
5544
localStorage.setItem('ID', data.value.ID)
56-
this.is_cloud_running = true
57-
return this.ping_task()
45+
geode_store.is_running = true
46+
return geode_store.ping_task()
5847
} else {
5948
console.log("error : ", error)
6049
errors_store.server_error = true
6150
}
6251
},
63-
64-
ping_task () {
65-
setInterval(() => this.do_ping(), 10 * 1000)
66-
},
67-
async do_ping () {
68-
const errors_store = use_errors_store()
69-
const { data, error } = await useFetch(`${this.geode_url}/ping`, { method: 'POST' })
70-
if (data.value !== null) {
71-
this.is_cloud_running = true
72-
} else {
73-
errors_store.server_error = true
74-
console.log("error : ", error)
75-
}
76-
},
77-
78-
mutations: {
79-
start_request (state) {
80-
state.request_counter++
81-
},
82-
stop_request (state) {
83-
state.request_counter--
84-
}
85-
}
8652
}
8753
})

stores/errors.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { defineStore } from 'pinia'
2-
31
export const use_errors_store = defineStore('errors', {
42
state: () => ({
53
errors: [],

stores/geode.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
export const use_geode_store = defineStore('geode', {
2+
state: () => ({
3+
request_counter: 0,
4+
is_running: false
5+
}),
6+
getters: {
7+
base_url: () => {
8+
const cloud_store = use_cloud_store()
9+
const public_runtime_config = useRuntimeConfig().public
10+
var geode_url = `${public_runtime_config.GEODE_PROTOCOL}://${public_runtime_config.API_URL}:${public_runtime_config.GEODE_PORT}`
11+
if (process.env.NODE_ENV == 'production') {
12+
geode_url = geode_url + `/${cloud_store.ID}/geode`
13+
}
14+
return geode_url
15+
},
16+
is_busy: (state) => {
17+
return state.request_counter > 0
18+
}
19+
},
20+
actions: {
21+
ping_task () {
22+
setInterval(() => this.do_ping(), 10 * 1000)
23+
},
24+
async do_ping () {
25+
const errors_store = use_errors_store()
26+
const { data, error } = await useFetch(`${this.base_url}/ping`, { method: 'POST' })
27+
if (data.value !== null) {
28+
this.is_running = true
29+
} else {
30+
errors_store.server_error = true
31+
console.log("error : ", error)
32+
}
33+
},
34+
start_request () {
35+
this.request_counter++
36+
},
37+
stop_request () {
38+
this.request_counter--
39+
}
40+
}
41+
})

0 commit comments

Comments
 (0)