Skip to content

Commit 51c38c8

Browse files
committed
fix(stage-*): not respond if no websocket works
1 parent b6497dc commit 51c38c8

File tree

14 files changed

+187
-220
lines changed

14 files changed

+187
-220
lines changed

apps/stage-tamagotchi-electron/src/renderer/main.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
import { initializeApp } from '@proj-airi/stage-ui/services'
1+
import type { Plugin } from 'vue'
2+
3+
import Tres from '@tresjs/core'
4+
5+
import { autoAnimatePlugin } from '@formkit/auto-animate/vue'
6+
import { MotionPlugin } from '@vueuse/motion'
27
import { createPinia } from 'pinia'
38
import { setupLayouts } from 'virtual:generated-layouts'
9+
import { createApp } from 'vue'
410
import { createRouter, createWebHashHistory } from 'vue-router'
511
import { routes } from 'vue-router/auto-routes'
612

@@ -32,11 +38,12 @@ const router = createRouter({
3238
routes: setupLayouts(routes),
3339
})
3440

35-
// Initialize and mount the app using the shared initialization logic
36-
initializeApp(App, {
37-
router,
38-
pinia,
39-
i18n,
40-
}).then((app) => {
41-
app.mount('#app')
42-
})
41+
createApp(App)
42+
.use(MotionPlugin)
43+
// TODO: Fix autoAnimatePlugin type error
44+
.use(autoAnimatePlugin as unknown as Plugin)
45+
.use(router)
46+
.use(pinia)
47+
.use(i18n)
48+
.use(Tres)
49+
.mount('#app')

apps/stage-tamagotchi/src/main.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
import { initializeApp } from '@proj-airi/stage-ui/services'
1+
import type { Plugin } from 'vue'
2+
3+
import Tres from '@tresjs/core'
4+
5+
import { autoAnimatePlugin } from '@formkit/auto-animate/vue'
6+
import { MotionPlugin } from '@vueuse/motion'
27
import { createPinia } from 'pinia'
38
import { setupLayouts } from 'virtual:generated-layouts'
9+
import { createApp } from 'vue'
410
import { createRouter, createWebHashHistory } from 'vue-router'
511
import { routes } from 'vue-router/auto-routes'
612

@@ -32,11 +38,12 @@ const router = createRouter({
3238
routes: setupLayouts(routes),
3339
})
3440

35-
// Initialize and mount the app using the shared initialization logic
36-
initializeApp(App, {
37-
router,
38-
pinia,
39-
i18n,
40-
}).then((app) => {
41-
app.mount('#app')
42-
})
41+
createApp(App)
42+
.use(MotionPlugin)
43+
// TODO: Fix autoAnimatePlugin type error
44+
.use(autoAnimatePlugin as unknown as Plugin)
45+
.use(router)
46+
.use(pinia)
47+
.use(i18n)
48+
.use(Tres)
49+
.mount('#app')

