@itstepstorybookpackage/ui-kit 0.0.5 → 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,7 +1,7 @@
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 { useRef, useMemo, useEffect, useContext, createContext } from 'react';
4
+ import { useState, useMemo, useEffect, useContext, createContext, useRef } from 'react';
5
5
  import { io } from 'socket.io-client';
6
6
 
7
7
  /******************************************************************************
@@ -130,15 +130,17 @@ function Notification(props) {
130
130
  });
131
131
  }
132
132
 
133
+ // SocketClient.ts
133
134
  class SocketClient {
134
135
  constructor(url, options) {
136
+ // Зробимо socket приватним, щоб заборонити прямий доступ
135
137
  this.socket = null;
136
138
  this.url = url;
137
139
  this.options = options;
138
140
  }
139
141
  connect() {
140
142
  var _a;
141
- if ((_a = this.socket) === null || _a === void 0 ? void 0 : _a.connected) return;
143
+ if ((_a = this.socket) === null || _a === void 0 ? void 0 : _a.connected) return this.socket;
142
144
  this.socket = io(this.url, Object.assign({
143
145
  transports: ['websocket'],
144
146
  reconnection: true
@@ -147,13 +149,14 @@ class SocketClient {
147
149
  }
148
150
  disconnect() {
149
151
  if (this.socket) {
152
+ this.socket.removeAllListeners();
150
153
  this.socket.disconnect();
151
154
  this.socket = null;
152
155
  }
153
156
  }
154
- emit(event, data) {
157
+ emit(event, ...args) {
155
158
  var _a;
156
- (_a = this.socket) === null || _a === void 0 ? void 0 : _a.emit(event, data);
159
+ (_a = this.socket) === null || _a === void 0 ? void 0 : _a.emit(event, ...args);
157
160
  }
158
161
  on(event, callback) {
159
162
  var _a;
@@ -171,79 +174,89 @@ class SocketClient {
171
174
  var _a;
172
175
  (_a = this.socket) === null || _a === void 0 ? void 0 : _a.offAny(callback);
173
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
+ }
174
185
  }
175
186
 
176
187
  const SocketContext = /*#__PURE__*/createContext({
177
- client: null
188
+ clients: {}
178
189
  });
179
190
  const SocketProvider = ({
180
- url,
181
- options,
182
- enabled: _enabled = true,
191
+ configs,
183
192
  children
184
193
  }) => {
185
- const clientRef = useRef(null);
186
- const memoizedOptions = useMemo(() => JSON.stringify(options), [options]);
194
+ const [clients, setClients] = useState({});
195
+ const memoizedConfigs = useMemo(() => JSON.stringify(configs), [configs]);
187
196
  useEffect(() => {
188
- var _a;
189
- if (!_enabled) {
190
- (_a = clientRef.current) === null || _a === void 0 ? void 0 : _a.disconnect();
191
- return;
192
- }
193
- if (clientRef.current) {
194
- clientRef.current.disconnect();
195
- }
196
- clientRef.current = new SocketClient(url, options);
197
- clientRef.current.connect();
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);
198
207
  return () => {
199
- var _a;
200
- (_a = clientRef.current) === null || _a === void 0 ? void 0 : _a.disconnect();
201
- clientRef.current = null;
208
+ Object.values(newClients).forEach(client => client.disconnect());
202
209
  };
203
- }, [url, memoizedOptions, _enabled]);
210
+ }, [memoizedConfigs]);
204
211
  return jsx(SocketContext.Provider, {
205
212
  value: {
206
- client: clientRef.current
213
+ clients
207
214
  },
208
215
  children: children
209
216
  });
210
217
  };
211
- const useSocketRaw = () => useContext(SocketContext);
218
+ const useSocketRaw = socketName => {
219
+ const {
220
+ clients
221
+ } = useContext(SocketContext);
222
+ return {
223
+ client: clients[socketName] || null
224
+ };
225
+ };
212
226
 
