@itstepstorybookpackage/ui-kit 0.0.4 → 0.0.5

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 { useRef, useMemo, useEffect, useContext, createContext } from 'react';
5
+ import { io } from 'socket.io-client';
4
6
 
5
7
  /******************************************************************************
6
8
  Copyright (c) Microsoft Corporation.
@@ -128,4 +130,120 @@ function Notification(props) {
128
130
  });
129
131
  }
130
132
 
131
- export { Button, Notification };
133
+ class SocketClient {
134
+ constructor(url, options) {
135
+ this.socket = null;
136
+ this.url = url;
137
+ this.options = options;
138
+ }
139
+ connect() {
140
+ var _a;
141
+ if ((_a = this.socket) === null || _a === void 0 ? void 0 : _a.connected) return;
142
+ this.socket = io(this.url, Object.assign({
143
+ transports: ['websocket'],
144
+ reconnection: true
145
+ }, this.options));
146
+ return this.socket;
147
+ }
148
+ disconnect() {
149
+ if (this.socket) {
150
+ this.socket.disconnect();
151
+ this.socket = null;
152
+ }
153
+ }
154
+ emit(event, data) {
155
+ var _a;
156
+ (_a = this.socket) === null || _a === void 0 ? void 0 : _a.emit(event, data);
157
+ }
158
+ on(event, callback) {
159
+ var _a;
160
+ (_a = this.socket) === null || _a === void 0 ? void 0 : _a.on(event, callback);
161
+ }
162
+ off(event, callback) {
163
+ var _a;
164
+ (_a = this.socket) === null || _a === void 0 ? void 0 : _a.off(event, callback);
165
+ }
166
+ onAny(callback) {
167
+ var _a;
168
+ (_a = this.socket) === null || _a === void 0 ? void 0 : _a.onAny(callback);
169
+ }
170
+ offAny(callback) {
171
+ var _a;
172
+ (_a = this.socket) === null || _a === void 0 ? void 0 : _a.offAny(callback);
173
+ }
174
+ }
175
+
176
+ const SocketContext = /*#__PURE__*/createContext({
177
+ client: null
178
+ });
179
+ const SocketProvider = ({
180
+ url,
181
+ options,
182
+ enabled: _enabled = true,
183
+ children
184
+ }) => {
185
+ const clientRef = useRef(null);
186
+ const memoizedOptions = useMemo(() => JSON.stringify(options), [options]);
187
+ 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();
198
+ return () => {
199
+ var _a;
200
+ (_a = clientRef.current) === null || _a === void 0 ? void 0 : _a.disconnect();
201
+ clientRef.current = null;
202
+ };
203
+ }, [url, memoizedOptions, _enabled]);
204
+ return jsx(SocketContext.Provider, {
205
+ value: {
206
+ client: clientRef.current
207
+ },
208
+ children: children
209
+ });
210
+ };
211
+ const useSocketRaw = () => useContext(SocketContext);
212
+
213
+ const useSocketHandlers = handlers => {
214
+ const {
215
+ client
216
+ } = useSocketRaw();
217
+ const handlersRef = useRef(handlers);
218
+ useEffect(() => {
219
+ handlersRef.current = handlers;
220
+ }, [handlers]);
221
+ useEffect(() => {
222
+ if (!(client === null || client === void 0 ? void 0 : client.socket)) return;
223
+ const socket = client.socket;
224
+ const activeListeners = {};
225
+ Object.keys(handlersRef.current).forEach(eventName => {
226
+ const listener = (...args) => {
227
+ var _a, _b;
228
+ (_b = (_a = handlersRef.current)[eventName]) === null || _b === void 0 ? void 0 : _b.call(_a, ...args);
229
+ };
230
+ activeListeners[eventName] = listener;
231
+ if (eventName === 'onAny') {
232
+ socket.onAny(listener);
233
+ } else {
234
+ socket.on(eventName, listener);
235
+ }
236
+ });
237
+ return () => {
238
+ Object.keys(activeListeners).forEach(eventName => {
239
+ if (eventName === 'onAny') {
240
+ socket.offAny(activeListeners[eventName]);
241
+ } else {
242
+ socket.off(eventName, activeListeners[eventName]);
243
+ }
244
+ });
245
+ };
246
+ }, [client === null || client === void 0 ? void 0 : client.socket]);
247
+ };
248
+
249
+ export { Button, Notification, SocketProvider, useSocketHandlers, useSocketRaw };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@itstepstorybookpackage/ui-kit",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "main": "./index.esm.js",
5
5
  "module": "./index.esm.js",
6
6
  "react-native": "./index.esm.js",
@@ -25,7 +25,8 @@
25
25
  "react": "*",
26
26
  "react-native": "*",
27
27
  "react-native-svg": "*",
28
- "styled-components": "*"
28
+ "styled-components": "*",
29
+ "socket.io-client": "*"
29
30
  },
30
31
  "type": "module"
31
32
  }
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,14 @@
1
+ import { Socket, SocketOptions, ManagerOptions } from 'socket.io-client';
2
+ export declare class SocketClient {
3
+ socket: Socket | null;
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> | undefined;
8
+ disconnect(): void;
9
+ emit(event: string, data: 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
+ }
@@ -0,0 +1,13 @@
1
+ import React from 'react';
2
+ import { SocketClient } from './SocketClient';
3
+ interface SocketProviderProps {
4
+ url: string;
5
+ options: any;
6
+ enabled?: boolean;
7
+ children: React.ReactNode;
8
+ }
9
+ export declare const SocketProvider: React.FC<SocketProviderProps>;
10
+ export declare const useSocketRaw: () => {
11
+ client: SocketClient | null;
12
+ };
13
+ export {};
@@ -0,0 +1,3 @@
1
+ type HandlersMap = Record<string, (...args: any[]) => void>;
2
+ export declare const useSocketHandlers: (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;