@ahoo-wang/fetcher-react 3.3.5 → 3.3.8

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 CHANGED
@@ -36,8 +36,9 @@ robust data fetching capabilities.
36
36
  - [usePromiseState Hook](#usepromisestate-hook)
37
37
  - [useRequestId Hook](#userequestid-hook)
38
38
  - [useLatest Hook](#uselatest-hook)
39
- - [useRefs Hook](#userefs-hook)
40
- - [useKeyStorage Hook](#usekeystorage-hook)
39
+ - [useRefs Hook](#userefs-hook)
40
+ - [useEventSubscription Hook](#useeventsubscription-hook)
41
+ - [useKeyStorage Hook](#usekeystorage-hook)
41
42
  - [useImmerKeyStorage Hook](#useimmerkeystorage-hook)
42
43
  - [Wow Query Hooks](#wow-query-hooks)
43
44
  - [useListQuery Hook](#uselistquery-hook)
@@ -476,6 +477,47 @@ Key features:
476
477
  - **Automatic Cleanup**: Refs are cleared when component unmounts
477
478
  - **Type Safety**: Full TypeScript support for ref types
478
479
 
480
+ ### useEventSubscription Hook
481
+
482
+ The `useEventSubscription` hook provides a React interface for subscribing to typed event buses. It automatically manages subscription lifecycle while offering manual control functions for additional flexibility.
483
+
484
+ ```typescript jsx
485
+ import { useEventSubscription } from '@ahoo-wang/fetcher-react';
486
+ import { eventBus } from './eventBus';
487
+
488
+ function MyComponent() {
489
+ const { subscribe, unsubscribe } = useEventSubscription({
490
+ bus: eventBus,
491
+ handler: {
492
+ name: 'myEvent',
493
+ handle: (event) => {
494
+ console.log('Received event:', event);
495
+ }
496
+ }
497
+ });
498
+
499
+ // The hook automatically subscribes on mount and unsubscribes on unmount
500
+ // You can also manually control subscription if needed
501
+ const handleToggleSubscription = () => {
502
+ if (someCondition) {
503
+ subscribe();
504
+ } else {
505
+ unsubscribe();
506
+ }
507
+ };
508
+
509
+ return <div>My Component</div>;
510
+ }
511
+ ```
512
+
513
+ Key features:
514
+
515
+ - **Automatic Lifecycle Management**: Automatically subscribes on component mount and unsubscribes on unmount
516
+ - **Manual Control**: Provides `subscribe` and `unsubscribe` functions for additional control
517
+ - **Type Safety**: Full TypeScript support with generic event types
518
+ - **Error Handling**: Logs warnings for failed subscription attempts
519
+ - **Event Bus Integration**: Works seamlessly with `@ahoo-wang/fetcher-eventbus` TypedEventBus instances
520
+
479
521
  ### useKeyStorage Hook
480
522
 
481
523
  The `useKeyStorage` hook provides reactive state management for a KeyStorage instance. It subscribes to storage changes and returns the current value along with a setter function. Optionally accepts a default value to use when the storage is empty.
@@ -1959,6 +2001,38 @@ An object implementing `UseRefsReturn<T>` with:
1959
2001
  - `RefKey = string | number | symbol`
1960
2002
  - `UseRefsReturn<T> extends Iterable<[RefKey, T]>`
1961
2003
 
2004
+ ### useEventSubscription
2005
+
2006
+ ```typescript
2007
+ function useEventSubscription<EVENT = unknown>(
2008
+ options: UseEventSubscriptionOptions<EVENT>,
2009
+ ): UseEventSubscriptionReturn;
2010
+ ```
2011
+
2012
+ A React hook for subscribing to events from a typed event bus. Automatically manages subscription lifecycle while providing manual control functions.
2013
+
2014
+ **Type Parameters:**
2015
+
2016
+ - `EVENT`: The type of events handled by the event bus (defaults to unknown)
2017
+
2018
+ **Parameters:**
2019
+
2020
+ - `options`: Configuration options for the subscription
2021
+ - `bus`: The TypedEventBus instance to subscribe to
2022
+ - `handler`: The event handler function with name and handle method
2023
+
2024
+ **Returns:**
2025
+
2026
+ An object containing:
2027
+
2028
+ - `subscribe`: Function to manually subscribe to the event bus (returns boolean success status)
2029
+ - `unsubscribe`: Function to manually unsubscribe from the event bus (returns boolean success status)
2030
+
2031
+ **Related Types:**
2032
+
2033
+ - `UseEventSubscriptionOptions<EVENT>`: Configuration interface with bus and handler properties
2034
+ - `UseEventSubscriptionReturn`: Return interface with subscribe and unsubscribe methods
2035
+
1962
2036
  ### useKeyStorage
1963
2037
 
1964
2038
  ```typescript
package/README.zh-CN.md CHANGED
@@ -35,8 +35,9 @@
35
35
  - [usePromiseState Hook](#usepromisestate-hook)
36
36
  - [useRequestId Hook](#userequestid-hook)
37
37
  - [useLatest Hook](#uselatest-hook)
38
- - [useRefs Hook](#userefs-hook)
39
- - [useKeyStorage Hook](#usekeystorage-hook)
38
+ - [useRefs Hook](#userefs-hook)
39
+ - [useEventSubscription Hook](#useeventsubscription-hook)
40
+ - [useKeyStorage Hook](#usekeystorage-hook)
40
41
  - [useImmerKeyStorage Hook](#useimmerkeystorage-hook)
41
42
  - [Wow 查询 Hooks](#wow-查询-hooks)
42
43
  - [useListQuery Hook](#uselistquery-hook)
@@ -859,6 +860,47 @@ React hook,用于使用 Map-like 接口管理多个 refs,允许通过键动
859
860
  - `RefKey = string | number | symbol`
860
861
  - `UseRefsReturn<T> extends Iterable<[RefKey, T]>`
861
862
 
863
+ ### useEventSubscription Hook
864
+
865
+ `useEventSubscription` hook 为类型化事件总线提供了 React 接口。它自动管理订阅生命周期,同时提供手动控制功能以增加灵活性。
866
+
867
+ ```typescript jsx
868
+ import { useEventSubscription } from '@ahoo-wang/fetcher-react';
869
+ import { eventBus } from './eventBus';
870
+
871
+ function MyComponent() {
872
+ const { subscribe, unsubscribe } = useEventSubscription({
873
+ bus: eventBus,
874
+ handler: {
875
+ name: 'myEvent',
876
+ handle: (event) => {
877
+ console.log('收到事件:', event);
878
+ }
879
+ }
880
+ });
881
+
882
+ // hook 在组件挂载时自动订阅,在卸载时自动取消订阅
883
+ // 如需要,您也可以手动控制订阅
884
+ const handleToggleSubscription = () => {
885
+ if (someCondition) {
886
+ subscribe();
887
+ } else {
888
+ unsubscribe();
889
+ }
890
+ };
891
+
892
+ return <div>我的组件</div>;
893
+ }
894
+ ```
895
+
896
+ 关键特性:
897
+
898
+ - **自动生命周期管理**: 在组件挂载时自动订阅,在卸载时自动取消订阅
899
+ - **手动控制**: 提供 `subscribe` 和 `unsubscribe` 函数以进行额外控制
900
+ - **类型安全**: 完全支持 TypeScript,具有泛型事件类型
901
+ - **错误处理**: 对失败的订阅尝试记录警告
902
+ - **事件总线集成**: 与 `@ahoo-wang/fetcher-eventbus` TypedEventBus 实例无缝配合
903
+
862
904
  ### useKeyStorage
863
905
 
864
906
  ```typescript jsx
@@ -0,0 +1,2 @@
1
+ export * from './useEventSubscription';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/eventbus/index.ts"],"names":[],"mappings":"AAaA,cAAc,wBAAwB,CAAC"}
@@ -0,0 +1,76 @@
1
+ import { EventHandler, TypedEventBus } from '@ahoo-wang/fetcher-eventbus';
2
+ /**
3
+ * Options for the useEventSubscription hook.
4
+ * @template EVENT - The type of events handled by the event bus.
5
+ */
6
+ export interface UseEventSubscriptionOptions<EVENT> {
7
+ /**
8
+ * The typed event bus instance to subscribe to.
9
+ */
10
+ bus: TypedEventBus<EVENT>;
11
+ /**
12
+ * The event handler function that will be called when events are published.
13
+ */
14
+ handler: EventHandler<EVENT>;
15
+ }
16
+ /**
17
+ * Return type for the useEventSubscription hook.
18
+ */
19
+ export interface UseEventSubscriptionReturn {
20
+ /**
21
+ * Function to manually subscribe to the event bus.
22
+ * @returns true if subscription was successful, false otherwise.
23
+ */
24
+ subscribe: () => boolean;
25
+ /**
26
+ * Function to manually unsubscribe from the event bus.
27
+ * @returns true if unsubscription was successful, false otherwise.
28
+ */
29
+ unsubscribe: () => boolean;
30
+ }
31
+ /**
32
+ * A React hook for subscribing to events from a typed event bus.
33
+ *
34
+ * This hook automatically subscribes to the event bus when the component mounts
35
+ * and unsubscribes when the component unmounts. It also provides manual subscribe
36
+ * and unsubscribe functions for additional control.
37
+ *
38
+ * @template EVENT - The type of events handled by the event bus. Defaults to unknown.
39
+ * @param options - Configuration options for the subscription.
40
+ * @param options.bus - The typed event bus instance to subscribe to.
41
+ * @param options.handler - The event handler function that will be called when events are published.
42
+ * @returns An object containing subscribe and unsubscribe functions.
43
+ * @throws Will throw an error if the event bus or handler is invalid.
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * import { useEventSubscription } from '@ahoo-wang/fetcher-react';
48
+ * import { eventBus } from './eventBus';
49
+ *
50
+ * function MyComponent() {
51
+ * const { subscribe, unsubscribe } = useEventSubscription({
52
+ * bus: eventBus,
53
+ * handler: {
54
+ * name: 'myEvent',
55
+ * handle: (event) => {
56
+ * console.log('Received event:', event);
57
+ * }
58
+ * }
59
+ * });
60
+ *
61
+ * // The hook automatically subscribes on mount and unsubscribes on unmount
62
+ * // You can also manually control subscription if needed
63
+ * const handleToggleSubscription = () => {
64
+ * if (someCondition) {
65
+ * subscribe();
66
+ * } else {
67
+ * unsubscribe();
68
+ * }
69
+ * };
70
+ *
71
+ * return <div>My Component</div>;
72
+ * }
73
+ * ```
74
+ */
75
+ export declare function useEventSubscription<EVENT = unknown>(options: UseEventSubscriptionOptions<EVENT>): UseEventSubscriptionReturn;
76
+ //# sourceMappingURL=useEventSubscription.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useEventSubscription.d.ts","sourceRoot":"","sources":["../../src/eventbus/useEventSubscription.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAE1E;;;GAGG;AACH,MAAM,WAAW,2BAA2B,CAAC,KAAK;IAChD;;OAEG;IACH,GAAG,EAAE,aAAa,CAAC,KAAK,CAAC,CAAC;IAC1B;;OAEG;IACH,OAAO,EAAE,YAAY,CAAC,KAAK,CAAC,CAAC;CAC9B;AAED;;GAEG;AACH,MAAM,WAAW,0BAA0B;IACzC;;;OAGG;IACH,SAAS,EAAE,MAAM,OAAO,CAAC;IACzB;;;OAGG;IACH,WAAW,EAAE,MAAM,OAAO,CAAC;CAC5B;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,GAAG,OAAO,EAClD,OAAO,EAAE,2BAA2B,CAAC,KAAK,CAAC,GAC1C,0BAA0B,CA0B5B"}
package/dist/index.d.ts CHANGED
@@ -2,4 +2,5 @@ export * from './core';
2
2
  export * from './storage';
3
3
  export * from './fetcher';
4
4
  export * from './wow';
5
+ export * from './eventbus';
5
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAaA,cAAc,QAAQ,CAAC;AACvB,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,OAAO,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAaA,cAAc,QAAQ,CAAC;AACvB,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,OAAO,CAAC;AACtB,cAAc,YAAY,CAAC"}