@leancodepl/hook-pipe-client 7.6.0 → 7.7.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/index.cjs.js CHANGED
@@ -3,26 +3,41 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var react = require('react');
6
+ var deepEqual = require('deep-equal');
7
+ var rxjs = require('rxjs');
8
+
9
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
10
+
11
+ var deepEqual__default = /*#__PURE__*/_interopDefaultLegacy(deepEqual);
6
12
 
7
13
  function mkPipeClient({ pipe }) {
8
14
  return {
9
15
  createTopic (topicType) {
10
- return (topic, { onData })=>{
16
+ function useTopic(topic, { onData }) {
11
17
  const [data, setData] = react.useState();
18
+ const onDataRef = react.useRef(onData);
19
+ onDataRef.current = onData;
20
+ const memoizedTopic = react.useRef();
21
+ if (memoizedTopic.current === undefined || !deepEqual__default["default"](memoizedTopic.current, topic)) {
22
+ memoizedTopic.current = topic;
23
+ }
12
24
  react.useEffect(()=>{
13
- const subscription = pipe.topic(topicType, topic).subscribe((notification)=>{
14
- onData == null ? void 0 : onData(notification);
15
- setData(notification);
25
+ const topic$ = pipe.topic(topicType, memoizedTopic.current).pipe(rxjs.share());
26
+ const subscription = topic$.subscribe((data)=>{
27
+ setData(data);
28
+ onDataRef.current == null ? void 0 : onDataRef.current.call(onDataRef, data);
16
29
  });
17
30
  return ()=>subscription.unsubscribe();
31
+ // eslint-disable-next-line react-hooks/exhaustive-deps
18
32
  }, [
19
- onData,
20
- topic
33
+ memoizedTopic.current
21
34
  ]);
22
35
  return {
23
36
  data
24
37
  };
25
- };
38
+ }
39
+ useTopic.topic = (topic)=>pipe.topic(topicType, topic);
40
+ return useTopic;
26
41
  }
27
42
  };
28
43
  }
package/package.json CHANGED
@@ -1,8 +1,12 @@
1
1
  {
2
2
  "name": "@leancodepl/hook-pipe-client",
3
- "version": "7.6.0",
3
+ "version": "7.7.0",
4
4
  "dependencies": {
5
- "@leancodepl/pipe": ">=0.0.2"
5
+ "@leancodepl/pipe": "^1.0.0",
6
+ "deep-equal": "^2.0.0"
7
+ },
8
+ "peerDependencies": {
9
+ "rxjs": ">=7.0.0"
6
10
  },
7
11
  "type": "commonjs",
8
12
  "main": "./index.cjs.js",
@@ -2,11 +2,13 @@ import { NotificationsUnion, Pipe } from "@leancodepl/pipe";
2
2
  export declare function mkPipeClient({ pipe }: {
3
3
  pipe: Pipe;
4
4
  }): {
5
- createTopic<TTopic, TNotifications extends Record<string, unknown>>(topicType: string): (topic: TTopic, { onData }: UseSubscriptionOptions<TNotifications>) => {
6
- data: NotificationsUnion<TNotifications> | undefined;
5
+ createTopic<TTopic, TNotifications extends Record<string, unknown>>(topicType: string): {
6
+ (topic: TTopic, { onData }: UseSubscriptionOptions<TNotifications>): {
7
+ data: NotificationsUnion<TNotifications> | undefined;
8
+ };
9
+ topic(topic: TTopic): import("rxjs").Observable<NotificationsUnion<TNotifications>>;
7
10
  };
8
11
  };
9
- type UseSubscriptionOptions<TNotifications extends Record<string, unknown>> = {
12
+ export type UseSubscriptionOptions<TNotifications extends Record<string, unknown>> = {
10
13
  onData?: (data: NotificationsUnion<TNotifications>) => void;
11
14
  };
12
- export {};