mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-03-05 13:10:24 +00:00
test: simplify test file mocking patterns (#8320)
Simplifies test mocking patterns across multiple test files. - Removes redundant `vi.hoisted()` calls - Cleans up mock implementations - Removes unused imports and variables ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-8320-test-simplify-test-file-mocking-patterns-2f46d73d36508150981bd8ecb99a6a11) by [Unito](https://www.unito.io) --------- Co-authored-by: Amp <amp@ampcode.com> Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
82
src/services/useNewUserService.ts
Normal file
82
src/services/useNewUserService.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
import { ref, shallowRef } from 'vue'
|
||||
import { createSharedComposable } from '@vueuse/core'
|
||||
import { useSettingStore } from '@/platform/settings/settingStore'
|
||||
|
||||
function _useNewUserService() {
|
||||
const settingStore = useSettingStore()
|
||||
const pendingCallbacks = shallowRef<Array<() => Promise<void>>>([])
|
||||
const isNewUserDetermined = ref(false)
|
||||
const isNewUserCached = ref<boolean | null>(null)
|
||||
|
||||
function reset() {
|
||||
pendingCallbacks.value = []
|
||||
isNewUserDetermined.value = false
|
||||
isNewUserCached.value = null
|
||||
}
|
||||
|
||||
function checkIsNewUser(): boolean {
|
||||
const isNewUserSettings =
|
||||
Object.keys(settingStore.settingValues).length === 0 ||
|
||||
!settingStore.get('Comfy.TutorialCompleted')
|
||||
const hasNoWorkflow = !localStorage.getItem('workflow')
|
||||
const hasNoPreviousWorkflow = !localStorage.getItem(
|
||||
'Comfy.PreviousWorkflow'
|
||||
)
|
||||
|
||||
return isNewUserSettings && hasNoWorkflow && hasNoPreviousWorkflow
|
||||
}
|
||||
|
||||
async function registerInitCallback(callback: () => Promise<void>) {
|
||||
if (isNewUserDetermined.value) {
|
||||
if (isNewUserCached.value) {
|
||||
try {
|
||||
await callback()
|
||||
} catch (error) {
|
||||
console.error('New user initialization callback failed:', error)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
pendingCallbacks.value = [...pendingCallbacks.value, callback]
|
||||
}
|
||||
}
|
||||
|
||||
async function initializeIfNewUser() {
|
||||
if (isNewUserDetermined.value) return
|
||||
|
||||
isNewUserCached.value = checkIsNewUser()
|
||||
isNewUserDetermined.value = true
|
||||
|
||||
if (!isNewUserCached.value) {
|
||||
pendingCallbacks.value = []
|
||||
return
|
||||
}
|
||||
|
||||
await settingStore.set(
|
||||
'Comfy.InstalledVersion',
|
||||
__COMFYUI_FRONTEND_VERSION__
|
||||
)
|
||||
|
||||
for (const callback of pendingCallbacks.value) {
|
||||
try {
|
||||
await callback()
|
||||
} catch (error) {
|
||||
console.error('New user initialization callback failed:', error)
|
||||
}
|
||||
}
|
||||
|
||||
pendingCallbacks.value = []
|
||||
}
|
||||
|
||||
function isNewUser(): boolean | null {
|
||||
return isNewUserDetermined.value ? isNewUserCached.value : null
|
||||
}
|
||||
|
||||
return {
|
||||
registerInitCallback,
|
||||
initializeIfNewUser,
|
||||
isNewUser,
|
||||
reset
|
||||
}
|
||||
}
|
||||
|
||||
export const useNewUserService = createSharedComposable(_useNewUserService)
|
||||
Reference in New Issue
Block a user