Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: createServerValue #1889

Closed
wants to merge 5 commits 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
11 changes: 11 additions & 0 deletions packages/solid/src/render/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,3 +388,14 @@ export function createUniqueId(): string {
const ctx = sharedConfig.context;
return ctx ? `${ctx.id}${ctx.count++}` : `cl-${counter++}`;
}

export function createServerValue<T>(cb: () => T): T {
const ctx = sharedConfig.context;
if (ctx && sharedConfig.load) {
const id = `${ctx.id}${ctx.count++}`;
if (sharedConfig.has(id)) {
return sharedConfig.load(id);
}
}
return cb();
}
12 changes: 12 additions & 0 deletions packages/solid/src/server/rendering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ export function createUniqueId(): string {
return `${ctx.id}${ctx.count++}`;
}

export function createServerValue<T>(cb: () => T): T {
const ctx = sharedConfig.context;
// This might be fun specially if the value is a Promise
// but of course, the only way they can translate the Promise

Choose a reason for hiding this comment

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

what do you mean by "translte the Promise"

Copy link
Member Author

Choose a reason for hiding this comment

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

convert the Promise into something the components and the reactivity system can understand

// is through createResource
const value = cb();
if (ctx) {
ctx.serialize(`${ctx.id}${ctx.count++}`, value, true);
}
return value;
}

export function createComponent<T>(Comp: (props: T) => JSX.Element, props: T): JSX.Element {
if (sharedConfig.context && !sharedConfig.context.noHydrate) {
const c = sharedConfig.context;
Expand Down
Loading