@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,69 @@
|
|
|
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 { useMainThreadRef } from '@lynx-js/react'
|
|
6
|
+
|
|
7
|
+
import { mainThreadifyEventsMapping } from '../utils'
|
|
8
|
+
|
|
9
|
+
const useMainThreadifyEvents = (
|
|
10
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
11
|
+
events: Record<string, any>,
|
|
12
|
+
mainThreadEvents: Record<string, string>, // e.g.: { 'onScroll': bindscroll } -> { 'main-thread:bindscroll': main-thread:bindscroll }
|
|
13
|
+
) => {
|
|
14
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
15
|
+
type reducedEventsType = Record<string, any>
|
|
16
|
+
const mainThreadifyEvents: reducedEventsType = {}
|
|
17
|
+
// biome-ignore lint/correctness/noUnusedVariables: <explanation>
|
|
18
|
+
const dummyRef = useMainThreadRef()
|
|
19
|
+
for (const event of Object.keys(events)) {
|
|
20
|
+
if (event in mainThreadEvents) {
|
|
21
|
+
mainThreadifyEvents[mainThreadEvents[event]] = (e: unknown) => {
|
|
22
|
+
'main thread'
|
|
23
|
+
if (events[event]) {
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
25
|
+
events[event](e)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return mainThreadifyEvents
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Only bind the events that are passed in and registered in the registeredProps to improve performance.
|
|
34
|
+
* @example
|
|
35
|
+
* ```
|
|
36
|
+
* const XXXOwnedProps: Record<string, string> = {
|
|
37
|
+
* ...ScrollEvents,
|
|
38
|
+
* ...ExposureEventsAndProps,
|
|
39
|
+
* ...TouchEvents,
|
|
40
|
+
* ...LayoutEvents,
|
|
41
|
+
* };
|
|
42
|
+
* const registerProps = useRegisteredEvents(props, XXXOwnedProps);
|
|
43
|
+
* <XXX {...registerProps} />
|
|
44
|
+
*
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export const useRegisteredEvents = (
|
|
48
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
49
|
+
events: Record<string, any>,
|
|
50
|
+
registeredEventsMapping: Record<string, string>,
|
|
51
|
+
) => {
|
|
52
|
+
// biome-ignore lint/suspicious/noExplicitAny: any
|
|
53
|
+
type reducedEventsType = Record<string, any>
|
|
54
|
+
const reducedEvents: reducedEventsType = {}
|
|
55
|
+
for (const event of Object.keys(events)) {
|
|
56
|
+
if (event in registeredEventsMapping) {
|
|
57
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
58
|
+
reducedEvents[registeredEventsMapping[event]] = events[event]
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const mainThreadifyEvents = mainThreadifyEventsMapping(
|
|
62
|
+
registeredEventsMapping,
|
|
63
|
+
)
|
|
64
|
+
const mainThreadifyEventsContents = useMainThreadifyEvents(
|
|
65
|
+
events,
|
|
66
|
+
mainThreadifyEvents,
|
|
67
|
+
)
|
|
68
|
+
return { ...reducedEvents, ...mainThreadifyEventsContents }
|
|
69
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
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 } from '@lynx-js/react'
|
|
6
|
+
|
|
7
|
+
import { useLatest } from './useLatest'
|
|
8
|
+
|
|
9
|
+
export const useUnmount = (fn: () => void) => {
|
|
10
|
+
const fnRef = useLatest(fn)
|
|
11
|
+
|
|
12
|
+
useEffect(
|
|
13
|
+
() => () => {
|
|
14
|
+
fnRef.current()
|
|
15
|
+
},
|
|
16
|
+
[],
|
|
17
|
+
)
|
|
18
|
+
}
|
package/src/index.tsx
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
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
|
+
export * from './hooks'
|
|
6
|
+
export * from './utils'
|
|
7
|
+
export * from './const'
|
|
8
|
+
export type * from './types'
|
|
9
|
+
|
|
10
|
+
export type {
|
|
11
|
+
ReactiveValueOptions,
|
|
12
|
+
ReactiveValueAPI,
|
|
13
|
+
ReactiveValueType,
|
|
14
|
+
Subscriber,
|
|
15
|
+
Unsubscribe,
|
|
16
|
+
} from './reactive'
|
|
17
|
+
|
|
18
|
+
export {
|
|
19
|
+
useReactiveValue,
|
|
20
|
+
useReactiveValueEvent,
|
|
21
|
+
updateReactiveValue,
|
|
22
|
+
useReactiveValueChange,
|
|
23
|
+
} from './reactive'
|
|
24
|
+
|
|
25
|
+
export { useMainThreadImperativeHandle } from '@lynx-js/react-use'
|
|
@@ -0,0 +1,132 @@
|
|
|
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, useMainThreadRef, useMemo } from '@lynx-js/react'
|
|
6
|
+
import type { MainThreadRef } from '@lynx-js/react'
|
|
7
|
+
|
|
8
|
+
import type {
|
|
9
|
+
ReactiveValueAPI,
|
|
10
|
+
ReactiveValueOptions,
|
|
11
|
+
Subscriber,
|
|
12
|
+
Unsubscribe,
|
|
13
|
+
} from '../types'
|
|
14
|
+
|
|
15
|
+
export type ReactiveValue<T> = ReturnType<
|
|
16
|
+
typeof createMainThreadReactiveValue<T>
|
|
17
|
+
>
|
|
18
|
+
|
|
19
|
+
export function createMainThreadReactiveValue<T>(
|
|
20
|
+
initialValue: T,
|
|
21
|
+
options: ReactiveValueOptions = {},
|
|
22
|
+
) {
|
|
23
|
+
'main thread'
|
|
24
|
+
class ReactiveValue<T> implements ReactiveValueAPI<T> {
|
|
25
|
+
_value: T
|
|
26
|
+
_subscribers = new Set<Subscriber<T>>()
|
|
27
|
+
_options: ReactiveValueOptions
|
|
28
|
+
_isDestroyed = false
|
|
29
|
+
|
|
30
|
+
constructor(initialValue: T, options: ReactiveValueOptions = {}) {
|
|
31
|
+
this._value = initialValue
|
|
32
|
+
this._options = options
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
get value(): T {
|
|
36
|
+
return this._value
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
set value(newValue: T) {
|
|
40
|
+
if (this._isDestroyed) return
|
|
41
|
+
|
|
42
|
+
if (this._value !== newValue) {
|
|
43
|
+
this._value = newValue
|
|
44
|
+
this._notifySubscribers()
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
get(): T {
|
|
49
|
+
return this._value
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
set(newValue: T): void {
|
|
53
|
+
this.value = newValue
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
subscribe(callback: Subscriber<T>): Unsubscribe {
|
|
57
|
+
// biome-ignore lint/suspicious/noEmptyBlockStatements: As expected
|
|
58
|
+
if (this._isDestroyed) return () => {}
|
|
59
|
+
|
|
60
|
+
this._subscribers.add(callback)
|
|
61
|
+
|
|
62
|
+
// Call immediately with current value
|
|
63
|
+
callback(this._value)
|
|
64
|
+
|
|
65
|
+
return () => this.unsubscribe(callback)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
unsubscribe(callback: Subscriber<T>): void {
|
|
69
|
+
this._subscribers.delete(callback)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
addListener(callback: Subscriber<T>): Unsubscribe {
|
|
73
|
+
return this.subscribe(callback)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
removeListener(callback: Subscriber<T>): void {
|
|
77
|
+
this.unsubscribe(callback)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
removeAllListeners(): void {
|
|
81
|
+
this._subscribers.clear()
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
destroy(): void {
|
|
85
|
+
this._isDestroyed = true
|
|
86
|
+
this._subscribers.clear()
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
_notifySubscribers(): void {
|
|
90
|
+
if (this._isDestroyed) return
|
|
91
|
+
|
|
92
|
+
for (const subscriber of this._subscribers) {
|
|
93
|
+
try {
|
|
94
|
+
subscriber(this._value)
|
|
95
|
+
} catch (error) {
|
|
96
|
+
if (this._options.debug) {
|
|
97
|
+
console.error(
|
|
98
|
+
`[ReactiveValue${
|
|
99
|
+
this._options.label ? ` ${this._options.label}` : ''
|
|
100
|
+
}]: Error in subscriber:`,
|
|
101
|
+
error,
|
|
102
|
+
)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return new ReactiveValue(initialValue, options)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function useReactiveValue<T>(
|
|
112
|
+
initialValue: T,
|
|
113
|
+
options: ReactiveValueOptions = {},
|
|
114
|
+
): MainThreadRef<ReactiveValue<T>> {
|
|
115
|
+
const mtRef = useMainThreadRef<ReactiveValue<T>>({} as ReactiveValue<T>)
|
|
116
|
+
|
|
117
|
+
function initReactiveValue() {
|
|
118
|
+
'main thread'
|
|
119
|
+
mtRef.current = createMainThreadReactiveValue(initialValue, options)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
useMemo(() => {
|
|
123
|
+
if (__BACKGROUND__) {
|
|
124
|
+
runOnMainThread(initReactiveValue)()
|
|
125
|
+
} else {
|
|
126
|
+
// Kept for preventing tree shaking
|
|
127
|
+
initReactiveValue
|
|
128
|
+
}
|
|
129
|
+
}, [])
|
|
130
|
+
|
|
131
|
+
return mtRef
|
|
132
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
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 type { MainThreadRef } from '@lynx-js/react'
|
|
6
|
+
|
|
7
|
+
import { useReactiveValueEvent } from './useReactiveValueEvent'
|
|
8
|
+
import type { ReactiveValue } from '../hooks/useReactiveValue'
|
|
9
|
+
|
|
10
|
+
export function useReactiveValueChange<T>(
|
|
11
|
+
valueRef: MainThreadRef<ReactiveValue<T>>,
|
|
12
|
+
callback: (value: T) => void,
|
|
13
|
+
) {
|
|
14
|
+
return useReactiveValueEvent(valueRef, 'change', callback)
|
|
15
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
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 } from '@lynx-js/react'
|
|
6
|
+
import type { MainThreadRef } from '@lynx-js/react'
|
|
7
|
+
|
|
8
|
+
import type { ReactiveValue } from '../hooks/useReactiveValue'
|
|
9
|
+
import type { ReactiveEventName } from '../types'
|
|
10
|
+
|
|
11
|
+
export function useReactiveValueEvent<T>(
|
|
12
|
+
valueRef: MainThreadRef<ReactiveValue<T>>,
|
|
13
|
+
event: ReactiveEventName,
|
|
14
|
+
callback: (value: T) => void,
|
|
15
|
+
) {
|
|
16
|
+
function handleEvent() {
|
|
17
|
+
'main thread'
|
|
18
|
+
valueRef.current?.subscribe?.(callback)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
if (event === 'change') {
|
|
23
|
+
runOnMainThread(handleEvent)()
|
|
24
|
+
}
|
|
25
|
+
}, [valueRef])
|
|
26
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
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
|
+
// Not Ready yet
|
|
6
|
+
// export { derived } from './derived'
|
|
7
|
+
|
|
8
|
+
// Types
|
|
9
|
+
export type {
|
|
10
|
+
ReactiveValueOptions,
|
|
11
|
+
ReactiveValueAPI,
|
|
12
|
+
ReactiveValueType,
|
|
13
|
+
Subscriber,
|
|
14
|
+
Unsubscribe,
|
|
15
|
+
} from './types'
|
|
16
|
+
|
|
17
|
+
export { useReactiveValue } from './hooks/useReactiveValue'
|
|
18
|
+
export { useReactiveValueEvent } from './hooks/useReactiveValueEvent'
|
|
19
|
+
export { useReactiveValueChange } from './hooks/useReactiveValueChange'
|
|
20
|
+
export { updateReactiveValue } from './updateReactive'
|
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
export type Subscriber<T> = (value: T) => void
|
|
6
|
+
export type Unsubscribe = () => void
|
|
7
|
+
|
|
8
|
+
export type ReactiveValueType =
|
|
9
|
+
| 'number'
|
|
10
|
+
| 'string'
|
|
11
|
+
| 'boolean'
|
|
12
|
+
| 'object'
|
|
13
|
+
| 'array'
|
|
14
|
+
|
|
15
|
+
export interface ReactiveValueOptions {
|
|
16
|
+
debug?: boolean
|
|
17
|
+
label?: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface ReactiveValueAPI<T> {
|
|
21
|
+
subscribe(callback: Subscriber<T>): Unsubscribe
|
|
22
|
+
unsubscribe(callback: Subscriber<T>): void
|
|
23
|
+
addListener(callback: Subscriber<T>): Unsubscribe
|
|
24
|
+
removeListener(callback: Subscriber<T>): void
|
|
25
|
+
removeAllListeners(): void
|
|
26
|
+
destroy(): void
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type ReactiveEventName = 'change' | 'animationstart'
|
|
@@ -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 type { MainThreadRef } from '@lynx-js/react'
|
|
6
|
+
|
|
7
|
+
import type { ReactiveValue } from './hooks/useReactiveValue'
|
|
8
|
+
|
|
9
|
+
export function updateReactiveValue<T>(
|
|
10
|
+
valueRef: MainThreadRef<ReactiveValue<T>>,
|
|
11
|
+
value: T,
|
|
12
|
+
): void {
|
|
13
|
+
'main thread'
|
|
14
|
+
if (!valueRef.current) return
|
|
15
|
+
valueRef.current.value = value
|
|
16
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
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 type {
|
|
6
|
+
ScrollViewProps as OriginalScrollViewProps,
|
|
7
|
+
ScrollEvent,
|
|
8
|
+
} from '@lynx-js/types'
|
|
9
|
+
|
|
10
|
+
export interface BaseScrollEvents {
|
|
11
|
+
/**
|
|
12
|
+
* Being triggered when scrolling occurs.
|
|
13
|
+
* @zh 滚动时触发。
|
|
14
|
+
* @defaultValue undefined
|
|
15
|
+
* @eventProperty
|
|
16
|
+
* @Android
|
|
17
|
+
* @iOS
|
|
18
|
+
*/
|
|
19
|
+
onScroll?: (e: ScrollEvent) => void
|
|
20
|
+
/**
|
|
21
|
+
* Being triggered when scrolling stops.
|
|
22
|
+
* @zh 滚动停止时触发。
|
|
23
|
+
* @defaultValue undefined
|
|
24
|
+
* @eventProperty
|
|
25
|
+
* @Android
|
|
26
|
+
* @iOS
|
|
27
|
+
*/
|
|
28
|
+
onScrollEnd?: (e: OriginalScrollViewProps['bindscrollend']) => void
|
|
29
|
+
/**
|
|
30
|
+
* Being triggered when scrolling to upper.
|
|
31
|
+
* @zh 滚动到顶部时触发。
|
|
32
|
+
* @defaultValue undefined
|
|
33
|
+
* @eventProperty
|
|
34
|
+
* @Android
|
|
35
|
+
* @iOS
|
|
36
|
+
*/
|
|
37
|
+
onScrollToUpper?: (e: OriginalScrollViewProps['bindscrolltoupper']) => void
|
|
38
|
+
/**
|
|
39
|
+
* Being triggered when scrolling to lower.
|
|
40
|
+
* @zh 滚动到底部时触发。
|
|
41
|
+
* @defaultValue undefined
|
|
42
|
+
* @eventProperty
|
|
43
|
+
* @Android
|
|
44
|
+
* @iOS
|
|
45
|
+
*/
|
|
46
|
+
onScrollToLower?: (e: OriginalScrollViewProps['bindscrolltolower']) => void
|
|
47
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
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 type {
|
|
6
|
+
CommonEvent,
|
|
7
|
+
LynxBindCatchEvent,
|
|
8
|
+
LynxEvent,
|
|
9
|
+
Target,
|
|
10
|
+
} from '@lynx-js/types'
|
|
11
|
+
|
|
12
|
+
export interface BaseUITouchEvents {
|
|
13
|
+
/**
|
|
14
|
+
* Send when a touch event is interrupted or cancelled before it is completed.
|
|
15
|
+
* @zh 当触摸事件在完成前被中断或取消时发送。
|
|
16
|
+
* @iOS
|
|
17
|
+
* @Android
|
|
18
|
+
* @Harmony
|
|
19
|
+
* @eventProperty
|
|
20
|
+
*/
|
|
21
|
+
onTouchCancel?: (e: LynxEvent<Target>['TouchCancel']) => void
|
|
22
|
+
/**
|
|
23
|
+
* Send when a touch-point is placed on the UI.
|
|
24
|
+
* @zh 当触摸点放置在 UI 上时发送。
|
|
25
|
+
* @iOS
|
|
26
|
+
* @Android
|
|
27
|
+
* @Harmony
|
|
28
|
+
* @eventProperty
|
|
29
|
+
*/
|
|
30
|
+
onTouchStart?: (e: LynxEvent<Target>['TouchStart']) => void
|
|
31
|
+
/**
|
|
32
|
+
* Send when a touch-point is removed from the UI.
|
|
33
|
+
* @zh 当触摸点从 UI 上移除时发送。
|
|
34
|
+
* @iOS
|
|
35
|
+
* @Android
|
|
36
|
+
* @Harmony
|
|
37
|
+
* @eventProperty
|
|
38
|
+
*/
|
|
39
|
+
onTouchEnd?: (e: LynxEvent<Target>['TouchEnd']) => void
|
|
40
|
+
/**
|
|
41
|
+
* Send when a touch-point is moving on the UI.
|
|
42
|
+
* @zh 当触摸点在 UI 上移动时发送。
|
|
43
|
+
* @iOS
|
|
44
|
+
* @Android
|
|
45
|
+
* @Harmony
|
|
46
|
+
* @eventProperty
|
|
47
|
+
*/
|
|
48
|
+
onTouchMove?: (e: LynxEvent<Target>['TouchMove']) => void
|
|
49
|
+
/**
|
|
50
|
+
* Send when a touch-point is held on the UI for a period of time.
|
|
51
|
+
* @zh 当触摸点在 UI 上按住一段时间后发送。
|
|
52
|
+
* @iOS
|
|
53
|
+
* @Android
|
|
54
|
+
* @Harmony
|
|
55
|
+
* @eventProperty
|
|
56
|
+
*/
|
|
57
|
+
onLongPress?: (e: LynxEvent<Target>['LongPress']) => void
|
|
58
|
+
/**
|
|
59
|
+
* Send when a touch-point is tapped on the UI.
|
|
60
|
+
* @zh 当触摸点在 UI 上轻触时发送。
|
|
61
|
+
* @iOS
|
|
62
|
+
* @Android
|
|
63
|
+
* @Harmony
|
|
64
|
+
* @eventProperty
|
|
65
|
+
*/
|
|
66
|
+
onTap?: (e: LynxBindCatchEvent<Target>['Tap']) => void
|
|
67
|
+
/**
|
|
68
|
+
* Same as onTouchCancel, but stops the event from propagating to parent elements.
|
|
69
|
+
* @zh 与 onTouchCancel 相同,但会阻止事件冒泡。
|
|
70
|
+
* @iOS
|
|
71
|
+
* @Android
|
|
72
|
+
* @Harmony
|
|
73
|
+
* @eventProperty
|
|
74
|
+
*/
|
|
75
|
+
catchTouchCancel?: (e: LynxEvent<Target>['TouchCancel']) => void
|
|
76
|
+
/**
|
|
77
|
+
* Same as onTouchStart, but stops the event from propagating to parent elements.
|
|
78
|
+
* @zh 与 onTouchStart 相同,但会阻止事件冒泡。
|
|
79
|
+
* @iOS
|
|
80
|
+
* @Android
|
|
81
|
+
* @Harmony
|
|
82
|
+
* @eventProperty
|
|
83
|
+
*/
|
|
84
|
+
catchTouchStart?: (e: LynxEvent<Target>['TouchStart']) => void
|
|
85
|
+
/**
|
|
86
|
+
* Same as onTouchEnd, but stops the event from propagating to parent elements.
|
|
87
|
+
* @zh 与 onTouchEnd 相同,但会阻止事件冒泡。
|
|
88
|
+
* @iOS
|
|
89
|
+
* @Android
|
|
90
|
+
* @Harmony
|
|
91
|
+
* @eventProperty
|
|
92
|
+
*/
|
|
93
|
+
catchTouchEnd?: (e: LynxEvent<Target>['TouchEnd']) => void
|
|
94
|
+
/**
|
|
95
|
+
* Same as onTouchMove, but stops the event from propagating to parent elements.
|
|
96
|
+
* @zh 与 onTouchMove 相同,但会阻止事件冒泡。
|
|
97
|
+
* @iOS
|
|
98
|
+
* @Android
|
|
99
|
+
* @Harmony
|
|
100
|
+
* @eventProperty
|
|
101
|
+
*/
|
|
102
|
+
catchTouchMove?: (e: LynxEvent<Target>['TouchMove']) => void
|
|
103
|
+
/**
|
|
104
|
+
* Same as onLongPress, but stops the event from propagating to parent elements.
|
|
105
|
+
* @zh 与 onLongPress 相同,但会阻止事件冒泡。
|
|
106
|
+
* @iOS
|
|
107
|
+
* @Android
|
|
108
|
+
* @Harmony
|
|
109
|
+
* @eventProperty
|
|
110
|
+
*/
|
|
111
|
+
catchLongPress?: (e: LynxEvent<Target>['LongPress']) => void
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export interface BaseUIExposureEvents {
|
|
115
|
+
/**
|
|
116
|
+
* Exposure event for UI
|
|
117
|
+
* @zh UI 的曝光事件。
|
|
118
|
+
* @iOS
|
|
119
|
+
* @Android
|
|
120
|
+
* @Harmony
|
|
121
|
+
*/
|
|
122
|
+
onUIAppear?: (e: CommonEvent) => void
|
|
123
|
+
/**
|
|
124
|
+
* Disappear event for UI
|
|
125
|
+
* @zh UI 的消失事件。
|
|
126
|
+
* @iOS
|
|
127
|
+
* @Android
|
|
128
|
+
* @Harmony
|
|
129
|
+
*/
|
|
130
|
+
onUIDisappear?: (e: CommonEvent) => void
|
|
131
|
+
}
|
|
132
|
+
export interface BaseUILayoutEvents {
|
|
133
|
+
/**
|
|
134
|
+
* Send when UI has changed viewport size.
|
|
135
|
+
* @zh 当 UI 视口大小改变时发送。
|
|
136
|
+
* @iOS
|
|
137
|
+
* @Android
|
|
138
|
+
* @Harmony
|
|
139
|
+
* @eventProperty
|
|
140
|
+
*/
|
|
141
|
+
onLayoutChange?: (e: LynxEvent<Target>['LayoutChange']) => void
|
|
142
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
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
|
+
export interface BaseUIExposureProps {
|
|
6
|
+
/**
|
|
7
|
+
* exposure-scene for global exposure
|
|
8
|
+
* @zh 用于全局曝光的 exposure-scene
|
|
9
|
+
* @iOS
|
|
10
|
+
* @Android
|
|
11
|
+
* @Harmony
|
|
12
|
+
*/
|
|
13
|
+
exposureScene?: string
|
|
14
|
+
/**
|
|
15
|
+
* exposure-id for global exposure
|
|
16
|
+
* @zh 用于全局曝光的 exposure-id
|
|
17
|
+
* @iOS
|
|
18
|
+
* @Android
|
|
19
|
+
* @Harmony
|
|
20
|
+
*/
|
|
21
|
+
exposureID?: string
|
|
22
|
+
}
|