Skip to content

Commit

Permalink
init prev value
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-nikitin-2022 committed Jan 14, 2025
1 parent bebf5da commit 369ad25
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 33 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
v1.7.3
-
- Устранены проблемы при исползовании хука useParams c жестом SwipeBack.

v1.7.2
-
- Устранены проблемы, связанные с работой runSync при использовании большого количества переходов в одной транзакции.
Expand Down
16 changes: 9 additions & 7 deletions src/hooks/useThrottledContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ import { EventBus } from '../services/EventBus';

export function useThrottledContext<T>(context: Context<T>): [T, T | null] {
const contextValue = useContext(context);
const contextName = context.displayName;
const [prevValue, setPrevValue] = useState<T>(contextValue);
const [throttledValue, setThrottledValue] = useState<T>(contextValue);
const contextName = context.displayName ?? '';
const initialContextData = ContextThrottleService.retrieveContextInfo(contextName, contextValue);

if (!contextName) {
console.error('No context display name found');
return [contextValue, null];
}
const [prevValue, setPrevValue] = useState<T | null>(initialContextData.prevValue);
const [throttledValue, setThrottledValue] = useState<T>(initialContextData.throttledValue);

useEffect(() => {
const unsubscribe = EventBus.subscribe<(throttleValue: T, prevValue: T) => void>(
Expand All @@ -29,5 +26,10 @@ export function useThrottledContext<T>(context: Context<T>): [T, T | null] {
ContextThrottleService.triggerContextUpdate(contextName, contextValue);
}, [contextValue, contextName]);

if (!contextName) {
console.error('No context display name found');
return [contextValue, null];
}

return [throttledValue, prevValue];
}
68 changes: 42 additions & 26 deletions src/services/ContextThrottleService.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { EventBus } from './EventBus';
import { TransactionExecutor } from './TransactionExecutor';

interface ContextThrottleInfo {
prevValue: unknown;
interface ContextThrottleInfo<T = unknown> {
prevValue: T | null;
updateTimerId: number;
throttledValue: unknown;
throttledValue: T;
lastUpdateTimestamp: number;
}

Expand All @@ -17,7 +17,7 @@ export class ContextThrottleService {
private static instance?: ContextThrottleService;
private interval = 0;
private throttled = true;
private contextThrottleMap: Record<string, ContextThrottleInfo> = {};
private contextThrottleMap: Map<string, ContextThrottleInfo> = new Map();

// eslint-disable-next-line @typescript-eslint/no-empty-function
private constructor() {}
Expand All @@ -30,21 +30,22 @@ export class ContextThrottleService {
return ContextThrottleService.instance;
}

private getContextThrottleInfoByName(contextName: string) {
if (!(contextName in this.contextThrottleMap)) {
this.contextThrottleMap[contextName] = {
prevValue: null,
throttledValue: null,
lastUpdateTimestamp: 0,
updateTimerId: 0,
};
private getWithInitThrottleInfoByName<T>(
contextName: string,
contextValue: T,
): ContextThrottleInfo<T> {
const contextData = this.contextThrottleMap.get(contextName) ?? {
prevValue: null,
throttledValue: contextValue,
lastUpdateTimestamp: 0,
updateTimerId: 0,
};

if (!this.contextThrottleMap.has(contextName)) {
this.contextThrottleMap.set(contextName, contextData);
}
return this.contextThrottleMap[contextName];
}

private isContextChange<T>(contextName: string, newValue: T) {
const contextData = this.getContextThrottleInfoByName(contextName);
return !(newValue === contextData.throttledValue);
return contextData as ContextThrottleInfo<T>;
}

private getTimeUntilNextUpdate(lastUpdateTimestamp: number) {
Expand All @@ -54,17 +55,25 @@ export class ContextThrottleService {
}

private updateContextValue<T>(contextName: string, newValue: T) {
const contextData = this.getContextThrottleInfoByName(contextName);
const contextData = this.getWithInitThrottleInfoByName(contextName, newValue);

if (newValue === contextData.throttledValue) {
return;
}

contextData.prevValue = contextData.throttledValue;
contextData.lastUpdateTimestamp = Date.now();
contextData.throttledValue = newValue;
EventBus.broadcast(contextName, [contextData.throttledValue, contextData.prevValue]);
}

private throttleUpdateContextValue<T>(contextName: string, newValue: T) {
const contextData = this.getContextThrottleInfoByName(contextName);
const contextData = this.getWithInitThrottleInfoByName(contextName, newValue);
clearTimeout(contextData.updateTimerId);
if (this.isRunSyncActive()) return;

if (TransactionExecutor.isRunSyncActive) {
return;
}

const lastUpdateTimestamp = contextData.lastUpdateTimestamp;
const timeUntilNextUpdate = this.getTimeUntilNextUpdate(lastUpdateTimestamp);
Expand All @@ -78,24 +87,31 @@ export class ContextThrottleService {
}
}

private isRunSyncActive() {
return TransactionExecutor.isRunSyncActive;
}

public static triggerContextUpdate<T>(contextName: string, newValue: T) {
const throttledService = ContextThrottleService.getInstance();
const contextData = throttledService.getWithInitThrottleInfoByName(contextName, newValue);

if (!throttledService.isContextChange(contextName, newValue)) {
if (newValue === contextData.throttledValue) {
return;
}

if (!throttledService.throttled && !throttledService.isRunSyncActive()) {
if (!throttledService.throttled && !TransactionExecutor.isRunSyncActive) {
throttledService.updateContextValue(contextName, newValue);
} else {
throttledService.throttleUpdateContextValue(contextName, newValue);
}
}

public static retrieveContextInfo<T>(contextName: string, contextValue: T) {
const throttledService = ContextThrottleService.getInstance();
const { prevValue, throttledValue } = throttledService.getWithInitThrottleInfoByName(
contextName,
contextValue,
);

return { prevValue, throttledValue };
}

public static updateThrottledServiceSettings(settings: ContextThrottleServiceSettings) {
const throttledService = ContextThrottleService.getInstance();
throttledService.interval = settings.interval;
Expand Down

0 comments on commit 369ad25

Please sign in to comment.