-
Notifications
You must be signed in to change notification settings - Fork 16
fix(ensadmin): registrar actions relative time #1340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
24f1d92
fix(ensadmin): registrar actions relative time
tk-o 13c3536
Merge branch 'main' into fix/1290-use-now
tk-o 7a85044
Merge remote-tracking branch 'origin/main' into fix/1290-use-now
tk-o 9b498d9
Capitalize "Namehash Labs" to "NameHash Labs" (#1346)
djstrong d973b4b
apply pr feedback
tk-o 8f2836d
Merge branch 'fix/1290-use-now' of github.com:namehash/ensnode into f…
tk-o 0f206ff
docs(changeset): Fix relative time values display on "Latest indexed …
tk-o 8a9fde5
fix: dependency security issues (#1348)
tk-o 1113cdf
fix(ensadmin): registrar actions relative time
tk-o 45f5b4f
apply pr feedback
tk-o 9c92646
docs(changeset): Fix relative time values display on "Latest indexed …
tk-o 8579e76
apply pr feedback
tk-o 446c6bc
Merge branch 'fix/1290-use-now' of github.com:namehash/ensnode into f…
tk-o 87e8da8
technical commit
tk-o File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { useEffect, useState } from "react"; | ||
|
|
||
| import { Duration, UnixTimestamp } from "@ensnode/ensnode-sdk"; | ||
|
|
||
| import { useSystemClock } from "./use-system-clock"; | ||
|
|
||
| const DEFAULT_TIME_TO_REFRESH: Duration = 1; | ||
|
lightwalker-eth marked this conversation as resolved.
Outdated
|
||
|
|
||
| interface UseNowProps { | ||
|
lightwalker-eth marked this conversation as resolved.
Outdated
|
||
| /** | ||
| * Duration after which time value will be refreshed. | ||
| * | ||
| * Defaults to {@link DEFAULT_TIME_TO_REFRESH}. | ||
| */ | ||
| timeToRefresh?: Duration; | ||
| } | ||
|
|
||
| /** | ||
| * Use now | ||
| * | ||
| * This hook returns the current system time while following `timeToRefresh` param. | ||
| * | ||
| * @param timeToRefresh Duration after which time value will be refreshed. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * // `now` will be updated each second (by default) | ||
| * const now = useNow(); | ||
| * ``` | ||
| * @example | ||
| * ```ts | ||
| * // `now` will be updated each 5 seconds | ||
| * const now = useNow({ timeToRefresh: 5 }); | ||
| * ``` | ||
| */ | ||
| export function useNow(props?: UseNowProps): UnixTimestamp { | ||
| const clock = useSystemClock(); | ||
| const [throttledClock, setThrottledClock] = useState(clock); | ||
| const { timeToRefresh = DEFAULT_TIME_TO_REFRESH } = props || {}; | ||
|
|
||
| useEffect(() => { | ||
| if (clock - throttledClock >= timeToRefresh) { | ||
| setThrottledClock(clock); | ||
| } | ||
| }, [timeToRefresh, clock, throttledClock]); | ||
|
|
||
| return throttledClock; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { getUnixTime } from "date-fns"; | ||
| import { useSyncExternalStore } from "react"; | ||
|
|
||
| import { UnixTimestamp } from "@ensnode/ensnode-sdk"; | ||
|
|
||
| import { systemClock } from "@/lib/system-clock"; | ||
|
|
||
| /** | ||
| * Use System Clock | ||
| * | ||
| * Allows reading the current system time {@link UnixTimestamp} which stays | ||
| * exactly the same across all callers of this hook. | ||
| * | ||
| * @see https://react.dev/reference/react/useSyncExternalStore | ||
| */ | ||
| export function useSystemClock(): UnixTimestamp { | ||
| const subscribe = (callback: () => void) => { | ||
| systemClock.addListener(callback); | ||
|
|
||
| return () => { | ||
| systemClock.removeListener(callback); | ||
| }; | ||
| }; | ||
| const getSnapshot = () => getUnixTime(new Date(systemClock.currentTime)); | ||
| const getServerSnapshot = () => getUnixTime(new Date()); | ||
|
|
||
| const syncedSystemClock = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot); | ||
|
|
||
| return syncedSystemClock || getSnapshot(); | ||
|
lightwalker-eth marked this conversation as resolved.
Outdated
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| import { act } from "@testing-library/react"; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| import { createSyncedClock } from "./synced-clock"; | ||
|
|
||
| describe("createSyncedClock", () => { | ||
| const mockedSystemTime = new Date("2025-01-01 00:00:00Z"); | ||
|
|
||
| beforeEach(() => { | ||
| vi.useFakeTimers(); | ||
| vi.setSystemTime(mockedSystemTime); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| describe("basic functionality", () => { | ||
| it("should create a clock instance with current time", () => { | ||
| const clock = createSyncedClock(); | ||
| const now = Date.now(); | ||
| expect(clock.currentTime).toStrictEqual(now); | ||
| }); | ||
|
|
||
| it("should start and stop the clock", async () => { | ||
| const clock = createSyncedClock(); | ||
| const initialTime = clock.currentTime; | ||
|
|
||
| // No listener → clock stopped | ||
| await act(() => vi.advanceTimersByTimeAsync(1000)); | ||
| expect(clock.currentTime).toBe(initialTime); | ||
|
|
||
| const listener = vi.fn(); | ||
| act(() => clock.addListener(listener)); | ||
|
|
||
| await act(() => vi.advanceTimersByTimeAsync(2000)); | ||
|
|
||
| expect(clock.currentTime).toStrictEqual(clock.currentTime); | ||
| expect(listener).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("listener management", () => { | ||
| it("should add listeners", () => { | ||
| const clock = createSyncedClock(); | ||
| const listener = vi.fn(); | ||
|
|
||
| act(() => { | ||
| clock.addListener(listener); | ||
| vi.advanceTimersByTime(100); | ||
| }); | ||
|
|
||
| expect(listener).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("should remove listeners", () => { | ||
| const clock = createSyncedClock(); | ||
| const listener = vi.fn(); | ||
|
|
||
| act(() => { | ||
| clock.addListener(listener); | ||
| vi.advanceTimersByTime(100); | ||
| }); | ||
| expect(listener).toHaveBeenCalled(); | ||
|
|
||
| vi.clearAllMocks(); | ||
|
|
||
| act(() => { | ||
| clock.removeListener(listener); | ||
| vi.advanceTimersByTime(100); | ||
| }); | ||
|
|
||
| expect(listener).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("should start clock when first listener is added", () => { | ||
| const clock = createSyncedClock(); | ||
| const initial = clock.currentTime; | ||
| const listener = vi.fn(); | ||
|
|
||
| act(() => { | ||
| clock.addListener(listener); | ||
| vi.advanceTimersByTime(1000); | ||
| }); | ||
|
|
||
| expect(clock.currentTime).toBeGreaterThan(initial); | ||
| expect(listener).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("should stop clock when last listener is removed", () => { | ||
| const clock = createSyncedClock(); | ||
| const l1 = vi.fn(); | ||
| const l2 = vi.fn(); | ||
|
|
||
| // add multiple listeners | ||
| act(() => { | ||
| clock.addListener(l1); | ||
| clock.addListener(l2); | ||
| vi.advanceTimersByTime(100); | ||
| }); | ||
| expect(l1).toHaveBeenCalled(); | ||
| expect(l2).toHaveBeenCalled(); | ||
|
|
||
| const timeWithOne = clock.currentTime; | ||
|
|
||
| l1.mockReset(); | ||
| l2.mockReset(); | ||
|
|
||
| // remove the one of multiple listeners | ||
| act(() => { | ||
| clock.removeListener(l1); | ||
| vi.advanceTimersByTime(1000); | ||
| }); | ||
| expect(clock.currentTime).toBeGreaterThan(timeWithOne); | ||
| expect(l1).not.toHaveBeenCalled(); | ||
| expect(l2).toHaveBeenCalled(); | ||
|
|
||
| l1.mockReset(); | ||
| l2.mockReset(); | ||
|
|
||
| const timeBeforeStop = clock.currentTime; | ||
|
|
||
| // remove the last listener | ||
| act(() => { | ||
| clock.removeListener(l2); | ||
| vi.advanceTimersByTime(1000); | ||
| }); | ||
|
|
||
| expect(clock.currentTime).toStrictEqual(timeBeforeStop); | ||
| expect(l1).not.toHaveBeenCalled(); | ||
| expect(l2).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("should handle multiple listeners", () => { | ||
| const clock = createSyncedClock(); | ||
| const l1 = vi.fn(), | ||
| l2 = vi.fn(), | ||
| l3 = vi.fn(); | ||
|
|
||
| act(() => { | ||
| clock.addListener(l1); | ||
| clock.addListener(l2); | ||
| clock.addListener(l3); | ||
| vi.advanceTimersByTime(100); | ||
| }); | ||
|
|
||
| expect(l1).toHaveBeenCalled(); | ||
| expect(l2).toHaveBeenCalled(); | ||
| expect(l3).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("should deduplicate listeners when same listener is added multiple times", () => { | ||
| const clock = createSyncedClock(); | ||
| const listener = vi.fn(); | ||
|
|
||
| act(() => { | ||
| clock.addListener(listener); | ||
| clock.addListener(listener); | ||
| vi.advanceTimersByTime(100); | ||
| }); | ||
|
|
||
| expect(listener).toHaveBeenCalled(); | ||
|
|
||
| listener.mockReset(); | ||
| const before = clock.currentTime; | ||
|
|
||
| act(() => { | ||
| clock.removeListener(listener); | ||
| vi.advanceTimersByTime(100); | ||
| }); | ||
|
|
||
| expect(clock.currentTime).toBe(before); | ||
| expect(listener).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.