213
- const useSocketHandlers = handlers => {
227
+ const useSocketHandlers = (socketName, handlers) => {
214
228
  const {
215
229
  client
216
- } = useSocketRaw();
230
+ } = useSocketRaw(socketName);
217
231
  const handlersRef = useRef(handlers);
218
232
  useEffect(() => {
219
233
  handlersRef.current = handlers;
220
234
  }, [handlers]);
221
235
  useEffect(() => {
222
- if (!(client === null || client === void 0 ? void 0 : client.socket)) return;
223
- const socket = client.socket;
236
+ if (!client) return;
224
237
  const activeListeners = {};
225
238
  Object.keys(handlersRef.current).forEach(eventName => {
226
239
  const listener = (...args) => {
227
240
  var _a, _b;
228
- (_b = (_a = handlersRef.current)[eventName]) === null || _b === void 0 ? void 0 : _b.call(_a, ...args);
241
+ return (_b = (_a = handlersRef.current)[eventName]) === null || _b === void 0 ? void 0 : _b.call(_a, ...args);
229
242
  };
230
243
  activeListeners[eventName] = listener;
231
244
  if (eventName === 'onAny') {
232
- socket.onAny(listener);
245
+ client.onAny(listener);
233
246
  } else {
234
- socket.on(eventName, listener);
247
+ client.on(eventName, listener);
235
248
  }
236
249
  });
237
250
  return () => {
238
251
  Object.keys(activeListeners).forEach(eventName => {
239
252
  if (eventName === 'onAny') {
240
- socket.offAny(activeListeners[eventName]);
253
+ client.offAny(activeListeners[eventName]);
241
254
  } else {
242
- socket.off(eventName, activeListeners[eventName]);
255
+ client.off(eventName, activeListeners[eventName]);
243
256
  }
244
257
  });
245
258
  };
246
- }, [client === null || client === void 0 ? void 0 : client.socket]);
259
+ }, [client]);
247
260
  };
248
261
 
249
262
  export { Button, Notification, SocketProvider, useSocketHandlers, useSocketRaw };
package/package.json CHANGED
@@ -1,24 +1,17 @@
1
1
  {
2
2
  "name": "@itstepstorybookpackage/ui-kit",
3
- "version": "0.0.5",
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": {
@@ -1,14 +1,16 @@
1
1
  import { Socket, SocketOptions, ManagerOptions } from 'socket.io-client';
2
2
  export declare class SocketClient {
3
- socket: Socket | null;
3
+ private socket;
4
4
  readonly url: string;
5
5
  private options;
6
6
  constructor(url: string, options: Partial<ManagerOptions & SocketOptions>);
7
- connect(): Socket<import("@socket.io/component-emitter").DefaultEventsMap, import("@socket.io/component-emitter").DefaultEventsMap> | undefined;
7
+ connect(): Socket<import("@socket.io/component-emitter").DefaultEventsMap, import("@socket.io/component-emitter").DefaultEventsMap>;
8
8
  disconnect(): void;
9
- emit(event: string, data: any): void;
9
+ emit(event: string, ...args: any[]): void;
10
10
  on(event: string, callback: (...args: any[]) => void): void;
11
11
  off(event: string, callback?: (...args: any[]) => void): void;
12
12
  onAny(callback: (event: string, ...args: any[]) => void): void;
13
13
  offAny(callback?: (event: string, ...args: any[]) => void): void;
14
+ get isConnected(): boolean;
15
+ once(event: string, callback: (...args: any[]) => void): void;
14
16
  }
@@ -1,13 +1,16 @@
1
1
  import React from 'react';
2
2
  import { SocketClient } from './SocketClient';
3
- interface SocketProviderProps {
3
+ export interface SocketConfig {
4
4
  url: string;
5
- options: any;
5
+ options?: any;
6
6
  enabled?: boolean;
7
+ }
8
+ interface SocketProviderProps {
9
+ configs: Record<string, SocketConfig>;
7
10
  children: React.ReactNode;
8
11
  }
9
12
  export declare const SocketProvider: React.FC<SocketProviderProps>;
10
- export declare const useSocketRaw: () => {
11
- client: SocketClient | null;
13
+ export declare const useSocketRaw: (socketName: string) => {
14
+ client: SocketClient;
12
15
  };
13
16
  export {};
@@ -0,0 +1,3 @@
1
+ export * from './SocketProvider';
2
+ export * from './useSocketHandlers';
3
+ export * from './SocketClient';
@@ -1,3 +1,3 @@
1
1
  type HandlersMap = Record<string, (...args: any[]) => void>;
2
- export declare const useSocketHandlers: (handlers: HandlersMap) => void;
2
+ export declare const useSocketHandlers: (socketName: string, handlers: HandlersMap) => void;
3
3
  export {};