taro-uno-ui 1.0.1 → 1.0.2
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/README.md +67 -30
- package/dist/js/{index-CDFsvu80.js → index-QpKiGsEQ.js} +39026 -28023
- package/dist/js/index-QpKiGsEQ.js.map +1 -0
- package/dist/js/{index-JffnTUrv.js → index-nnTHNhwl.js} +38743 -27720
- package/dist/js/index-nnTHNhwl.js.map +1 -0
- package/package.json +4 -4
- package/dist/js/index-CDFsvu80.js.map +0 -1
- package/dist/js/index-DFdcksbe.js +0 -1165
- package/dist/js/index-DFdcksbe.js.map +0 -1
- package/dist/js/index-DXRIkWX1.js +0 -1148
- package/dist/js/index-DXRIkWX1.js.map +0 -1
- package/dist/js/index-JffnTUrv.js.map +0 -1
- package/dist/utils/http/request.d.ts +0 -280
- package/src/app.config.ts +0 -55
- package/src/app.scss +0 -508
- package/src/app.tsx +0 -44
- package/src/components/basic/Button/Button.styles.ts +0 -130
- package/src/components/basic/Button/Button.test.tsx +0 -154
- package/src/components/basic/Button/Button.tsx +0 -133
- package/src/components/basic/Button/Button.types.ts +0 -81
- package/src/components/basic/Button/index.tsx +0 -6
- package/src/components/basic/Divider/Divider.styles.ts +0 -488
- package/src/components/basic/Divider/Divider.test.tsx +0 -551
- package/src/components/basic/Divider/Divider.tsx +0 -392
- package/src/components/basic/Divider/Divider.types.ts +0 -261
- package/src/components/basic/Divider/index.tsx +0 -25
- package/src/components/basic/Icon/Icon.data.ts +0 -474
- package/src/components/basic/Icon/Icon.styles.ts +0 -359
- package/src/components/basic/Icon/Icon.test.tsx +0 -357
- package/src/components/basic/Icon/Icon.tsx +0 -167
- package/src/components/basic/Icon/Icon.types.ts +0 -210
- package/src/components/basic/Icon/IconManager.ts +0 -229
- package/src/components/basic/Icon/index.tsx +0 -22
- package/src/components/basic/Text/Text.styles.ts +0 -500
- package/src/components/basic/Text/Text.test.tsx +0 -299
- package/src/components/basic/Text/Text.tsx +0 -340
- package/src/components/basic/Text/Text.types.ts +0 -329
- package/src/components/basic/Text/index.tsx +0 -27
- package/src/components/basic/Typography/Typography.styles.ts +0 -347
- package/src/components/basic/Typography/Typography.tsx +0 -207
- package/src/components/basic/Typography/Typography.types.ts +0 -296
- package/src/components/basic/Typography/index.tsx +0 -14
- package/src/components/basic/Video/Video.styles.ts +0 -777
- package/src/components/basic/Video/Video.test.tsx +0 -490
- package/src/components/basic/Video/Video.tsx +0 -1468
- package/src/components/basic/Video/Video.types.ts +0 -500
- package/src/components/basic/Video/index.tsx +0 -26
- package/src/components/basic/index.tsx +0 -300
- package/src/components/common/ErrorBoundary.tsx +0 -87
- package/src/components/common/LazyComponent.tsx +0 -247
- package/src/components/common/ResponsiveContainer.tsx +0 -93
- package/src/components/common/ResponsiveGrid.tsx +0 -183
- package/src/components/common/SecurityProvider.tsx +0 -98
- package/src/components/common/ThemeProvider.tsx +0 -115
- package/src/components/common/VirtualList.tsx +0 -350
- package/src/components/common/__tests__/ErrorBoundary.test.tsx +0 -249
- package/src/components/common/index.tsx +0 -32
- package/src/components/display/Avatar/Avatar.styles.ts +0 -62
- package/src/components/display/Avatar/Avatar.test.tsx +0 -390
- package/src/components/display/Avatar/Avatar.tsx +0 -66
- package/src/components/display/Avatar/Avatar.types.ts +0 -40
- package/src/components/display/Avatar/index.ts +0 -3
- package/src/components/display/Badge/Badge.tsx +0 -29
- package/src/components/display/Badge/Badge.types.ts +0 -29
- package/src/components/display/Badge/index.ts +0 -2
- package/src/components/display/Calendar/Calendar.styles.ts +0 -255
- package/src/components/display/Calendar/Calendar.test.tsx +0 -42
- package/src/components/display/Calendar/Calendar.tsx +0 -358
- package/src/components/display/Calendar/Calendar.types.ts +0 -91
- package/src/components/display/Calendar/index.ts +0 -3
- package/src/components/display/Card/Card.styles.ts +0 -89
- package/src/components/display/Card/Card.test.tsx +0 -182
- package/src/components/display/Card/Card.tsx +0 -135
- package/src/components/display/Card/Card.types.ts +0 -55
- package/src/components/display/Card/index.ts +0 -3
- package/src/components/display/Carousel/Carousel.styles.ts +0 -206
- package/src/components/display/Carousel/Carousel.tsx +0 -290
- package/src/components/display/Carousel/Carousel.types.ts +0 -57
- package/src/components/display/Carousel/index.ts +0 -3
- package/src/components/display/List/List.styles.ts +0 -79
- package/src/components/display/List/List.tsx +0 -114
- package/src/components/display/List/List.types.ts +0 -68
- package/src/components/display/List/index.ts +0 -3
- package/src/components/display/Rate/Rate.styles.ts +0 -254
- package/src/components/display/Rate/Rate.tsx +0 -324
- package/src/components/display/Rate/Rate.types.ts +0 -112
- package/src/components/display/Rate/index.ts +0 -20
- package/src/components/display/Table/Table.styles.ts +0 -269
- package/src/components/display/Table/Table.test.tsx +0 -345
- package/src/components/display/Table/Table.tsx +0 -426
- package/src/components/display/Table/Table.types.ts +0 -256
- package/src/components/display/Table/index.tsx +0 -16
- package/src/components/display/Tag/Tag.styles.ts +0 -197
- package/src/components/display/Tag/Tag.test.tsx +0 -541
- package/src/components/display/Tag/Tag.tsx +0 -139
- package/src/components/display/Tag/Tag.types.ts +0 -49
- package/src/components/display/Tag/index.ts +0 -3
- package/src/components/display/Timeline/Timeline.styles.ts +0 -211
- package/src/components/display/Timeline/Timeline.tsx +0 -220
- package/src/components/display/Timeline/Timeline.types.ts +0 -56
- package/src/components/display/Timeline/index.ts +0 -3
- package/src/components/display/index.tsx +0 -147
- package/src/components/feedback/Loading/Loading.styles.ts +0 -117
- package/src/components/feedback/Loading/Loading.test.tsx +0 -534
- package/src/components/feedback/Loading/Loading.tsx +0 -132
- package/src/components/feedback/Loading/Loading.types.ts +0 -33
- package/src/components/feedback/Loading/index.ts +0 -6
- package/src/components/feedback/Message/Message.styles.ts +0 -41
- package/src/components/feedback/Message/Message.test.tsx +0 -234
- package/src/components/feedback/Message/Message.tsx +0 -96
- package/src/components/feedback/Message/Message.types.ts +0 -37
- package/src/components/feedback/Message/index.ts +0 -6
- package/src/components/feedback/Modal/Modal.styles.ts +0 -21
- package/src/components/feedback/Modal/Modal.test.tsx +0 -11
- package/src/components/feedback/Modal/Modal.tsx +0 -269
- package/src/components/feedback/Modal/Modal.types.ts +0 -151
- package/src/components/feedback/Modal/index.tsx +0 -11
- package/src/components/feedback/Notification/Notification.styles.ts +0 -453
- package/src/components/feedback/Notification/Notification.test.tsx +0 -401
- package/src/components/feedback/Notification/Notification.tsx +0 -347
- package/src/components/feedback/Notification/Notification.types.ts +0 -339
- package/src/components/feedback/Notification/NotificationManager.tsx +0 -405
- package/src/components/feedback/Notification/index.ts +0 -40
- package/src/components/feedback/Notification/index.tsx +0 -154
- package/src/components/feedback/Progress/Progress.styles.ts +0 -469
- package/src/components/feedback/Progress/Progress.test.simple.tsx +0 -14
- package/src/components/feedback/Progress/Progress.test.tsx +0 -312
- package/src/components/feedback/Progress/Progress.tsx +0 -488
- package/src/components/feedback/Progress/Progress.types.ts +0 -163
- package/src/components/feedback/Progress/index.ts +0 -3
- package/src/components/feedback/Progress/index.tsx +0 -38
- package/src/components/feedback/Progress/utils/animation.ts +0 -193
- package/src/components/feedback/Progress/utils/index.ts +0 -26
- package/src/components/feedback/Progress/utils/progress-calculator.ts +0 -199
- package/src/components/feedback/Result/Result.styles.ts +0 -139
- package/src/components/feedback/Result/Result.tsx +0 -221
- package/src/components/feedback/Result/Result.types.ts +0 -128
- package/src/components/feedback/Result/index.tsx +0 -3
- package/src/components/feedback/Toast/Toast.styles.ts +0 -17
- package/src/components/feedback/Toast/Toast.test.tsx +0 -10
- package/src/components/feedback/Toast/Toast.tsx +0 -384
- package/src/components/feedback/Toast/Toast.types.ts +0 -86
- package/src/components/feedback/Toast/index.tsx +0 -3
- package/src/components/feedback/Tooltip/Tooltip.examples.tsx +0 -435
- package/src/components/feedback/Tooltip/Tooltip.styles.ts +0 -340
- package/src/components/feedback/Tooltip/Tooltip.test.tsx +0 -446
- package/src/components/feedback/Tooltip/Tooltip.tsx +0 -302
- package/src/components/feedback/Tooltip/Tooltip.types.ts +0 -166
- package/src/components/feedback/Tooltip/index.ts +0 -3
- package/src/components/feedback/Tooltip/index.tsx +0 -246
- package/src/components/feedback/index.tsx +0 -176
- package/src/components/form/Cascader/Cascader.styles.ts +0 -526
- package/src/components/form/Cascader/Cascader.test.tsx +0 -77
- package/src/components/form/Cascader/Cascader.tsx +0 -581
- package/src/components/form/Cascader/Cascader.types.ts +0 -581
- package/src/components/form/Cascader/hooks/index.ts +0 -3
- package/src/components/form/Cascader/hooks/useCascaderFieldNames.ts +0 -19
- package/src/components/form/Cascader/hooks/useCascaderOptions.ts +0 -127
- package/src/components/form/Cascader/hooks/useCascaderState.ts +0 -139
- package/src/components/form/Cascader/index.ts +0 -25
- package/src/components/form/Cascader/utils/formatDisplayValue.ts +0 -19
- package/src/components/form/Cascader/utils/index.ts +0 -1
- package/src/components/form/Checkbox/Checkbox.styles.ts +0 -607
- package/src/components/form/Checkbox/Checkbox.test.tsx +0 -1140
- package/src/components/form/Checkbox/Checkbox.tsx +0 -489
- package/src/components/form/Checkbox/Checkbox.types.ts +0 -472
- package/src/components/form/Checkbox/CheckboxGroup.tsx +0 -444
- package/src/components/form/Checkbox/index.tsx +0 -27
- package/src/components/form/DatePicker/DatePicker.styles.ts +0 -393
- package/src/components/form/DatePicker/DatePicker.test.tsx +0 -407
- package/src/components/form/DatePicker/DatePicker.tsx +0 -376
- package/src/components/form/DatePicker/DatePicker.types.ts +0 -250
- package/src/components/form/DatePicker/index.tsx +0 -15
- package/src/components/form/Form/Form.styles.ts +0 -357
- package/src/components/form/Form/Form.test.tsx +0 -122
- package/src/components/form/Form/Form.tsx +0 -257
- package/src/components/form/Form/Form.types.ts +0 -422
- package/src/components/form/Form/index.tsx +0 -31
- package/src/components/form/Form/useFormLogic.ts +0 -497
- package/src/components/form/Input/Input.styles.ts +0 -445
- package/src/components/form/Input/Input.test.tsx +0 -494
- package/src/components/form/Input/Input.tsx +0 -305
- package/src/components/form/Input/Input.types.ts +0 -297
- package/src/components/form/Input/index.tsx +0 -26
- package/src/components/form/Input/useInputLogic.test.ts +0 -82
- package/src/components/form/Input/useInputLogic.ts +0 -260
- package/src/components/form/InputNumber/InputNumber.styles.ts +0 -716
- package/src/components/form/InputNumber/InputNumber.tsx +0 -402
- package/src/components/form/InputNumber/InputNumber.types.ts +0 -336
- package/src/components/form/InputNumber/components/InputNumberClearButton.tsx +0 -24
- package/src/components/form/InputNumber/components/InputNumberControls.tsx +0 -33
- package/src/components/form/InputNumber/components/index.ts +0 -2
- package/src/components/form/InputNumber/hooks/index.ts +0 -4
- package/src/components/form/InputNumber/hooks/useInputNumberState.ts +0 -313
- package/src/components/form/InputNumber/hooks/useInputNumberValidation.ts +0 -148
- package/src/components/form/InputNumber/index.ts +0 -25
- package/src/components/form/Radio/Radio.styles.ts +0 -451
- package/src/components/form/Radio/Radio.test.tsx +0 -547
- package/src/components/form/Radio/Radio.tsx +0 -277
- package/src/components/form/Radio/Radio.types.ts +0 -414
- package/src/components/form/Radio/index.tsx +0 -21
- package/src/components/form/Select/Select.styles.ts +0 -518
- package/src/components/form/Select/Select.test.tsx +0 -648
- package/src/components/form/Select/Select.tsx +0 -474
- package/src/components/form/Select/Select.types.ts +0 -429
- package/src/components/form/Select/index.tsx +0 -30
- package/src/components/form/Slider/Slider.styles.ts +0 -139
- package/src/components/form/Slider/Slider.test.tsx +0 -553
- package/src/components/form/Slider/Slider.tsx +0 -312
- package/src/components/form/Slider/Slider.types.ts +0 -110
- package/src/components/form/Slider/index.tsx +0 -3
- package/src/components/form/Switch/Switch.styles.ts +0 -534
- package/src/components/form/Switch/Switch.test.tsx +0 -345
- package/src/components/form/Switch/Switch.tsx +0 -458
- package/src/components/form/Switch/Switch.types.ts +0 -386
- package/src/components/form/Switch/index.tsx +0 -26
- package/src/components/form/Textarea/Textarea.styles.ts +0 -592
- package/src/components/form/Textarea/Textarea.test.tsx +0 -1075
- package/src/components/form/Textarea/Textarea.tsx +0 -608
- package/src/components/form/Textarea/Textarea.types.ts +0 -374
- package/src/components/form/Textarea/index.tsx +0 -26
- package/src/components/form/TimePicker/TimePicker.styles.ts +0 -434
- package/src/components/form/TimePicker/TimePicker.test.tsx +0 -306
- package/src/components/form/TimePicker/TimePicker.tsx +0 -250
- package/src/components/form/TimePicker/TimePicker.types.ts +0 -385
- package/src/components/form/TimePicker/index.ts +0 -21
- package/src/components/form/Transfer/Transfer.styles.ts +0 -490
- package/src/components/form/Transfer/Transfer.test.tsx +0 -316
- package/src/components/form/Transfer/Transfer.tsx +0 -414
- package/src/components/form/Transfer/Transfer.types.ts +0 -565
- package/src/components/form/Transfer/components/TransferItem.tsx +0 -94
- package/src/components/form/Transfer/components/TransferList.tsx +0 -298
- package/src/components/form/Transfer/components/TransferOperations.tsx +0 -81
- package/src/components/form/Transfer/components/TransferPagination.tsx +0 -139
- package/src/components/form/Transfer/components/TransferSearch.tsx +0 -85
- package/src/components/form/Transfer/components/index.ts +0 -6
- package/src/components/form/Transfer/hooks/index.ts +0 -3
- package/src/components/form/Transfer/hooks/useTransferData.ts +0 -202
- package/src/components/form/Transfer/hooks/useTransferState.ts +0 -120
- package/src/components/form/Transfer/index.ts +0 -27
- package/src/components/form/Upload/Upload.styles.ts +0 -145
- package/src/components/form/Upload/Upload.test.tsx +0 -10
- package/src/components/form/Upload/Upload.tsx +0 -498
- package/src/components/form/Upload/Upload.types.ts +0 -200
- package/src/components/form/Upload/index.tsx +0 -12
- package/src/components/form/index.tsx +0 -152
- package/src/components/index.tsx +0 -145
- package/src/components/layout/Affix/Affix.styles.ts +0 -42
- package/src/components/layout/Affix/Affix.test.tsx +0 -10
- package/src/components/layout/Affix/Affix.tsx +0 -83
- package/src/components/layout/Affix/Affix.types.ts +0 -29
- package/src/components/layout/Affix/index.tsx +0 -3
- package/src/components/layout/Col/Col.styles.ts +0 -185
- package/src/components/layout/Col/Col.test.tsx +0 -537
- package/src/components/layout/Col/Col.tsx +0 -97
- package/src/components/layout/Col/Col.types.ts +0 -59
- package/src/components/layout/Col/index.tsx +0 -3
- package/src/components/layout/Container/Container.styles.ts +0 -163
- package/src/components/layout/Container/Container.test.tsx +0 -380
- package/src/components/layout/Container/Container.tsx +0 -123
- package/src/components/layout/Container/Container.types.ts +0 -63
- package/src/components/layout/Container/index.tsx +0 -3
- package/src/components/layout/Grid/Grid.styles.ts +0 -183
- package/src/components/layout/Grid/Grid.test.tsx +0 -637
- package/src/components/layout/Grid/Grid.tsx +0 -123
- package/src/components/layout/Grid/Grid.types.ts +0 -78
- package/src/components/layout/Grid/index.tsx +0 -3
- package/src/components/layout/Layout/Content.tsx +0 -30
- package/src/components/layout/Layout/Footer.tsx +0 -30
- package/src/components/layout/Layout/Header.tsx +0 -30
- package/src/components/layout/Layout/Layout.styles.ts +0 -84
- package/src/components/layout/Layout/Layout.test.tsx +0 -10
- package/src/components/layout/Layout/Layout.tsx +0 -28
- package/src/components/layout/Layout/Layout.types.ts +0 -58
- package/src/components/layout/Layout/Sider.tsx +0 -44
- package/src/components/layout/Layout/index.tsx +0 -22
- package/src/components/layout/Row/Row.styles.ts +0 -159
- package/src/components/layout/Row/Row.test.tsx +0 -467
- package/src/components/layout/Row/Row.tsx +0 -111
- package/src/components/layout/Row/Row.types.ts +0 -60
- package/src/components/layout/Row/index.tsx +0 -3
- package/src/components/layout/Space/Space.styles.ts +0 -255
- package/src/components/layout/Space/Space.test.tsx +0 -682
- package/src/components/layout/Space/Space.tsx +0 -203
- package/src/components/layout/Space/Space.types.ts +0 -92
- package/src/components/layout/Space/index.tsx +0 -12
- package/src/components/layout/index.tsx +0 -78
- package/src/components/navigation/Menu/Menu.constants.ts +0 -69
- package/src/components/navigation/Menu/Menu.stories.tsx +0 -107
- package/src/components/navigation/Menu/Menu.styles.ts +0 -767
- package/src/components/navigation/Menu/Menu.tsx +0 -352
- package/src/components/navigation/Menu/Menu.types.ts +0 -231
- package/src/components/navigation/Menu/Menu.utils.ts +0 -187
- package/src/components/navigation/Menu/MenuItem.tsx +0 -124
- package/src/components/navigation/Menu/SubMenu.tsx +0 -150
- package/src/components/navigation/Menu/index.tsx +0 -59
- package/src/components/navigation/NavBar/NavBar.styles.ts +0 -129
- package/src/components/navigation/NavBar/NavBar.test.tsx +0 -287
- package/src/components/navigation/NavBar/NavBar.tsx +0 -228
- package/src/components/navigation/NavBar/NavBar.types.ts +0 -54
- package/src/components/navigation/NavBar/index.tsx +0 -3
- package/src/components/navigation/Pagination/Pagination.styles.ts +0 -187
- package/src/components/navigation/Pagination/Pagination.test.tsx +0 -672
- package/src/components/navigation/Pagination/Pagination.tsx +0 -395
- package/src/components/navigation/Pagination/Pagination.types.ts +0 -87
- package/src/components/navigation/Pagination/index.ts +0 -24
- package/src/components/navigation/Pagination/index.tsx +0 -9
- package/src/components/navigation/Steps/Step.tsx +0 -36
- package/src/components/navigation/Steps/Steps.styles.ts +0 -169
- package/src/components/navigation/Steps/Steps.test.tsx +0 -14
- package/src/components/navigation/Steps/Steps.tsx +0 -112
- package/src/components/navigation/Steps/Steps.types.ts +0 -47
- package/src/components/navigation/Steps/index.tsx +0 -3
- package/src/components/navigation/Tabs/Tabs.styles.ts +0 -199
- package/src/components/navigation/Tabs/Tabs.test.tsx +0 -662
- package/src/components/navigation/Tabs/Tabs.tsx +0 -253
- package/src/components/navigation/Tabs/Tabs.types.ts +0 -115
- package/src/components/navigation/Tabs/index.tsx +0 -3
- package/src/components/navigation/index.tsx +0 -88
- package/src/constants/index.ts +0 -785
- package/src/hooks/index.ts +0 -60
- package/src/hooks/types.ts +0 -9
- package/src/hooks/useAsync.ts +0 -64
- package/src/hooks/useClickOutside.ts +0 -52
- package/src/hooks/useCounter.ts +0 -87
- package/src/hooks/useDebounce.ts +0 -150
- package/src/hooks/useDeepCompareEffect.ts +0 -88
- package/src/hooks/useEventHandling.ts +0 -444
- package/src/hooks/useEventListener.ts +0 -77
- package/src/hooks/useLifecycle.ts +0 -399
- package/src/hooks/useMediaQuery.ts +0 -75
- package/src/hooks/useMutation.ts +0 -233
- package/src/hooks/usePerformance.ts +0 -378
- package/src/hooks/usePerformanceMonitor.ts +0 -348
- package/src/hooks/usePlatform.ts +0 -64
- package/src/hooks/usePrevious.ts +0 -25
- package/src/hooks/useRequest.test.ts +0 -11
- package/src/hooks/useRequest.ts +0 -140
- package/src/hooks/useStateManagement.ts +0 -300
- package/src/hooks/useStorage.ts +0 -169
- package/src/hooks/useStyle.ts +0 -543
- package/src/hooks/useTheme.ts +0 -347
- package/src/hooks/useToggle.ts +0 -54
- package/src/hooks/useVirtualScroll.ts +0 -331
- package/src/index.ts +0 -323
- package/src/platform/index.ts +0 -1188
- package/src/providers/AppProvider.test.tsx +0 -63
- package/src/providers/AppProvider.tsx +0 -155
- package/src/providers/index.ts +0 -1
- package/src/theme/ThemeProvider.tsx +0 -279
- package/src/theme/ThemeProvider.types.ts +0 -26
- package/src/theme/animations.tsx +0 -660
- package/src/theme/defaults.ts +0 -188
- package/src/theme/design-system.ts +0 -562
- package/src/theme/design-tokens.ts +0 -1122
- package/src/theme/generated/dark-theme.scss +0 -120
- package/src/theme/generated/tokens.css +0 -441
- package/src/theme/generated/tokens.scss +0 -384
- package/src/theme/index.ts +0 -99
- package/src/theme/responsive.tsx +0 -195
- package/src/theme/styles/mixins.scss +0 -612
- package/src/theme/styles/variables.scss +0 -295
- package/src/theme/styles.ts +0 -403
- package/src/theme/tokens/colors.ts +0 -256
- package/src/theme/tokens/effects.ts +0 -260
- package/src/theme/tokens/index.ts +0 -217
- package/src/theme/tokens/spacing.ts +0 -137
- package/src/theme/tokens/typography.ts +0 -186
- package/src/theme/types.ts +0 -188
- package/src/theme/useThemeUtils.ts +0 -313
- package/src/theme/utils.ts +0 -501
- package/src/theme/variables.ts +0 -602
- package/src/types/accessibility.ts +0 -50
- package/src/types/button.ts +0 -560
- package/src/types/component-props.ts +0 -322
- package/src/types/env.d.ts +0 -20
- package/src/types/glob.d.ts +0 -4
- package/src/types/index.ts +0 -427
- package/src/types/modules.d.ts +0 -40
- package/src/types/standardized-components.ts +0 -550
- package/src/types/taro-adapter.d.ts +0 -174
- package/src/types/taro-components.d.ts +0 -73
- package/src/types/utils.ts +0 -400
- package/src/utils/__tests__/inputValidator.test.ts +0 -338
- package/src/utils/__tests__/responsiveUtils.test.ts +0 -311
- package/src/utils/__tests__/xssProtection.test.ts +0 -268
- package/src/utils/abort-controller.ts +0 -48
- package/src/utils/cache.ts +0 -79
- package/src/utils/createNamespace.ts +0 -24
- package/src/utils/environment.ts +0 -115
- package/src/utils/error-handler.ts +0 -88
- package/src/utils/errorLogger.ts +0 -193
- package/src/utils/formatUtils.ts +0 -412
- package/src/utils/http/error-codes.ts +0 -314
- package/src/utils/http/http-client.test.ts +0 -63
- package/src/utils/http/http-client.ts +0 -161
- package/src/utils/http/request-cache.ts +0 -127
- package/src/utils/http/request.ts +0 -954
- package/src/utils/http/taro-adapter.test.ts +0 -74
- package/src/utils/http/taro-adapter.ts +0 -24
- package/src/utils/http/types.ts +0 -414
- package/src/utils/http/web-adapter.ts +0 -33
- package/src/utils/index.ts +0 -112
- package/src/utils/inputValidator.ts +0 -264
- package/src/utils/performance/performance.ts +0 -839
- package/src/utils/responsiveUtils.ts +0 -348
- package/src/utils/rtl-support.ts +0 -354
- package/src/utils/security/api-security.ts +0 -394
- package/src/utils/security/xss-protection.ts +0 -69
- package/src/utils/securityHeaders.ts +0 -308
- package/src/utils/typeHelpers.ts +0 -16
- package/src/utils/types/dataProcessing.ts +0 -544
- package/src/utils/types/typeHelpers.ts +0 -197
- package/src/utils/xssProtection.ts +0 -468
|
@@ -1,839 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 性能优化工具函数
|
|
3
|
-
* 包含防抖、节流、懒加载等工具
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { useCallback, useEffect, useRef, useState, useMemo } from 'react';
|
|
7
|
-
|
|
8
|
-
// 性能标记类
|
|
9
|
-
export class PerformanceMarker {
|
|
10
|
-
private static markers = new Map<string, number>();
|
|
11
|
-
|
|
12
|
-
static start(name: string): void {
|
|
13
|
-
this.markers.set(name, performance.now());
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
static end(name: string): number {
|
|
17
|
-
const start = this.markers.get(name);
|
|
18
|
-
if (start === undefined) {
|
|
19
|
-
throw new Error(`Marker '${name}' not found`);
|
|
20
|
-
}
|
|
21
|
-
const duration = performance.now() - start;
|
|
22
|
-
this.markers.delete(name);
|
|
23
|
-
return duration;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
static measure(name: string, callback: () => void): number {
|
|
27
|
-
this.start(name);
|
|
28
|
-
callback();
|
|
29
|
-
return this.end(name);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// 内存管理工具
|
|
34
|
-
export class MemoryManager {
|
|
35
|
-
private static weakRefs = new WeakMap<object, number>();
|
|
36
|
-
private static cleanupCallbacks = new Set<() => void>();
|
|
37
|
-
|
|
38
|
-
static createWeakRef(obj: object): number {
|
|
39
|
-
const id = Math.random();
|
|
40
|
-
this.weakRefs.set(obj, id);
|
|
41
|
-
return id;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
static addCleanup(callback: () => void): void {
|
|
45
|
-
this.cleanupCallbacks.add(callback);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
static cleanup(): void {
|
|
49
|
-
this.cleanupCallbacks.forEach((callback) => callback());
|
|
50
|
-
this.cleanupCallbacks.clear();
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
static getMemoryInfo() {
|
|
54
|
-
if ('memory' in performance) {
|
|
55
|
-
const memory = (performance as any).memory;
|
|
56
|
-
return {
|
|
57
|
-
used: memory.usedJSHeapSize,
|
|
58
|
-
total: memory.totalJSHeapSize,
|
|
59
|
-
limit: memory.jsHeapSizeLimit,
|
|
60
|
-
usage: (memory.usedJSHeapSize / memory.totalJSHeapSize) * 100,
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
return null;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// 性能优化Hooks
|
|
68
|
-
export function useDebounce<T extends (...args: unknown[]) => unknown>(callback: T, delay: number): T {
|
|
69
|
-
return useMemo(() => debounce(callback, delay) as unknown as T, [callback, delay]);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export function useThrottle<T extends (...args: unknown[]) => unknown>(callback: T, delay: number): T {
|
|
73
|
-
return useMemo(() => throttle(callback, delay) as unknown as T, [callback, delay]);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export function useLazyLoad<T>(
|
|
77
|
-
loader: () => Promise<T>,
|
|
78
|
-
deps: any[] = [],
|
|
79
|
-
): { data: T | null; loading: boolean; error: Error | null; reload: () => void } {
|
|
80
|
-
const [data, setData] = useState<T | null>(null);
|
|
81
|
-
const [loading, setLoading] = useState(false);
|
|
82
|
-
const [error, setError] = useState<Error | null>(null);
|
|
83
|
-
|
|
84
|
-
const load = useCallback(async () => {
|
|
85
|
-
setLoading(true);
|
|
86
|
-
setError(null);
|
|
87
|
-
|
|
88
|
-
try {
|
|
89
|
-
const result = await loader();
|
|
90
|
-
setData(result);
|
|
91
|
-
} catch (err) {
|
|
92
|
-
setError(err as Error);
|
|
93
|
-
} finally {
|
|
94
|
-
setLoading(false);
|
|
95
|
-
}
|
|
96
|
-
}, [loader]);
|
|
97
|
-
|
|
98
|
-
useEffect(() => {
|
|
99
|
-
load();
|
|
100
|
-
}, deps);
|
|
101
|
-
|
|
102
|
-
return { data, loading, error, reload: load };
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
export function useIntersectionObserver(
|
|
106
|
-
callback: (entry: IntersectionObserverEntry) => void,
|
|
107
|
-
options: IntersectionObserverInit = {},
|
|
108
|
-
): (node: Element | null) => void {
|
|
109
|
-
const [node, setNode] = useState<Element | null>(null);
|
|
110
|
-
|
|
111
|
-
useEffect(() => {
|
|
112
|
-
if (!node) return;
|
|
113
|
-
|
|
114
|
-
const observer = new IntersectionObserver((entries) => {
|
|
115
|
-
entries.forEach(callback);
|
|
116
|
-
}, options);
|
|
117
|
-
|
|
118
|
-
observer.observe(node);
|
|
119
|
-
|
|
120
|
-
return () => {
|
|
121
|
-
observer.disconnect();
|
|
122
|
-
};
|
|
123
|
-
}, [node, callback, options]);
|
|
124
|
-
|
|
125
|
-
return setNode;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
export function useResizeObserver(callback: (entry: ResizeObserverEntry) => void): (node: Element | null) => void {
|
|
129
|
-
const [node, setNode] = useState<Element | null>(null);
|
|
130
|
-
|
|
131
|
-
useEffect(() => {
|
|
132
|
-
if (!node) return;
|
|
133
|
-
|
|
134
|
-
const observer = new ResizeObserver((entries) => {
|
|
135
|
-
entries.forEach(callback);
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
observer.observe(node);
|
|
139
|
-
|
|
140
|
-
return () => {
|
|
141
|
-
observer.disconnect();
|
|
142
|
-
};
|
|
143
|
-
}, [node, callback]);
|
|
144
|
-
|
|
145
|
-
return setNode;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
export function usePerformanceMonitor({
|
|
149
|
-
enabled = false,
|
|
150
|
-
sampleInterval = 1000,
|
|
151
|
-
thresholds = {},
|
|
152
|
-
}: {
|
|
153
|
-
enabled?: boolean;
|
|
154
|
-
sampleInterval?: number;
|
|
155
|
-
thresholds?: {
|
|
156
|
-
fps?: number;
|
|
157
|
-
memory?: number;
|
|
158
|
-
loadTime?: number;
|
|
159
|
-
};
|
|
160
|
-
} = {}) {
|
|
161
|
-
const [metrics, setMetrics] = useState({
|
|
162
|
-
fps: 0,
|
|
163
|
-
memory: 0,
|
|
164
|
-
loadTime: 0,
|
|
165
|
-
isHealthy: true,
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
useEffect(() => {
|
|
169
|
-
if (!enabled) return;
|
|
170
|
-
|
|
171
|
-
const monitor = setInterval(() => {
|
|
172
|
-
const fps = calculateFPS();
|
|
173
|
-
const memoryInfo = MemoryManager.getMemoryInfo();
|
|
174
|
-
const memory = memoryInfo ? memoryInfo.usage : 0;
|
|
175
|
-
|
|
176
|
-
const navigation = performance.getEntriesByType('navigation')[0] as PerformanceNavigationTiming;
|
|
177
|
-
const loadTime = navigation ? navigation.loadEventEnd - navigation.fetchStart : 0;
|
|
178
|
-
|
|
179
|
-
const isHealthy = fps >= (thresholds.fps || 30) && memory <= (thresholds.memory || 80);
|
|
180
|
-
|
|
181
|
-
setMetrics({
|
|
182
|
-
fps,
|
|
183
|
-
memory,
|
|
184
|
-
loadTime,
|
|
185
|
-
isHealthy,
|
|
186
|
-
});
|
|
187
|
-
}, sampleInterval);
|
|
188
|
-
|
|
189
|
-
return () => clearInterval(monitor);
|
|
190
|
-
}, [enabled, sampleInterval, thresholds]);
|
|
191
|
-
|
|
192
|
-
return metrics;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
function calculateFPS(): number {
|
|
196
|
-
let lastTime = performance.now();
|
|
197
|
-
let frames = 0;
|
|
198
|
-
let fps = 0;
|
|
199
|
-
|
|
200
|
-
const counter = () => {
|
|
201
|
-
const now = performance.now();
|
|
202
|
-
frames++;
|
|
203
|
-
|
|
204
|
-
if (now >= lastTime + 1000) {
|
|
205
|
-
fps = Math.round((frames * 1000) / (now - lastTime));
|
|
206
|
-
frames = 0;
|
|
207
|
-
lastTime = now;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
requestAnimationFrame(counter);
|
|
211
|
-
};
|
|
212
|
-
|
|
213
|
-
requestAnimationFrame(counter);
|
|
214
|
-
return fps;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
export function useBatch<T>(callback: (items: T[]) => void, delay: number = 100) {
|
|
218
|
-
const batchRef = useRef<T[]>([]);
|
|
219
|
-
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
|
|
220
|
-
|
|
221
|
-
const flush = useCallback(() => {
|
|
222
|
-
if (batchRef.current.length > 0) {
|
|
223
|
-
callback(batchRef.current);
|
|
224
|
-
batchRef.current = [];
|
|
225
|
-
}
|
|
226
|
-
}, [callback]);
|
|
227
|
-
|
|
228
|
-
const addItem = useCallback(
|
|
229
|
-
(item: T) => {
|
|
230
|
-
batchRef.current.push(item);
|
|
231
|
-
|
|
232
|
-
if (timeoutRef.current) {
|
|
233
|
-
clearTimeout(timeoutRef.current);
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
timeoutRef.current = setTimeout(() => {
|
|
237
|
-
flush();
|
|
238
|
-
}, delay);
|
|
239
|
-
},
|
|
240
|
-
[flush, delay],
|
|
241
|
-
);
|
|
242
|
-
|
|
243
|
-
useEffect(() => {
|
|
244
|
-
return () => {
|
|
245
|
-
if (timeoutRef.current) {
|
|
246
|
-
clearTimeout(timeoutRef.current);
|
|
247
|
-
}
|
|
248
|
-
flush();
|
|
249
|
-
};
|
|
250
|
-
}, [flush]);
|
|
251
|
-
|
|
252
|
-
return addItem;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
export function useCache<T>(key: string, initialValue: T, deps: any[] = []): [T, (value: T) => void] {
|
|
256
|
-
const [value, setValue] = useState<T>(() => {
|
|
257
|
-
const cached = localStorage.getItem(`cache_${key}`);
|
|
258
|
-
return cached ? JSON.parse(cached) : initialValue;
|
|
259
|
-
});
|
|
260
|
-
|
|
261
|
-
const updateValue = useCallback(
|
|
262
|
-
(newValue: T) => {
|
|
263
|
-
setValue(newValue);
|
|
264
|
-
localStorage.setItem(`cache_${key}`, JSON.stringify(newValue));
|
|
265
|
-
},
|
|
266
|
-
[key],
|
|
267
|
-
);
|
|
268
|
-
|
|
269
|
-
useEffect(() => {
|
|
270
|
-
const cached = localStorage.getItem(`cache_${key}`);
|
|
271
|
-
if (cached) {
|
|
272
|
-
setValue(JSON.parse(cached));
|
|
273
|
-
}
|
|
274
|
-
}, deps);
|
|
275
|
-
|
|
276
|
-
return [value, updateValue];
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
export function useOptimizedRequest<T>(
|
|
280
|
-
request: () => Promise<T>,
|
|
281
|
-
options: {
|
|
282
|
-
cacheKey?: string;
|
|
283
|
-
cacheTime?: number;
|
|
284
|
-
retryCount?: number;
|
|
285
|
-
retryDelay?: number;
|
|
286
|
-
debounceTime?: number;
|
|
287
|
-
throttleTime?: number;
|
|
288
|
-
} = {},
|
|
289
|
-
) {
|
|
290
|
-
const {
|
|
291
|
-
cacheKey,
|
|
292
|
-
cacheTime = 5 * 60 * 1000,
|
|
293
|
-
retryCount = 3,
|
|
294
|
-
retryDelay = 1000,
|
|
295
|
-
debounceTime = 0,
|
|
296
|
-
throttleTime = 0,
|
|
297
|
-
} = options;
|
|
298
|
-
|
|
299
|
-
const [data, setData] = useState<T | null>(null);
|
|
300
|
-
const [loading, setLoading] = useState(false);
|
|
301
|
-
const [error, setError] = useState<Error | null>(null);
|
|
302
|
-
|
|
303
|
-
const execute = useCallback(async () => {
|
|
304
|
-
setLoading(true);
|
|
305
|
-
setError(null);
|
|
306
|
-
|
|
307
|
-
let lastError: Error | null = null;
|
|
308
|
-
let attempts = 0;
|
|
309
|
-
|
|
310
|
-
while (attempts < retryCount) {
|
|
311
|
-
try {
|
|
312
|
-
const result = await request();
|
|
313
|
-
setData(result);
|
|
314
|
-
|
|
315
|
-
if (cacheKey) {
|
|
316
|
-
const cacheData = {
|
|
317
|
-
data: result,
|
|
318
|
-
timestamp: Date.now(),
|
|
319
|
-
};
|
|
320
|
-
localStorage.setItem(`request_${cacheKey}`, JSON.stringify(cacheData));
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
return result;
|
|
324
|
-
} catch (err) {
|
|
325
|
-
lastError = err as Error;
|
|
326
|
-
attempts++;
|
|
327
|
-
|
|
328
|
-
if (attempts < retryCount) {
|
|
329
|
-
await new Promise((resolve) => setTimeout(resolve, retryDelay));
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
setError(lastError);
|
|
335
|
-
throw lastError;
|
|
336
|
-
}, [request, retryCount, retryDelay, cacheKey]);
|
|
337
|
-
|
|
338
|
-
useEffect(() => {
|
|
339
|
-
if (cacheKey) {
|
|
340
|
-
const cached = localStorage.getItem(`request_${cacheKey}`);
|
|
341
|
-
if (cached) {
|
|
342
|
-
const { data: cachedData, timestamp } = JSON.parse(cached);
|
|
343
|
-
if (Date.now() - timestamp < cacheTime) {
|
|
344
|
-
setData(cachedData);
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
}, [cacheKey, cacheTime]);
|
|
349
|
-
|
|
350
|
-
const optimizedExecute = useMemo(() => {
|
|
351
|
-
if (debounceTime > 0) {
|
|
352
|
-
return debounce(execute, debounceTime);
|
|
353
|
-
}
|
|
354
|
-
if (throttleTime > 0) {
|
|
355
|
-
return throttle(execute, throttleTime);
|
|
356
|
-
}
|
|
357
|
-
return execute;
|
|
358
|
-
}, [execute, debounceTime, throttleTime]);
|
|
359
|
-
|
|
360
|
-
return {
|
|
361
|
-
data,
|
|
362
|
-
loading,
|
|
363
|
-
error,
|
|
364
|
-
execute: optimizedExecute,
|
|
365
|
-
reset: () => {
|
|
366
|
-
setData(null);
|
|
367
|
-
setError(null);
|
|
368
|
-
},
|
|
369
|
-
};
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
/**
|
|
373
|
-
* 防抖函数
|
|
374
|
-
* @param func 需要防抖的函数
|
|
375
|
-
* @param delay 延迟时间(毫秒)
|
|
376
|
-
* @param options 选项
|
|
377
|
-
* @returns 防抖后的函数
|
|
378
|
-
*/
|
|
379
|
-
export function debounce<T extends (...args: unknown[]) => unknown>(
|
|
380
|
-
func: T,
|
|
381
|
-
delay: number,
|
|
382
|
-
options: {
|
|
383
|
-
leading?: boolean;
|
|
384
|
-
trailing?: boolean;
|
|
385
|
-
maxWait?: number;
|
|
386
|
-
} = {},
|
|
387
|
-
): (...args: Parameters<T>) => void {
|
|
388
|
-
const { leading = false, trailing = true, maxWait } = options;
|
|
389
|
-
let timeoutId: NodeJS.Timeout | null = null;
|
|
390
|
-
let lastCallTime = 0;
|
|
391
|
-
let lastArgs: Parameters<T> | null = null;
|
|
392
|
-
let lastThis: unknown = null;
|
|
393
|
-
let result: ReturnType<T> | null = null;
|
|
394
|
-
|
|
395
|
-
const invokeFunc = (time: number) => {
|
|
396
|
-
const args = lastArgs;
|
|
397
|
-
const thisArg = lastThis;
|
|
398
|
-
|
|
399
|
-
lastArgs = null;
|
|
400
|
-
lastThis = null;
|
|
401
|
-
lastCallTime = time;
|
|
402
|
-
|
|
403
|
-
if (args) {
|
|
404
|
-
result = func.apply(thisArg as any, args) as ReturnType<T>;
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
return result;
|
|
408
|
-
};
|
|
409
|
-
|
|
410
|
-
const shouldInvoke = (time: number) => {
|
|
411
|
-
const timeSinceLastCall = time - lastCallTime;
|
|
412
|
-
|
|
413
|
-
return (
|
|
414
|
-
lastCallTime === 0 || // 首次调用
|
|
415
|
-
timeSinceLastCall >= delay || // 超过延迟时间
|
|
416
|
-
(maxWait !== undefined && timeSinceLastCall >= maxWait) // 超过最大等待时间
|
|
417
|
-
);
|
|
418
|
-
};
|
|
419
|
-
|
|
420
|
-
const trailingEdge = (time: number) => {
|
|
421
|
-
timeoutId = null;
|
|
422
|
-
|
|
423
|
-
if (trailing && lastArgs) {
|
|
424
|
-
return invokeFunc(time);
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
lastArgs = null;
|
|
428
|
-
lastThis = null;
|
|
429
|
-
return result;
|
|
430
|
-
};
|
|
431
|
-
|
|
432
|
-
const timerExpired = (): ReturnType<T> | null => {
|
|
433
|
-
const time = Date.now();
|
|
434
|
-
|
|
435
|
-
if (shouldInvoke(time)) {
|
|
436
|
-
return trailingEdge(time);
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
// 重新计算剩余时间
|
|
440
|
-
const timeSinceLastCall = time - lastCallTime;
|
|
441
|
-
const timeWaiting = delay - timeSinceLastCall;
|
|
442
|
-
const remainingWait = maxWait !== undefined ? Math.min(timeWaiting, maxWait - timeSinceLastCall) : timeWaiting;
|
|
443
|
-
|
|
444
|
-
timeoutId = setTimeout(timerExpired, remainingWait);
|
|
445
|
-
return null;
|
|
446
|
-
};
|
|
447
|
-
|
|
448
|
-
const debounced = function (this: unknown, ...args: Parameters<T>) {
|
|
449
|
-
const time = Date.now();
|
|
450
|
-
lastArgs = args;
|
|
451
|
-
lastThis = this;
|
|
452
|
-
|
|
453
|
-
if (timeoutId) {
|
|
454
|
-
clearTimeout(timeoutId);
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
if (leading && shouldInvoke(time)) {
|
|
458
|
-
return invokeFunc(time);
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
timeoutId = setTimeout(timerExpired, delay);
|
|
462
|
-
return result;
|
|
463
|
-
} as (...args: Parameters<T>) => void & {
|
|
464
|
-
cancel: () => void;
|
|
465
|
-
flush: () => ReturnType<T> | null;
|
|
466
|
-
};
|
|
467
|
-
|
|
468
|
-
(debounced as unknown as { cancel: () => void }).cancel = () => {
|
|
469
|
-
if (timeoutId) {
|
|
470
|
-
clearTimeout(timeoutId);
|
|
471
|
-
timeoutId = null;
|
|
472
|
-
}
|
|
473
|
-
lastArgs = null;
|
|
474
|
-
lastThis = null;
|
|
475
|
-
lastCallTime = 0;
|
|
476
|
-
};
|
|
477
|
-
|
|
478
|
-
(debounced as unknown as { flush: () => ReturnType<T> | null }).flush = () => {
|
|
479
|
-
if (timeoutId) {
|
|
480
|
-
return trailingEdge(Date.now());
|
|
481
|
-
}
|
|
482
|
-
return result;
|
|
483
|
-
};
|
|
484
|
-
|
|
485
|
-
return debounced;
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
/**
|
|
489
|
-
* 节流函数
|
|
490
|
-
* @param func 需要节流的函数
|
|
491
|
-
* @param limit 时间限制(毫秒)
|
|
492
|
-
* @param options 选项
|
|
493
|
-
* @returns 节流后的函数
|
|
494
|
-
*/
|
|
495
|
-
export function throttle<T extends (...args: unknown[]) => unknown>(
|
|
496
|
-
func: T,
|
|
497
|
-
limit: number,
|
|
498
|
-
options: {
|
|
499
|
-
leading?: boolean;
|
|
500
|
-
trailing?: boolean;
|
|
501
|
-
} = {},
|
|
502
|
-
): (...args: Parameters<T>) => void {
|
|
503
|
-
const { leading = true, trailing = true } = options;
|
|
504
|
-
let lastCallTime = 0;
|
|
505
|
-
let timeoutId: NodeJS.Timeout | null = null;
|
|
506
|
-
let lastArgs: Parameters<T> | null = null;
|
|
507
|
-
let lastThis: unknown = null;
|
|
508
|
-
|
|
509
|
-
const throttled = function (this: unknown, ...args: Parameters<T>) {
|
|
510
|
-
const now = Date.now();
|
|
511
|
-
const timeSinceLastCall = now - lastCallTime;
|
|
512
|
-
|
|
513
|
-
lastArgs = args;
|
|
514
|
-
lastThis = this;
|
|
515
|
-
|
|
516
|
-
// 如果是首次调用且leading为false,设置lastCallTime
|
|
517
|
-
if (!leading && lastCallTime === 0) {
|
|
518
|
-
lastCallTime = now;
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
if (timeSinceLastCall >= limit || (leading && lastCallTime === 0)) {
|
|
522
|
-
if (timeoutId) {
|
|
523
|
-
clearTimeout(timeoutId);
|
|
524
|
-
timeoutId = null;
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
lastCallTime = now;
|
|
528
|
-
func.apply(this, args);
|
|
529
|
-
} else if (trailing && !timeoutId) {
|
|
530
|
-
timeoutId = setTimeout(() => {
|
|
531
|
-
lastCallTime = leading ? Date.now() : 0;
|
|
532
|
-
timeoutId = null;
|
|
533
|
-
|
|
534
|
-
if (lastArgs && trailing) {
|
|
535
|
-
func.apply(lastThis, lastArgs);
|
|
536
|
-
}
|
|
537
|
-
}, limit - timeSinceLastCall);
|
|
538
|
-
}
|
|
539
|
-
} as ((...args: Parameters<T>) => void) & { cancel: () => void };
|
|
540
|
-
|
|
541
|
-
(throttled as unknown as { cancel: () => void }).cancel = () => {
|
|
542
|
-
if (timeoutId) {
|
|
543
|
-
clearTimeout(timeoutId);
|
|
544
|
-
timeoutId = null;
|
|
545
|
-
}
|
|
546
|
-
lastArgs = null;
|
|
547
|
-
lastThis = null;
|
|
548
|
-
lastCallTime = 0;
|
|
549
|
-
};
|
|
550
|
-
|
|
551
|
-
return throttled;
|
|
552
|
-
}
|
|
553
|
-
|
|
554
|
-
/**
|
|
555
|
-
* RAF 节流函数
|
|
556
|
-
* 使用 requestAnimationFrame 实现节流
|
|
557
|
-
* @param func 需要节流的函数
|
|
558
|
-
* @returns 节流后的函数
|
|
559
|
-
*/
|
|
560
|
-
export function rafThrottle<T extends (...args: unknown[]) => unknown>(func: T): (...args: Parameters<T>) => void {
|
|
561
|
-
let rafId: number | null = null;
|
|
562
|
-
let lastArgs: Parameters<T> | null = null;
|
|
563
|
-
let lastThis: unknown = null;
|
|
564
|
-
|
|
565
|
-
const throttled = function (this: unknown, ...args: Parameters<T>) {
|
|
566
|
-
lastArgs = args;
|
|
567
|
-
lastThis = this;
|
|
568
|
-
|
|
569
|
-
if (rafId === null) {
|
|
570
|
-
rafId = requestAnimationFrame(() => {
|
|
571
|
-
if (lastArgs) {
|
|
572
|
-
func.apply(lastThis, lastArgs);
|
|
573
|
-
}
|
|
574
|
-
rafId = null;
|
|
575
|
-
lastArgs = null;
|
|
576
|
-
lastThis = null;
|
|
577
|
-
});
|
|
578
|
-
}
|
|
579
|
-
} as ((...args: Parameters<T>) => void) & { cancel: () => void };
|
|
580
|
-
|
|
581
|
-
(throttled as unknown as { cancel: () => void }).cancel = () => {
|
|
582
|
-
if (rafId !== null) {
|
|
583
|
-
cancelAnimationFrame(rafId);
|
|
584
|
-
rafId = null;
|
|
585
|
-
}
|
|
586
|
-
lastArgs = null;
|
|
587
|
-
lastThis = null;
|
|
588
|
-
};
|
|
589
|
-
|
|
590
|
-
return throttled;
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
/**
|
|
594
|
-
* 懒加载函数
|
|
595
|
-
* @param loader 加载函数
|
|
596
|
-
* @param options 选项
|
|
597
|
-
* @returns 懒加载的 Promise
|
|
598
|
-
*/
|
|
599
|
-
export function lazyLoad<T>(
|
|
600
|
-
loader: () => Promise<T>,
|
|
601
|
-
options: {
|
|
602
|
-
timeout?: number;
|
|
603
|
-
retryCount?: number;
|
|
604
|
-
retryDelay?: number;
|
|
605
|
-
} = {},
|
|
606
|
-
): Promise<T> {
|
|
607
|
-
const { timeout = 5000, retryCount = 3, retryDelay = 1000 } = options;
|
|
608
|
-
let retries = 0;
|
|
609
|
-
|
|
610
|
-
const loadWithRetry = (): Promise<T> => {
|
|
611
|
-
return new Promise((resolve, reject) => {
|
|
612
|
-
const timeoutId = setTimeout(() => {
|
|
613
|
-
reject(new Error('Lazy load timeout'));
|
|
614
|
-
}, timeout);
|
|
615
|
-
|
|
616
|
-
loader()
|
|
617
|
-
.then((result) => {
|
|
618
|
-
clearTimeout(timeoutId);
|
|
619
|
-
resolve(result);
|
|
620
|
-
})
|
|
621
|
-
.catch((error) => {
|
|
622
|
-
clearTimeout(timeoutId);
|
|
623
|
-
|
|
624
|
-
if (retries < retryCount) {
|
|
625
|
-
retries++;
|
|
626
|
-
setTimeout(() => {
|
|
627
|
-
loadWithRetry().then(resolve).catch(reject);
|
|
628
|
-
}, retryDelay);
|
|
629
|
-
} else {
|
|
630
|
-
reject(error);
|
|
631
|
-
}
|
|
632
|
-
});
|
|
633
|
-
});
|
|
634
|
-
};
|
|
635
|
-
|
|
636
|
-
return loadWithRetry();
|
|
637
|
-
}
|
|
638
|
-
|
|
639
|
-
/**
|
|
640
|
-
* 图片懒加载
|
|
641
|
-
* @param element 图片元素
|
|
642
|
-
* @param src 图片地址
|
|
643
|
-
* @param options 选项
|
|
644
|
-
*/
|
|
645
|
-
export function lazyLoadImage(
|
|
646
|
-
element: HTMLImageElement,
|
|
647
|
-
src: string,
|
|
648
|
-
options: {
|
|
649
|
-
threshold?: number;
|
|
650
|
-
rootMargin?: string;
|
|
651
|
-
placeholder?: string;
|
|
652
|
-
onLoad?: () => void;
|
|
653
|
-
onError?: () => void;
|
|
654
|
-
} = {},
|
|
655
|
-
): void {
|
|
656
|
-
const {
|
|
657
|
-
threshold = 0.1,
|
|
658
|
-
rootMargin = '0px',
|
|
659
|
-
placeholder = 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==',
|
|
660
|
-
onLoad,
|
|
661
|
-
onError,
|
|
662
|
-
} = options;
|
|
663
|
-
|
|
664
|
-
// 设置占位图
|
|
665
|
-
if (placeholder) {
|
|
666
|
-
element.src = placeholder;
|
|
667
|
-
}
|
|
668
|
-
|
|
669
|
-
// 如果 IntersectionObserver 不可用,直接加载
|
|
670
|
-
if (!('IntersectionObserver' in window)) {
|
|
671
|
-
element.src = src;
|
|
672
|
-
element.onload = onLoad || null;
|
|
673
|
-
element.onerror = onError || null;
|
|
674
|
-
return;
|
|
675
|
-
}
|
|
676
|
-
|
|
677
|
-
const observer = new IntersectionObserver(
|
|
678
|
-
(entries) => {
|
|
679
|
-
entries.forEach((entry) => {
|
|
680
|
-
if (entry.isIntersecting) {
|
|
681
|
-
element.src = src;
|
|
682
|
-
element.onload = () => {
|
|
683
|
-
onLoad?.();
|
|
684
|
-
observer.unobserve(element);
|
|
685
|
-
};
|
|
686
|
-
element.onerror = () => {
|
|
687
|
-
onError?.();
|
|
688
|
-
observer.unobserve(element);
|
|
689
|
-
};
|
|
690
|
-
}
|
|
691
|
-
});
|
|
692
|
-
},
|
|
693
|
-
{ threshold, rootMargin },
|
|
694
|
-
);
|
|
695
|
-
|
|
696
|
-
observer.observe(element);
|
|
697
|
-
}
|
|
698
|
-
|
|
699
|
-
/**
|
|
700
|
-
* 批处理函数
|
|
701
|
-
* @param func 需要批处理的函数
|
|
702
|
-
* @param wait 等待时间(毫秒)
|
|
703
|
-
* @returns 批处理后的函数
|
|
704
|
-
*/
|
|
705
|
-
export function batch<T extends (...args: any[]) => any>(
|
|
706
|
-
func: (items: Parameters<T>[]) => void,
|
|
707
|
-
wait: number,
|
|
708
|
-
): (...args: Parameters<T>) => void {
|
|
709
|
-
let batch: Parameters<T>[] = [];
|
|
710
|
-
let timeoutId: NodeJS.Timeout | null = null;
|
|
711
|
-
|
|
712
|
-
const flush = () => {
|
|
713
|
-
if (batch.length > 0) {
|
|
714
|
-
func(batch);
|
|
715
|
-
batch = [];
|
|
716
|
-
}
|
|
717
|
-
timeoutId = null;
|
|
718
|
-
};
|
|
719
|
-
|
|
720
|
-
return (...args: Parameters<T>) => {
|
|
721
|
-
batch.push(args);
|
|
722
|
-
|
|
723
|
-
if (!timeoutId) {
|
|
724
|
-
timeoutId = setTimeout(flush, wait);
|
|
725
|
-
}
|
|
726
|
-
};
|
|
727
|
-
}
|
|
728
|
-
|
|
729
|
-
/**
|
|
730
|
-
* 记忆化函数
|
|
731
|
-
* @param func 需要记忆化的函数
|
|
732
|
-
* @param keyGenerator 键生成器
|
|
733
|
-
* @returns 记忆化后的函数
|
|
734
|
-
*/
|
|
735
|
-
export function memoize<T extends (...args: unknown[]) => unknown>(
|
|
736
|
-
func: T,
|
|
737
|
-
keyGenerator?: (...args: Parameters<T>) => string,
|
|
738
|
-
): T {
|
|
739
|
-
const cache = new Map<string, ReturnType<T>>();
|
|
740
|
-
|
|
741
|
-
return ((...args: Parameters<T>) => {
|
|
742
|
-
const key = keyGenerator ? keyGenerator(...args) : JSON.stringify(args);
|
|
743
|
-
|
|
744
|
-
if (cache.has(key)) {
|
|
745
|
-
return cache.get(key)!;
|
|
746
|
-
}
|
|
747
|
-
|
|
748
|
-
const result = func(...args);
|
|
749
|
-
cache.set(key, result as ReturnType<T>);
|
|
750
|
-
return result;
|
|
751
|
-
}) as T;
|
|
752
|
-
}
|
|
753
|
-
|
|
754
|
-
/**
|
|
755
|
-
* 性能监控装饰器
|
|
756
|
-
* @param options 选项
|
|
757
|
-
*/
|
|
758
|
-
export function performanceMonitor(
|
|
759
|
-
options: {
|
|
760
|
-
name?: string;
|
|
761
|
-
threshold?: number;
|
|
762
|
-
logLevel?: 'warn' | 'error' | 'info';
|
|
763
|
-
} = {},
|
|
764
|
-
) {
|
|
765
|
-
return function (_target: unknown, propertyKey: string, descriptor: PropertyDescriptor) {
|
|
766
|
-
const originalMethod = descriptor.value;
|
|
767
|
-
const { name = propertyKey, threshold = 16, logLevel = 'warn' } = options;
|
|
768
|
-
|
|
769
|
-
descriptor.value = function (...args: unknown[]) {
|
|
770
|
-
const startTime = performance.now();
|
|
771
|
-
const result = (originalMethod as any).apply(this, args);
|
|
772
|
-
const endTime = performance.now();
|
|
773
|
-
const duration = endTime - startTime;
|
|
774
|
-
|
|
775
|
-
if (duration > threshold) {
|
|
776
|
-
console[logLevel](`[Performance] ${name} took ${duration.toFixed(2)}ms`);
|
|
777
|
-
}
|
|
778
|
-
|
|
779
|
-
return result;
|
|
780
|
-
};
|
|
781
|
-
};
|
|
782
|
-
}
|
|
783
|
-
|
|
784
|
-
/**
|
|
785
|
-
* 请求动画帧优化
|
|
786
|
-
* @param callback 回调函数
|
|
787
|
-
* @returns 取消函数
|
|
788
|
-
*/
|
|
789
|
-
export function raf(callback: () => void): () => void {
|
|
790
|
-
let rafId: number;
|
|
791
|
-
|
|
792
|
-
const animate = () => {
|
|
793
|
-
callback();
|
|
794
|
-
rafId = requestAnimationFrame(animate);
|
|
795
|
-
};
|
|
796
|
-
|
|
797
|
-
rafId = requestAnimationFrame(animate);
|
|
798
|
-
|
|
799
|
-
return () => {
|
|
800
|
-
cancelAnimationFrame(rafId);
|
|
801
|
-
};
|
|
802
|
-
}
|
|
803
|
-
|
|
804
|
-
/**
|
|
805
|
-
* 空闲时间执行
|
|
806
|
-
* @param callback 回调函数
|
|
807
|
-
* @param options 选项
|
|
808
|
-
*/
|
|
809
|
-
export function idleCallback(callback: () => void, options: { timeout?: number } = {}): () => void {
|
|
810
|
-
let handle: number;
|
|
811
|
-
|
|
812
|
-
if ('requestIdleCallback' in window) {
|
|
813
|
-
handle = requestIdleCallback(callback, options);
|
|
814
|
-
} else {
|
|
815
|
-
// 降级处理
|
|
816
|
-
handle = (window as any).setTimeout(callback, options.timeout || 50);
|
|
817
|
-
}
|
|
818
|
-
|
|
819
|
-
return () => {
|
|
820
|
-
if ('cancelIdleCallback' in window) {
|
|
821
|
-
cancelIdleCallback(handle);
|
|
822
|
-
} else {
|
|
823
|
-
clearTimeout(handle);
|
|
824
|
-
}
|
|
825
|
-
};
|
|
826
|
-
}
|
|
827
|
-
|
|
828
|
-
export default {
|
|
829
|
-
debounce,
|
|
830
|
-
throttle,
|
|
831
|
-
rafThrottle,
|
|
832
|
-
lazyLoad,
|
|
833
|
-
lazyLoadImage,
|
|
834
|
-
batch,
|
|
835
|
-
memoize,
|
|
836
|
-
performanceMonitor,
|
|
837
|
-
raf,
|
|
838
|
-
idleCallback,
|
|
839
|
-
};
|