quidproquo-web-react 0.0.199 → 0.0.200

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.
Files changed (40) hide show
  1. package/lib/commonjs/websocket/WebSocketAuthSync/hooks/useWebsocketAuthSync.d.ts +1 -1
  2. package/lib/esm/hooks/index.d.ts +3 -0
  3. package/lib/esm/hooks/index.js +3 -0
  4. package/lib/esm/hooks/useFastCallback.d.ts +2 -0
  5. package/lib/esm/hooks/useFastCallback.js +15 -0
  6. package/lib/esm/hooks/useRunEvery.d.ts +1 -0
  7. package/lib/esm/hooks/useRunEvery.js +18 -0
  8. package/lib/esm/hooks/useThrottledMemo.d.ts +1 -0
  9. package/lib/esm/hooks/useThrottledMemo.js +28 -0
  10. package/lib/esm/index.d.ts +2 -0
  11. package/lib/esm/index.js +2 -0
  12. package/lib/esm/websocket/WebSocketAuthSync/WebSocketAuthSync.d.ts +7 -0
  13. package/lib/esm/websocket/WebSocketAuthSync/WebSocketAuthSync.js +12 -0
  14. package/lib/esm/websocket/WebSocketAuthSync/hooks/index.d.ts +2 -0
  15. package/lib/esm/websocket/WebSocketAuthSync/hooks/index.js +2 -0
  16. package/lib/esm/websocket/WebSocketAuthSync/hooks/useWebsocketAuthSync.d.ts +2 -0
  17. package/lib/esm/websocket/WebSocketAuthSync/hooks/useWebsocketAuthSync.js +33 -0
  18. package/lib/esm/websocket/WebSocketAuthSync/hooks/useWebsocketPingPong.d.ts +1 -0
  19. package/lib/esm/websocket/WebSocketAuthSync/hooks/useWebsocketPingPong.js +17 -0
  20. package/lib/esm/websocket/WebSocketAuthSync/index.d.ts +1 -0
  21. package/lib/esm/websocket/WebSocketAuthSync/index.js +1 -0
  22. package/lib/esm/websocket/WebsocketContext.d.ts +3 -0
  23. package/lib/esm/websocket/WebsocketContext.js +2 -0
  24. package/lib/esm/websocket/WebsocketProvider.d.ts +6 -0
  25. package/lib/esm/websocket/WebsocketProvider.js +7 -0
  26. package/lib/esm/websocket/hooks/index.d.ts +5 -0
  27. package/lib/esm/websocket/hooks/index.js +5 -0
  28. package/lib/esm/websocket/hooks/useSubscribeToWebSocketEvent.d.ts +3 -0
  29. package/lib/esm/websocket/hooks/useSubscribeToWebSocketEvent.js +13 -0
  30. package/lib/esm/websocket/hooks/useSubscribeToWebsocket.d.ts +2 -0
  31. package/lib/esm/websocket/hooks/useSubscribeToWebsocket.js +13 -0
  32. package/lib/esm/websocket/hooks/useWebsocketApi.d.ts +1 -0
  33. package/lib/esm/websocket/hooks/useWebsocketApi.js +6 -0
  34. package/lib/esm/websocket/hooks/useWebsocketManagement.d.ts +2 -0
  35. package/lib/esm/websocket/hooks/useWebsocketManagement.js +13 -0
  36. package/lib/esm/websocket/hooks/useWebsocketSendEvent.d.ts +2 -0
  37. package/lib/esm/websocket/hooks/useWebsocketSendEvent.js +11 -0
  38. package/lib/esm/websocket/index.d.ts +3 -0
  39. package/lib/esm/websocket/index.js +3 -0
  40. package/package.json +12 -8
@@ -1,2 +1,2 @@
1
- import { AuthenticationInfo } from 'quidproquo';
1
+ import { AuthenticationInfo } from 'quidproquo-core';
2
2
  export declare const useWebsocketAuthSync: (accessToken: AuthenticationInfo['accessToken']) => void;
