Skip to content

Commit

Permalink
return searchHook from memoryLocation
Browse files Browse the repository at this point in the history
  • Loading branch information
mitulagr2 committed Nov 14, 2024
1 parent 48b63a9 commit 42718dd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
12 changes: 10 additions & 2 deletions packages/wouter/src/memory-location.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import { useSyncExternalStore } from "./react-deps.js";

export const memoryLocation = ({
path = "/",
searchPath = "",
static: staticLocation,
record,
} = {}) => {
let currentPath = path;
let currentPath = path + (searchPath && (path.split("?")[1] ? "&" : "?")) + searchPath;
let currentSearch = currentPath.split("?")[1] || "";
const history = [currentPath];
const emitter = mitt();

Expand All @@ -24,6 +26,7 @@ export const memoryLocation = ({
}

currentPath = path;
currentSearch = path.split("?")[1] || "";
emitter.emit("navigate", path);
};

Expand All @@ -39,15 +42,20 @@ export const memoryLocation = ({
navigate,
];

const useMemoryQuery = () => [
useSyncExternalStore(subscribe, () => currentSearch)
];

function reset() {
// clean history array with mutation to preserve link
history.splice(0, history.length);

navigateImplementation(path);
navigateImplementation(path + (searchPath && (path.split("?")[1] ? "&" : "?")) + searchPath);
}

return {
hook: useMemoryLocation,
searchHook: useMemoryQuery,
navigate,
history: record ? history : undefined,
reset: record ? reset : undefined,
Expand Down
6 changes: 4 additions & 2 deletions packages/wouter/types/memory-location.d.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import { BaseLocationHook, Path } from "./location-hook.js";
import { BaseLocationHook, BaseSearchHook, Path, SearchString } from "./location-hook.js";

type Navigate<S = any> = (
to: Path,
options?: { replace?: boolean; state?: S }
) => void;

type HookReturnValue = { hook: BaseLocationHook; navigate: Navigate };
type HookReturnValue = { hook: BaseLocationHook; searchHook: BaseSearchHook, navigate: Navigate };
type StubHistory = { history: Path[]; reset: () => void };

export function memoryLocation(options?: {
path?: Path;
searchPath?: SearchString;
static?: boolean;
record?: false;
}): HookReturnValue;
export function memoryLocation(options?: {
path?: Path;
searchPath?: SearchString;
static?: boolean;
record: true;
}): HookReturnValue & StubHistory;

0 comments on commit 42718dd

Please sign in to comment.