Skip to content

Commit

Permalink
useContextMonitor hook added + disconnect method improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
RNEvok committed Mar 29, 2024
1 parent fb106ca commit aefce96
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/npmPublish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
paths-ignore:
- '.github/workflows/*' # не запускать воркфлоу при обновлении самих воркфлоу
env:
NPM_VERSION_MODIFIER: minor
NPM_VERSION_MODIFIER: patch
NODE_VERSION: 18.11.0
jobs:
Prepare_and_publish:
Expand Down
105 changes: 77 additions & 28 deletions Connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class Connector {
private _currentInterceptedStorageActionId = 0;
private _currentCapturedEventId = 0;
private _currentInterceptedTanStackQueryEventId = 0;
private _contextValueCopiesTable: T.ObjectT<any> = {};
private _connectorInitiated: boolean = false;
private _apiKey: string = "";
private _projectInfo: T.ProjectInfo | null = null;
Expand Down Expand Up @@ -53,6 +52,8 @@ class Connector {
private _unsubscribeFromTanStackQueryEvents: (() => void) | undefined;
private _sendTanStackQueryEventsBatchingTimer: NodeJS.Timeout | null = null;
private _currentTanStackQueryEventsBatch: T.InterceptedTanStackQueryEventPreparedData[] = [];
private _contextValueCopiesTable: T.ObjectT<any> = {};
private _sendContextValueThrottleTimersTable: T.ObjectT<NodeJS.Timeout | null> = {};

public lastEvent: T.RemoteEvent | null = null;

Expand All @@ -64,26 +65,6 @@ class Connector {
delete this._eventListenersTable[key];
};

public handleMonitoredContextValueUpdated(contextId: string, value: any, wait: number) {
// const previousTanStackQueriesDataCopyStr = JSON.stringify(this._currentTanStackQueriesDataCopy);
// this._currentTanStackQueriesDataCopy = queriesData;

// if (this._socket.connected && previousTanStackQueriesDataCopyStr !== JSON.stringify(this._currentTanStackQueriesDataCopy)) {
// if (this._sendTanStackQueriesDataBatchingTimer)
// clearTimeout(this._sendTanStackQueriesDataBatchingTimer);

// this._sendTanStackQueriesDataBatchingTimer = setTimeout(() => {
// const encryptedData = this._encryptData({queriesData: this._currentTanStackQueriesDataCopy, timestamp: moment().valueOf()});
// encryptedData.ok && this._socket.emit(SOCKET_EVENTS_EMIT.SAVE_TANSTACK_QUERIES_DATA_COPY, encryptedData.result);
// }, batchingTimeMs);
// }

const previousValueOfContextCopyStr = JSON.stringify(this._contextValueCopiesTable[contextId]);
this._contextValueCopiesTable[contextId] = value;

// if ()
};

private _prepareEnvironmentInfo(config?: T.PackageConfig): T.ObjectT<any> {
try {
const envInfo = getProcessEnv();
Expand Down Expand Up @@ -508,7 +489,7 @@ class Connector {

// Return unsubscribe function
return () => {
if (this._tanStackQueriesDataMonitorInterval !== null)
if (this._tanStackQueriesDataMonitorInterval)
clearInterval(this._tanStackQueriesDataMonitorInterval);
}
} catch (e) {
Expand Down Expand Up @@ -549,17 +530,50 @@ class Connector {
}
}

public handleMonitoredContextValueUpdated(contextId: string, value: any, waitMs: number) {
if (!this._connectorInitiated)
return;

const previousValueOfContextCopyStr = JSON.stringify(this._contextValueCopiesTable[contextId]);
this._contextValueCopiesTable[contextId] = value;

if (this._socket?.connected && previousValueOfContextCopyStr !== JSON.stringify(this._contextValueCopiesTable[contextId])) {
if (this._sendContextValueThrottleTimersTable[contextId])
clearTimeout(this._sendContextValueThrottleTimersTable[contextId]!);

this._sendContextValueThrottleTimersTable[contextId] = setTimeout(() => {
const encryptedData = this._encryptData({contextId, value: this._contextValueCopiesTable[contextId], timestamp: moment().valueOf()});
encryptedData.ok && this._socket?.emit(SOCKET_EVENTS_EMIT.SAVE_CONTEXT_VALUE_COPY, encryptedData.result);
}, waitMs);
}
};

public disconnect() {
this._connectorInitiated = false;
this._socket?.disconnect();
this._socket = undefined;
this._apiKey = "";
this._projectInfo = null;
this._instructionsTable = {};
this._onEventUsersCustomCallback = () => {};
this._onEventUsersCustomCallback = undefined;
this._connectionInfoPacket = undefined;
this._shouldStopScenarioExecution = false;
this._lastEventLog = undefined;
this._dataToForward = null;

if (this._sendReduxStateBatchingTimer)
clearTimeout(this._sendReduxStateBatchingTimer);

this._currentReduxStateCopy = null;

if (this._sendReduxActionsBatchingTimer)
clearTimeout(this._sendReduxActionsBatchingTimer);

this._currentReduxActionsBatch = [];

if (this._sendZustandStateBatchingTimer)
clearTimeout(this._sendZustandStateBatchingTimer);

this._encryption = null;

if (this._networkInterceptor) {
Expand All @@ -568,24 +582,59 @@ class Connector {
}

if (this._asyncStorageHandler) {
this._untrackAsyncStorage && this._untrackAsyncStorage();
if (this._untrackAsyncStorage) {
this._untrackAsyncStorage();
this._untrackAsyncStorage = undefined;
}
this._trackAsyncStorage = undefined;
this._asyncStorageHandler = null;
}

if (this._localStorageHandler) {
this._untrackLocalStorage && this._untrackLocalStorage();
if (this._untrackLocalStorage) {
this._untrackLocalStorage();
this._untrackLocalStorage = undefined;
}
this._trackLocalStorage = undefined;
this._localStorageHandler = null;
}

this._storageActionsBatchingTimeMs = 500;

if (this._sendStorageActionsBatchingTimer)
clearTimeout(this._sendStorageActionsBatchingTimer);

this._currentStorageActionsBatch = [];

this._unsubscribeFromAppStateChanges && this._unsubscribeFromAppStateChanges();
if (this._unsubscribeFromAppStateChanges) {
this._unsubscribeFromAppStateChanges();
this._unsubscribeFromAppStateChanges = undefined;
}

if (this._tanStackQueriesDataMonitorInterval !== null)
if (this._tanStackQueriesDataMonitorInterval)
clearInterval(this._tanStackQueriesDataMonitorInterval);

this._unsubscribeFromTanStackQueryEvents && this._unsubscribeFromTanStackQueryEvents();
if (this._sendTanStackQueriesDataBatchingTimer)
clearTimeout(this._sendTanStackQueriesDataBatchingTimer);

this._currentTanStackQueriesDataCopy = null;

if (this._unsubscribeFromTanStackQueryEvents) {
this._unsubscribeFromTanStackQueryEvents();
this._unsubscribeFromTanStackQueryEvents = undefined;
}

if (this._sendTanStackQueryEventsBatchingTimer)
clearTimeout(this._sendTanStackQueryEventsBatchingTimer);

this._currentTanStackQueryEventsBatch = [];
this._contextValueCopiesTable = {};

for (const contextId of Object.keys(this._sendContextValueThrottleTimersTable))
if (this._sendContextValueThrottleTimersTable[contextId])
clearTimeout(this._sendContextValueThrottleTimersTable[contextId]!);

this._sendContextValueThrottleTimersTable = {};

this.lastEvent = null;
this._eventListenersTable = {};
Expand Down
3 changes: 3 additions & 0 deletions api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ const SOCKET_EVENTS_EMIT = {
CAPTURE_EVENT: "captureEvent",
SAVE_TANSTACK_QUERIES_DATA_COPY: "saveTanStackQueriesDataCopy",
SAVE_TANSTACK_QUERY_EVENTS_BATCH: "saveTanStackQueryEventsBatch",
SAVE_CONTEXT_VALUE_COPY: "saveContextValueCopy",
SAVE_MOBX_STATE_COPY: "saveMobXStateCopy",
SAVE_MOBX_EVENTS_BATCH: "saveMobXEventsBatch"
};

const sauce = create({
Expand Down
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,12 @@ declare module '@appklaar/codebud/react' {
): void;

export function useRemoteSettings(): RemoteSettings | null;

export function useContextMonitor(
SomeContext: any,
label?: string,
waitMs?: number
): void;
}

declare module '@appklaar/codebud/Network/NetworkInterceptorClassic' {
Expand Down
5 changes: 2 additions & 3 deletions react/hooks/useContextMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import { useContext, Context, useEffect, useRef } from "react";
import { getId } from './../../helpers/random';
import { connector } from './../../Connector';

export const useContextMonitor = (SomeContext: Context<any>, label: string = "", wait: number = 500) => {
export const useContextMonitor = (SomeContext: Context<any>, label: string = "", waitMs: number = 500) => {
const contextId = useRef(label || getId());
const value = useContext(SomeContext);

useEffect(() => {
console.log("SomeContext.displayName", SomeContext.displayName);
connector.handleMonitoredContextValueUpdated(contextId.current, value, wait);
connector.handleMonitoredContextValueUpdated(contextId.current, value, waitMs);
}, [value]);
};
3 changes: 2 additions & 1 deletion react/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { useEvent } from './hooks/useEvent';
export { useRemoteSettings } from './hooks/useRemoteSettings';
export { useRemoteSettings } from './hooks/useRemoteSettings';
export { useContextMonitor } from './hooks/useContextMonitor';

0 comments on commit aefce96

Please sign in to comment.