mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-18 21:45:12 +00:00
Merge pull request #642 from sveltejs/context-stores
give each app its own page, preloading and session stores, using context
This commit is contained in:
@@ -1,12 +0,0 @@
|
|||||||
<script>
|
|
||||||
import { setContext } from 'svelte';
|
|
||||||
import { CONTEXT_KEY } from './shared';
|
|
||||||
|
|
||||||
export let Root;
|
|
||||||
export let props;
|
|
||||||
export let session;
|
|
||||||
|
|
||||||
setContext(CONTEXT_KEY, session);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<Root {...props}/>
|
|
||||||
@@ -1,10 +1,5 @@
|
|||||||
import { writable } from 'svelte/store';
|
import { writable } from 'svelte/store';
|
||||||
|
|
||||||
export const stores = {
|
|
||||||
preloading: writable(false),
|
|
||||||
page: writable(null)
|
|
||||||
};
|
|
||||||
|
|
||||||
export const CONTEXT_KEY = {};
|
export const CONTEXT_KEY = {};
|
||||||
|
|
||||||
export const preload = () => ({});
|
export const preload = () => ({});
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
import { writable } from 'svelte/store.mjs';
|
import { writable } from 'svelte/store.mjs';
|
||||||
import App from '@sapper/internal/App.svelte';
|
import App from '@sapper/internal/App.svelte';
|
||||||
import { stores } from '@sapper/internal/shared';
|
import { root_preload, ErrorComponent, ignore, components, routes } from '@sapper/internal/manifest-client';
|
||||||
import { Root, root_preload, ErrorComponent, ignore, components, routes } from '@sapper/internal/manifest-client';
|
|
||||||
import {
|
import {
|
||||||
Target,
|
Target,
|
||||||
ScrollPosition,
|
ScrollPosition,
|
||||||
@@ -24,12 +23,16 @@ let current_token: {};
|
|||||||
let root_preloaded: Promise<any>;
|
let root_preloaded: Promise<any>;
|
||||||
let current_branch = [];
|
let current_branch = [];
|
||||||
|
|
||||||
const session = writable(initial_data && initial_data.session);
|
const stores = {
|
||||||
|
page: writable({}),
|
||||||
|
preloading: writable(null),
|
||||||
|
session: writable(initial_data && initial_data.session)
|
||||||
|
};
|
||||||
|
|
||||||
let $session;
|
let $session;
|
||||||
let session_dirty: boolean;
|
let session_dirty: boolean;
|
||||||
|
|
||||||
session.subscribe(async value => {
|
stores.session.subscribe(async value => {
|
||||||
$session = value;
|
$session = value;
|
||||||
|
|
||||||
if (!ready) return;
|
if (!ready) return;
|
||||||
@@ -216,7 +219,11 @@ async function render(redirect: Redirect, branch: any[], props: any, page: Page)
|
|||||||
if (root_component) {
|
if (root_component) {
|
||||||
root_component.$set(props);
|
root_component.$set(props);
|
||||||
} else {
|
} else {
|
||||||
props.session = session;
|
props.stores = {
|
||||||
|
page: { subscribe: stores.page.subscribe },
|
||||||
|
preloading: { subscribe: stores.preloading.subscribe },
|
||||||
|
session: stores.session
|
||||||
|
};
|
||||||
props.level0 = {
|
props.level0 = {
|
||||||
props: await root_preloaded
|
props: await root_preloaded
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,10 +1,7 @@
|
|||||||
import { getContext } from 'svelte';
|
import { getContext } from 'svelte';
|
||||||
import { CONTEXT_KEY, stores } from '@sapper/internal/shared';
|
import { CONTEXT_KEY } from '@sapper/internal/shared';
|
||||||
|
|
||||||
export const preloading = { subscribe: stores.preloading.subscribe };
|
export const stores = () => getContext(CONTEXT_KEY);
|
||||||
export const page = { subscribe: stores.page.subscribe };
|
|
||||||
|
|
||||||
export const getSession = () => getContext(CONTEXT_KEY);
|
|
||||||
|
|
||||||
export { default as start } from './start/index';
|
export { default as start } from './start/index';
|
||||||
export { default as goto } from './goto/index';
|
export { default as goto } from './goto/index';
|
||||||
|
|||||||
@@ -204,10 +204,22 @@ export function get_page_handler(
|
|||||||
});
|
});
|
||||||
|
|
||||||
const props = {
|
const props = {
|
||||||
|
stores: {
|
||||||
|
page: {
|
||||||
|
subscribe: writable({
|
||||||
|
path: req.path,
|
||||||
|
query: req.query,
|
||||||
|
params
|
||||||
|
}).subscribe
|
||||||
|
},
|
||||||
|
preloading: {
|
||||||
|
subscribe: writable(null).subscribe
|
||||||
|
},
|
||||||
|
session: writable(session)
|
||||||
|
},
|
||||||
segments: layout_segments,
|
segments: layout_segments,
|
||||||
status: error ? status : 200,
|
status: error ? status : 200,
|
||||||
error: error ? error instanceof Error ? error : { message: error } : null,
|
error: error ? error instanceof Error ? error : { message: error } : null,
|
||||||
session: writable(session),
|
|
||||||
level0: {
|
level0: {
|
||||||
props: preloaded[0]
|
props: preloaded[0]
|
||||||
},
|
},
|
||||||
@@ -231,12 +243,6 @@ export function get_page_handler(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stores.page.set({
|
|
||||||
path: req.path,
|
|
||||||
query: req.query,
|
|
||||||
params: params
|
|
||||||
});
|
|
||||||
|
|
||||||
const { html, head, css } = App.render(props);
|
const { html, head, css } = App.render(props);
|
||||||
|
|
||||||
const serialized = {
|
const serialized = {
|
||||||
|
|||||||
@@ -271,14 +271,14 @@ function generate_app(manifest_data: ManifestData, path_to_routes: string) {
|
|||||||
import Layout from '${get_file(path_to_routes, manifest_data.root)}';
|
import Layout from '${get_file(path_to_routes, manifest_data.root)}';
|
||||||
import Error from '${get_file(path_to_routes, manifest_data.error)}';
|
import Error from '${get_file(path_to_routes, manifest_data.error)}';
|
||||||
|
|
||||||
export let session;
|
export let stores;
|
||||||
export let error;
|
export let error;
|
||||||
export let status;
|
export let status;
|
||||||
export let segments;
|
export let segments;
|
||||||
export let level0;
|
export let level0;
|
||||||
${levels.map(l => `export let level${l} = null;`).join('\n\t\t\t')}
|
${levels.map(l => `export let level${l} = null;`).join('\n\t\t\t')}
|
||||||
|
|
||||||
setContext(CONTEXT_KEY, session);
|
setContext(CONTEXT_KEY, stores);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<Layout segment={segments[0]} {...level0.props}>
|
<Layout segment={segments[0]} {...level0.props}>
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
<script>
|
<script>
|
||||||
import { page } from '@sapper/app';
|
import { stores } from '@sapper/app';
|
||||||
|
const { page } = stores();
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<h1>{$page.params.rest.join(',')}</h1>
|
<h1>{$page.params.rest.join(',')}</h1>
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
<script>
|
<script>
|
||||||
import { page } from '@sapper/app';
|
import { stores } from '@sapper/app';
|
||||||
|
const { page } = stores();
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<h1>{$page.params.rest.join(',')}</h1>
|
<h1>{$page.params.rest.join(',')}</h1>
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
<script>
|
<script>
|
||||||
import { page } from '@sapper/app';
|
import { stores } from '@sapper/app';
|
||||||
|
const { page } = stores();
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<h1>{$page.params.slug.toUpperCase()}</h1>
|
<h1>{$page.params.slug.toUpperCase()}</h1>
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
<script>
|
<script>
|
||||||
import { page } from '@sapper/app';
|
import { stores } from '@sapper/app';
|
||||||
|
const { page } = stores();
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<h1>{JSON.stringify($page.query)}</h1>
|
<h1>{JSON.stringify($page.query)}</h1>
|
||||||
@@ -7,7 +7,10 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { page } from '@sapper/app';
|
import { stores } from '@sapper/app';
|
||||||
|
|
||||||
|
const { page } = stores();
|
||||||
|
|
||||||
export let slug;
|
export let slug;
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,8 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { page } from '@sapper/app';
|
import { stores } from '@sapper/app';
|
||||||
|
const { page } = stores();
|
||||||
|
|
||||||
export let count;
|
export let count;
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -9,7 +9,8 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { page } from '@sapper/app';
|
import { stores } from '@sapper/app';
|
||||||
|
const { page } = stores();
|
||||||
|
|
||||||
export let count;
|
export let count;
|
||||||
export let segment;
|
export let segment;
|
||||||
|
|||||||
@@ -7,12 +7,14 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { preloading } from '@sapper/app';
|
import { stores } from '@sapper/app';
|
||||||
import { setContext } from 'svelte';
|
import { setContext } from 'svelte';
|
||||||
|
|
||||||
export let child;
|
export let child;
|
||||||
export let rootPreloadFunctionRan;
|
export let rootPreloadFunctionRan;
|
||||||
|
|
||||||
|
const { preloading } = stores();
|
||||||
|
|
||||||
setContext('x', { rootPreloadFunctionRan });
|
setContext('x', { rootPreloadFunctionRan });
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
<script>
|
<script>
|
||||||
import { page } from '@sapper/app';
|
import { stores } from '@sapper/app';
|
||||||
|
const { page } = stores();
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<h1>{$page.params.slug}</h1>
|
<h1>{$page.params.slug}</h1>
|
||||||
|
|||||||
@@ -5,8 +5,8 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { getSession } from '@sapper/app';
|
import { stores } from '@sapper/app';
|
||||||
const session = getSession();
|
const { session } = stores();
|
||||||
|
|
||||||
export let title;
|
export let title;
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<script>
|
<script>
|
||||||
import { getSession } from '@sapper/app';
|
import { stores } from '@sapper/app';
|
||||||
const session = getSession();
|
const { session } = stores();
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<h1>{$session.title}</h1>
|
<h1>{$session.title}</h1>
|
||||||
|
|||||||
Reference in New Issue
Block a user