@itstepstorybookpackage/ui-kit 0.0.4 → 0.0.6

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.esm.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
2
2
  import { ActivityIndicator, View, Text } from 'react-native';
3
3
  import styled from 'styled-components/native';
4
+ import { useState, useMemo, useEffect, useContext, createContext, useRef } from 'react';
5
+ import { io } from 'socket.io-client';
4
6
 
5
7
  /******************************************************************************
6
8
  Copyright (c) Microsoft Corporation.
@@ -128,4 +130,133 @@ function Notification(props) {
128
130
  });
129
131
  }
130
132
 
131
- export { Button, Notification };
133
+ // SocketClient.ts
134
+ class SocketClient {
135
+ constructor(url, options) {
136
+ // Зробимо socket приватним, щоб заборонити прямий доступ
137
+ this.socket = null;
138
+ this.url = url;
139
+ this.options = options;
140
+ }
141
+ connect() {
142
+ var _a;
143
+ if ((_a = this.socket) === null || _a === void 0 ? void 0 : _a.connected) return this.socket;
144
+ this.socket = io(this.url, Object.assign({
145
+ transports: ['websocket'],
146
+ reconnection: true
147
+ }, this.options));
148
+ return this.socket;
149
+ }
150
+ disconnect() {
151
+ if (this.socket) {
152
+ this.socket.removeAllListeners();
153
+ this.socket.disconnect();
154
+ this.socket = null;
155
+ }
156
+ }
157
+ emit(event, ...args) {
158
+ var _a;
159
+ (_a = this.socket) === null || _a === void 0 ? void 0 : _a.emit(event, ...args);
160
+ }
161
+ on(event, callback) {
162
+ var _a;
163
+ (_a = this.socket) === null || _a === void 0 ? void 0 : _a.on(event, callback);
164
+ }
165
+ off(event, callback) {
166
+ var _a;
167
+ (_a = this.socket) === null || _a === void 0 ? void 0 : _a.off(event, callback);
168
+ }
169
+ onAny(callback) {
170
+ var _a;
171
+ (_a = this.socket) === null || _a === void 0 ? void 0 : _a.onAny(callback);
172
+ }
173
+ offAny(callback) {
174
+ var _a;
175
+ (_a = this.socket) === null || _a === void 0 ? void 0 : _a.offAny(callback);
176
+ }
177
+ get isConnected() {
178
+ var _a;
179
+ return ((_a = this.socket) === null || _a === void 0 ? void 0 : _a.connected) || false;
180
+ }
181
+ once(event, callback) {
182
+ var _a;
183
+ (_a = this.socket) === null || _a === void 0 ? void 0 : _a.once(event, callback);
184
+ }
185
+ }
186
+
187
+ const SocketContext = /*#__PURE__*/createContext({
188
+ clients: {}
189
+ });
190
+ const SocketProvider = ({
191
+ configs,
192
+ children
193
+ }) => {
194
+ const [clients, setClients] = useState({});
195
+ const memoizedConfigs = useMemo(() => JSON.stringify(configs), [configs]);
196
+ useEffect(() => {
197
+ const parsedConfigs = JSON.parse(memoizedConfigs);
198
+ const newClients = {};
199
+ Object.entries(parsedConfigs).forEach(([key, config]) => {
200
+ if (config.enabled !== false) {
201
+ const client = new SocketClient(config.url, config.options || {});
202
+ client.connect();
203
+ newClients[key] = client;
204
+ }
205
+ });
206
+ setClients(newClients);
207
+ return () => {
208
+ Object.values(newClients).forEach(client => client.disconnect());
209
+ };
210
+ }, [memoizedConfigs]);
211
+ return jsx(SocketContext.Provider, {
212
+ value: {
213
+ clients
214
+ },
215
+ children: children
216
+ });
217
+ };
218
+ const useSocketRaw = socketName => {
219
+ const {
220
+ clients
221
+ } = useContext(SocketContext);
222
+ return {
223
+ client: clients[socketName] || null
224
+ };
225
+ };
226
+
227
+ const useSocketHandlers = (socketName, handlers) => {
228
+ const {
229
+ client
230
+ } = useSocketRaw(socketName);
231
+ const handlersRef = useRef(handlers);
232
+ useEffect(() => {
233
+ handlersRef.current = handlers;
234
+ }, [handlers]);
235
+ useEffect(() => {
236
+ if (!client) return;
237
+ const activeListeners = {};
238
+ Object.keys(handlersRef.current).forEach(eventName => {
239
+ const listener = (...args) => {
240
+ var _a, _b;
241
+ return (_b = (_a = handlersRef.current)[eventName]) === null || _b === void 0 ? void 0 : _b.call(_a, ...args);
242
+ };
243
+ activeListeners[eventName] = listener;
244
+ if (eventName === 'onAny') {
245
+ client.onAny(listener);
246
+ } else {
247
+ client.on(eventName, listener);
248
+ }
249
+ });
250
+ return () => {
251
+ Object.keys(activeListeners).forEach(eventName => {
252
+ if (eventName === 'onAny') {
253
+ client.offAny(activeListeners[eventName]);
254
+ } else {
255
+ client.off(eventName, activeListeners[eventName]);
256
+ }
257
+ });
258
+ };
259
+ }, [client]);
260
+ };
261
+
262
+ export { Button, Notification, SocketProvider, useSocketHandlers, useSocketRaw };
package/package.json CHANGED
@@ -1,31 +1,25 @@
1
1
  {
2
2
  "name": "@itstepstorybookpackage/ui-kit",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "main": "./index.esm.js",
5
5
  "module": "./index.esm.js",
6
6
  "react-native": "./index.esm.js",
7
7
  "types": "./index.d.ts",
8
- "browser": {
9
- "fs": false,
10
- "path": false,
11
- "os": false,
12
- "url": false,
13
- "util": false,
14
- "events": false,
15
- "stream": false,
16
- "child_process": false
17
- },
18
8
  "exports": {
19
9
  ".": {
10
+ "types": "./index.d.ts",
11
+ "react-native": "./index.esm.js",
20
12
  "import": "./index.esm.js",
21
- "require": "./index.esm.js"
13
+ "require": "./index.esm.js",
14
+ "default": "./index.esm.js"
22
15
  }
23
16
  },
24
17
  "peerDependencies": {
25
18
  "react": "*",
26
19
  "react-native": "*",
27
20
  "react-native-svg": "*",
28
- "styled-components": "*"
21
+ "styled-components": "*",
22
+ "socket.io-client": "*"
29
23
  },
30
24
  "type": "module"
31
25
  }
