Skip to content

Commit 6f64812

Browse files
committed
make Payload a class
1 parent 926331f commit 6f64812

File tree

4 files changed

+25
-26
lines changed

4 files changed

+25
-26
lines changed

packages/svelte/src/internal/server/blocks/snippet.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/** @import { Snippet } from 'svelte' */
2-
/** @import { Payload } from '#server' */
2+
/** @import { Payload } from '../index' */
33
/** @import { Getters } from '#shared' */
44

55
/**

packages/svelte/src/internal/server/dev.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
/** @import { Component, Payload } from '#server' */
1+
/** @import { Component } from '#server' */
22
import { FILENAME } from '../../constants.js';
33
import {
44
is_tag_valid_with_ancestor,
55
is_tag_valid_with_parent
66
} from '../../html-tree-validation.js';
77
import { current_component } from './context.js';
88
import { invalid_snippet_arguments } from '../shared/errors.js';
9+
import { Payload } from './index.js';
910

1011
/**
1112
* @typedef {{
@@ -104,7 +105,7 @@ export function pop_element() {
104105
* @param {Payload} payload
105106
*/
106107
export function validate_snippet_args(payload) {
107-
if (typeof payload !== 'object' || !('out' in payload)) {
108+
if (typeof payload !== 'object' || !(payload instanceof Payload)) {
108109
invalid_snippet_arguments();
109110
}
110111
}

packages/svelte/src/internal/server/index.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/** @import { ComponentType, SvelteComponent } from 'svelte' */
2-
/** @import { Component, Payload, RenderOutput } from '#server' */
2+
/** @import { Component, RenderOutput } from '#server' */
33
/** @import { Store } from '#shared' */
44
export { FILENAME, HMR } from '../../constants.js';
55
import { attr, clsx, to_class, to_style } from '../shared/attributes.js';
@@ -96,6 +96,24 @@ function props_id_generator(prefix) {
9696
return () => `${prefix}s${uid++}`;
9797
}
9898

99+
class Payload {
100+
out = '';
101+
/**@type {Set<{ hash: string; code: string }>} */
102+
css = new Set();
103+
uid = () => '';
104+
head = {
105+
/**@type {Set<{ hash: string; code: string }>} */
106+
css: new Set(),
107+
title: '',
108+
out: '',
109+
uid: () => ''
110+
};
111+
constructor(idPrefix = '') {
112+
this.uid = props_id_generator(idPrefix);
113+
this.head.uid = this.uid;
114+
}
115+
}
116+
99117
/**
100118
* Only available on the server and when compiling with the `server` option.
101119
* Takes a component and returns an object with `body` and `head` properties on it, which you can use to populate the HTML when server-rendering your app.
@@ -105,14 +123,7 @@ function props_id_generator(prefix) {
105123
* @returns {RenderOutput}
106124
*/
107125
export function render(component, options = {}) {
108-
const uid = props_id_generator(options.idPrefix ? options.idPrefix + '-' : '');
109-
/** @type {Payload} */
110-
const payload = {
111-
out: '',
112-
css: new Set(),
113-
head: { title: '', out: '', css: new Set(), uid },
114-
uid
115-
};
126+
const payload = new Payload(options.idPrefix ? options.idPrefix + '' : '');
116127

117128
const prev_on_destroy = on_destroy;
118129
on_destroy = [];
@@ -535,7 +546,7 @@ export function props_id(payload) {
535546
return uid;
536547
}
537548

538-
export { attr, clsx };
549+
export { attr, clsx, Payload };
539550

540551
export { html } from './blocks/html.js';
541552

packages/svelte/src/internal/server/types.d.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,6 @@ export interface Component {
1111
function?: any;
1212
}
1313

14-
export interface Payload {
15-
out: string;
16-
css: Set<{ hash: string; code: string }>;
17-
head: {
18-
title: string;
19-
out: string;
20-
uid: () => string;
21-
css: Set<{ hash: string; code: string }>;
22-
};
23-
/** Function that generates a unique ID */
24-
uid: () => string;
25-
}
26-
2714
export interface RenderOutput {
2815
/** HTML that goes into the `<head>` */
2916
head: string;

0 commit comments

Comments
 (0)