@@ -0,0 +1,3 @@
1
+ export * from './useThrottledMemo';
2
+ export * from './useRunEvery';
3
+ export * from './useFastCallback';
@@ -0,0 +1,3 @@
1
+ export * from './useThrottledMemo';
2
+ export * from './useRunEvery';
3
+ export * from './useFastCallback';
@@ -0,0 +1,2 @@
1
+ import { DependencyList } from 'react';
2
+ export declare function useFastCallback<T extends Function>(callback: T, deps?: DependencyList): T;
@@ -0,0 +1,15 @@
1
+ import { useRef, useCallback } from 'react';
2
+ export function useFastCallback(callback, deps) {
3
+ const callbackRef = useRef(callback);
4
+ const depsRef = useRef(deps);
5
+ // If the deps have changed, update the callback ref
6
+ // We support undefined deps, which means they are always updated
7
+ // This is different to [] where its never updated.
8
+ const hasDepsChanged = !depsRef.current || !deps || depsRef.current.some((dep, index) => dep !== deps[index]);
9
+ if (hasDepsChanged) {
10
+ callbackRef.current = callback;
11
+ }
12
+ depsRef.current = deps;
13
+ const memoFunc = useCallback((...args) => callbackRef.current(...args), []);
14
+ return memoFunc;
15
+ }
@@ -0,0 +1 @@
1
+ export declare function useRunEvery<T>(func: () => T, interval: number): T;
@@ -0,0 +1,18 @@
1
+ import { useState, useEffect, useRef } from 'react';
2
+ export function useRunEvery(func, interval) {
3
+ const [value, setValue] = useState(func);
4
+ const intervalRef = useRef(null);
5
+ useEffect(() => {
6
+ intervalRef.current = setInterval(() => {
7
+ const newValue = func();
8
+ setValue(newValue);
9
+ }, interval * 1000);
10
+ return () => {
11
+ if (intervalRef.current) {
12
+ clearInterval(intervalRef.current);
13
+ intervalRef.current = null;
14
+ }
15
+ };
16
+ }, [interval]);
17
+ return value;
18
+ }
@@ -0,0 +1 @@
1
+ export declare function useThrottledMemo<T>(factory: () => T, deps: any[], delaySeconds?: number): T;
@@ -0,0 +1,28 @@
1
+ import { useState, useEffect, useRef } from 'react';
2
+ export function useThrottledMemo(factory, deps, delaySeconds = 1) {
3
+ const [value, setValue] = useState(factory);
4
+ const isFirstRun = useRef(true);
5
+ const timeoutRef = useRef(null);
6
+ useEffect(() => {
7
+ // This skips the throttling on the initial mount
8
+ if (isFirstRun.current) {
9
+ isFirstRun.current = false;
10
+ return;
11
+ }
12
+ if (timeoutRef.current) {
13
+ clearTimeout(timeoutRef.current);
14
+ }
15
+ timeoutRef.current = setTimeout(() => {
16
+ setValue(factory);
17
+ timeoutRef.current = null;
18
+ }, delaySeconds * 1000);
19
+ // Cleanup on component unmount or when dependencies change
20
+ return () => {
21
+ if (timeoutRef.current) {
22
+ clearTimeout(timeoutRef.current);
23
+ timeoutRef.current = null;
24
+ }
25
+ };
26
+ }, [...deps, delaySeconds]);
27
+ return value;
28
+ }
@@ -0,0 +1,2 @@
1
+ export * from './websocket';
2
+ export * from './hooks';
@@ -0,0 +1,2 @@
1
+ export * from './websocket';
2
+ export * from './hooks';
@@ -0,0 +1,7 @@
1
+ /// <reference types="react" />
2
+ import { AuthenticationInfo } from 'quidproquo-core';
3
+ export type WebSocketAuthSyncProps = {
4
+ children: React.ReactNode;
5
+ accessToken: AuthenticationInfo['accessToken'];
6
+ };
7
+ export declare const WebSocketAuthSync: import("react").NamedExoticComponent<WebSocketAuthSyncProps>;
@@ -0,0 +1,12 @@
1
+ import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
2
+ import { memo } from 'react';
3
+ import { useWebsocketAuthSync, useWebsocketPingPong } from './hooks';
4
+ const component = ({ children, accessToken }) => {
5
+ useWebsocketAuthSync(accessToken);
6
+ // Keep the active websocket alive
7
+ useWebsocketPingPong();
8
+ // This fragment is not useless!
9
+ // eslint-disable-next-line react/jsx-no-useless-fragment
10
+ return _jsx(_Fragment, { children: children });
11
+ };
12
+ export const WebSocketAuthSync = memo(component);
@@ -0,0 +1,2 @@
1
+ export * from './useWebsocketAuthSync';
2
+ export * from './useWebsocketPingPong';
@@ -0,0 +1,2 @@
1
+ export * from './useWebsocketAuthSync';
2
+ export * from './useWebsocketPingPong';
@@ -0,0 +1,2 @@
1
+ import { AuthenticationInfo } from 'quidproquo-core';
2
+ export declare const useWebsocketAuthSync: (accessToken: AuthenticationInfo['accessToken']) => void;
@@ -0,0 +1,33 @@
1
+ import { useEffect } from 'react';
2
+ import { WebsocketServiceEvent } from 'quidproquo-web';
3
+ import { WebsocketClientMessageEventType, } from 'quidproquo-webserver';
4
+ import { useSubscribeToWebsocket, useWebsocketApi, useWebsocketSendEvent } from '../../hooks';
5
+ import { useFastCallback } from '../../../hooks';
6
+ export const useWebsocketAuthSync = (accessToken) => {
7
+ const sendMessage = useWebsocketSendEvent();
8
+ const websocketApi = useWebsocketApi();
9
+ const updateAuthTokens = useFastCallback(() => {
10
+ if (!websocketApi?.isConnected()) {
11
+ return;
12
+ }
13
+ if (accessToken) {
14
+ const authMessage = {
15
+ type: WebsocketClientMessageEventType.Authenticate,
16
+ payload: {
17
+ accessToken: accessToken,
18
+ },
19
+ };
20
+ sendMessage(authMessage);
21
+ }
22
+ else {
23
+ const authMessage = {
24
+ type: WebsocketClientMessageEventType.Unauthenticate,
25
+ };
26
+ sendMessage(authMessage);
27
+ }
28
+ });
29
+ // Sync the tokens in on open
30
+ useSubscribeToWebsocket(WebsocketServiceEvent.OPEN, updateAuthTokens);
31
+ // Sync the tokens when they change
32
+ useEffect(updateAuthTokens, [accessToken, websocketApi]);
33
+ };
@@ -0,0 +1 @@
1
+ export declare const useWebsocketPingPong: () => void;
@@ -0,0 +1,17 @@
1
+ import { useEffect } from 'react';
2
+ import { useWebsocketSendEvent } from '../../hooks';
3
+ import { WebsocketClientMessageEventType, } from 'quidproquo-webserver';
4
+ export const useWebsocketPingPong = () => {
5
+ const sendMessage = useWebsocketSendEvent();
6
+ useEffect(() => {
7
+ const intervalId = setInterval(() => {
8
+ const pingEvent = {
9
+ type: WebsocketClientMessageEventType.Ping,
10
+ };
11
+ sendMessage(pingEvent);
12
+ }, 8 * 60 * 1000);
13
+ return () => {
14
+ clearInterval(intervalId);
15
+ };
16
+ }, [sendMessage]);
17
+ };
@@ -0,0 +1 @@
1
+ export * from './WebSocketAuthSync';
@@ -0,0 +1 @@
1
+ export * from './WebSocketAuthSync';
@@ -0,0 +1,3 @@
1
+ /// <reference types="react" />
2
+ import { WebsocketService } from 'quidproquo-web';
3
+ export declare const WebSocketContext: import("react").Context<WebsocketService | null>;
@@ -0,0 +1,2 @@
1
+ import { createContext } from 'react';
2
+ export const WebSocketContext = createContext(null);
@@ -0,0 +1,6 @@
1
+ /// <reference types="react" />
2
+ export type WebSocketContextProps = {
3
+ children: React.ReactNode;
4
+ wsUrl: string;
5
+ };
6
+ export declare const WebsocketProvider: React.FC<WebSocketContextProps>;
@@ -0,0 +1,7 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { useWebsocketManagement } from './hooks';
3
+ import { WebSocketContext } from './WebsocketContext';
4
+ export const WebsocketProvider = ({ children, wsUrl }) => {
5
+ const websocketManagement = useWebsocketManagement(wsUrl);
6
+ return (_jsx(WebSocketContext.Provider, { value: websocketManagement, children: children }));
7
+ };
@@ -0,0 +1,5 @@
1
+ export * from './useSubscribeToWebsocket';
2
+ export * from './useSubscribeToWebSocketEvent';
3
+ export * from './useWebsocketApi';
4
+ export * from './useWebsocketManagement';
5
+ export * from './useWebsocketSendEvent';
@@ -0,0 +1,5 @@
1
+ export * from './useSubscribeToWebsocket';
2
+ export * from './useSubscribeToWebSocketEvent';
3
+ export * from './useWebsocketApi';
4
+ export * from './useWebsocketManagement';
5
+ export * from './useWebsocketSendEvent';
@@ -0,0 +1,3 @@
1
+ import { AnyEventMessage } from 'quidproquo-core';
2
+ import { WebSocketServiceEventSubscriptionFunction } from 'quidproquo-web';
3
+ export declare const useSubscribeToWebSocketEvent: <E extends AnyEventMessage>(subscriptionType: E["type"], callback: WebSocketServiceEventSubscriptionFunction<E>) => void;
@@ -0,0 +1,13 @@
1
+ import { useEffect } from 'react';
2
+ import { useWebsocketApi } from './useWebsocketApi';
3
+ export const useSubscribeToWebSocketEvent = (subscriptionType, callback) => {
4
+ const websocketService = useWebsocketApi();
5
+ useEffect(() => {
6
+ const subscriptionHandle = websocketService?.subscribeToEvent(subscriptionType, callback);
7
+ return () => {
8
+ if (subscriptionHandle) {
9
+ websocketService?.unsubscribe(subscriptionHandle);
10
+ }
11
+ };
12
+ }, [websocketService, subscriptionType, callback]);
13
+ };
@@ -0,0 +1,2 @@
1
+ import { WebSocketServiceSubscriptionFunction, WebsocketServiceEvent } from 'quidproquo-web';
2
+ export declare const useSubscribeToWebsocket: (subscriptionType: WebsocketServiceEvent, callback: WebSocketServiceSubscriptionFunction) => void;
@@ -0,0 +1,13 @@
1
+ import { useEffect } from 'react';
2
+ import { useWebsocketApi } from './useWebsocketApi';
3
+ export const useSubscribeToWebsocket = (subscriptionType, callback) => {
4
+ const websocketService = useWebsocketApi();
5
+ useEffect(() => {
6
+ const handle = websocketService?.subscribe(subscriptionType, callback);
7
+ return () => {
8
+ if (handle) {
9
+ websocketService?.unsubscribe(handle);
10
+ }
11
+ };
12
+ }, [websocketService, subscriptionType, callback]);
13
+ };
@@ -0,0 +1 @@
1
+ export declare const useWebsocketApi: () => import("quidproquo-web").WebsocketService | null;
@@ -0,0 +1,6 @@
1
+ import { useContext } from 'react';
2
+ import { WebSocketContext } from '../WebsocketContext';
3
+ export const useWebsocketApi = () => {
4
+ const websocketService = useContext(WebSocketContext);
5
+ return websocketService;
6
+ };
@@ -0,0 +1,2 @@
1
+ import { WebsocketService } from 'quidproquo-web';
2
+ export declare const useWebsocketManagement: (wsUrl: string) => WebsocketService | null;
@@ -0,0 +1,13 @@
1
+ import { WebsocketService } from 'quidproquo-web';
2
+ import { useEffect, useState } from 'react';
3
+ export const useWebsocketManagement = (wsUrl) => {
4
+ const [websocketApi, setWebsocketApi] = useState(null);
5
+ useEffect(() => {
6
+ const wsService = new WebsocketService(wsUrl);
7
+ setWebsocketApi(wsService);
8
+ return () => {
9
+ wsService.destroy();
10
+ };
11
+ }, [wsUrl]);
12
+ return websocketApi;
13
+ };
@@ -0,0 +1,2 @@
1
+ import { AnyEventMessage } from 'quidproquo-core';
2
+ export declare const useWebsocketSendEvent: () => (event: AnyEventMessage) => void;
@@ -0,0 +1,11 @@
1
+ import { useWebsocketApi } from './useWebsocketApi';
2
+ import { useFastCallback } from '../../hooks';
3
+ export const useWebsocketSendEvent = () => {
4
+ const websocketApi = useWebsocketApi();
5
+ const sendMessage = useFastCallback((event) => {
6
+ if (websocketApi) {
7
+ websocketApi.sendEvent(event);
8
+ }
9
+ });
10
+ return sendMessage;
11
+ };
@@ -0,0 +1,3 @@
1
+ export * from './hooks';
2
+ export * from './WebSocketAuthSync';
3
+ export * from './WebsocketProvider';
@@ -0,0 +1,3 @@
1
+ export * from './hooks';
2
+ export * from './WebSocketAuthSync';
3
+ export * from './WebsocketProvider';
package/package.json CHANGED
@@ -1,17 +1,21 @@
1
1
  {
2
2
  "name": "quidproquo-web-react",
3
- "version": "0.0.199",
3
+ "version": "0.0.200",
4
4
  "description": "",
5
5
  "main": "./lib/commonjs/index.js",
6
- "types": "./lib/commonjs/index.d.ts",
6
+ "module": "./lib/esm/index.js",
7
+ "types": "./lib/esm/index.d.ts",
8
+ "sideEffects": false,
7
9
  "files": [
8
10
  "lib/**/*"
9
11
  ],
10
12
  "scripts": {
11
13
  "test": "echo \"Error: no test specified\" && exit 1",
12
14
  "clean": "npx rimraf lib && npx rimraf node_modules",
13
- "build": "npm run clean && tsc -p tsconfig.commonjs.json",
14
- "watch": "tsc -p tsconfig.commonjs.json -w"
15
+ "build": "npm run clean && npm run build:esm && npm run build:cjs",
16
+ "watch": "tsc -p tsconfig.esm.json -w",
17
+ "build:cjs": "tsc -p tsconfig.commonjs.json",
18
+ "build:esm": "tsc -p tsconfig.esm.json"
15
19
  },
16
20
  "repository": {
17
21
  "type": "git",
@@ -25,10 +29,10 @@
25
29
  },
26
30
  "homepage": "https://github.com/joe-coady/quidproquo#readme",
27
31
  "devDependencies": {
28
- "quidproquo-core": "0.0.199",
29
- "quidproquo-tsconfig": "0.0.199",
30
- "quidproquo-webserver": "0.0.199",
31
- "quidproquo-web": "0.0.199",
32
+ "quidproquo-core": "0.0.200",
33
+ "quidproquo-tsconfig": "0.0.200",
34
+ "quidproquo-webserver": "0.0.200",
35
+ "quidproquo-web": "0.0.200",
32
36
  "typescript": "^4.9.3"
33
37
  },
34
38
  "peerDependencies": {