package/src/index.d.ts CHANGED
@@ -1,2 +1,4 @@
1
1
  export * from './lib/components/button/button';
2
2
  export * from './lib/mystat/notification/notification';
3
+ export * from './lib/utils/notification/SocketProvider';
4
+ export * from './lib/utils/notification/useSocketHandlers';
@@ -0,0 +1,16 @@
1
+ import { Socket, SocketOptions, ManagerOptions } from 'socket.io-client';
2
+ export declare class SocketClient {
3
+ private socket;
4
+ readonly url: string;
5
+ private options;
6
+ constructor(url: string, options: Partial<ManagerOptions & SocketOptions>);
7
+ connect(): Socket<import("@socket.io/component-emitter").DefaultEventsMap, import("@socket.io/component-emitter").DefaultEventsMap>;
8
+ disconnect(): void;
9
+ emit(event: string, ...args: any[]): void;
10
+ on(event: string, callback: (...args: any[]) => void): void;
11
+ off(event: string, callback?: (...args: any[]) => void): void;
12
+ onAny(callback: (event: string, ...args: any[]) => void): void;
13
+ offAny(callback?: (event: string, ...args: any[]) => void): void;
14
+ get isConnected(): boolean;
15
+ once(event: string, callback: (...args: any[]) => void): void;
16
+ }
@@ -0,0 +1,16 @@
1
+ import React from 'react';
2
+ import { SocketClient } from './SocketClient';
3
+ export interface SocketConfig {
4
+ url: string;
5
+ options?: any;
6
+ enabled?: boolean;
7
+ }
8
+ interface SocketProviderProps {
9
+ configs: Record<string, SocketConfig>;
10
+ children: React.ReactNode;
11
+ }
12
+ export declare const SocketProvider: React.FC<SocketProviderProps>;
13
+ export declare const useSocketRaw: (socketName: string) => {
14
+ client: SocketClient;
15
+ };
16
+ export {};
@@ -0,0 +1,3 @@
1
+ export * from './SocketProvider';
2
+ export * from './useSocketHandlers';
3
+ export * from './SocketClient';
@@ -0,0 +1,3 @@
1
+ type HandlersMap = Record<string, (...args: any[]) => void>;
2
+ export declare const useSocketHandlers: (socketName: string, handlers: HandlersMap) => void;
3
+ export {};
@@ -1,29 +0,0 @@
1
- export declare const join: () => string;
2
- export declare const resolve: () => string;
3
- export declare const dirname: () => string;
4
- export declare const basename: () => string;
5
- export declare const extname: () => string;
6
- export declare const parse: () => {};
7
- export declare const format: () => string;
8
- export declare const sep = "/";
9
- export declare const delimiter = ":";
10
- export declare const readFileSync: () => string;
11
- export declare const writeFileSync: () => void;
12
- export declare const existsSync: () => boolean;
13
- export declare const promises: {};
14
- declare const defaultObj: {
15
- join: () => string;
16
- resolve: () => string;
17
- dirname: () => string;
18
- basename: () => string;
19
- extname: () => string;
20
- parse: () => {};
21
- format: () => string;
22
- sep: string;
23
- delimiter: string;
24
- readFileSync: () => string;
25
- writeFileSync: () => void;
26
- existsSync: () => boolean;
27
- promises: {};
28
- };
29
- export default defaultObj;