mirror of
https://github.com/wanderer-industries/wanderer
synced 2025-12-12 18:56:01 +00:00
chore: got rid of timestamp checking on server
This commit is contained in:
@@ -1,7 +1,8 @@
|
|||||||
import { MutableRefObject, useCallback, useEffect, useRef } from 'react';
|
|
||||||
import { Command, Commands, MapHandlers } from '@/hooks/Mapper/types';
|
|
||||||
import { MapEvent } from '@/hooks/Mapper/events';
|
import { MapEvent } from '@/hooks/Mapper/events';
|
||||||
|
import { useThrottle } from '@/hooks/Mapper/hooks';
|
||||||
import { useMapRootState } from '@/hooks/Mapper/mapRootProvider';
|
import { useMapRootState } from '@/hooks/Mapper/mapRootProvider';
|
||||||
|
import { Command, Commands, MapHandlers } from '@/hooks/Mapper/types';
|
||||||
|
import { MutableRefObject, useCallback, useEffect, useRef } from 'react';
|
||||||
|
|
||||||
export const useCommonMapEventProcessor = () => {
|
export const useCommonMapEventProcessor = () => {
|
||||||
const mapRef = useRef<MapHandlers>() as MutableRefObject<MapHandlers>;
|
const mapRef = useRef<MapHandlers>() as MutableRefObject<MapHandlers>;
|
||||||
@@ -26,9 +27,16 @@ export const useCommonMapEventProcessor = () => {
|
|||||||
mapRef.current?.command(name, data);
|
mapRef.current?.command(name, data);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
const processQueue = useCallback(() => {
|
||||||
refQueue.current.forEach(x => mapRef.current?.command(x.name, x.data));
|
const commands = [...refQueue.current];
|
||||||
refQueue.current = [];
|
refQueue.current = [];
|
||||||
|
commands.forEach(x => mapRef.current?.command(x.name, x.data));
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const throttledProcessQueue = useThrottle(processQueue, 200);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
throttledProcessQueue();
|
||||||
}, [systems]);
|
}, [systems]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
export * from './usePageVisibility';
|
export * from './useActualizeSettings';
|
||||||
export * from './useClipboard';
|
export * from './useClipboard';
|
||||||
export * from './useHotkey';
|
export * from './useHotkey';
|
||||||
|
export * from './usePageVisibility';
|
||||||
export * from './useSkipContextMenu';
|
export * from './useSkipContextMenu';
|
||||||
export * from './useActualizeSettings';
|
export * from './useThrottle';
|
||||||
|
|||||||
13
assets/js/hooks/Mapper/hooks/useThrottle.ts
Normal file
13
assets/js/hooks/Mapper/hooks/useThrottle.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import { useCallback, useRef } from 'react';
|
||||||
|
|
||||||
|
export const useThrottle = (callback: any, limit: number) => {
|
||||||
|
const lastCallRef = useRef(0);
|
||||||
|
const throttledCallback = useCallback(() => {
|
||||||
|
const now = Date.now();
|
||||||
|
if (now - lastCallRef.current >= limit) {
|
||||||
|
lastCallRef.current = now;
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
}, [callback, limit]);
|
||||||
|
return throttledCallback;
|
||||||
|
};
|
||||||
@@ -4,8 +4,6 @@ defmodule WandererAppWeb.MapLive do
|
|||||||
|
|
||||||
require Logger
|
require Logger
|
||||||
|
|
||||||
@server_event_unsync_timeout :timer.minutes(2)
|
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def mount(%{"slug" => map_slug} = _params, _session, socket) when is_connected?(socket) do
|
def mount(%{"slug" => map_slug} = _params, _session, socket) when is_connected?(socket) do
|
||||||
Process.send_after(self(), %{event: :load_map}, Enum.random(10..800))
|
Process.send_after(self(), %{event: :load_map}, Enum.random(10..800))
|
||||||
@@ -96,17 +94,11 @@ defmodule WandererAppWeb.MapLive do
|
|||||||
|> push_navigate(to: ~p"/tracking/#{map_slug}")}
|
|> push_navigate(to: ~p"/tracking/#{map_slug}")}
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def handle_info(%{timestamp: timestamp} = info, %{assigns: %{map_slug: map_slug}} = socket) do
|
def handle_info(info, %{assigns: %{map_slug: map_slug}} = socket) do
|
||||||
duration = DateTime.diff(DateTime.utc_now(), timestamp, :millisecond)
|
|
||||||
|
|
||||||
if duration > @server_event_unsync_timeout do
|
|
||||||
{:noreply, socket |> push_navigate(to: ~p"/#{map_slug}")}
|
|
||||||
else
|
|
||||||
{:noreply,
|
{:noreply,
|
||||||
socket
|
socket
|
||||||
|> WandererAppWeb.MapEventHandler.handle_event(info)}
|
|> WandererAppWeb.MapEventHandler.handle_event(info)}
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def handle_info(info, socket),
|
def handle_info(info, socket),
|
||||||
|
|||||||
Reference in New Issue
Block a user