Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Implemented onNavigate #1047

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions runtime/src/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export function set_cid(n) {
}

const _history = typeof history !== 'undefined' ? history : {
go: (delta: number) => {},
pushState: (state: any, title: string, href: string) => {},
replaceState: (state: any, title: string, href: string) => {},
scrollRestoration: ''
Expand Down
7 changes: 6 additions & 1 deletion runtime/src/app/goto/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { history, select_target, navigate, cid } from '../app';
import { canNavigate } from '../onNavigate/index';

export default function goto(href: string, opts = { replaceState: false }) {
export default async function goto(href: string, opts = { replaceState: false }) {
const target = select_target(new URL(href, document.baseURI));

if (target) {
if (!(await canNavigate(target.page))) {
return;
}

history[opts.replaceState ? 'replaceState' : 'pushState']({ id: cid }, '', href);
return navigate(target, null).then(() => {});
}
Expand Down
3 changes: 2 additions & 1 deletion runtime/src/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export const stores = () => getContext(CONTEXT_KEY);
export { default as start } from './start/index';
export { default as goto } from './goto/index';
export { default as prefetch } from './prefetch/index';
export { default as prefetchRoutes } from './prefetchRoutes/index';
export { default as prefetchRoutes } from './prefetchRoutes/index';
export { default as onNavigate } from './onNavigate/index';
21 changes: 21 additions & 0 deletions runtime/src/app/onNavigate/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { OnNavigateCallback, Page } from '../types';

const callbacks: OnNavigateCallback[] = [];

export default function onNavigate(callback: OnNavigateCallback): () => void {
if (!callbacks.includes(callback)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need to check if the callback is already present?

Copy link
Member Author

@PatrickG PatrickG Jun 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise the callback could be added (and then later called) multiple times.

callbacks.push(callback);
}

return () => {
const index = callbacks.indexOf(callback);
if (index !== -1) {
callbacks.splice(index, 1);
}
};
}

export async function canNavigate(page: Page): Promise<boolean> {
const results = await Promise.all(callbacks.map(callback => callback(page)));
return !results.some(result => result === false);
}
16 changes: 13 additions & 3 deletions runtime/src/app/start/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
set_cid
} from '../app';
import prefetch from '../prefetch/index';
import { canNavigate } from '../onNavigate/index';

export default function start(opts: {
target: Node
Expand Down Expand Up @@ -60,7 +61,7 @@ function trigger_prefetch(event: MouseEvent | TouchEvent) {
prefetch(a.href);
}

function handle_click(event: MouseEvent) {
async function handle_click(event: MouseEvent) {
// Adapted from https://github.com/visionmedia/page.js
// MIT license https://github.com/visionmedia/page.js#license
if (which(event) !== 1) return;
Expand Down Expand Up @@ -97,9 +98,13 @@ function handle_click(event: MouseEvent) {

const target = select_target(url);
if (target) {
event.preventDefault();
if (!(await canNavigate(target.page))) {
return;
}

const noscroll = a.hasAttribute('sapper-noscroll');
navigate(target, null, noscroll, url.hash);
event.preventDefault();
history.pushState({ id: cid }, '', url.href);
}
}
Expand All @@ -113,13 +118,18 @@ function find_anchor(node: Node) {
return node;
}

function handle_popstate(event: PopStateEvent) {
async function handle_popstate(event: PopStateEvent) {
scroll_history[cid] = scroll_state();

if (event.state) {
const url = new URL(location.href);
const target = select_target(url);
if (target) {
if (!(await canNavigate(target.page))) {
history.go(cid - event.state.id);
return;
}

navigate(target, event.state.id);
} else {
location.href = location.href;
Expand Down
4 changes: 3 additions & 1 deletion runtime/src/app/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ export type Page = {
path: string;
params: Record<string, string>;
query: Record<string, string | string[]>;
};
};

export type OnNavigateCallback = (page: Page) => any;