@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 {
|
|
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,
|
|
157
|
+
emit(event, ...args) {
|
|
155
158
|
var _a;
|
|
156
|
-
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.emit(event,
|
|
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
|
-
|
|
188
|
+
clients: {}
|
|
178
189
|
});
|
|
179
190
|
const SocketProvider = ({
|
|
180
|
-
|
|
181
|
-
options,
|
|
182
|
-
enabled: _enabled = true,
|
|
191
|
+
configs,
|
|
183
192
|
children
|
|
184
193
|
}) => {
|
|
185
|
-
const
|
|
186
|
-
const
|
|
194
|
+
const [clients, setClients] = useState({});
|
|
195
|
+
const memoizedConfigs = useMemo(() => JSON.stringify(configs), [configs]);
|
|
187
196
|
useEffect(() => {
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
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
|
-
|
|
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
|
-
}, [
|
|
210
|
+
}, [memoizedConfigs]);
|
|
204
211
|
return jsx(SocketContext.Provider, {
|
|
205
212
|
value: {
|
|
206
|
-
|
|
213
|
+
clients
|
|
207
214
|
},
|
|
208
215
|
children: children
|
|
209
216
|
});
|
|
210
217
|
};
|
|
211
|
-
const useSocketRaw =
|
|
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 (!
|
|
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
|
-
|
|
245
|
+
client.onAny(listener);
|
|
233
246
|
} else {
|
|
234
|
-
|
|
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
|
-
|
|
253
|
+
client.offAny(activeListeners[eventName]);
|
|
241
254
|
} else {
|
|
242
|
-
|
|
255
|
+
client.off(eventName, activeListeners[eventName]);
|
|
243
256
|
}
|
|
244
257
|
});
|
|
245
258
|
};
|
|
246
|
-
}, [client
|
|
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.
|
|
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
|
|
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
|
|
7
|
+
connect(): Socket<import("@socket.io/component-emitter").DefaultEventsMap, import("@socket.io/component-emitter").DefaultEventsMap>;
|
|
8
8
|
disconnect(): void;
|
|
9
|
-
emit(event: string,
|
|
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
|
|
3
|
+
export interface SocketConfig {
|
|
4
4
|
url: string;
|
|
5
|
-
options
|
|
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
|
|
13
|
+
export declare const useSocketRaw: (socketName: string) => {
|
|
14
|
+
client: SocketClient;
|
|
12
15
|
};
|
|
13
16
|
export {};
|