@mkgalaxy/utilities
Advanced tools
| import React, { ReactNode, Dispatch } from "react"; | ||
| import { AppState, AppAction } from "./GeneralAppReducer"; | ||
| interface AppProviderProps { | ||
| children: ReactNode; | ||
| } | ||
| export declare const GeneralAppProvider: ({ children, }: AppProviderProps) => React.JSX.Element; | ||
| export declare function useGeneralAppState(): AppState; | ||
| export declare function useGeneralAppDispatch(): Dispatch<AppAction>; | ||
| export {}; |
| var __assign = (this && this.__assign) || function () { | ||
| __assign = Object.assign || function(t) { | ||
| for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
| s = arguments[i]; | ||
| for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
| t[p] = s[p]; | ||
| } | ||
| return t; | ||
| }; | ||
| return __assign.apply(this, arguments); | ||
| }; | ||
| import { jsx as _jsx } from "react/jsx-runtime"; | ||
| import { createContext, useReducer, useContext, } from "react"; | ||
| import { reducer, initialState, } from "./GeneralAppReducer"; | ||
| /* | ||
| USAGE EXAMPLE: | ||
| 1. Wrap your app or component tree with GeneralAppProvider: | ||
| // In your main App.tsx or _app.tsx (Next.js) | ||
| import { GeneralAppProvider } from './context/GeneralAppContext'; | ||
| function App() { | ||
| return ( | ||
| <GeneralAppProvider> | ||
| <YourMainComponent /> | ||
| <AnotherComponent /> | ||
| </GeneralAppProvider> | ||
| ); | ||
| } | ||
| 2. Use the hooks in any child component: | ||
| import { useGeneralAppState, useGeneralAppDispatch, useAge, useGender, useApp } from './context/AppContext'; | ||
| // Method 1: Get full state and dispatch separately | ||
| function MyComponent1() { | ||
| const state = useGeneralAppState(); // { age: 30, gender: "male" } | ||
| const dispatch = useGeneralAppDispatch(); | ||
| return ( | ||
| <div> | ||
| <p>Age: {state.dynamicData.age}</p> | ||
| <button onClick={() => dispatch({ type: "SET_DYNAMIC_VALUE", payload: { key: "age", value: 25 } })}> | ||
| Update Age | ||
| </button> | ||
| </div> | ||
| ); | ||
| } | ||
| */ | ||
| // Define context types | ||
| var StateContext = createContext(undefined); | ||
| var DispatchContext = createContext(undefined); | ||
| export var GeneralAppProvider = function (_a) { | ||
| var children = _a.children; | ||
| var _b = useReducer(reducer, initialState), state = _b[0], dispatch = _b[1]; | ||
| return (_jsx(StateContext.Provider, __assign({ value: state }, { children: _jsx(DispatchContext.Provider, __assign({ value: dispatch }, { children: children })) }))); | ||
| }; | ||
| // Hook to access the current state | ||
| // Returns: { age: number, gender: string } | ||
| export function useGeneralAppState() { | ||
| var context = useContext(StateContext); | ||
| if (context === undefined) { | ||
| throw new Error("useGeneralAppState must be used within a GeneralAppProvider"); | ||
| } | ||
| return context; | ||
| } | ||
| // Hook to access the dispatch function for updating state | ||
| // Usage: dispatch({ type: "UPDATE_AGE", payload: 30 }) | ||
| // dispatch({ type: "UPDATE_GENDER", payload: "male" }) | ||
| export function useGeneralAppDispatch() { | ||
| var context = useContext(DispatchContext); | ||
| if (context === undefined) { | ||
| throw new Error("useGeneralAppDispatch must be used within a GeneralAppProvider"); | ||
| } | ||
| return context; | ||
| } |
| export interface AppState { | ||
| dynamicData: { | ||
| [key: string]: any; | ||
| }; | ||
| } | ||
| export type AppAction = { | ||
| type: "SET_DYNAMIC_VALUE"; | ||
| payload: { | ||
| key: string; | ||
| value: any; | ||
| }; | ||
| } | { | ||
| type: "UPDATE_DYNAMIC_VALUES"; | ||
| payload: { | ||
| [key: string]: any; | ||
| }; | ||
| }; | ||
| export declare const initialState: AppState; | ||
| export declare function reducer(state: AppState, action: AppAction): AppState; |
| var __assign = (this && this.__assign) || function () { | ||
| __assign = Object.assign || function(t) { | ||
| for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
| s = arguments[i]; | ||
| for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
| t[p] = s[p]; | ||
| } | ||
| return t; | ||
| }; | ||
| return __assign.apply(this, arguments); | ||
| }; | ||
| import { produce } from "immer"; | ||
| export var initialState = { | ||
| dynamicData: {}, | ||
| }; | ||
| export function reducer(state, action) { | ||
| return produce(state, function (draft) { | ||
| switch (action.type) { | ||
| case "SET_DYNAMIC_VALUE": { | ||
| draft.dynamicData[action.payload.key] = action.payload.value; | ||
| break; | ||
| } | ||
| case "UPDATE_DYNAMIC_VALUES": { | ||
| // Merge new values with existing dynamicData | ||
| draft.dynamicData = __assign(__assign({}, draft.dynamicData), action.payload); | ||
| break; | ||
| } | ||
| default: | ||
| return; // Immer will return unchanged state | ||
| } | ||
| }); | ||
| } |
| /// <reference types="react" /> | ||
| export declare function useGeneralApp(): { | ||
| generalState: import("./GeneralAppReducer").AppState; | ||
| generalDispatch: import("react").Dispatch<import("./GeneralAppReducer").AppAction>; | ||
| setDynamicValue: (key: string, value: any) => void; | ||
| updateDynamicValue: (obj: Record<string, unknown>) => void; | ||
| }; |
| import { useCallback } from "react"; | ||
| import { useGeneralAppDispatch, useGeneralAppState } from "./GeneralAppContext"; | ||
| // Custom hook that combines state and dispatch for convenience | ||
| // Usage: const { generalState, generalDispatch, setDynamicValue, updateDynamicValue } = useGeneralApp(); | ||
| export function useGeneralApp() { | ||
| var generalState = useGeneralAppState(); | ||
| var generalDispatch = useGeneralAppDispatch(); | ||
| var setDynamicValue = useCallback(function (key, value) { | ||
| generalDispatch({ type: "SET_DYNAMIC_VALUE", payload: { key: key, value: value } }); | ||
| }, [generalDispatch]); | ||
| var updateDynamicValue = useCallback(function (obj) { | ||
| generalDispatch({ type: "UPDATE_DYNAMIC_VALUES", payload: obj }); | ||
| }, [generalDispatch]); | ||
| return { | ||
| generalState: generalState, | ||
| generalDispatch: generalDispatch, | ||
| setDynamicValue: setDynamicValue, | ||
| updateDynamicValue: updateDynamicValue, | ||
| }; | ||
| } |
| import React, { ReactNode } from "react"; | ||
| import { RNWebSocketClient } from "./wsClients"; | ||
| interface SocketContextType { | ||
| socketClient: RNWebSocketClient | null; | ||
| isConnected: boolean; | ||
| } | ||
| interface SocketProviderProps { | ||
| children: ReactNode; | ||
| serverWSUrl: string; | ||
| } | ||
| export declare const SocketProvider: React.FC<SocketProviderProps>; | ||
| export declare const useSocketClient: () => SocketContextType; | ||
| export {}; |
| var __assign = (this && this.__assign) || function () { | ||
| __assign = Object.assign || function(t) { | ||
| for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
| s = arguments[i]; | ||
| for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
| t[p] = s[p]; | ||
| } | ||
| return t; | ||
| }; | ||
| return __assign.apply(this, arguments); | ||
| }; | ||
| import { jsx as _jsx } from "react/jsx-runtime"; | ||
| import React, { createContext, useContext, useEffect, useMemo, } from "react"; | ||
| import { RNWebSocketClient } from "./wsClients"; | ||
| var SocketContext = createContext(undefined); | ||
| export var SocketProvider = function (_a) { | ||
| var children = _a.children, serverWSUrl = _a.serverWSUrl; | ||
| var _b = React.useState(null), socketClient = _b[0], setSocketClient = _b[1]; | ||
| var _c = React.useState(false), isConnected = _c[0], setIsConnected = _c[1]; | ||
| useEffect(function () { | ||
| console.log("Initializing global socket connection..."); | ||
| // Initialize socket connection | ||
| var client = new RNWebSocketClient({ | ||
| url: serverWSUrl, | ||
| // query: { room: 'default' }, | ||
| maxBackoffMs: 8000, | ||
| pingIntervalMs: 25000, | ||
| }); | ||
| // Set up connection event listeners | ||
| var unsubConnect = client.on("connect", function () { | ||
| console.log("✅ Global WebSocket connected"); | ||
| setIsConnected(true); | ||
| }); | ||
| var unsubDisconnect = client.on("disconnect", function () { | ||
| console.log("❌ Global WebSocket disconnected"); | ||
| setIsConnected(false); | ||
| }); | ||
| var unsubError = client.on("error", function (error) { | ||
| console.log("🚨 Global WebSocket error:", error); | ||
| setIsConnected(false); | ||
| }); | ||
| var unsubWelcome = client.on("welcome", function (message) { | ||
| console.log("👋 Global WebSocket welcome:", message); | ||
| }); | ||
| // Connect the socket | ||
| client.connect(); | ||
| setSocketClient(client); | ||
| return function () { | ||
| console.log("Cleaning up global socket connection..."); | ||
| unsubConnect(); | ||
| unsubDisconnect(); | ||
| unsubError(); | ||
| unsubWelcome(); | ||
| client.close(); | ||
| }; | ||
| }, [serverWSUrl]); | ||
| var contextValue = useMemo(function () { return ({ socketClient: socketClient, isConnected: isConnected }); }, [socketClient, isConnected]); | ||
| return (_jsx(SocketContext.Provider, __assign({ value: contextValue }, { children: children }))); | ||
| }; | ||
| export var useSocketClient = function () { | ||
| var context = useContext(SocketContext); | ||
| if (context === undefined) { | ||
| throw new Error("useSocketClient must be used within a SocketProvider"); | ||
| } | ||
| return context; | ||
| }; |
| type Listener = (msg: unknown) => void; | ||
| type Options = { | ||
| url: string; | ||
| query?: Record<string, string | number | boolean>; | ||
| maxBackoffMs?: number; | ||
| pingIntervalMs?: number; | ||
| }; | ||
| export declare class RNWebSocketClient { | ||
| private url; | ||
| private socket?; | ||
| private backoff; | ||
| private maxBackoff; | ||
| private listeners; | ||
| private pingTimer?; | ||
| private pingEvery; | ||
| private isExplicitlyClosed; | ||
| constructor(opts: Options); | ||
| connect(): void; | ||
| private open; | ||
| close(): void; | ||
| send(obj: Record<string, unknown>): void; | ||
| on(type: string | "*", fn: Listener): () => void; | ||
| off(type: string | "*", fn: Listener): void; | ||
| private emit; | ||
| } | ||
| export {}; |
| var RNWebSocketClient = /** @class */ (function () { | ||
| function RNWebSocketClient(opts) { | ||
| var _a, _b; | ||
| this.backoff = 500; | ||
| this.listeners = new Map(); | ||
| this.isExplicitlyClosed = false; | ||
| var qs = opts.query | ||
| ? "?" + | ||
| Object.entries(opts.query) | ||
| .map(function (_a) { | ||
| var k = _a[0], v = _a[1]; | ||
| return "".concat(encodeURIComponent(k), "=").concat(encodeURIComponent(String(v))); | ||
| }) | ||
| .join("&") | ||
| : ""; | ||
| this.url = opts.url + qs; | ||
| this.maxBackoff = (_a = opts.maxBackoffMs) !== null && _a !== void 0 ? _a : 10000; | ||
| this.pingEvery = (_b = opts.pingIntervalMs) !== null && _b !== void 0 ? _b : 25000; | ||
| } | ||
| RNWebSocketClient.prototype.connect = function () { | ||
| this.isExplicitlyClosed = false; | ||
| this.open(); | ||
| }; | ||
| RNWebSocketClient.prototype.open = function () { | ||
| var _this = this; | ||
| console.log("🔄 Attempting to connect to:", this.url); | ||
| this.socket = new WebSocket(this.url); | ||
| this.socket.onopen = function () { | ||
| console.log("✅ WebSocket connection opened"); | ||
| _this.backoff = 500; | ||
| _this.emit("connect", { status: "connected", url: _this.url }); | ||
| // start client-side ping (optional; server also pings) | ||
| if (_this.pingTimer) { | ||
| clearInterval(_this.pingTimer); | ||
| } | ||
| _this.pingTimer = setInterval(function () { return _this.send({ type: "ping" }); }, _this.pingEvery); | ||
| }; | ||
| this.socket.onmessage = function (evt) { | ||
| var _a; | ||
| var data = typeof evt.data === "string" ? evt.data : ""; | ||
| console.log("📨 Raw WebSocket message received:", data); | ||
| try { | ||
| var msg = JSON.parse(data); | ||
| var type = (_a = msg === null || msg === void 0 ? void 0 : msg.type) !== null && _a !== void 0 ? _a : "*"; | ||
| console.log("📨 Parsed message:", msg, "type:", type); | ||
| _this.emit(type, msg); | ||
| _this.emit("*", msg); // wildcard listeners | ||
| } | ||
| catch (error) { | ||
| console.log("❌ Failed to parse JSON message:", data, "Error:", error); | ||
| // ignore bad JSON but still emit raw data | ||
| _this.emit("raw", { data: data }); | ||
| } | ||
| }; | ||
| this.socket.onerror = function (event) { | ||
| console.log("🚨 WebSocket error event:", event); | ||
| _this.emit("error", { | ||
| event: event, | ||
| url: _this.url, | ||
| message: "WebSocket error occurred", | ||
| }); | ||
| }; | ||
| this.socket.onclose = function (event) { | ||
| console.log("❌ WebSocket connection closed:", event.code, event.reason); | ||
| _this.emit("disconnect", { | ||
| code: event.code, | ||
| reason: event.reason, | ||
| url: _this.url, | ||
| }); | ||
| if (_this.pingTimer) { | ||
| clearInterval(_this.pingTimer); | ||
| } | ||
| if (_this.isExplicitlyClosed) | ||
| return; | ||
| // exponential backoff | ||
| console.log("\uD83D\uDD04 Reconnecting in ".concat(_this.backoff, "ms...")); | ||
| setTimeout(function () { return _this.open(); }, _this.backoff); | ||
| _this.backoff = Math.min(_this.backoff * 2, _this.maxBackoff); | ||
| }; | ||
| }; | ||
| RNWebSocketClient.prototype.close = function () { | ||
| var _a; | ||
| this.isExplicitlyClosed = true; | ||
| if (this.pingTimer) { | ||
| clearInterval(this.pingTimer); | ||
| } | ||
| (_a = this.socket) === null || _a === void 0 ? void 0 : _a.close(); | ||
| }; | ||
| RNWebSocketClient.prototype.send = function (obj) { | ||
| var _a; | ||
| var json = JSON.stringify(obj); | ||
| console.log("📤 Sending WebSocket message:", json); | ||
| if (this.socket && this.socket.readyState === 1) { | ||
| // 1 = WebSocket.OPEN | ||
| this.socket.send(json); | ||
| } | ||
| else { | ||
| console.log("⚠️ WebSocket not ready, readyState:", (_a = this.socket) === null || _a === void 0 ? void 0 : _a.readyState); | ||
| } | ||
| }; | ||
| RNWebSocketClient.prototype.on = function (type, fn) { | ||
| var _this = this; | ||
| if (!this.listeners.has(type)) | ||
| this.listeners.set(type, new Set()); | ||
| this.listeners.get(type).add(fn); | ||
| return function () { return _this.off(type, fn); }; | ||
| }; | ||
| RNWebSocketClient.prototype.off = function (type, fn) { | ||
| var _a; | ||
| (_a = this.listeners.get(type)) === null || _a === void 0 ? void 0 : _a.delete(fn); | ||
| }; | ||
| RNWebSocketClient.prototype.emit = function (type, msg) { | ||
| var _a; | ||
| (_a = this.listeners.get(type)) === null || _a === void 0 ? void 0 : _a.forEach(function (fn) { return fn(msg); }); | ||
| }; | ||
| return RNWebSocketClient; | ||
| }()); | ||
| export { RNWebSocketClient }; |
+3
-0
@@ -7,1 +7,4 @@ export { default as Speak } from "./Speak/Speak"; | ||
| export { getDeepSeekResponse } from "./ai/deepseek"; | ||
| export { GeneralAppProvider, useGeneralAppState, useGeneralAppDispatch, } from "./GeneralAppContext/GeneralAppContext"; | ||
| export { useGeneralApp } from "./GeneralAppContext/useGeneralApp"; | ||
| export { SocketProvider, useSocketClient } from "./SocketContext/SocketContext"; |
+3
-0
@@ -7,1 +7,4 @@ export { default as Speak } from "./Speak/Speak"; | ||
| export { getDeepSeekResponse } from "./ai/deepseek"; | ||
| export { GeneralAppProvider, useGeneralAppState, useGeneralAppDispatch, } from "./GeneralAppContext/GeneralAppContext"; | ||
| export { useGeneralApp } from "./GeneralAppContext/useGeneralApp"; | ||
| export { SocketProvider, useSocketClient } from "./SocketContext/SocketContext"; |
+1
-1
| { | ||
| "name": "@mkgalaxy/utilities", | ||
| "version": "0.2.3", | ||
| "version": "0.2.4", | ||
| "main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "publishConfig": { |
AI-detected potential code anomaly
Supply chain riskAI has identified unusual behaviors that may pose a security risk.
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
AI-detected potential code anomaly
Supply chain riskAI has identified unusual behaviors that may pose a security risk.
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
82579
23.4%31
47.62%1348
40.12%