Skip to content

Commit 79d55d8

Browse files
Add memoryIntegration (#251)
1 parent b97c802 commit 79d55d8

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ It supports all of Solid's SSR methods and has Solid's transitions baked in, so
1818
- [Data Functions](#data-functions)
1919
- [Nested Routes](#nested-routes)
2020
- [Hash Mode Router](#hash-mode-router)
21+
- [Hash Mode Router](#memory-mode-router)
2122
- [Config Based Routing](#config-based-routing)
2223
- [Router Primitives](#router-primitives)
2324
- [useParams](#useparams)
@@ -419,6 +420,16 @@ import { Router, hashIntegration } from '@solidjs/router'
419420
<Router source={hashIntegration()}><App /></Router>
420421
```
421422

423+
## Memory Mode Router
424+
425+
You can also use memory mode router for testing purpose.
426+
427+
```jsx
428+
import { Router, memoryIntegration } from '@solidjs/router'
429+
430+
<Router source={memoryIntegration()}><App /></Router>
431+
```
432+
422433
## Config Based Routing
423434

424435
You don't have to use JSX to set up your routes; you can pass an object directly with `useRoutes`:

src/integration.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,49 @@ function scrollToHash(hash: string, fallbackTop?: boolean) {
3232
}
3333
}
3434

35+
export function createMemoryHistory() {
36+
const entries = ["/"];
37+
let index = 0;
38+
const listeners: ((value: string) => void)[] = [];
39+
40+
const go = (n: number) => {
41+
// https://github.com/remix-run/react-router/blob/682810ca929d0e3c64a76f8d6e465196b7a2ac58/packages/router/history.ts#L245
42+
index = Math.max(0, Math.min(index + n, entries.length - 1));
43+
44+
const value = entries[index];
45+
listeners.forEach(listener => listener(value));
46+
};
47+
48+
return {
49+
get: () => entries[index],
50+
set: ({ value, scroll, replace }: LocationChange) => {
51+
if (replace) {
52+
entries[index] = value;
53+
} else {
54+
entries.splice(index + 1, entries.length - index, value);
55+
index++;
56+
}
57+
if (scroll) {
58+
scrollToHash(value.split("#")[1] || "", true);
59+
}
60+
},
61+
back: () => {
62+
go(-1);
63+
},
64+
forward: () => {
65+
go(1);
66+
},
67+
go,
68+
listen: (listener: (value: string) => void) => {
69+
listeners.push(listener);
70+
return () => {
71+
const index = listeners.indexOf(listener);
72+
listeners.splice(index, 1);
73+
};
74+
}
75+
};
76+
}
77+
3578
export function createIntegration(
3679
get: () => string | LocationChange,
3780
set: (next: LocationChange) => void,
@@ -137,3 +180,10 @@ export function hashIntegration() {
137180
}
138181
);
139182
}
183+
184+
export function memoryIntegration() {
185+
const memoryHistory = createMemoryHistory();
186+
return createIntegration(memoryHistory.get, memoryHistory.set, memoryHistory.listen, {
187+
go: memoryHistory.go
188+
});
189+
}

0 commit comments

Comments
 (0)