feat: migrate pre-request script, test script, settings to nuxt composition
This commit is contained in:
60
helpers/utils/composables.ts
Normal file
60
helpers/utils/composables.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import {
|
||||
customRef,
|
||||
onBeforeUnmount,
|
||||
readonly,
|
||||
Ref,
|
||||
ref,
|
||||
} from "@nuxtjs/composition-api"
|
||||
import { Observable, Subscription } from "rxjs"
|
||||
|
||||
export function useReadonlyStream<T>(stream$: Observable<T>, initialValue: T) {
|
||||
let sub: Subscription | null = null
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
if (sub) {
|
||||
sub.unsubscribe()
|
||||
}
|
||||
})
|
||||
|
||||
const targetRef = ref(initialValue) as Ref<T>
|
||||
|
||||
sub = stream$.subscribe((value) => {
|
||||
targetRef.value = value
|
||||
})
|
||||
|
||||
return readonly(targetRef)
|
||||
}
|
||||
|
||||
export function useStream<T>(
|
||||
stream$: Observable<T>,
|
||||
initialValue: T,
|
||||
setter: (val: T) => void
|
||||
) {
|
||||
let sub: Subscription | null = null
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
if (sub) {
|
||||
sub.unsubscribe()
|
||||
}
|
||||
})
|
||||
|
||||
return customRef((track, trigger) => {
|
||||
let value = initialValue
|
||||
|
||||
sub = stream$.subscribe((val) => {
|
||||
value = val
|
||||
trigger()
|
||||
})
|
||||
|
||||
return {
|
||||
get() {
|
||||
track()
|
||||
return value
|
||||
},
|
||||
set(value: T) {
|
||||
trigger()
|
||||
setter(value)
|
||||
},
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user