@lynx-js/lynx-ui-common 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +28 -0
- package/dist/const/BaseScrollEventsMapping.d.ts +2 -0
- package/dist/const/BaseScrollEventsMapping.d.ts.map +1 -0
- package/dist/const/BaseScrollEventsMapping.jsx +13 -0
- package/dist/const/BaseScrollEventsMapping.jsx.map +1 -0
- package/dist/const/BaseUIEventsMapping.d.ts +4 -0
- package/dist/const/BaseUIEventsMapping.d.ts.map +1 -0
- package/dist/const/BaseUIEventsMapping.jsx +24 -0
- package/dist/const/BaseUIEventsMapping.jsx.map +1 -0
- package/dist/const/index.d.ts +3 -0
- package/dist/const/index.d.ts.map +1 -0
- package/dist/const/index.jsx +2 -0
- package/dist/hooks/docs/useBounce.jsx +0 -0
- package/dist/hooks/docs/useRefresh.jsx +0 -0
- package/dist/hooks/index.d.ts +19 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.jsx +73 -0
- package/dist/hooks/index.jsx.map +1 -0
- package/dist/hooks/useBounce.d.ts +26 -0
- package/dist/hooks/useBounce.d.ts.map +1 -0
- package/dist/hooks/useBounce.jsx +383 -0
- package/dist/hooks/useBounce.jsx.map +1 -0
- package/dist/hooks/useGCCollector.d.ts +18 -0
- package/dist/hooks/useGCCollector.d.ts.map +1 -0
- package/dist/hooks/useGCCollector.jsx +48 -0
- package/dist/hooks/useGCCollector.jsx.map +1 -0
- package/dist/hooks/useGlobalEventListener.d.ts +9 -0
- package/dist/hooks/useGlobalEventListener.d.ts.map +1 -0
- package/dist/hooks/useGlobalEventListener.jsx +19 -0
- package/dist/hooks/useGlobalEventListener.jsx.map +1 -0
- package/dist/hooks/useLatest.d.ts +6 -0
- package/dist/hooks/useLatest.d.ts.map +1 -0
- package/dist/hooks/useLatest.jsx +9 -0
- package/dist/hooks/useLatest.jsx.map +1 -0
- package/dist/hooks/useMemoizedFn.d.ts +8 -0
- package/dist/hooks/useMemoizedFn.d.ts.map +1 -0
- package/dist/hooks/useMemoizedFn.jsx +15 -0
- package/dist/hooks/useMemoizedFn.jsx.map +1 -0
- package/dist/hooks/usePrevious.d.ts +16 -0
- package/dist/hooks/usePrevious.d.ts.map +1 -0
- package/dist/hooks/usePrevious.jsx +11 -0
- package/dist/hooks/usePrevious.jsx.map +1 -0
- package/dist/hooks/useRefresh.d.ts +24 -0
- package/dist/hooks/useRefresh.d.ts.map +1 -0
- package/dist/hooks/useRefresh.jsx +554 -0
- package/dist/hooks/useRefresh.jsx.map +1 -0
- package/dist/hooks/useRegisteredEvents.d.ts +19 -0
- package/dist/hooks/useRegisteredEvents.d.ts.map +1 -0
- package/dist/hooks/useRegisteredEvents.jsx +24 -0
- package/dist/hooks/useRegisteredEvents.jsx.map +1 -0
- package/dist/hooks/useUnmount.d.ts +2 -0
- package/dist/hooks/useUnmount.d.ts.map +1 -0
- package/dist/hooks/useUnmount.jsx +11 -0
- package/dist/hooks/useUnmount.jsx.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.jsx +6 -0
- package/dist/reactive/hooks/useReactiveValue.d.ts +22 -0
- package/dist/reactive/hooks/useReactiveValue.d.ts.map +1 -0
- package/dist/reactive/hooks/useReactiveValue.jsx +75 -0
- package/dist/reactive/hooks/useReactiveValue.jsx.map +1 -0
- package/dist/reactive/hooks/useReactiveValueChange.d.ts +4 -0
- package/dist/reactive/hooks/useReactiveValueChange.d.ts.map +1 -0
- package/dist/reactive/hooks/useReactiveValueChange.jsx +7 -0
- package/dist/reactive/hooks/useReactiveValueChange.jsx.map +1 -0
- package/dist/reactive/hooks/useReactiveValueEvent.d.ts +5 -0
- package/dist/reactive/hooks/useReactiveValueEvent.d.ts.map +1 -0
- package/dist/reactive/hooks/useReactiveValueEvent.jsx +15 -0
- package/dist/reactive/hooks/useReactiveValueEvent.jsx.map +1 -0
- package/dist/reactive/index.d.ts +6 -0
- package/dist/reactive/index.d.ts.map +1 -0
- package/dist/reactive/index.jsx +5 -0
- package/dist/reactive/types.d.ts +17 -0
- package/dist/reactive/types.d.ts.map +1 -0
- package/dist/reactive/types.jsx +0 -0
- package/dist/reactive/updateReactive.d.ts +4 -0
- package/dist/reactive/updateReactive.d.ts.map +1 -0
- package/dist/reactive/updateReactive.jsx +8 -0
- package/dist/reactive/updateReactive.jsx.map +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.jsx +0 -0
- package/dist/types/interfaces/BaseScrollEvents.d.ts +40 -0
- package/dist/types/interfaces/BaseScrollEvents.d.ts.map +1 -0
- package/dist/types/interfaces/BaseScrollEvents.jsx +0 -0
- package/dist/types/interfaces/BaseUIEvents.d.ts +132 -0
- package/dist/types/interfaces/BaseUIEvents.d.ts.map +1 -0
- package/dist/types/interfaces/BaseUIEvents.jsx +0 -0
- package/dist/types/interfaces/BaseUIProps.d.ts +19 -0
- package/dist/types/interfaces/BaseUIProps.d.ts.map +1 -0
- package/dist/types/interfaces/BaseUIProps.jsx +0 -0
- package/dist/types/interfaces/BounceableInterface.d.ts +120 -0
- package/dist/types/interfaces/BounceableInterface.d.ts.map +1 -0
- package/dist/types/interfaces/BounceableInterface.jsx +0 -0
- package/dist/types/interfaces/EventMTSInterface.d.ts +1 -0
- package/dist/types/interfaces/EventMTSInterface.d.ts.map +1 -0
- package/dist/types/interfaces/EventMTSInterface.jsx +0 -0
- package/dist/types/interfaces/LazyInterface.d.ts +72 -0
- package/dist/types/interfaces/LazyInterface.d.ts.map +1 -0
- package/dist/types/interfaces/LazyInterface.jsx +0 -0
- package/dist/types/interfaces/RefreshInterface.d.ts +98 -0
- package/dist/types/interfaces/RefreshInterface.d.ts.map +1 -0
- package/dist/types/interfaces/RefreshInterface.jsx +10 -0
- package/dist/types/interfaces/RefreshInterface.jsx.map +1 -0
- package/dist/types/interfaces/index.d.ts +36 -0
- package/dist/types/interfaces/index.d.ts.map +1 -0
- package/dist/types/interfaces/index.jsx +0 -0
- package/dist/utils/common.d.ts +15 -0
- package/dist/utils/common.d.ts.map +1 -0
- package/dist/utils/common.jsx +12 -0
- package/dist/utils/common.jsx.map +1 -0
- package/dist/utils/common.test.jsx +55 -0
- package/dist/utils/common.test.jsx.map +1 -0
- package/dist/utils/convertToPx.d.ts +4 -0
- package/dist/utils/convertToPx.d.ts.map +1 -0
- package/dist/utils/convertToPx.jsx +20 -0
- package/dist/utils/convertToPx.jsx.map +1 -0
- package/dist/utils/delayFrames.d.ts +2 -0
- package/dist/utils/delayFrames.d.ts.map +1 -0
- package/dist/utils/delayFrames.jsx +12 -0
- package/dist/utils/delayFrames.jsx.map +1 -0
- package/dist/utils/getEventDetail.d.ts +23 -0
- package/dist/utils/getEventDetail.d.ts.map +1 -0
- package/dist/utils/getEventDetail.jsx +7 -0
- package/dist/utils/getEventDetail.jsx.map +1 -0
- package/dist/utils/index.d.ts +13 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.jsx +12 -0
- package/dist/utils/interpolation.d.ts +40 -0
- package/dist/utils/interpolation.d.ts.map +1 -0
- package/dist/utils/interpolation.jsx +86 -0
- package/dist/utils/interpolation.jsx.map +1 -0
- package/dist/utils/interpolationJS.d.ts +39 -0
- package/dist/utils/interpolationJS.d.ts.map +1 -0
- package/dist/utils/interpolationJS.jsx +80 -0
- package/dist/utils/interpolationJS.jsx.map +1 -0
- package/dist/utils/log.d.ts +3 -0
- package/dist/utils/log.d.ts.map +1 -0
- package/dist/utils/log.jsx +10 -0
- package/dist/utils/log.jsx.map +1 -0
- package/dist/utils/mainThreadify.d.ts +2 -0
- package/dist/utils/mainThreadify.d.ts.map +1 -0
- package/dist/utils/mainThreadify.jsx +11 -0
- package/dist/utils/mainThreadify.jsx.map +1 -0
- package/dist/utils/popoverUtils.d.ts +3 -0
- package/dist/utils/popoverUtils.d.ts.map +1 -0
- package/dist/utils/popoverUtils.jsx +12 -0
- package/dist/utils/popoverUtils.jsx.map +1 -0
- package/dist/utils/renderContentWithExtraProps.d.ts +5 -0
- package/dist/utils/renderContentWithExtraProps.d.ts.map +1 -0
- package/dist/utils/renderContentWithExtraProps.jsx +10 -0
- package/dist/utils/renderContentWithExtraProps.jsx.map +1 -0
- package/dist/utils/selector.d.ts +36 -0
- package/dist/utils/selector.d.ts.map +1 -0
- package/dist/utils/selector.jsx +85 -0
- package/dist/utils/selector.jsx.map +1 -0
- package/dist/utils/selector.test.jsx +215 -0
- package/dist/utils/selector.test.jsx.map +1 -0
- package/dist/utils/version.d.ts +6 -0
- package/dist/utils/version.d.ts.map +1 -0
- package/dist/utils/version.jsx +28 -0
- package/dist/utils/version.jsx.map +1 -0
- package/dist/utils/version.test.jsx +77 -0
- package/dist/utils/version.test.jsx.map +1 -0
- package/package.json +52 -0
- package/src/const/BaseScrollEventsMapping.ts +14 -0
- package/src/const/BaseUIEventsMapping.ts +25 -0
- package/src/const/index.ts +6 -0
- package/src/hooks/docs/useBounce.mdx +2 -0
- package/src/hooks/docs/useRefresh.mdx +2 -0
- package/src/hooks/index.ts +96 -0
- package/src/hooks/useBounce.tsx +814 -0
- package/src/hooks/useGCCollector.tsx +76 -0
- package/src/hooks/useGlobalEventListener.ts +50 -0
- package/src/hooks/useLatest.ts +16 -0
- package/src/hooks/useMemoizedFn.ts +35 -0
- package/src/hooks/usePrevious.ts +28 -0
- package/src/hooks/useRefresh.ts +1054 -0
- package/src/hooks/useRegisteredEvents.ts +69 -0
- package/src/hooks/useUnmount.ts +18 -0
- package/src/index.tsx +25 -0
- package/src/reactive/hooks/useReactiveValue.ts +132 -0
- package/src/reactive/hooks/useReactiveValueChange.ts +15 -0
- package/src/reactive/hooks/useReactiveValueEvent.ts +26 -0
- package/src/reactive/index.ts +20 -0
- package/src/reactive/types.ts +29 -0
- package/src/reactive/updateReactive.ts +16 -0
- package/src/types/index.ts +5 -0
- package/src/types/interfaces/BaseScrollEvents.ts +47 -0
- package/src/types/interfaces/BaseUIEvents.ts +142 -0
- package/src/types/interfaces/BaseUIProps.ts +22 -0
- package/src/types/interfaces/BounceableInterface.ts +125 -0
- package/src/types/interfaces/EventMTSInterface.ts +35 -0
- package/src/types/interfaces/LazyInterface.tsx +78 -0
- package/src/types/interfaces/RefreshInterface.ts +105 -0
- package/src/types/interfaces/index.ts +44 -0
- package/src/utils/common.test.ts +64 -0
- package/src/utils/common.ts +35 -0
- package/src/utils/convertToPx.ts +32 -0
- package/src/utils/delayFrames.ts +24 -0
- package/src/utils/getEventDetail.ts +36 -0
- package/src/utils/index.ts +16 -0
- package/src/utils/interpolation.ts +222 -0
- package/src/utils/interpolationJS.ts +212 -0
- package/src/utils/log.ts +16 -0
- package/src/utils/mainThreadify.ts +17 -0
- package/src/utils/popoverUtils.ts +25 -0
- package/src/utils/renderContentWithExtraProps.ts +20 -0
- package/src/utils/selector.test.ts +237 -0
- package/src/utils/selector.ts +222 -0
- package/src/utils/version.test.ts +98 -0
- package/src/utils/version.ts +50 -0
- package/tsconfig.docs.json +7 -0
- package/tsconfig.json +12 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// Copyright 2026 The Lynx Authors. All rights reserved.
|
|
2
|
+
// Licensed under the Apache License Version 2.0 that can be found in the
|
|
3
|
+
// LICENSE file in the root directory of this source tree.
|
|
4
|
+
|
|
5
|
+
import { runOnMainThread, useEffect, useRef } from '@lynx-js/react'
|
|
6
|
+
|
|
7
|
+
import { useMemoizedFn } from './useMemoizedFn'
|
|
8
|
+
|
|
9
|
+
export interface UseGCCollectorOptions {
|
|
10
|
+
/** GC trigger interval time in milliseconds, default 1000ms */
|
|
11
|
+
interval?: number
|
|
12
|
+
/** Whether to enable periodic GC, default true */
|
|
13
|
+
enabled?: boolean
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Hook for periodic garbage collection
|
|
18
|
+
* @param options Configuration options
|
|
19
|
+
* @returns Control function object
|
|
20
|
+
*/
|
|
21
|
+
export function useGCCollector(options: UseGCCollectorOptions = {}) {
|
|
22
|
+
const { interval = 3000, enabled = true } = options
|
|
23
|
+
const timeRef = useRef<number | null>(null)
|
|
24
|
+
|
|
25
|
+
const triggerGCMT = () => {
|
|
26
|
+
'main thread'
|
|
27
|
+
const globalScope = globalThis as unknown as { lepusng_gc?: () => void }
|
|
28
|
+
if (typeof globalScope.lepusng_gc === 'function') {
|
|
29
|
+
globalScope.lepusng_gc()
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const requestTimeout = useMemoizedFn(() => {
|
|
34
|
+
if (enabled) {
|
|
35
|
+
timeRef.current = setTimeout(() => {
|
|
36
|
+
requestTimeout()
|
|
37
|
+
runOnMainThread(triggerGCMT)()
|
|
38
|
+
}, interval) as unknown as number
|
|
39
|
+
}
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
const start = useMemoizedFn(() => {
|
|
43
|
+
if (timeRef.current === null) {
|
|
44
|
+
requestTimeout()
|
|
45
|
+
}
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
const stop = useMemoizedFn(() => {
|
|
49
|
+
if (timeRef.current !== null) {
|
|
50
|
+
clearTimeout(timeRef.current)
|
|
51
|
+
timeRef.current = null
|
|
52
|
+
}
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
const restart = useMemoizedFn(() => {
|
|
56
|
+
stop()
|
|
57
|
+
start()
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
if (enabled) {
|
|
62
|
+
start()
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return () => {
|
|
66
|
+
stop()
|
|
67
|
+
}
|
|
68
|
+
}, [enabled, interval])
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
start,
|
|
72
|
+
stop,
|
|
73
|
+
restart,
|
|
74
|
+
triggerGCMT,
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// Copyright 2026 The Lynx Authors. All rights reserved.
|
|
2
|
+
// Licensed under the Apache License Version 2.0 that can be found in the
|
|
3
|
+
// LICENSE file in the root directory of this source tree.
|
|
4
|
+
|
|
5
|
+
import { useCallback, useEffect, useMemo } from '@lynx-js/react'
|
|
6
|
+
|
|
7
|
+
// biome-ignore lint/suspicious/noExplicitAny: generic type definition
|
|
8
|
+
type noop = (...p: any) => void
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Use this hook to register events
|
|
12
|
+
* Events will be registered at first render, preventing possible message missing.
|
|
13
|
+
* When deps updates, handlerFunc will be updated, old event listener will be removed.
|
|
14
|
+
*/
|
|
15
|
+
export function useGlobalEventListener(
|
|
16
|
+
eventName: string,
|
|
17
|
+
handler: noop,
|
|
18
|
+
deps?: readonly unknown[],
|
|
19
|
+
) {
|
|
20
|
+
const handlerFunc = useCallback(handler, deps ?? [])
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* We use useMemo to make it only run one time
|
|
24
|
+
* useMemo will run at render.
|
|
25
|
+
* If dependency changes, useMemo will rerun to have correct handlerFunc
|
|
26
|
+
* then a new listener will be added with correct handlerFunc
|
|
27
|
+
*/
|
|
28
|
+
useMemo(() => {
|
|
29
|
+
if (!__LEPUS__) {
|
|
30
|
+
lynx
|
|
31
|
+
.getJSModule('GlobalEventEmitter')
|
|
32
|
+
.addListener(eventName, handlerFunc)
|
|
33
|
+
}
|
|
34
|
+
}, [eventName, handlerFunc])
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* We use return value of useEffect to removeListener
|
|
38
|
+
* Dependency is just the same as `useMemo` above
|
|
39
|
+
* If dependency changes, useEffect's cleaning up func will be run, which is essentially
|
|
40
|
+
* the cleaning up func of `addListener` run at useMemo
|
|
41
|
+
*/
|
|
42
|
+
useEffect(
|
|
43
|
+
() => () => {
|
|
44
|
+
lynx
|
|
45
|
+
.getJSModule('GlobalEventEmitter')
|
|
46
|
+
.removeListener(eventName, handlerFunc)
|
|
47
|
+
},
|
|
48
|
+
[eventName, handlerFunc],
|
|
49
|
+
)
|
|
50
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Copyright 2026 The Lynx Authors. All rights reserved.
|
|
2
|
+
// Licensed under the Apache License Version 2.0 that can be found in the
|
|
3
|
+
// LICENSE file in the root directory of this source tree.
|
|
4
|
+
|
|
5
|
+
import { useRef } from '@lynx-js/react'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* `useLatest` will be useful when you are facing dependency hell.
|
|
9
|
+
* Or you want always-update ref
|
|
10
|
+
*/
|
|
11
|
+
export function useLatest<T>(value: T) {
|
|
12
|
+
const ref = useRef(value)
|
|
13
|
+
ref.current = value
|
|
14
|
+
|
|
15
|
+
return ref
|
|
16
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// Copyright 2026 The Lynx Authors. All rights reserved.
|
|
2
|
+
// Licensed under the Apache License Version 2.0 that can be found in the
|
|
3
|
+
// LICENSE file in the root directory of this source tree.
|
|
4
|
+
|
|
5
|
+
import { useMemo, useRef } from '@lynx-js/react'
|
|
6
|
+
|
|
7
|
+
// biome-ignore lint/suspicious/noExplicitAny: generic type definition
|
|
8
|
+
type noop = (this: any, ...args: any[]) => any
|
|
9
|
+
|
|
10
|
+
type PickFunction<T extends noop> = (
|
|
11
|
+
this: ThisParameterType<T>,
|
|
12
|
+
...args: Parameters<T>
|
|
13
|
+
) => ReturnType<T>
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Hooks for persistent functions. In general, useMemoizedFn can be used instead of useCallback.
|
|
17
|
+
* In some scenarios, we need to use useCallback to cache a function, but when the second parameter deps changes, the function will be regenerated, causing the function reference to change.
|
|
18
|
+
*/
|
|
19
|
+
function useMemoizedFn<T extends noop>(fn: T) {
|
|
20
|
+
const fnRef = useRef<T>(fn)
|
|
21
|
+
|
|
22
|
+
// why not write `fnRef.current = fn`?
|
|
23
|
+
// https://github.com/alibaba/hooks/issues/728
|
|
24
|
+
fnRef.current = useMemo<T>(() => fn, [fn])
|
|
25
|
+
|
|
26
|
+
const memoizedFn = useRef<PickFunction<T>>()
|
|
27
|
+
memoizedFn.current ??= function(this, ...args) {
|
|
28
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
29
|
+
return fnRef.current.apply(this, args)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return memoizedFn.current as unknown as T
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export { useMemoizedFn }
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Copyright 2026 The Lynx Authors. All rights reserved.
|
|
2
|
+
// Licensed under the Apache License Version 2.0 that can be found in the
|
|
3
|
+
// LICENSE file in the root directory of this source tree.
|
|
4
|
+
|
|
5
|
+
import { useEffect, useRef } from '@lynx-js/react'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Tracks previous state of a value.
|
|
9
|
+
*
|
|
10
|
+
* @param value Props, state or any other calculated value.
|
|
11
|
+
* @returns Value from the previous render of the enclosing component.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* function Component() {
|
|
15
|
+
* const [count, setCount] = useState(0);
|
|
16
|
+
* const prevCount = usePrevious(count);
|
|
17
|
+
* // ...
|
|
18
|
+
* return `Now: ${count}, before: ${prevCount}`;
|
|
19
|
+
* }
|
|
20
|
+
*/
|
|
21
|
+
export function usePrevious<T>(value: T): T | undefined {
|
|
22
|
+
// Source: https://reactjs.org/docs/hooks-faq.html#how-to-get-the-previous-props-or-state
|
|
23
|
+
const ref = useRef<T>()
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
ref.current = value
|
|
26
|
+
})
|
|
27
|
+
return ref.current
|
|
28
|
+
}
|