Skip to content

Commit

Permalink
chore: reduce bundle size
Browse files Browse the repository at this point in the history
  • Loading branch information
Ernxst committed Dec 1, 2023
1 parent 1c46152 commit 73397dd
Showing 1 changed file with 5 additions and 22 deletions.
27 changes: 5 additions & 22 deletions packages/core/src/lib/adapters/dom.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
/// <reference lib="dom" />
import type {
QueryFetcher,
QueryParamsOptions,
QueryUpdater,
} from "../types.ts";
import type { QueryParamsOptions } from "../types.ts";
import type { Adapter } from "./types.ts";

export interface DomAdapterOptions
Expand All @@ -18,33 +14,20 @@ export interface DomAdapterOptions
replace?: boolean;
}

function createQueryFetcher(
windowObj: Pick<typeof window, "location">
): QueryFetcher {
return () => windowObj.location;
}

function createQueryUpdater(
replace: boolean,
{ replaceState, pushState }: Pick<History, "replaceState" | "pushState">
): QueryUpdater {
const update = replace ? replaceState : pushState;
/** Get the function ahead of time so we don't need an if statement every call */
return (search, hash) => update(null, "", `${search}${hash}`);
}

// Called it the dom adapter as there's noting svelte about it

export function dom(options: DomAdapterOptions = {}): Adapter {
const { windowObj = window, replace = false } = options;

/** Get the function ahead of time so we don't need an if statement every call */
const replaceState = windowObj.history.replaceState.bind(windowObj.history);
const pushState = windowObj.history.pushState.bind(windowObj.history);
const update = replace ? replaceState : pushState;

return {
isBrowser: () => typeof window !== "undefined",
getBrowserUrl: createQueryFetcher(windowObj),
updateBrowserUrl: createQueryUpdater(replace, { replaceState, pushState }),
getBrowserUrl: () => windowObj.location,
updateBrowserUrl: (search, hash) => update(null, "", `${search}${hash}`),
// Client-side only adapter
getServerUrl: () => ({ hash: "", search: "" }),
updateServerUrl: () => {},
Expand Down

0 comments on commit 73397dd

Please sign in to comment.