@leancodepl/hook-pipe-client 8.5.0 → 8.5.1

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 ADDED
@@ -0,0 +1,124 @@
1
+ # @leancodepl/hook-pipe-client
2
+
3
+ React hooks for real-time data subscriptions using @leancodepl/pipe.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @leancodepl/hook-pipe-client
9
+ # or
10
+ yarn add @leancodepl/hook-pipe-client
11
+ ```
12
+
13
+ ## API
14
+
15
+ ### `mkPipeClient(pipe)`
16
+
17
+ Creates React hooks for real-time data subscriptions using @leancodepl/pipe.
18
+
19
+ **Parameters:**
20
+
21
+ - `pipe: Pipe` - Pipe instance from @leancodepl/pipe
22
+
23
+ **Returns:** Object containing `createTopic` method for creating typed hooks
24
+
25
+ ## Usage Examples
26
+
27
+ ### Basic Setup
28
+
29
+ ```typescript
30
+ import { mkPipeClient } from "@leancodepl/hook-pipe-client"
31
+ import { createPipe } from "@leancodepl/pipe"
32
+
33
+ const pipe = createPipe({
34
+ url: "wss://api.example.com/pipe",
35
+ getAccessToken: () => localStorage.getItem("token"),
36
+ })
37
+
38
+ const pipeClient = mkPipeClient({ pipe })
39
+ ```
40
+
41
+ ### Chat Messages
42
+
43
+ ```typescript
44
+ import React, { useState } from 'react';
45
+
46
+ interface ChatTopic {
47
+ roomId: string;
48
+ }
49
+
50
+ interface ChatNotifications {
51
+ MessageReceived: {
52
+ id: string;
53
+ content: string;
54
+ authorId: string;
55
+ };
56
+ }
57
+
58
+ const useChatTopic = pipeClient.createTopic<ChatTopic, ChatNotifications>('chat');
59
+
60
+ function ChatRoom({ roomId }: { roomId: string }) {
61
+ const [messages, setMessages] = useState<string[]>([]);
62
+
63
+ const { data } = useChatTopic(
64
+ { roomId },
65
+ {
66
+ onData: (notification) => {
67
+ if (notification.type === 'MessageReceived') {
68
+ setMessages(prev => [...prev, notification.data.content]);
69
+ }
70
+ },
71
+ }
72
+ );
73
+
74
+ return (
75
+ <div>
76
+ {messages.map((message, index) => (
77
+ <div key={index}>{message}</div>
78
+ ))}
79
+ </div>
80
+ );
81
+ }
82
+ ```
83
+
84
+ ### Live Metrics
85
+
86
+ ```typescript
87
+ import React, { useState } from 'react';
88
+
89
+ interface MetricsTopic {
90
+ dashboardId: string;
91
+ }
92
+
93
+ interface MetricsNotifications {
94
+ CpuUpdate: { value: number };
95
+ MemoryUpdate: { value: number };
96
+ }
97
+
98
+ const useMetricsTopic = pipeClient.createTopic<MetricsTopic, MetricsNotifications>('metrics');
99
+
100
+ function Dashboard() {
101
+ const [cpu, setCpu] = useState(0);
102
+ const [memory, setMemory] = useState(0);
103
+
104
+ useMetricsTopic(
105
+ { dashboardId: 'main' },
106
+ {
107
+ onData: (notification) => {
108
+ if (notification.type === 'CpuUpdate') {
109
+ setCpu(notification.data.value);
110
+ } else if (notification.type === 'MemoryUpdate') {
111
+ setMemory(notification.data.value);
112
+ }
113
+ },
114
+ }
115
+ );
116
+
117
+ return (
118
+ <div>
119
+ <div>CPU: {cpu}%</div>
120
+ <div>Memory: {memory}%</div>
121
+ </div>
122
+ );
123
+ }
124
+ ```
@@ -0,0 +1 @@
1
+ exports._default = require('./index.cjs.js').default;
package/index.cjs.js ADDED
@@ -0,0 +1,51 @@
1
+ 'use strict';
2
+
3
+ var react = require('react');
4
+ var deepEqual = require('deep-equal');
5
+ var rxjs = require('rxjs');
6
+
7
+ /**
8
+ * Creates React hooks for real-time data subscriptions using "@leancodepl/pipe".
9
+ *
10
+ * @param pipe - Pipe instance from "@leancodepl/pipe"
11
+ * @returns Object containing `createTopic` method for creating typed hooks
12
+ * @example
13
+ * ```typescript
14
+ * const pipe = createPipe({ url: 'wss://api.example.com/pipe' });
15
+ * const pipeClient = mkPipeClient({ pipe });
16
+ *
17
+ * const useChatTopic = pipeClient.createTopic('chat');
18
+ * ```
19
+ */ function mkPipeClient({ pipe }) {
20
+ return {
21
+ createTopic (topicType) {
22
+ function useTopic(topic, { onData }) {
23
+ const [data, setData] = react.useState();
24
+ const onDataRef = react.useRef(onData);
25
+ onDataRef.current = onData;
26
+ const memoizedTopic = react.useRef();
27
+ if (memoizedTopic.current === undefined || !deepEqual(memoizedTopic.current, topic)) {
28
+ memoizedTopic.current = topic;
29
+ }
30
+ react.useEffect(()=>{
31
+ const topic$ = pipe.topic(topicType, memoizedTopic.current).pipe(rxjs.share());
32
+ const subscription = topic$.subscribe((data)=>{
33
+ setData(data);
34
+ onDataRef.current == null ? void 0 : onDataRef.current.call(onDataRef, data);
35
+ });
36
+ return ()=>subscription.unsubscribe();
37
+ // eslint-disable-next-line react-hooks/exhaustive-deps
38
+ }, [
39
+ memoizedTopic.current
40
+ ]);
41
+ return {
42
+ data
43
+ };
44
+ }
45
+ useTopic.topic = (topic)=>pipe.topic(topicType, topic);
46
+ return useTopic;
47
+ }
48
+ };
49
+ }
50
+
51
+ exports.mkPipeClient = mkPipeClient;
package/index.cjs.mjs ADDED
@@ -0,0 +1,2 @@
1
+ export * from './index.cjs.js';
2
+ export { _default as default } from './index.cjs.default.js';
package/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./src/index";
package/index.esm.js ADDED
@@ -0,0 +1,49 @@
1
+ import { useState, useRef, useEffect } from 'react';
2
+ import deepEqual from 'deep-equal';
3
+ import { share } from 'rxjs';
4
+
5
+ /**
6
+ * Creates React hooks for real-time data subscriptions using "@leancodepl/pipe".
7
+ *
8
+ * @param pipe - Pipe instance from "@leancodepl/pipe"
9
+ * @returns Object containing `createTopic` method for creating typed hooks
10
+ * @example
11
+ * ```typescript
12
+ * const pipe = createPipe({ url: 'wss://api.example.com/pipe' });
13
+ * const pipeClient = mkPipeClient({ pipe });
14
+ *
15
+ * const useChatTopic = pipeClient.createTopic('chat');
16
+ * ```
17
+ */ function mkPipeClient({ pipe }) {
18
+ return {
19
+ createTopic (topicType) {
20
+ function useTopic(topic, { onData }) {
21
+ const [data, setData] = useState();
22
+ const onDataRef = useRef(onData);
23
+ onDataRef.current = onData;
24
+ const memoizedTopic = useRef();
25
+ if (memoizedTopic.current === undefined || !deepEqual(memoizedTopic.current, topic)) {
26
+ memoizedTopic.current = topic;
27
+ }
28
+ useEffect(()=>{
29
+ const topic$ = pipe.topic(topicType, memoizedTopic.current).pipe(share());
30
+ const subscription = topic$.subscribe((data)=>{
31
+ setData(data);
32
+ onDataRef.current == null ? void 0 : onDataRef.current.call(onDataRef, data);
33
+ });
34
+ return ()=>subscription.unsubscribe();
35
+ // eslint-disable-next-line react-hooks/exhaustive-deps
36
+ }, [
37
+ memoizedTopic.current
38
+ ]);
39
+ return {
40
+ data
41
+ };
42
+ }
43
+ useTopic.topic = (topic)=>pipe.topic(topicType, topic);
44
+ return useTopic;
45
+ }
46
+ };
47
+ }
48
+
49
+ export { mkPipeClient };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leancodepl/hook-pipe-client",
3
- "version": "8.5.0",
3
+ "version": "8.5.1",
4
4
  "license": "Apache-2.0",
5
5
  "dependencies": {
6
6
  "@leancodepl/pipe": "^1.0.0",
@@ -43,11 +43,6 @@
43
43
  "name": "LeanCode",
44
44
  "url": "https://leancode.co"
45
45
  },
46
- "files": [
47
- "dist",
48
- "README.md",
49
- "CHANGELOG.md"
50
- ],
51
46
  "sideEffects": false,
52
47
  "exports": {
53
48
  "./package.json": "./package.json",
package/src/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./lib/mkPipeClient";
@@ -0,0 +1,27 @@
1
+ import { NotificationsUnion, Pipe } from "@leancodepl/pipe";
2
+ /**
3
+ * Creates React hooks for real-time data subscriptions using "@leancodepl/pipe".
4
+ *
5
+ * @param pipe - Pipe instance from "@leancodepl/pipe"
6
+ * @returns Object containing `createTopic` method for creating typed hooks
7
+ * @example
8
+ * ```typescript
9
+ * const pipe = createPipe({ url: 'wss://api.example.com/pipe' });
10
+ * const pipeClient = mkPipeClient({ pipe });
11
+ *
12
+ * const useChatTopic = pipeClient.createTopic('chat');
13
+ * ```
14
+ */
15
+ export declare function mkPipeClient({ pipe }: {
16
+ pipe: Pipe;
17
+ }): {
18
+ createTopic<TTopic, TNotifications extends Record<string, unknown>>(topicType: string): {
19
+ (topic: TTopic, { onData }: UseSubscriptionOptions<TNotifications>): {
20
+ data: NotificationsUnion<TNotifications> | undefined;
21
+ };
22
+ topic(topic: TTopic): import("rxjs").Observable<NotificationsUnion<TNotifications>>;
23
+ };
24
+ };
25
+ export type UseSubscriptionOptions<TNotifications extends Record<string, unknown>> = {
26
+ onData?: (data: NotificationsUnion<TNotifications>) => void;
27
+ };