@lynx-js/lynx-ui-common 3.139.0 → 3.143.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/CHANGELOG.md +12 -0
- package/dist/hooks/useGlobalEventListener.d.ts +2 -2
- package/dist/hooks/useGlobalEventListener.jsx.map +1 -1
- package/dist/hooks/useMemoizedFn.d.ts +2 -2
- package/dist/hooks/useMemoizedFn.jsx.map +1 -1
- package/dist/hooks/useRegisteredEvents.jsx.map +1 -1
- package/dist/reactive/hooks/useReactiveValue.jsx.map +1 -1
- package/dist/utils/getEventDetail.jsx.map +1 -1
- package/dist/utils/selector.jsx.map +1 -1
- package/package.json +5 -5
- package/src/hooks/useGlobalEventListener.ts +3 -3
- package/src/hooks/useMemoizedFn.ts +4 -4
- package/src/hooks/useRegisteredEvents.ts +9 -9
- package/src/reactive/hooks/useReactiveValue.ts +2 -2
- package/src/utils/getEventDetail.ts +1 -1
- package/src/utils/selector.ts +5 -5
- package/src/utils/common.test.ts +0 -65
- package/src/utils/selector.test.ts +0 -238
- package/src/utils/version.test.ts +0 -99
- package/tsconfig.docs.json +0 -7
- package/tsconfig.json +0 -14
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @lynx-js/lynx-ui-common
|
|
2
2
|
|
|
3
|
+
## 3.143.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Include missing README/CHANGELOG/SKILL/LICENSE files in published tarballs and exclude test and workspace-only config files. ([#404](https://github.com/lynx-family/lynx-ui/pull/404))
|
|
8
|
+
|
|
9
|
+
## 3.142.0
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- Accept React 18 and newer types. ReactLynx still defines the compatible versions for each app. ([#383](https://github.com/lynx-family/lynx-ui/pull/383))
|
|
14
|
+
|
|
3
15
|
## 3.139.0
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
type
|
|
1
|
+
type Noop = (...p: any) => void;
|
|
2
2
|
/**
|
|
3
3
|
* Use this hook to register events
|
|
4
4
|
* Events will be registered at first render, preventing possible message missing.
|
|
5
5
|
* When deps updates, handlerFunc will be updated, old event listener will be removed.
|
|
6
6
|
*/
|
|
7
|
-
export declare function useGlobalEventListener(eventName: string, handler:
|
|
7
|
+
export declare function useGlobalEventListener(eventName: string, handler: Noop, deps?: readonly unknown[]): void;
|
|
8
8
|
export {};
|
|
9
9
|
//# sourceMappingURL=useGlobalEventListener.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hooks/useGlobalEventListener.jsx","sources":["../../src/hooks/useGlobalEventListener.ts"],"sourcesContent":["// Copyright 2026 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\nimport { useCallback, useEffect, useMemo } from '@lynx-js/react'\n\n//
|
|
1
|
+
{"version":3,"file":"hooks/useGlobalEventListener.jsx","sources":["../../src/hooks/useGlobalEventListener.ts"],"sourcesContent":["// Copyright 2026 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\nimport { useCallback, useEffect, useMemo } from '@lynx-js/react'\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any -- generic type definition\ntype Noop = (...p: any) => void\n\n/**\n * Use this hook to register events\n * Events will be registered at first render, preventing possible message missing.\n * When deps updates, handlerFunc will be updated, old event listener will be removed.\n */\nexport function useGlobalEventListener(\n eventName: string,\n handler: Noop,\n deps?: readonly unknown[],\n) {\n const handlerFunc = useCallback(handler, deps ?? [])\n\n /**\n * We use useMemo to make it only run one time\n * useMemo will run at render.\n * If dependency changes, useMemo will rerun to have correct handlerFunc\n * then a new listener will be added with correct handlerFunc\n */\n useMemo(() => {\n if (!__LEPUS__) {\n lynx\n .getJSModule('GlobalEventEmitter')\n .addListener(eventName, handlerFunc)\n }\n }, [eventName, handlerFunc])\n\n /**\n * We use return value of useEffect to removeListener\n * Dependency is just the same as `useMemo` above\n * If dependency changes, useEffect's cleaning up func will be run, which is essentially\n * the cleaning up func of `addListener` run at useMemo\n */\n useEffect(\n () => () => {\n lynx\n .getJSModule('GlobalEventEmitter')\n .removeListener(eventName, handlerFunc)\n },\n [eventName, handlerFunc],\n )\n}\n"],"names":["useGlobalEventListener","eventName","handler","deps","handlerFunc","useCallback","useMemo","__LEPUS__","lynx","useEffect"],"mappings":";AAcO,SAASA,uBACdC,SAAiB,EACjBC,OAAa,EACbC,IAAyB;IAEzB,MAAMC,cAAcC,YAAYH,SAASC,QAAQ,EAAE;IAQnDG,QAAQ;QACN,IAAI,CAACC,WACHC,KACG,WAAW,CAAC,sBACZ,WAAW,CAACP,WAAWG;IAE9B,GAAG;QAACH;QAAWG;KAAY;IAQ3BK,UACE,IAAM;YACJD,KACG,WAAW,CAAC,sBACZ,cAAc,CAACP,WAAWG;QAC/B,GACA;QAACH;QAAWG;KAAY;AAE5B"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
type
|
|
1
|
+
type Noop = (this: any, ...args: any[]) => any;
|
|
2
2
|
/**
|
|
3
3
|
* Hooks for persistent functions. In general, useMemoizedFn can be used instead of useCallback.
|
|
4
4
|
* 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.
|
|
5
5
|
*/
|
|
6
|
-
declare function useMemoizedFn<T extends
|
|
6
|
+
declare function useMemoizedFn<T extends Noop>(fn: T): T;
|
|
7
7
|
export { useMemoizedFn };
|
|
8
8
|
//# sourceMappingURL=useMemoizedFn.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hooks/useMemoizedFn.jsx","sources":["../../src/hooks/useMemoizedFn.ts"],"sourcesContent":["// Copyright 2026 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\nimport { useMemo, useRef } from '@lynx-js/react'\n\n//
|
|
1
|
+
{"version":3,"file":"hooks/useMemoizedFn.jsx","sources":["../../src/hooks/useMemoizedFn.ts"],"sourcesContent":["// Copyright 2026 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\nimport { useMemo, useRef } from '@lynx-js/react'\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any -- generic type definition\ntype Noop = (this: any, ...args: any[]) => any\n\ntype PickFunction<T extends Noop> = (\n this: ThisParameterType<T>,\n ...args: Parameters<T>\n) => ReturnType<T>\n\n/**\n * Hooks for persistent functions. In general, useMemoizedFn can be used instead of useCallback.\n * 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.\n */\nfunction useMemoizedFn<T extends Noop>(fn: T) {\n const fnRef = useRef<T>(fn)\n\n // why not write `fnRef.current = fn`?\n // https://github.com/alibaba/hooks/issues/728\n fnRef.current = useMemo<T>(() => fn, [fn])\n\n const memoizedFn = useRef<PickFunction<T>>()\n memoizedFn.current ??= function(this, ...args) {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return fnRef.current.apply(this, args)\n }\n\n return memoizedFn.current as unknown as T\n}\n\nexport { useMemoizedFn }\n"],"names":["useMemoizedFn","fn","fnRef","useRef","useMemo","memoizedFn","args"],"mappings":";AAkBA,SAASA,cAA8BC,EAAK;IAC1C,MAAMC,QAAQC,OAAUF;IAIxBC,MAAM,OAAO,GAAGE,QAAW,IAAMH,IAAI;QAACA;KAAG;IAEzC,MAAMI,aAAaF;IACnBE,WAAW,OAAO,KAAK,SAAe,GAAGC,IAAI;QAE3C,OAAOJ,MAAM,OAAO,CAAC,KAAK,CAAC,IAAI,EAAEI;IACnC;IAEA,OAAOD,WAAW,OAAO;AAC3B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hooks/useRegisteredEvents.jsx","sources":["../../src/hooks/useRegisteredEvents.ts"],"sourcesContent":["// Copyright 2026 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\nimport { useMainThreadRef } from '@lynx-js/react'\n\nimport { useTouchEmulation } from '@lynx-js/react-use'\nimport type { TouchEvent } from '@lynx-js/types'\n\nimport { mainThreadifyEventsMapping } from '../utils'\n\n// These standard touch callbacks are registered through useTouchEmulation to fit the base touch-style API for both touch and mouse inputs.\nconst EMULATED_TOUCH_EVENT_NAMES = new Set([\n 'onTouchStart',\n 'onTouchMove',\n 'onTouchEnd',\n 'onTouchCancel',\n])\n\nconst useMainThreadifyEvents = (\n //
|
|
1
|
+
{"version":3,"file":"hooks/useRegisteredEvents.jsx","sources":["../../src/hooks/useRegisteredEvents.ts"],"sourcesContent":["// Copyright 2026 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\nimport { useMainThreadRef } from '@lynx-js/react'\n\nimport { useTouchEmulation } from '@lynx-js/react-use'\nimport type { TouchEvent } from '@lynx-js/types'\n\nimport { mainThreadifyEventsMapping } from '../utils'\n\n// These standard touch callbacks are registered through useTouchEmulation to fit the base touch-style API for both touch and mouse inputs.\nconst EMULATED_TOUCH_EVENT_NAMES = new Set([\n 'onTouchStart',\n 'onTouchMove',\n 'onTouchEnd',\n 'onTouchCancel',\n])\n\nconst useMainThreadifyEvents = (\n // eslint-disable-next-line @typescript-eslint/no-explicit-any -- dynamic event map\n events: Record<string, any>,\n mainThreadEvents: Record<string, string>, // e.g.: { 'onScroll': bindscroll } -> { 'main-thread:bindscroll': main-thread:bindscroll }\n) => {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any -- dynamic event map\n type ReducedEventsType = Record<string, any>\n const mainThreadifyEvents: ReducedEventsType = {}\n // eslint-disable-next-line @typescript-eslint/no-unused-vars -- main-thread marker\n const dummyRef = useMainThreadRef()\n for (const event of Object.keys(events)) {\n if (event in mainThreadEvents) {\n mainThreadifyEvents[mainThreadEvents[event]] = (e: unknown) => {\n 'main thread'\n if (events[event]) {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-call\n events[event](e)\n }\n }\n }\n }\n return mainThreadifyEvents\n}\n/**\n * Only bind the events that are passed in and registered in the registeredProps to improve performance.\n * @example\n * ```\n * const XXXOwnedProps: Record<string, string> = {\n * ...ScrollEvents,\n * ...ExposureEventsAndProps,\n * ...TouchEvents,\n * ...LayoutEvents,\n * };\n * const registerProps = useRegisteredEvents(props, XXXOwnedProps);\n * <XXX {...registerProps} />\n *\n * ```\n */\nexport const useRegisteredEvents = (\n // eslint-disable-next-line @typescript-eslint/no-explicit-any -- dynamic event map\n events: Record<string, any>,\n registeredEventsMapping: Record<string, string>,\n) => {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any -- dynamic event map\n type ReducedEventsType = Record<string, any>\n const reducedEvents: ReducedEventsType = {}\n for (const event of Object.keys(events)) {\n if (\n event in registeredEventsMapping\n && !EMULATED_TOUCH_EVENT_NAMES.has(event)\n ) {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n reducedEvents[registeredEventsMapping[event]] = events[event]\n }\n }\n const touchHandlers = useTouchEmulation({\n onTouchStart: events.onTouchStart as\n | ((event: TouchEvent) => void)\n | undefined,\n onTouchMove: events.onTouchMove as\n | ((event: TouchEvent) => void)\n | undefined,\n onTouchEnd: events.onTouchEnd as\n | ((event: TouchEvent) => void)\n | undefined,\n onTouchCancel: events.onTouchCancel as\n | ((event: TouchEvent) => void)\n | undefined,\n })\n const mainThreadifyEvents = mainThreadifyEventsMapping(\n registeredEventsMapping,\n )\n const mainThreadifyEventsContents = useMainThreadifyEvents(\n events,\n mainThreadifyEvents,\n )\n return { ...reducedEvents, ...touchHandlers, ...mainThreadifyEventsContents }\n}\n"],"names":["EMULATED_TOUCH_EVENT_NAMES","Set","useMainThreadifyEvents","events","mainThreadEvents","mainThreadifyEvents","useMainThreadRef","event","Object","e","useRegisteredEvents","registeredEventsMapping","reducedEvents","touchHandlers","useTouchEmulation","mainThreadifyEventsMapping","mainThreadifyEventsContents"],"mappings":";;;AAYA,MAAMA,6BAA6B,IAAIC,IAAI;IACzC;IACA;IACA;IACA;CACD;AAED,MAAMC,yBAAyB,CAE7BC,QACAC;IAIA,MAAMC,sBAAyC,CAAC;IAE/BC;IACjB,KAAK,MAAMC,SAASC,OAAO,IAAI,CAACL,QAC9B,IAAII,SAASH,kBACXC,mBAAmB,CAACD,gBAAgB,CAACG,MAAM,CAAC,GAAG,CAACE;QAC9C;QACA,IAAIN,MAAM,CAACI,MAAM,EAEfJ,MAAM,CAACI,MAAM,CAACE;IAElB;IAGJ,OAAOJ;AACT;AAgBO,MAAMK,sBAAsB,CAEjCP,QACAQ;IAIA,MAAMC,gBAAmC,CAAC;IAC1C,KAAK,MAAML,SAASC,OAAO,IAAI,CAACL,QAC9B,IACEI,SAASI,2BACN,CAACX,2BAA2B,GAAG,CAACO,QAGnCK,aAAa,CAACD,uBAAuB,CAACJ,MAAM,CAAC,GAAGJ,MAAM,CAACI,MAAM;IAGjE,MAAMM,gBAAgBC,kBAAkB;QACtC,cAAcX,OAAO,YAAY;QAGjC,aAAaA,OAAO,WAAW;QAG/B,YAAYA,OAAO,UAAU;QAG7B,eAAeA,OAAO,aAAa;IAGrC;IACA,MAAME,sBAAsBU,2BAC1BJ;IAEF,MAAMK,8BAA8Bd,uBAClCC,QACAE;IAEF,OAAO;QAAE,GAAGO,aAAa;QAAE,GAAGC,aAAa;QAAE,GAAGG,2BAA2B;IAAC;AAC9E"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reactive/hooks/useReactiveValue.jsx","sources":["../../../src/reactive/hooks/useReactiveValue.ts"],"sourcesContent":["// Copyright 2026 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\nimport { runOnMainThread, useMainThreadRef, useMemo } from '@lynx-js/react'\nimport type { MainThreadRef } from '@lynx-js/react'\n\nimport type {\n ReactiveValueAPI,\n ReactiveValueOptions,\n Subscriber,\n Unsubscribe,\n} from '../types'\n\nexport type ReactiveValue<T> = ReturnType<\n typeof createMainThreadReactiveValue<T>\n>\n\nexport function createMainThreadReactiveValue<T>(\n initialValue: T,\n options: ReactiveValueOptions = {},\n) {\n 'main thread'\n class ReactiveValue<T> implements ReactiveValueAPI<T> {\n _value: T\n _subscribers = new Set<Subscriber<T>>()\n _options: ReactiveValueOptions\n _isDestroyed = false\n\n constructor(initialValue: T, options: ReactiveValueOptions = {}) {\n this._value = initialValue\n this._options = options\n }\n\n get value(): T {\n return this._value\n }\n\n set value(newValue: T) {\n if (this._isDestroyed) return\n\n if (this._value !== newValue) {\n this._value = newValue\n this._notifySubscribers()\n }\n }\n\n get(): T {\n return this._value\n }\n\n set(newValue: T): void {\n this.value = newValue\n }\n\n subscribe(callback: Subscriber<T>): Unsubscribe {\n //
|
|
1
|
+
{"version":3,"file":"reactive/hooks/useReactiveValue.jsx","sources":["../../../src/reactive/hooks/useReactiveValue.ts"],"sourcesContent":["// Copyright 2026 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\nimport { runOnMainThread, useMainThreadRef, useMemo } from '@lynx-js/react'\nimport type { MainThreadRef } from '@lynx-js/react'\n\nimport type {\n ReactiveValueAPI,\n ReactiveValueOptions,\n Subscriber,\n Unsubscribe,\n} from '../types'\n\nexport type ReactiveValue<T> = ReturnType<\n typeof createMainThreadReactiveValue<T>\n>\n\nexport function createMainThreadReactiveValue<T>(\n initialValue: T,\n options: ReactiveValueOptions = {},\n) {\n 'main thread'\n class ReactiveValue<T> implements ReactiveValueAPI<T> {\n _value: T\n _subscribers = new Set<Subscriber<T>>()\n _options: ReactiveValueOptions\n _isDestroyed = false\n\n constructor(initialValue: T, options: ReactiveValueOptions = {}) {\n this._value = initialValue\n this._options = options\n }\n\n get value(): T {\n return this._value\n }\n\n set value(newValue: T) {\n if (this._isDestroyed) return\n\n if (this._value !== newValue) {\n this._value = newValue\n this._notifySubscribers()\n }\n }\n\n get(): T {\n return this._value\n }\n\n set(newValue: T): void {\n this.value = newValue\n }\n\n subscribe(callback: Subscriber<T>): Unsubscribe {\n // eslint-disable-next-line @typescript-eslint/no-empty-function -- intentional no-op cleanup\n if (this._isDestroyed) return () => {}\n\n this._subscribers.add(callback)\n\n // Call immediately with current value\n callback(this._value)\n\n return () => this.unsubscribe(callback)\n }\n\n unsubscribe(callback: Subscriber<T>): void {\n this._subscribers.delete(callback)\n }\n\n addListener(callback: Subscriber<T>): Unsubscribe {\n return this.subscribe(callback)\n }\n\n removeListener(callback: Subscriber<T>): void {\n this.unsubscribe(callback)\n }\n\n removeAllListeners(): void {\n this._subscribers.clear()\n }\n\n destroy(): void {\n this._isDestroyed = true\n this._subscribers.clear()\n }\n\n _notifySubscribers(): void {\n if (this._isDestroyed) return\n\n for (const subscriber of this._subscribers) {\n try {\n subscriber(this._value)\n } catch (error) {\n if (this._options.debug) {\n console.error(\n `[ReactiveValue${\n this._options.label ? ` ${this._options.label}` : ''\n }]: Error in subscriber:`,\n error,\n )\n }\n }\n }\n }\n }\n return new ReactiveValue(initialValue, options)\n}\n\nexport function useReactiveValue<T>(\n initialValue: T,\n options: ReactiveValueOptions = {},\n): MainThreadRef<ReactiveValue<T>> {\n const mtRef = useMainThreadRef<ReactiveValue<T>>({} as ReactiveValue<T>)\n\n function initReactiveValue() {\n 'main thread'\n mtRef.current = createMainThreadReactiveValue(initialValue, options)\n }\n\n useMemo(() => {\n if (__BACKGROUND__) {\n runOnMainThread(initReactiveValue)()\n } else {\n // Kept for preventing tree shaking\n void initReactiveValue\n }\n }, [])\n\n return mtRef\n}\n"],"names":["createMainThreadReactiveValue","initialValue","options","ReactiveValue","Set","newValue","callback","subscriber","error","console","useReactiveValue","mtRef","useMainThreadRef","initReactiveValue","useMemo","__BACKGROUND__","run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,KAAK,MAAMC,cAAc,IAAI,CAAC,YAAY,CACxC,IAAI;gBACFA,WAAW,IAAI,CAAC,MAAM;YACxB,EAAE,OAAOC,OAAO;gBACd,IAAI,IAAI,CAAC,QAAQ,CAAC,KAAK,EACrBC,QAAQ,KAAK,CACX,CAAC,cAAc,EACb,IAAI,CAAC,QAAQ,CAAC,KAAK,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,GAAG,GACnD,uBAAuB,CAAC,EACzBD;YAGN;QAEJ;IACF;IACA,OAAO,IAAIL,cAAcF,cAAcC;AACzC;AAEO,SAASQ,iBACdT,YAAe,EACfC,UAAgC,CAAC,CAAC;IAElC,MAAMS,QAAQC,iBAAmC,CAAC;IAElD,SAASC;QACP;QACAF,MAAM,OAAO,GAAGX,8BAA8BC,cAAcC;IAC9D;IAEAY,QAAQ;QACN,IAAIC,gBACFC,gBAAgBH;IAKpB,GAAG,EAAE;IAEL,OAAOF;AACT"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils/getEventDetail.jsx","sources":["../../src/utils/getEventDetail.ts"],"sourcesContent":["// Copyright 2026 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\n/**\n * A type representing event details that may contain layout and positioning information.\n * It includes optional properties for dimensions and coordinates, and allows for other properties.\n */\nexport type EventDetailWithLayout = {\n left?: number\n top?: number\n right?: number\n bottom?: number\n width?: number\n height?: number\n} & Record<string, unknown>\n\n/**\n * Safely retrieves detailed information from an event object.\n * It attempts to access `event.detail` first. If `detail` is not available,\n * it falls back to `event.params`. This is useful for maintaining\n * compatibility across different event structures on different platforms and versions.\n *\n * @param event The event object, which can be of any type.\n * @returns The `detail` or `params` object from the event, or an empty object if neither is found.\n */\nexport function getEventDetail<\n T extends EventDetailWithLayout = EventDetailWithLayout,\n> //
|
|
1
|
+
{"version":3,"file":"utils/getEventDetail.jsx","sources":["../../src/utils/getEventDetail.ts"],"sourcesContent":["// Copyright 2026 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\n/**\n * A type representing event details that may contain layout and positioning information.\n * It includes optional properties for dimensions and coordinates, and allows for other properties.\n */\nexport type EventDetailWithLayout = {\n left?: number\n top?: number\n right?: number\n bottom?: number\n width?: number\n height?: number\n} & Record<string, unknown>\n\n/**\n * Safely retrieves detailed information from an event object.\n * It attempts to access `event.detail` first. If `detail` is not available,\n * it falls back to `event.params`. This is useful for maintaining\n * compatibility across different event structures on different platforms and versions.\n *\n * @param event The event object, which can be of any type.\n * @returns The `detail` or `params` object from the event, or an empty object if neither is found.\n */\nexport function getEventDetail<\n T extends EventDetailWithLayout = EventDetailWithLayout,\n> // eslint-disable-next-line @typescript-eslint/no-explicit-any -- accept any event type\n(event: any): T {\n if (!event) {\n return {} as T\n }\n // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\n return (event.detail ?? event.params ?? {}) as T\n}\n"],"names":["getEventDetail","event"],"mappings":"AA0BO,SAASA,eAGfC,KAAU;IACT,IAAI,CAACA,OACH,OAAO,CAAC;IAGV,OAAQA,MAAM,MAAM,IAAIA,MAAM,MAAM,IAAI,CAAC;AAC3C"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils/selector.jsx","sources":["../../src/utils/selector.ts"],"sourcesContent":["// Copyright 2026 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\nimport type { RefObject } from '@lynx-js/react'\n\nimport type { AnyObject, MainThread, NodesRef } from '@lynx-js/types'\n\nexport class InvokeRejectError extends Error {\n errorCode: number\n detail?: object | string\n constructor(errorCode: number, errorMsg?: string) {\n super(typeof errorMsg === 'string' ? errorMsg : 'unknown error')\n this.errorCode = errorCode\n this.detail = errorMsg\n }\n}\n\nexport const setNativePropsByRef = (\n ref: RefObject<NodesRef> | undefined,\n //
|
|
1
|
+
{"version":3,"file":"utils/selector.jsx","sources":["../../src/utils/selector.ts"],"sourcesContent":["// Copyright 2026 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\nimport type { RefObject } from '@lynx-js/react'\n\nimport type { AnyObject, MainThread, NodesRef } from '@lynx-js/types'\n\nexport class InvokeRejectError extends Error {\n errorCode: number\n detail?: object | string\n constructor(errorCode: number, errorMsg?: string) {\n super(typeof errorMsg === 'string' ? errorMsg : 'unknown error')\n this.errorCode = errorCode\n this.detail = errorMsg\n }\n}\n\nexport const setNativePropsByRef = (\n ref: RefObject<NodesRef> | undefined,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Lynx selector result\n props: Record<string, any>,\n) => {\n ref?.current?.setNativeProps(props).exec()\n}\n\nexport const setNativePropsById = (\n id: string,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Lynx selector result\n props: Record<string, any>,\n) => {\n lynx.createSelectorQuery().select(`#${id}`).setNativeProps(props).exec()\n}\n\nexport const setNativeProps = (\n target: {\n ref?: RefObject<NodesRef>\n id?: string\n },\n // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Lynx selector result\n props: Record<string, any>,\n) =>\n target.id\n ? setNativePropsById(target.id, props)\n : setNativePropsByRef(target.ref, props)\n\nexport const invokeByRef = (\n ref: RefObject<NodesRef> | undefined,\n method: string,\n params?: AnyObject,\n): Promise<unknown> =>\n new Promise((resolve, reject) => {\n if (!ref?.current) {\n reject(new InvokeRejectError(2, 'no node found for the ref'))\n return\n }\n ref?.current\n ?.invoke({\n method,\n params,\n success: (res) => {\n resolve(res)\n },\n // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Lynx selector result\n fail: (res: { code: number, data?: any }) => {\n reject(new InvokeRejectError(res.code, JSON.stringify(res.data)))\n },\n })\n .exec()\n })\n\nexport const invokeById = (\n id: string,\n method: string,\n params?: AnyObject,\n): Promise<unknown> =>\n new Promise((resolve, reject) => {\n lynx\n .createSelectorQuery()\n .select(`#${id}`)\n .invoke({\n method,\n params,\n success: (res) => {\n resolve(res)\n },\n // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Lynx selector result\n fail: (res: { code: number, data?: any }) => {\n reject(new InvokeRejectError(res.code, JSON.stringify(res.data)))\n },\n })\n .exec()\n })\n\nexport const invoke = (\n target: {\n ref?: RefObject<NodesRef>\n id?: string\n },\n method: string,\n params?: AnyObject,\n): Promise<unknown> =>\n target.id\n ? invokeById(target.id, method, params)\n : invokeByRef(target.ref, method, params)\n\nexport interface GetRectPromise {\n left: number\n top: number\n width: number\n height: number\n bottom: number\n right: number\n}\n\nexport const getRectByRef = (\n ref?: RefObject<NodesRef>,\n relativeToScreen = false,\n relativeTo = '',\n): Promise<GetRectPromise> =>\n new Promise((resolve, reject) => {\n invokeByRef(ref, 'boundingClientRect', {\n relativeTo: relativeToScreen ? 'screen' : relativeTo,\n // Match the standard getBoundingClientRect behavior: transforms are\n // included in the returned rect. Keep this enabled by default across\n // platforms to avoid incorrect bounds for transformed nodes, such as\n // scaled views. A few animation-related cases may opt out explicitly.\n androidEnableTransformProps: true,\n iosEnableTransformProps: true,\n harmonyEnableTransformProps: true,\n })\n .then((res) => {\n resolve(\n res as {\n left: number\n top: number\n width: number\n height: number\n bottom: number\n right: number\n },\n )\n })\n .catch((error: InvokeRejectError) => {\n reject(error)\n })\n })\n\nexport const getRootRect = (\n relativeToScreen = false,\n relativeTo = '',\n): Promise<GetRectPromise> =>\n new Promise((resolve, reject) => {\n lynx\n .createSelectorQuery()\n .selectRoot()\n .invoke({\n method: 'boundingClientRect',\n params: {\n relativeTo: relativeToScreen ? 'screen' : relativeTo,\n // Match the standard getBoundingClientRect behavior: transforms are\n // included in the returned rect. Keep this enabled by default across\n // platforms to avoid incorrect bounds for transformed nodes, such as\n // scaled views. A few animation-related cases may opt out explicitly.\n androidEnableTransformProps: true,\n iosEnableTransformProps: true,\n harmonyEnableTransformProps: true,\n },\n success: (res) => {\n resolve(\n res as {\n left: number\n top: number\n width: number\n height: number\n bottom: number\n right: number\n },\n )\n },\n fail: (res: { code: number, data?: object }) => {\n reject(new InvokeRejectError(res.code, JSON.stringify(res.data)))\n },\n })\n .exec()\n })\n\nexport const getRectById = (\n id: string,\n relativeToScreen = false,\n relativeTo = '',\n): Promise<GetRectPromise> =>\n new Promise((resolve, reject) => {\n invokeById(id, 'boundingClientRect', {\n relativeTo: relativeToScreen ? 'screen' : relativeTo,\n // Match the standard getBoundingClientRect behavior: transforms are\n // included in the returned rect. Keep this enabled by default across\n // platforms to avoid incorrect bounds for transformed nodes, such as\n // scaled views. A few animation-related cases may opt out explicitly.\n androidEnableTransformProps: true,\n iosEnableTransformProps: true,\n harmonyEnableTransformProps: true,\n })\n .then((res) => {\n resolve(\n res as {\n left: number\n top: number\n width: number\n height: number\n bottom: number\n right: number\n },\n )\n })\n .catch((error: InvokeRejectError) => {\n reject(error)\n })\n })\n\nexport const getRect = (\n target: {\n ref?: RefObject<NodesRef>\n id?: string\n },\n relativeToScreen = false,\n): Promise<GetRectPromise> =>\n target.id\n ? getRectById(target.id, relativeToScreen)\n : getRectByRef(target.ref, relativeToScreen)\n\nexport const selectorMT: (selector: string) => MainThread.Element | null = (\n selector: string,\n) => {\n 'main thread'\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n\n return lynx.querySelector(`#${selector}`)\n}\n"],"names":["InvokeRejectError","Error","errorCode","errorMsg","setNativePropsByRef","ref","props","setNativePropsById","id","lynx","setNativeProps","target","invokeByRef","method","params","Promise","resolve","reject","res","JSON","invokeById","invoke","getRectByRef","relativeToScreen","relativeTo","error","getRootRect","getRectById","getRect","selectorMT","selector"],"mappings":"AAQO,MAAMA,0BAA0BC;IACrC,UAAiB;IACjB,OAAwB;IACxB,YAAYC,SAAiB,EAAEC,QAAiB,CAAE;QAChD,KAAK,CAAC,AAAoB,YAApB,OAAOA,WAAwBA,WAAW;QAChD,IAAI,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|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lynx-js/lynx-ui-common",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.143.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/lynx-family/lynx-ui.git",
|
|
@@ -16,12 +16,12 @@
|
|
|
16
16
|
"module": "./dist/index.jsx",
|
|
17
17
|
"types": "./dist/index.d.ts",
|
|
18
18
|
"files": [
|
|
19
|
-
"src
|
|
19
|
+
"src",
|
|
20
|
+
"!src/**/__tests__/**",
|
|
21
|
+
"!src/**/*.test.*",
|
|
20
22
|
"README.md",
|
|
21
23
|
"CHANGELOG.md",
|
|
22
24
|
"LICENSE",
|
|
23
|
-
"tsconfig.docs.json",
|
|
24
|
-
"tsconfig.json",
|
|
25
25
|
"dist/**"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"peerDependencies": {
|
|
39
39
|
"@lynx-js/react": ">=0.100.0",
|
|
40
40
|
"@lynx-js/types": "*",
|
|
41
|
-
"@types/react": "
|
|
41
|
+
"@types/react": ">=18"
|
|
42
42
|
},
|
|
43
43
|
"publishConfig": {
|
|
44
44
|
"access": "public"
|
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
|
|
5
5
|
import { useCallback, useEffect, useMemo } from '@lynx-js/react'
|
|
6
6
|
|
|
7
|
-
//
|
|
8
|
-
type
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- generic type definition
|
|
8
|
+
type Noop = (...p: any) => void
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Use this hook to register events
|
|
@@ -14,7 +14,7 @@ type noop = (...p: any) => void
|
|
|
14
14
|
*/
|
|
15
15
|
export function useGlobalEventListener(
|
|
16
16
|
eventName: string,
|
|
17
|
-
handler:
|
|
17
|
+
handler: Noop,
|
|
18
18
|
deps?: readonly unknown[],
|
|
19
19
|
) {
|
|
20
20
|
const handlerFunc = useCallback(handler, deps ?? [])
|
|
@@ -4,10 +4,10 @@
|
|
|
4
4
|
|
|
5
5
|
import { useMemo, useRef } from '@lynx-js/react'
|
|
6
6
|
|
|
7
|
-
//
|
|
8
|
-
type
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- generic type definition
|
|
8
|
+
type Noop = (this: any, ...args: any[]) => any
|
|
9
9
|
|
|
10
|
-
type PickFunction<T extends
|
|
10
|
+
type PickFunction<T extends Noop> = (
|
|
11
11
|
this: ThisParameterType<T>,
|
|
12
12
|
...args: Parameters<T>
|
|
13
13
|
) => ReturnType<T>
|
|
@@ -16,7 +16,7 @@ type PickFunction<T extends noop> = (
|
|
|
16
16
|
* Hooks for persistent functions. In general, useMemoizedFn can be used instead of useCallback.
|
|
17
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
18
|
*/
|
|
19
|
-
function useMemoizedFn<T extends
|
|
19
|
+
function useMemoizedFn<T extends Noop>(fn: T) {
|
|
20
20
|
const fnRef = useRef<T>(fn)
|
|
21
21
|
|
|
22
22
|
// why not write `fnRef.current = fn`?
|
|
@@ -18,14 +18,14 @@ const EMULATED_TOUCH_EVENT_NAMES = new Set([
|
|
|
18
18
|
])
|
|
19
19
|
|
|
20
20
|
const useMainThreadifyEvents = (
|
|
21
|
-
//
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- dynamic event map
|
|
22
22
|
events: Record<string, any>,
|
|
23
23
|
mainThreadEvents: Record<string, string>, // e.g.: { 'onScroll': bindscroll } -> { 'main-thread:bindscroll': main-thread:bindscroll }
|
|
24
24
|
) => {
|
|
25
|
-
//
|
|
26
|
-
type
|
|
27
|
-
const mainThreadifyEvents:
|
|
28
|
-
//
|
|
25
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- dynamic event map
|
|
26
|
+
type ReducedEventsType = Record<string, any>
|
|
27
|
+
const mainThreadifyEvents: ReducedEventsType = {}
|
|
28
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- main-thread marker
|
|
29
29
|
const dummyRef = useMainThreadRef()
|
|
30
30
|
for (const event of Object.keys(events)) {
|
|
31
31
|
if (event in mainThreadEvents) {
|
|
@@ -56,13 +56,13 @@ const useMainThreadifyEvents = (
|
|
|
56
56
|
* ```
|
|
57
57
|
*/
|
|
58
58
|
export const useRegisteredEvents = (
|
|
59
|
-
//
|
|
59
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- dynamic event map
|
|
60
60
|
events: Record<string, any>,
|
|
61
61
|
registeredEventsMapping: Record<string, string>,
|
|
62
62
|
) => {
|
|
63
|
-
//
|
|
64
|
-
type
|
|
65
|
-
const reducedEvents:
|
|
63
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- dynamic event map
|
|
64
|
+
type ReducedEventsType = Record<string, any>
|
|
65
|
+
const reducedEvents: ReducedEventsType = {}
|
|
66
66
|
for (const event of Object.keys(events)) {
|
|
67
67
|
if (
|
|
68
68
|
event in registeredEventsMapping
|
|
@@ -54,7 +54,7 @@ export function createMainThreadReactiveValue<T>(
|
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
subscribe(callback: Subscriber<T>): Unsubscribe {
|
|
57
|
-
//
|
|
57
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function -- intentional no-op cleanup
|
|
58
58
|
if (this._isDestroyed) return () => {}
|
|
59
59
|
|
|
60
60
|
this._subscribers.add(callback)
|
|
@@ -124,7 +124,7 @@ export function useReactiveValue<T>(
|
|
|
124
124
|
runOnMainThread(initReactiveValue)()
|
|
125
125
|
} else {
|
|
126
126
|
// Kept for preventing tree shaking
|
|
127
|
-
initReactiveValue
|
|
127
|
+
void initReactiveValue
|
|
128
128
|
}
|
|
129
129
|
}, [])
|
|
130
130
|
|
|
@@ -26,7 +26,7 @@ export type EventDetailWithLayout = {
|
|
|
26
26
|
*/
|
|
27
27
|
export function getEventDetail<
|
|
28
28
|
T extends EventDetailWithLayout = EventDetailWithLayout,
|
|
29
|
-
> //
|
|
29
|
+
> // eslint-disable-next-line @typescript-eslint/no-explicit-any -- accept any event type
|
|
30
30
|
(event: any): T {
|
|
31
31
|
if (!event) {
|
|
32
32
|
return {} as T
|
package/src/utils/selector.ts
CHANGED
|
@@ -18,7 +18,7 @@ export class InvokeRejectError extends Error {
|
|
|
18
18
|
|
|
19
19
|
export const setNativePropsByRef = (
|
|
20
20
|
ref: RefObject<NodesRef> | undefined,
|
|
21
|
-
//
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Lynx selector result
|
|
22
22
|
props: Record<string, any>,
|
|
23
23
|
) => {
|
|
24
24
|
ref?.current?.setNativeProps(props).exec()
|
|
@@ -26,7 +26,7 @@ export const setNativePropsByRef = (
|
|
|
26
26
|
|
|
27
27
|
export const setNativePropsById = (
|
|
28
28
|
id: string,
|
|
29
|
-
//
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Lynx selector result
|
|
30
30
|
props: Record<string, any>,
|
|
31
31
|
) => {
|
|
32
32
|
lynx.createSelectorQuery().select(`#${id}`).setNativeProps(props).exec()
|
|
@@ -37,7 +37,7 @@ export const setNativeProps = (
|
|
|
37
37
|
ref?: RefObject<NodesRef>
|
|
38
38
|
id?: string
|
|
39
39
|
},
|
|
40
|
-
//
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Lynx selector result
|
|
41
41
|
props: Record<string, any>,
|
|
42
42
|
) =>
|
|
43
43
|
target.id
|
|
@@ -61,7 +61,7 @@ export const invokeByRef = (
|
|
|
61
61
|
success: (res) => {
|
|
62
62
|
resolve(res)
|
|
63
63
|
},
|
|
64
|
-
//
|
|
64
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Lynx selector result
|
|
65
65
|
fail: (res: { code: number, data?: any }) => {
|
|
66
66
|
reject(new InvokeRejectError(res.code, JSON.stringify(res.data)))
|
|
67
67
|
},
|
|
@@ -84,7 +84,7 @@ export const invokeById = (
|
|
|
84
84
|
success: (res) => {
|
|
85
85
|
resolve(res)
|
|
86
86
|
},
|
|
87
|
-
//
|
|
87
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Lynx selector result
|
|
88
88
|
fail: (res: { code: number, data?: any }) => {
|
|
89
89
|
reject(new InvokeRejectError(res.code, JSON.stringify(res.data)))
|
|
90
90
|
},
|
package/src/utils/common.test.ts
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
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 { describe, expect, it } from 'vitest'
|
|
6
|
-
|
|
7
|
-
import { get, noop } from './common'
|
|
8
|
-
|
|
9
|
-
describe('common utils', () => {
|
|
10
|
-
describe('get', () => {
|
|
11
|
-
const testObj = {
|
|
12
|
-
a: {
|
|
13
|
-
b: {
|
|
14
|
-
c: 'value',
|
|
15
|
-
},
|
|
16
|
-
arr: [{ nested: 'array-value' }],
|
|
17
|
-
},
|
|
18
|
-
nullValue: null,
|
|
19
|
-
undefinedValue: undefined,
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
it('should return value for dot notation path', () => {
|
|
23
|
-
expect(get(testObj, 'a.b.c')).toBe('value')
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
it('should return value for bracket notation path', () => {
|
|
27
|
-
expect(get(testObj, 'a[b][c]')).toBe('value')
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
it('should return value for array index path', () => {
|
|
31
|
-
expect(get(testObj, 'a.arr[0].nested')).toBe('array-value')
|
|
32
|
-
expect(get(testObj, 'a.arr.0.nested')).toBe('array-value')
|
|
33
|
-
})
|
|
34
|
-
|
|
35
|
-
it('should return default value when path does not exist', () => {
|
|
36
|
-
expect(get(testObj, 'a.b.d', 'default')).toBe('default')
|
|
37
|
-
expect(get(testObj, 'x.y.z', 'default')).toBe('default')
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
it('should return undefined when no default value is provided and path does not exist', () => {
|
|
41
|
-
expect(get(testObj, 'a.b.d')).toBeUndefined()
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
it('should handle null and undefined values', () => {
|
|
45
|
-
expect(get(testObj, 'nullValue.something', 'default')).toBe('default')
|
|
46
|
-
expect(get(testObj, 'undefinedValue.something', 'default')).toBe(
|
|
47
|
-
'default',
|
|
48
|
-
)
|
|
49
|
-
})
|
|
50
|
-
|
|
51
|
-
it('should return default value when input is not an object', () => {
|
|
52
|
-
expect(get(null, 'any.path', 'default')).toBe('default')
|
|
53
|
-
expect(get(undefined, 'any.path', 'default')).toBe('default')
|
|
54
|
-
expect(get('string', 'any.path', 'default')).toBe('default')
|
|
55
|
-
expect(get(123, 'any.path', 'default')).toBe('default')
|
|
56
|
-
})
|
|
57
|
-
})
|
|
58
|
-
|
|
59
|
-
describe('noop', () => {
|
|
60
|
-
it('should be a function that returns undefined', () => {
|
|
61
|
-
expect(typeof noop).toBe('function')
|
|
62
|
-
expect(noop()).toBeUndefined()
|
|
63
|
-
})
|
|
64
|
-
})
|
|
65
|
-
})
|
|
@@ -1,238 +0,0 @@
|
|
|
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 { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
InvokeRejectError,
|
|
9
|
-
getRect,
|
|
10
|
-
getRectById,
|
|
11
|
-
getRectByRef,
|
|
12
|
-
invokeById,
|
|
13
|
-
invokeByRef,
|
|
14
|
-
setNativeProps,
|
|
15
|
-
setNativePropsById,
|
|
16
|
-
setNativePropsByRef,
|
|
17
|
-
} from './selector'
|
|
18
|
-
|
|
19
|
-
// Mock the global lynx object
|
|
20
|
-
const mockExec = vi.fn()
|
|
21
|
-
const mockSelect = vi.fn().mockReturnThis()
|
|
22
|
-
const mockSetNativeProps = vi.fn().mockReturnThis()
|
|
23
|
-
const mockInvoke = vi.fn().mockReturnThis()
|
|
24
|
-
|
|
25
|
-
global.lynx = {
|
|
26
|
-
// @ts-expect-error expected
|
|
27
|
-
createSelectorQuery: () => ({
|
|
28
|
-
select: mockSelect,
|
|
29
|
-
setNativeProps: mockSetNativeProps,
|
|
30
|
-
invoke: mockInvoke,
|
|
31
|
-
exec: mockExec,
|
|
32
|
-
}),
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
describe('selector utils', () => {
|
|
36
|
-
beforeEach(() => {
|
|
37
|
-
vi.clearAllMocks()
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
describe('InvokeRejectError', () => {
|
|
41
|
-
it('should create error with code and message', () => {
|
|
42
|
-
const error = new InvokeRejectError(404, 'Not Found')
|
|
43
|
-
expect(error.errorCode).toBe(404)
|
|
44
|
-
expect(error.message).toBe('Not Found')
|
|
45
|
-
})
|
|
46
|
-
})
|
|
47
|
-
|
|
48
|
-
describe('setNativeProps functions', () => {
|
|
49
|
-
const testProps = { style: { color: 'red' } }
|
|
50
|
-
|
|
51
|
-
describe('setNativePropsByRef', () => {
|
|
52
|
-
it('should call setNativeProps with ref', () => {
|
|
53
|
-
const mockRef = {
|
|
54
|
-
current: {
|
|
55
|
-
setNativeProps: vi.fn().mockReturnThis(),
|
|
56
|
-
exec: vi.fn(),
|
|
57
|
-
},
|
|
58
|
-
}
|
|
59
|
-
// @ts-expect-error expected
|
|
60
|
-
setNativePropsByRef(mockRef, testProps)
|
|
61
|
-
expect(mockRef.current.setNativeProps).toHaveBeenCalledWith(testProps)
|
|
62
|
-
expect(mockRef.current.exec).toHaveBeenCalled()
|
|
63
|
-
})
|
|
64
|
-
})
|
|
65
|
-
|
|
66
|
-
describe('setNativePropsById', () => {
|
|
67
|
-
it('should call setNativeProps with id', () => {
|
|
68
|
-
setNativePropsById('test-id', testProps)
|
|
69
|
-
expect(mockSelect).toHaveBeenCalledWith('#test-id')
|
|
70
|
-
expect(mockSetNativeProps).toHaveBeenCalledWith(testProps)
|
|
71
|
-
expect(mockExec).toHaveBeenCalled()
|
|
72
|
-
})
|
|
73
|
-
})
|
|
74
|
-
|
|
75
|
-
describe('setNativeProps', () => {
|
|
76
|
-
it('should use id when available', () => {
|
|
77
|
-
setNativeProps({ id: 'test-id' }, testProps)
|
|
78
|
-
expect(mockSelect).toHaveBeenCalledWith('#test-id')
|
|
79
|
-
})
|
|
80
|
-
|
|
81
|
-
it('should use ref when id is not available', () => {
|
|
82
|
-
const mockRef = {
|
|
83
|
-
current: {
|
|
84
|
-
setNativeProps: vi.fn().mockReturnThis(),
|
|
85
|
-
exec: vi.fn(),
|
|
86
|
-
},
|
|
87
|
-
}
|
|
88
|
-
// @ts-expect-error expected
|
|
89
|
-
setNativeProps({ ref: mockRef }, testProps)
|
|
90
|
-
expect(mockRef.current.setNativeProps).toHaveBeenCalledWith(testProps)
|
|
91
|
-
})
|
|
92
|
-
})
|
|
93
|
-
})
|
|
94
|
-
|
|
95
|
-
describe('invoke functions', () => {
|
|
96
|
-
const method = 'testMethod'
|
|
97
|
-
const params = { test: true }
|
|
98
|
-
|
|
99
|
-
describe('invokeByRef', () => {
|
|
100
|
-
it('should resolve successfully', async () => {
|
|
101
|
-
const successResponse = { data: 'success' }
|
|
102
|
-
const mockRef = {
|
|
103
|
-
current: {
|
|
104
|
-
invoke: vi.fn().mockImplementation(({ success }) => ({
|
|
105
|
-
exec: () => success(successResponse),
|
|
106
|
-
})),
|
|
107
|
-
},
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// @ts-expect-error expected
|
|
111
|
-
const result = await invokeByRef(mockRef, method, params)
|
|
112
|
-
expect(result).toBe(successResponse)
|
|
113
|
-
})
|
|
114
|
-
|
|
115
|
-
it('should reject when no ref current', async () => {
|
|
116
|
-
// @ts-expect-error expected
|
|
117
|
-
await expect(invokeByRef({}, method, params)).rejects.toThrow(
|
|
118
|
-
'no node found for the ref',
|
|
119
|
-
)
|
|
120
|
-
})
|
|
121
|
-
|
|
122
|
-
it('should reject on failure', async () => {
|
|
123
|
-
const mockRef = {
|
|
124
|
-
current: {
|
|
125
|
-
invoke: vi.fn().mockImplementation(({ fail }) => ({
|
|
126
|
-
exec: () => fail({ code: 500, data: 'error' }),
|
|
127
|
-
})),
|
|
128
|
-
},
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
// @ts-expect-error expected
|
|
132
|
-
await expect(invokeByRef(mockRef, method, params)).rejects.toThrow()
|
|
133
|
-
})
|
|
134
|
-
})
|
|
135
|
-
|
|
136
|
-
describe('invokeById', () => {
|
|
137
|
-
it('should resolve successfully', async () => {
|
|
138
|
-
const successResponse = { data: 'success' }
|
|
139
|
-
mockInvoke.mockImplementation(({ success }) => {
|
|
140
|
-
success(successResponse)
|
|
141
|
-
return { exec: mockExec }
|
|
142
|
-
})
|
|
143
|
-
|
|
144
|
-
const result = await invokeById('test-id', method, params)
|
|
145
|
-
expect(result).toBe(successResponse)
|
|
146
|
-
})
|
|
147
|
-
|
|
148
|
-
it('should reject on failure', async () => {
|
|
149
|
-
mockInvoke.mockImplementation(({ fail }) => {
|
|
150
|
-
fail({ code: 500, data: 'error' })
|
|
151
|
-
return { exec: mockExec }
|
|
152
|
-
})
|
|
153
|
-
|
|
154
|
-
await expect(invokeById('test-id', method, params)).rejects.toThrow()
|
|
155
|
-
})
|
|
156
|
-
})
|
|
157
|
-
})
|
|
158
|
-
|
|
159
|
-
describe('getRect functions', () => {
|
|
160
|
-
const mockRect = {
|
|
161
|
-
left: 0,
|
|
162
|
-
top: 0,
|
|
163
|
-
width: 100,
|
|
164
|
-
height: 100,
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
describe('getRectByRef', () => {
|
|
168
|
-
it('should get rect relative to viewport', async () => {
|
|
169
|
-
const mockRef = {
|
|
170
|
-
current: {
|
|
171
|
-
invoke: vi.fn().mockImplementation(({ success }) => ({
|
|
172
|
-
exec: () => success(mockRect),
|
|
173
|
-
})),
|
|
174
|
-
},
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// @ts-expect-error expected
|
|
178
|
-
const result = await getRectByRef(mockRef)
|
|
179
|
-
expect(result).toEqual(mockRect)
|
|
180
|
-
})
|
|
181
|
-
|
|
182
|
-
it('should get rect relative to screen', async () => {
|
|
183
|
-
const mockRef = {
|
|
184
|
-
current: {
|
|
185
|
-
invoke: vi.fn().mockImplementation(({ success, params }) => ({
|
|
186
|
-
exec: () => {
|
|
187
|
-
expect(params.relativeTo).toBe('screen')
|
|
188
|
-
success(mockRect)
|
|
189
|
-
},
|
|
190
|
-
})),
|
|
191
|
-
},
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
// @ts-expect-error expected
|
|
195
|
-
const result = await getRectByRef(mockRef, true)
|
|
196
|
-
expect(result).toEqual(mockRect)
|
|
197
|
-
})
|
|
198
|
-
})
|
|
199
|
-
|
|
200
|
-
describe('getRectById', () => {
|
|
201
|
-
it('should get rect by id', async () => {
|
|
202
|
-
mockInvoke.mockImplementation(({ success }) => {
|
|
203
|
-
success(mockRect)
|
|
204
|
-
return { exec: mockExec }
|
|
205
|
-
})
|
|
206
|
-
|
|
207
|
-
const result = await getRectById('test-id')
|
|
208
|
-
expect(result).toEqual(mockRect)
|
|
209
|
-
})
|
|
210
|
-
})
|
|
211
|
-
|
|
212
|
-
describe('getRect', () => {
|
|
213
|
-
it('should use id when available', async () => {
|
|
214
|
-
mockInvoke.mockImplementation(({ success }) => {
|
|
215
|
-
success(mockRect)
|
|
216
|
-
return { exec: mockExec }
|
|
217
|
-
})
|
|
218
|
-
|
|
219
|
-
const result = await getRect({ id: 'test-id' })
|
|
220
|
-
expect(result).toEqual(mockRect)
|
|
221
|
-
})
|
|
222
|
-
|
|
223
|
-
it('should use ref when id is not available', async () => {
|
|
224
|
-
const mockRef = {
|
|
225
|
-
current: {
|
|
226
|
-
invoke: vi.fn().mockImplementation(({ success }) => ({
|
|
227
|
-
exec: () => success(mockRect),
|
|
228
|
-
})),
|
|
229
|
-
},
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
// @ts-expect-error expected
|
|
233
|
-
const result = await getRect({ ref: mockRef })
|
|
234
|
-
expect(result).toEqual(mockRect)
|
|
235
|
-
})
|
|
236
|
-
})
|
|
237
|
-
})
|
|
238
|
-
})
|
|
@@ -1,99 +0,0 @@
|
|
|
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 { beforeEach, describe, expect, it } from 'vitest'
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
lynxSDKVersionStringToNumber,
|
|
9
|
-
nativeLynxSDKVersionGreaterThan,
|
|
10
|
-
nativeLynxSDKVersionLessThan,
|
|
11
|
-
} from './version'
|
|
12
|
-
|
|
13
|
-
function createMockSystemInfo(lynxSdkVersion: string): typeof SystemInfo {
|
|
14
|
-
return {
|
|
15
|
-
lynxSdkVersion,
|
|
16
|
-
osVersion: '1.0',
|
|
17
|
-
pixelHeight: 100,
|
|
18
|
-
pixelWidth: 100,
|
|
19
|
-
pixelRatio: 1,
|
|
20
|
-
platform: 'Android',
|
|
21
|
-
runtimeType: 'quickjs',
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
describe('version utils', () => {
|
|
26
|
-
describe('lynxSDKVersionStringToNumber', () => {
|
|
27
|
-
it('should convert version string to number correctly', () => {
|
|
28
|
-
expect(lynxSDKVersionStringToNumber('1.2.3')).toBe(10203)
|
|
29
|
-
expect(lynxSDKVersionStringToNumber('2.0.0')).toBe(20000)
|
|
30
|
-
expect(lynxSDKVersionStringToNumber('0.1.0')).toBe(100)
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
it('should handle missing version parts', () => {
|
|
34
|
-
expect(lynxSDKVersionStringToNumber('1')).toBe(10000)
|
|
35
|
-
expect(lynxSDKVersionStringToNumber('1.2')).toBe(10200)
|
|
36
|
-
expect(lynxSDKVersionStringToNumber('')).toBe(0)
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
it('should handle version strings with leading zeros', () => {
|
|
40
|
-
expect(lynxSDKVersionStringToNumber('01.02.03')).toBe(10203)
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
it('should handle large version numbers', () => {
|
|
44
|
-
expect(lynxSDKVersionStringToNumber('99.99.99')).toBe(999999)
|
|
45
|
-
})
|
|
46
|
-
})
|
|
47
|
-
|
|
48
|
-
describe('nativeLynxSDKVersionGreaterThan', () => {
|
|
49
|
-
beforeEach(() => {
|
|
50
|
-
global.SystemInfo = createMockSystemInfo('1.2.3')
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
it('should return true when native version is greater', () => {
|
|
54
|
-
global.SystemInfo = createMockSystemInfo('1.2.4')
|
|
55
|
-
expect(nativeLynxSDKVersionGreaterThan('1.2.3')).toBe(true)
|
|
56
|
-
})
|
|
57
|
-
|
|
58
|
-
it('should return false when native version is equal', () => {
|
|
59
|
-
global.SystemInfo = createMockSystemInfo('1.2.3')
|
|
60
|
-
expect(nativeLynxSDKVersionGreaterThan('1.2.3')).toBe(false)
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
it('should return false when native version is less', () => {
|
|
64
|
-
global.SystemInfo = createMockSystemInfo('1.2.2')
|
|
65
|
-
expect(nativeLynxSDKVersionGreaterThan('1.2.3')).toBe(false)
|
|
66
|
-
})
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
describe('nativeLynxSDKVersionLessThan', () => {
|
|
70
|
-
beforeEach(() => {
|
|
71
|
-
global.SystemInfo = createMockSystemInfo('1.2.3')
|
|
72
|
-
})
|
|
73
|
-
|
|
74
|
-
it('should return true when native version is less', () => {
|
|
75
|
-
global.SystemInfo = createMockSystemInfo('1.2.2')
|
|
76
|
-
expect(nativeLynxSDKVersionLessThan('1.2.3')).toBe(true)
|
|
77
|
-
})
|
|
78
|
-
|
|
79
|
-
it('should return false when native version is equal', () => {
|
|
80
|
-
global.SystemInfo = createMockSystemInfo('1.2.3')
|
|
81
|
-
expect(nativeLynxSDKVersionLessThan('1.2.3')).toBe(false)
|
|
82
|
-
})
|
|
83
|
-
|
|
84
|
-
it('should return false when native version is greater', () => {
|
|
85
|
-
global.SystemInfo = createMockSystemInfo('1.2.4')
|
|
86
|
-
expect(nativeLynxSDKVersionLessThan('1.2.3')).toBe(false)
|
|
87
|
-
})
|
|
88
|
-
|
|
89
|
-
it('should handle major version differences', () => {
|
|
90
|
-
global.SystemInfo = createMockSystemInfo('1.0.0')
|
|
91
|
-
expect(nativeLynxSDKVersionLessThan('2.0.0')).toBe(true)
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
it('should handle minor version differences', () => {
|
|
95
|
-
global.SystemInfo = createMockSystemInfo('1.1.0')
|
|
96
|
-
expect(nativeLynxSDKVersionLessThan('1.2.0')).toBe(true)
|
|
97
|
-
})
|
|
98
|
-
})
|
|
99
|
-
})
|
package/tsconfig.docs.json
DELETED