perf: 优化获取数据 hooks 函数

This commit is contained in:
奔跑的面条 2022-06-25 17:03:03 +08:00
parent 340492f784
commit 78626b3c04
3 changed files with 89 additions and 67 deletions

View File

@ -1,7 +1,7 @@
import { ref, toRefs, watchEffect, nextTick } from 'vue' import { ref, toRefs } from 'vue'
import type VChart from 'vue-echarts' import type VChart from 'vue-echarts'
import { http } from '@/api/http' import { http } from '@/api/http'
import { CreateComponentType, PackagesCategoryEnum } from '@/packages/index.d' import { CreateComponentType, ChartFrameEnum } from '@/packages/index.d'
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore' import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
import { RequestDataTypeEnum } from '@/enums/httpEnum' import { RequestDataTypeEnum } from '@/enums/httpEnum'
import { isPreview } from '@/utils' import { isPreview } from '@/utils'
@ -23,17 +23,18 @@ export const useChartDataFetch = (
const vChartRef = ref<typeof VChart | null>(null) const vChartRef = ref<typeof VChart | null>(null)
let fetchInterval: any = 0 let fetchInterval: any = 0
isPreview() && const requestInterval = () => {
watchEffect(() => {
clearInterval(fetchInterval)
const chartEditStore = useChartEditStore() const chartEditStore = useChartEditStore()
const { requestOriginUrl, requestInterval } = toRefs( const { requestOriginUrl, requestInterval } = toRefs(
chartEditStore.getRequestGlobalConfig chartEditStore.getRequestGlobalConfig
) )
// 组件类型
const { chartFrame } = targetComponent.chartConfig
// 请求配置
const { requestDataType, requestHttpType, requestUrl } = toRefs( const { requestDataType, requestHttpType, requestUrl } = toRefs(
targetComponent.data targetComponent.data
) )
// 非请求类型
if (requestDataType.value !== RequestDataTypeEnum.AJAX) return if (requestDataType.value !== RequestDataTypeEnum.AJAX) return
// 处理地址 // 处理地址
if (requestUrl?.value && requestInterval.value > 0) { if (requestUrl?.value && requestInterval.value > 0) {
@ -42,25 +43,20 @@ export const useChartDataFetch = (
requestOriginUrl && requestOriginUrl.value + requestUrl.value requestOriginUrl && requestOriginUrl.value + requestUrl.value
if (!completePath) return if (!completePath) return
fetchInterval = setInterval(async () => { const fetchFn = async () => {
const res:any = await http(requestHttpType.value)(completePath || '', {}) const res: any = await http(requestHttpType.value)(completePath || '', {})
if (res.data) { if (res.data) {
// 是否是 Echarts 组件
const isECharts =
targetComponent.chartConfig.package ===
PackagesCategoryEnum.CHARTS
try { try {
if (isECharts && vChartRef?.value?.setOption) { // echarts 组件更新方式
nextTick(() => { if (chartFrame === ChartFrameEnum.ECHARTS) {
if (vChartRef.value) { if (vChartRef.value) {
vChartRef.value.setOption({ dataset: res.data }) vChartRef.value.setOption({ dataset: res.data })
} }
})
} else { } else {
// 若遵守规范使用 dataset 作为数据 key则会自动赋值数据 // 若遵守规范使用 dataset 作为数据 key则会自动赋值数据
targetComponent.option.dataset && (targetComponent.option.dataset = res.data) targetComponent.option.dataset && (targetComponent.option.dataset = res.data)
} }
// 更新回调函数
if (updateCallback) { if (updateCallback) {
updateCallback(res.data) updateCallback(res.data)
} }
@ -68,9 +64,17 @@ export const useChartDataFetch = (
console.error(error) console.error(error)
} }
} }
}, requestInterval.value * 1000)
} }
})
// 立即调用
fetchFn()
// 开启定时
setInterval(fetchFn, requestInterval.value * 1000)
}
}
isPreview() && requestInterval()
return { vChartRef } return { vChartRef }
} }

View File

@ -15,19 +15,21 @@
background-color:${backgroundColor}`" background-color:${backgroundColor}`"
> >
{{ dataset }} {{ option.dataset }}
</div> </div>
</div> </div>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { PropType, toRefs } from 'vue' import { PropType, toRefs, shallowReactive, watch } from 'vue'
import { CreateComponentType } from '@/packages/index.d' import { CreateComponentType } from '@/packages/index.d'
import { useChartDataFetch } from '@/hooks'
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
const props = defineProps({ const props = defineProps({
chartConfig: { chartConfig: {
type: Object as PropType<CreateComponentType>, type: Object as PropType<CreateComponentType>,
required: true, required: true
}, }
}) })
const { w, h } = toRefs(props.chartConfig.attr) const { w, h } = toRefs(props.chartConfig.attr)
@ -42,8 +44,27 @@ const {
borderColor, borderColor,
borderRadius, borderRadius,
writingMode, writingMode,
backgroundColor, backgroundColor
} = toRefs(props.chartConfig.option) } = toRefs(props.chartConfig.option)
const option = shallowReactive({
dataset: ''
})
//
watch(
() => props.chartConfig.option.dataset,
(newData: any) => {
option.dataset = newData
}, {
immediate: true
}
)
//
useChartDataFetch(props.chartConfig, useChartEditStore, (newData: any) => {
option.dataset = newData
})
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>

View File

@ -6,7 +6,7 @@
</div> </div>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { PropType, toRefs, reactive, watch } from 'vue' import { PropType, toRefs, shallowReactive, watch } from 'vue'
import { CreateComponentType } from '@/packages/index.d' import { CreateComponentType } from '@/packages/index.d'
import { useChartDataFetch } from '@/hooks' import { useChartDataFetch } from '@/hooks'
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore' import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
@ -14,33 +14,30 @@ import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore
const props = defineProps({ const props = defineProps({
chartConfig: { chartConfig: {
type: Object as PropType<CreateComponentType>, type: Object as PropType<CreateComponentType>,
required: true, required: true
}, }
}) })
const option = reactive({ const option = shallowReactive({
dataset: '' dataset: ''
}) })
const { w, h } = toRefs(props.chartConfig.attr) const { w, h } = toRefs(props.chartConfig.attr)
const { size, gradient } = toRefs(props.chartConfig.option)
const {
size,
gradient
} = toRefs(props.chartConfig.option)
const callback = (newData: string) => {
option.dataset = newData
}
watch( watch(
() => props.chartConfig.option.dataset, () => props.chartConfig.option.dataset,
() => { (newData: any) => {
option.dataset = props.chartConfig.option.dataset option.dataset = newData
}, { immediate: true } }, {
immediate: true
}
) )
useChartDataFetch(props.chartConfig, useChartEditStore, callback) useChartDataFetch(props.chartConfig, useChartEditStore, (newData: any) => {
option.dataset = newData
})
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>