Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
Brendonovich committed Jul 14, 2024
1 parent 59dcbcc commit fffdf1c
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 38 deletions.
12 changes: 2 additions & 10 deletions src/components.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
/*@refresh skip*/
import type { JSX } from "solid-js";
import { createMemo, mergeProps, splitProps } from "solid-js";
import {
useHref,
useLocation,
useNavigate,
useResolvedPath
} from "./routing.js";
import type {
Location,
Navigator
} from "./types.js";
import { useHref, useLocation, useNavigate, useResolvedPath } from "./routing.js";
import type { Location, Navigator } from "./types.js";
import { normalizePath } from "./utils.js";

declare module "solid-js" {
Expand Down
11 changes: 9 additions & 2 deletions src/data/action.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { $TRACK, createMemo, createSignal, JSX, onCleanup, getOwner } from "solid-js";
import { isServer } from "solid-js/web";
import { useRouter } from "../routing.js";
import type { RouterContext, Submission, SubmissionStub, Navigator, NarrowResponse } from "../types.js";
import type {
RouterContext,
Submission,
SubmissionStub,
Navigator,
NarrowResponse
} from "../types.js";
import { mockBase } from "../utils.js";
import { cacheKeyOp, hashKey, revalidate, cache } from "./cache.js";

Expand Down Expand Up @@ -44,7 +50,8 @@ export function useSubmission<T extends Array<any>, U>(
{},
{
get(_, property) {
if (submissions.length === 0 && property === "clear" || property === "retry") return (() => {});
if ((submissions.length === 0 && property === "clear") || property === "retry")
return () => {};
return submissions[submissions.length - 1]?.[property as keyof Submission<T, U>];
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/data/createAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export function createAsync<T>(
}
): Accessor<T | undefined> {
let resource: () => T;
let prev = () => !resource || (resource as any).state === "unresolved" ? undefined : (resource as any).latest;
let prev = () =>
!resource || (resource as any).state === "unresolved" ? undefined : (resource as any).latest;
[resource] = createResource(
() => subFetch(fn, untrack(prev)),
v => v,
Expand Down Expand Up @@ -67,7 +68,10 @@ export function createAsyncStore<T>(
} = {}
): Accessor<T | undefined> {
let resource: () => T;
let prev = () => !resource || (resource as any).state === "unresolved" ? undefined : unwrap((resource as any).latest);
let prev = () =>
!resource || (resource as any).state === "unresolved"
? undefined
: unwrap((resource as any).latest);
[resource] = createResource(
() => subFetch(fn, untrack(prev)),
v => v,
Expand Down
1 change: 0 additions & 1 deletion src/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ export { createAsync, createAsyncStore } from "./createAsync.js";
export { action, useSubmission, useSubmissions, useAction, type Action } from "./action.js";
export { cache, revalidate, type CachedFunction } from "./cache.js";
export { redirect, reload, json } from "./response.js";

9 changes: 7 additions & 2 deletions src/lifecycle.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { isServer } from "solid-js/web";
import { BeforeLeaveLifecycle, BeforeLeaveListener, LocationChange, NavigateOptions } from "./types.js";
import {
BeforeLeaveLifecycle,
BeforeLeaveListener,
LocationChange,
NavigateOptions
} from "./types.js";

export function createBeforeLeave(): BeforeLeaveLifecycle {
let listeners = new Set<BeforeLeaveListener>();
Expand All @@ -24,7 +29,7 @@ export function createBeforeLeave(): BeforeLeaveLifecycle {
from: l.location,
retry: (force?: boolean) => {
force && (ignore = true);
l.navigate(to as string, {...options, resolve: false});
l.navigate(to as string, { ...options, resolve: false });
}
});
return !e.defaultPrevented;
Expand Down
18 changes: 15 additions & 3 deletions src/routers/HashRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import type { JSX } from "solid-js";
import { setupNativeEvents } from "../data/events.js";
import type { BaseRouterProps } from "./components.js";
import { createRouter, scrollToHash, bindEvent } from "./createRouter.js";
import { createBeforeLeave, keepDepth, notifyIfNotBlocked, saveCurrentDepth } from "../lifecycle.js";
import {
createBeforeLeave,
keepDepth,
notifyIfNotBlocked,
saveCurrentDepth
} from "../lifecycle.js";

export function hashParser(str: string) {
const to = str.replace(/^.*?#/, "");
Expand All @@ -16,7 +21,11 @@ export function hashParser(str: string) {
return to;
}

export type HashRouterProps = BaseRouterProps & { actionBase?: string, explicitLinks?: boolean, preload?: boolean };
export type HashRouterProps = BaseRouterProps & {
actionBase?: string;
explicitLinks?: boolean;
preload?: boolean;
};

export function HashRouter(props: HashRouterProps): JSX.Element {
const getSource = () => window.location.hash.slice(1);
Expand All @@ -34,7 +43,10 @@ export function HashRouter(props: HashRouterProps): JSX.Element {
scrollToHash(hash, scroll);
saveCurrentDepth();
},
init: notify => bindEvent(window, "hashchange",
init: notify =>
bindEvent(
window,
"hashchange",
notifyIfNotBlocked(
notify,
delta => !beforeLeave.confirm(delta && delta < 0 ? delta : getSource())
Expand Down
8 changes: 6 additions & 2 deletions src/routers/MemoryRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export function createMemoryHistory() {
scrollToHash(value.split("#")[1] || "", true);
}
}, 0);

},
back: () => {
go(-1);
Expand All @@ -60,7 +59,12 @@ export function createMemoryHistory() {
};
}

export type MemoryRouterProps = BaseRouterProps & { history?: MemoryHistory, actionBase?: string, explicitLinks?: boolean, preload?: boolean };
export type MemoryRouterProps = BaseRouterProps & {
history?: MemoryHistory;
actionBase?: string;
explicitLinks?: boolean;
preload?: boolean;
};

export function MemoryRouter(props: MemoryRouterProps): JSX.Element {
const memoryHistory = props.history || createMemoryHistory();
Expand Down
32 changes: 26 additions & 6 deletions src/routers/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,30 @@ import { StaticRouter } from "./StaticRouter.js";
import { setupNativeEvents } from "../data/events.js";
import type { BaseRouterProps } from "./components.jsx";
import type { JSX } from "solid-js";
import { createBeforeLeave, keepDepth, notifyIfNotBlocked, saveCurrentDepth } from "../lifecycle.js";
import {
createBeforeLeave,
keepDepth,
notifyIfNotBlocked,
saveCurrentDepth
} from "../lifecycle.js";

export type RouterProps = BaseRouterProps & { url?: string, actionBase?: string, explicitLinks?: boolean, preload?: boolean };
export type RouterProps = BaseRouterProps & {
url?: string;
actionBase?: string;
explicitLinks?: boolean;
preload?: boolean;
};

export function Router(props: RouterProps): JSX.Element {
if (isServer) return StaticRouter(props);
const getSource = () => {
const url = window.location.pathname.replace(/^\/+/, "/") + window.location.search;
return {
value: props.transformUrl ? props.transformUrl(url) + window.location.hash : url + window.location.hash,
value: props.transformUrl
? props.transformUrl(url) + window.location.hash
: url + window.location.hash,
state: window.history.state
}
};
};
const beforeLeave = createBeforeLeave();
return createRouter({
Expand All @@ -29,7 +41,10 @@ export function Router(props: RouterProps): JSX.Element {
scrollToHash(decodeURIComponent(window.location.hash.slice(1)), scroll);
saveCurrentDepth();
},
init: notify => bindEvent(window, "popstate",
init: notify =>
bindEvent(
window,
"popstate",
notifyIfNotBlocked(notify, delta => {
if (delta && delta < 0) {
return !beforeLeave.confirm(delta);
Expand All @@ -39,7 +54,12 @@ export function Router(props: RouterProps): JSX.Element {
}
})
),
create: setupNativeEvents(props.preload, props.explicitLinks, props.actionBase, props.transformUrl),
create: setupNativeEvents(
props.preload,
props.explicitLinks,
props.actionBase,
props.transformUrl
),
utils: {
go: delta => window.history.go(delta),
beforeLeave
Expand Down
4 changes: 2 additions & 2 deletions src/routers/StaticRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export type StaticRouterProps = BaseRouterProps & { url?: string };

export function StaticRouter(props: StaticRouterProps): JSX.Element {
let e;
const url = props.url || ((e = getRequestEvent()) && getPath(e.request.url)) || ""
const url = props.url || ((e = getRequestEvent()) && getPath(e.request.url)) || "";
const obj = {
value: props.transformUrl ? props.transformUrl(url) : url,
value: props.transformUrl ? props.transformUrl(url) : url
};
return createRouterComponent({
signal: [() => obj, next => Object.assign(obj, next)]
Expand Down
10 changes: 5 additions & 5 deletions src/routers/createRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ function querySelector<T extends Element>(selector: string) {
}

export function createRouter(config: {
get: () => string | LocationChange,
set: (next: LocationChange) => void,
init?: (notify: (value?: string | LocationChange) => void) => () => void,
create?: (router: RouterContext) => void,
utils?: Partial<RouterUtils>
get: () => string | LocationChange;
set: (next: LocationChange) => void;
init?: (notify: (value?: string | LocationChange) => void) => () => void;
create?: (router: RouterContext) => void;
utils?: Partial<RouterUtils>;
}) {
let ignore = false;
const wrap = (value: string | LocationChange) => (typeof value === "string" ? { value } : value);
Expand Down
2 changes: 1 addition & 1 deletion src/routers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export type { HashRouterProps } from "./HashRouter.js";
export { MemoryRouter, createMemoryHistory } from "./MemoryRouter.js";
export type { MemoryRouterProps, MemoryHistory } from "./MemoryRouter.js";
export { StaticRouter } from "./StaticRouter.js";
export type { StaticRouterProps } from "./StaticRouter.js";
export type { StaticRouterProps } from "./StaticRouter.js";
11 changes: 9 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { createMemo, getOwner, runWithOwner } from "solid-js";
import type { MatchFilter, MatchFilters, Params, PathMatch, RouteDescription, SetParams } from "./types.ts";
import type {
MatchFilter,
MatchFilters,
Params,
PathMatch,
RouteDescription,
SetParams
} from "./types.ts";

const hasSchemeRegex = /^(?:[a-z0-9]+:)?\/\//i;
const trimPathRegex = /^\/+|(\/)\/+$/g;
export const mockBase = "http://sr"
export const mockBase = "http://sr";

export function normalizePath(path: string, omitSlash: boolean = false) {
const s = path.replace(trimPathRegex, "$1");
Expand Down

0 comments on commit fffdf1c

Please sign in to comment.