[ci] format

This commit is contained in:
matthewp 2022-09-21 19:23:58 +00:00 committed by fredkbot
parent 5e46be5468
commit e3c78c5b16
9 changed files with 56 additions and 44 deletions

View file

@ -2,7 +2,7 @@ import { h, Fragment } from 'preact';
import './Counter.css';
export default function Counter({ children, count }) {
const add = () => count.value++
const add = () => count.value++;
const subtract = () => count.value--;
return (

View file

@ -93,7 +93,6 @@ describe('Preact component', () => {
expect(sigs1Raw).to.not.be.undefined;
expect(sigs2Raw).to.not.be.undefined;
const sigs1 = JSON.parse(sigs1Raw);
const sigs2 = JSON.parse(sigs2Raw);

View file

@ -36,6 +36,6 @@ describe('Using Astro.response in SSR', () => {
const headers = response.headers;
expect(headers.get('one-two')).to.equal('three');
expect(headers.get('four-five')).to.equal('six');
expect(headers.get('Cache-Control')).to.equal(`max-age=0, s-maxage=86400`)
expect(headers.get('Cache-Control')).to.equal(`max-age=0, s-maxage=86400`);
});
});

View file

@ -1,11 +1,15 @@
import type { SignalLike } from './types';
import { h, render } from 'preact';
import StaticHtml from './static-html.js';
import type { SignalLike } from './types';
const sharedSignalMap: Map<string, SignalLike> = new Map();
export default (element: HTMLElement) =>
async (Component: any, props: Record<string, any>, { default: children, ...slotted }: Record<string, any>) => {
async (
Component: any,
props: Record<string, any>,
{ default: children, ...slotted }: Record<string, any>
) => {
if (!element.hasAttribute('ssr')) return;
for (const [key, value] of Object.entries(slotted)) {
props[key] = h(StaticHtml, { value, name: key });

View file

@ -1,4 +1,4 @@
import type { RendererContext, SignalLike, PropNameToSignalMap } from './types';
import type { PropNameToSignalMap, RendererContext, SignalLike } from './types';
export type Context = {
id: string;
@ -19,7 +19,7 @@ export function getContext(result: RendererContext['result']): Context {
return 'p' + this.c.toString();
},
signals: new Map(),
propsToSignals: new Map()
propsToSignals: new Map(),
};
contexts.set(result, ctx);
return ctx;

View file

@ -1,9 +1,9 @@
import type { AstroPreactAttrs, RendererContext } from './types';
import { h, Component as BaseComponent } from 'preact';
import { Component as BaseComponent, h } from 'preact';
import render from 'preact-render-to-string';
import StaticHtml from './static-html.js';
import { getContext } from './context.js';
import { restoreSignalsOnProps, serializeSignals } from './signals.js';
import StaticHtml from './static-html.js';
import type { AstroPreactAttrs, RendererContext } from './types';
const slotName = (str: string) => str.trim().replace(/[-_]([a-z])/g, (_, w) => w.toUpperCase());
@ -38,7 +38,12 @@ function check(this: RendererContext, Component: any, props: Record<string, any>
}
}
function renderToStaticMarkup(this: RendererContext, Component: any, props: Record<string, any>, { default: children, ...slotted }: Record<string, any>) {
function renderToStaticMarkup(
this: RendererContext,
Component: any,
props: Record<string, any>,
{ default: children, ...slotted }: Record<string, any>
) {
const ctx = getContext(this.result);
const slots: Record<string, ReturnType<typeof h>> = {};
@ -60,11 +65,10 @@ function renderToStaticMarkup(this: RendererContext, Component: any, props: Reco
);
return {
attrs,
html
html,
};
}
/**
* Reduces console noise by filtering known non-problematic errors.
*

View file

@ -1,6 +1,6 @@
import type { AstroPreactAttrs, PropNameToSignalMap, SignalLike } from './types';
import type { Context } from './context';
import { incrementId } from './context.js';
import type { AstroPreactAttrs, PropNameToSignalMap, SignalLike } from './types';
function isSignal(x: any): x is SignalLike {
return x != null && typeof x === 'object' && typeof x.peek === 'function' && 'value' in x;
@ -10,7 +10,7 @@ export function restoreSignalsOnProps(ctx: Context, props: Record<string, any>)
// Restore signal props that were mutated for serialization
let propMap: PropNameToSignalMap;
if (ctx.propsToSignals.has(props)) {
propMap = ctx.propsToSignals.get(props)!
propMap = ctx.propsToSignals.get(props)!;
} else {
propMap = new Map();
ctx.propsToSignals.set(props, propMap);
@ -21,7 +21,12 @@ export function restoreSignalsOnProps(ctx: Context, props: Record<string, any>)
return propMap;
}
export function serializeSignals(ctx: Context, props: Record<string, any>, attrs: AstroPreactAttrs, map: PropNameToSignalMap){
export function serializeSignals(
ctx: Context,
props: Record<string, any>,
attrs: AstroPreactAttrs,
map: PropNameToSignalMap
) {
// Check for signals
const signals: Record<string, string> = {};
for (const [key, value] of Object.entries(props)) {

View file

@ -7,7 +7,7 @@ import { h } from 'preact';
* As a bonus, we can signal to Preact that this subtree is
* entirely static and will never change via `shouldComponentUpdate`.
*/
const StaticHtml = ({ value, name }: { value: string; name?: string; }) => {
const StaticHtml = ({ value, name }: { value: string; name?: string }) => {
if (!value) return null;
return h('astro-slot', { name, dangerouslySetInnerHTML: { __html: value } });
};

View file

@ -10,5 +10,5 @@ export type SignalLike = {
export type PropNameToSignalMap = Map<string, SignalLike>;
export type AstroPreactAttrs = {
['data-preact-signals']?: string
['data-preact-signals']?: string;
};