apps/stage-web/src/App.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<script setup lang="ts">
22
import { OnboardingDialog, ToasterRoot } from '@proj-airi/stage-ui/components'
3+
import { useConfiguratorForAiriSdk } from '@proj-airi/stage-ui/stores/configurator'
34
import { useDisplayModelsStore } from '@proj-airi/stage-ui/stores/display-models'
45
import { useOnboardingStore } from '@proj-airi/stage-ui/stores/onboarding'
56
import { useSettings } from '@proj-airi/stage-ui/stores/settings'
67
import { StageTransitionGroup } from '@proj-airi/ui-transitions'
78
import { useDark } from '@vueuse/core'
89
import { storeToRefs } from 'pinia'
9-
import { computed, onMounted, watch } from 'vue'
10+
import { computed, onMounted, onUnmounted, watch } from 'vue'
1011
import { useI18n } from 'vue-i18n'
1112
import { RouterView } from 'vue-router'
1213
import { toast, Toaster } from 'vue-sonner'
@@ -23,6 +24,7 @@ const settings = storeToRefs(settingsStore)
2324
const onboardingStore = useOnboardingStore()
2425
const { shouldShowSetup } = storeToRefs(onboardingStore)
2526
const isDark = useDark()
27+
const { dispose } = useConfiguratorForAiriSdk()
2628
2729
const primaryColor = computed(() => {
2830
return isDark.value
@@ -66,6 +68,10 @@ onMounted(async () => {
6668
await settingsStore.initializeStageModel()
6769
})
6870
71+
onUnmounted(() => {
72+
dispose()
73+
})
74+
6975
// Handle first-time setup events
7076
function handleSetupConfigured() {
7177
onboardingStore.markSetupCompleted()

apps/stage-web/src/main.ts

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import type { App as VueApp } from 'vue'
1+
import type { Plugin } from 'vue'
22
import type { Router } from 'vue-router'
33

4+
import Tres from '@tresjs/core'
45
import NProgress from 'nprogress'
56

6-
import { initializeApp } from '@proj-airi/stage-ui/services'
7+
import { autoAnimatePlugin } from '@formkit/auto-animate/vue'
8+
import { MotionPlugin } from '@vueuse/motion'
79
import { createPinia } from 'pinia'
810
import { setupLayouts } from 'virtual:generated-layouts'
11+
import { createApp } from 'vue'
912
import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
1013
import { routes } from 'vue-router/auto-routes'
1114

@@ -37,30 +40,12 @@ router.afterEach(() => {
3740
NProgress.done()
3841
})
3942

40-
// Custom initialization callback for web-specific post-initialization
41-
async function onInitialized(_app: VueApp, router: Router) {
42-
// Handle PWA registration after router is ready
43-
router.isReady()
44-
.then(async () => {
45-
if (import.meta.env.SSR) {
46-
return
47-
}
48-
if (import.meta.env.VITE_APP_TARGET_HUGGINGFACE_SPACE) {
49-
return
50-
}
51-
52-
const { registerSW } = await import('./modules/pwa')
53-
registerSW({ immediate: true })
54-
})
55-
.catch(error => console.error('Failed during post-initialization:', error))
56-
}
57-
58-
// Initialize and mount the app using the shared initialization logic with web-specific options
59-
initializeApp(App, {
60-
router,
61-
pinia,
62-
i18n,
63-
onInitialized,
64-
}).then((app) => {
65-
app.mount('#app')
66-
})
43+
createApp(App)
44+
.use(MotionPlugin)
45+
// TODO: Fix autoAnimatePlugin type error
46+
.use(autoAnimatePlugin as unknown as Plugin)
47+
.use(router)
48+
.use(pinia)
49+
.use(i18n)
50+
.use(Tres)
51+
.mount('#app')

apps/stage-web/vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export default defineConfig({
4949

5050
resolve: {
5151
alias: {
52+
'@proj-airi/server-sdk': resolve(join(import.meta.dirname, '..', '..', 'packages', 'server-sdk', 'src')),
5253
'@proj-airi/stage-ui': resolve(join(import.meta.dirname, '..', '..', 'packages', 'stage-ui', 'src')),
5354
'@proj-airi/i18n': resolve(join(import.meta.dirname, '..', '..', 'packages', 'i18n', 'src')),
5455
},

packages/server-sdk/src/client.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ import WebSocket from 'crossws/websocket'
44

55
import { sleep } from '@moeru/std'
66

7+
class ReconnectingError extends Error {
8+
constructor(message: string) {
9+
super(message)
10+
this.name = 'ReconnectingError'
11+
}
12+
}
13+
714
export interface ClientOptions<C = undefined> {
815
url?: string
916
name: string
@@ -18,6 +25,7 @@ export interface ClientOptions<C = undefined> {
1825

1926
export class Client<C = undefined> {
2027
private connected = false
28+
private connecting = false
2129
private websocket?: WebSocket
2230
private shouldClose = false
2331

@@ -70,6 +78,10 @@ export class Client<C = undefined> {
7078
return
7179
}
7280
catch (err) {
81+
if (err instanceof ReconnectingError) {
82+
return
83+
}
84+
7385
this.opts.onError?.(err)
7486
const delay = Math.min(2 ** attempts * 1000, 30_000) // capped exponential backoff
7587
await sleep(delay)
@@ -82,25 +94,35 @@ export class Client<C = undefined> {
8294
if (this.shouldClose) {
8395
return
8496
}
97+
8598
await this.retryWithExponentialBackoff(() => this._connect())
8699
}
87100

88101
private _connect(): Promise<void> {
89102
if (this.shouldClose || this.connected) {
90103
return Promise.resolve()
91104
}
105+
if (this.connecting) {
106+
return Promise.reject(new ReconnectingError('Already connecting'))
107+
}
92108

93109
return new Promise((resolve, reject) => {
110+
this.connecting = true
111+
94112
const ws = new WebSocket(this.opts.url)
95113
this.websocket = ws
96114

115+
ws.onmessage = this.handleMessageBound
97116
ws.onerror = (event: any) => {
117+
this.connecting = false
98118
this.connected = false
119+
99120
this.opts.onError?.(event)
100121
reject(event?.error ?? new Error('WebSocket error'))
101122
}
102-
103123
ws.onclose = () => {
124+
this.connecting = false
125+
104126
if (this.connected) {
105127
this.connected = false
106128
this.opts.onClose?.()
@@ -109,11 +131,10 @@ export class Client<C = undefined> {
109131
void this.tryReconnectWithExponentialBackoff()
110132
}
111133
}
112-
113-
ws.onmessage = this.handleMessageBound
114-
115134
ws.onopen = () => {
135+
this.connecting = false
116136
this.connected = true
137+
117138
this.opts.token ? this.tryAuthenticate() : this.tryAnnounce()
118139
resolve()
119140
}

packages/stage-ui/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
"./constants": "./src/constants/index.ts",
2727
"./libs/*": "./src/libs/*.ts",
2828
"./libs": "./src/libs/index.ts",
29-
"./services/*": "./src/services/*.ts",
30-
"./services": "./src/services/index.ts",
3129
"./stores/*": "./src/stores/*.ts",
3230
"./stores": "./src/stores/index.ts",
3331
"./utils": "./src/utils/index.ts"

packages/stage-ui/src/services/app-initializer.ts

Lines changed: 0 additions & 66 deletions
This file was deleted.

packages/stage-ui/src/services/index.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)