Skip to content

Commit

Permalink
fix: remove useless bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
CyanSalt committed Feb 22, 2023
1 parent 33d5076 commit 9380ce7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
7 changes: 4 additions & 3 deletions addons/proxy/src/main/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { effect } from '@vue/reactivity'
import * as address from 'address'
import * as commas from 'commas:api/main'
import { quote } from 'shell-quote'
Expand Down Expand Up @@ -47,19 +48,19 @@ export default () => {
commas.ipcMain.provide('system-proxy-status', $$(systemStatus))

let rootCAStatus = $ref(false)
const rootCAEffect = commas.helper.watchBaseEffect(async () => {
const rootCAEffect = effect(async () => {
rootCAStatus = await checkRootCA()
})
commas.ipcMain.provide('proxy-root-ca-status', $$(rootCAStatus))

commas.ipcMain.handle('install-proxy-root-ca', async () => {
await installRootCA()
return rootCAEffect.runner()
return rootCAEffect()
})

commas.ipcMain.handle('uninstall-proxy-root-ca', async () => {
await uninstallRootCA()
return rootCAEffect.runner()
return rootCAEffect()
})

commas.app.onCleanup(() => {
Expand Down
2 changes: 1 addition & 1 deletion addons/sync/src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default () => {

const syncData = useSyncData()

commas.helper.watchBaseEffect(() => {
commas.app.effect(() => {
commas.context.provide('sync.file', `themes/${settings['terminal.theme.name']}.json`)
// TODO: shall we sync addons?
})
Expand Down
28 changes: 11 additions & 17 deletions src/shared/compositions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ReactiveEffectOptions, ReactiveEffectRunner, Ref } from '@vue/reactivity'
import { customRef, deferredComputed, effect, getCurrentScope, onScopeDispose, shallowReactive, ref, stop, toRaw, unref } from '@vue/reactivity'
import { customRef, deferredComputed, effect, shallowReactive, ref, stop, toRaw, unref } from '@vue/reactivity'
import { cloneDeep, difference, intersection, isEqual } from 'lodash'

export function useAsyncComputed<T>(factory: () => Promise<T>): Ref<T | undefined>
Expand All @@ -9,7 +9,7 @@ export function useAsyncComputed<T>(factory: () => Promise<T>, defaultValue?: T)
return customRef<T | undefined>((track, trigger) => {
let currentValue = defaultValue
let initialized = false
const { runner } = watchBaseEffect(async () => {
const reactiveEffect = effect(async () => {
try {
currentValue = await factory()
} catch {
Expand All @@ -22,7 +22,7 @@ export function useAsyncComputed<T>(factory: () => Promise<T>, defaultValue?: T)
track()
if (!initialized) {
initialized = true
runner()
reactiveEffect()
}
return currentValue
},
Expand All @@ -35,7 +35,7 @@ export function useAsyncComputed<T>(factory: () => Promise<T>, defaultValue?: T)

function initializeSurface<T extends object>(valueRef: Ref<T>, reactiveObject: T) {
let isUpdated = true
watchBaseEffect(() => {
effect(() => {
const latest = unref(valueRef)
const rawObject = toRaw(reactiveObject)
const latestKeys = Object.keys(latest)
Expand All @@ -57,7 +57,7 @@ function initializeSurface<T extends object>(valueRef: Ref<T>, reactiveObject: T
})
// Make `Object.assign` to trigger once only
const objectRef = deferredComputed(() => ({ ...reactiveObject }))
watchBaseEffect(() => {
effect(() => {
const value = unref(objectRef)
if (isUpdated) {
isUpdated = false
Expand All @@ -71,7 +71,6 @@ export function watchBaseEffect<T>(
fn: (onInvalidate: (callback: () => void) => void) => T,
options?: ReactiveEffectOptions,
) {
const onStop = options?.onStop
let cleanupFn: (() => void) | undefined
const onInvalidate = (callback: () => void) => {
cleanupFn = callback
Expand All @@ -82,25 +81,20 @@ export function watchBaseEffect<T>(
cleanupFn = undefined
}
}
const onStop = options?.onStop
const reactiveEffect: ReactiveEffectRunner<T> = effect(() => {
cleanupEffect()
return fn(onInvalidate)
}, {
...options,
onStop() {
cleanupEffect()
if (onStop) {
onStop()
}
onStop?.()
},
})
const dispose = () => {
stop(reactiveEffect)
}
const scope = getCurrentScope()
if (scope) {
onScopeDispose(dispose)
}
dispose.runner = reactiveEffect
return dispose
}
Expand All @@ -109,14 +103,14 @@ export function surface<T extends object>(valueRef: Ref<T>, lazy?: boolean) {
const reactiveObject = shallowReactive({} as T) as T
if (lazy) {
let initialized = false
const stopEffect = watchBaseEffect(() => {
const reactiveEffect = effect(() => {
const value = unref(valueRef)
if (!initialized) {
initialized = true
Object.assign(reactiveObject, value)
return
}
stopEffect()
stop(reactiveEffect)
initializeSurface(valueRef, reactiveObject)
})
} else {
Expand All @@ -128,11 +122,11 @@ export function surface<T extends object>(valueRef: Ref<T>, lazy?: boolean) {
export function deepRef<T extends object>(valueRef: Ref<T>) {
const reactiveRef = ref() as Ref<T>
let initialized = false
watchBaseEffect(() => {
effect(() => {
initialized = false
reactiveRef.value = unref(valueRef)
})
watchBaseEffect(() => {
effect(() => {
const value = cloneDeep(unref(reactiveRef))
if (!initialized) {
initialized = true
Expand Down

0 comments on commit 9380ce7

Please sign in to comment.