-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathcontext.ts
More file actions
53 lines (40 loc) · 1.64 KB
/
context.ts
File metadata and controls
53 lines (40 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { useRequest } from '@hooks/useRequest'
import { useWindowFocus } from '@hooks/useWindowFocus'
import limitService from "@service/limit-service"
import { type App, inject, provide, type Ref, shallowRef, watch } from "vue"
import { type LimitReason } from "../common"
const REASON_KEY = "display_reason"
const RULE_KEY = "display_rule"
const GLOBAL_KEY = "delay_global"
const DELAY_HANDLER_KEY = 'delay_handler'
type GlobalParam = {
url: string
}
export const provideGlobalParam = (app: App<Element>, gp: GlobalParam) => {
app.provide(GLOBAL_KEY, gp)
}
export const useGlobalParam = () => inject(GLOBAL_KEY) as GlobalParam
export const provideReason = (app: App<Element>): Ref<LimitReason | undefined> => {
const reason = shallowRef<LimitReason>()
app.provide(REASON_KEY, reason)
return reason
}
export const useReason = () => inject(REASON_KEY) as Ref<LimitReason | undefined>
export const provideRule = () => {
const reason = useReason()
const windowFocus = useWindowFocus()
const { data: rule, refresh } = useRequest(async () => {
if (!windowFocus.value) return null
const reasonId = reason.value?.id
if (!reasonId) return null
const rules = await limitService.select({ id: reasonId, filterDisabled: false })
return rules?.[0]
})
watch([reason, windowFocus], refresh)
provide(RULE_KEY, rule)
}
export const useRule = () => inject(RULE_KEY) as Ref<timer.limit.Item | null>
export const provideDelayHandler = (app: App<Element>, handlers: () => void) => {
app?.provide(DELAY_HANDLER_KEY, handlers)
}
export const useDelayHandler = () => inject(DELAY_HANDLER_KEY) as () => void