From e3c78c5b164c338389c437743ba02a7be64e27fb Mon Sep 17 00:00:00 2001 From: matthewp Date: Wed, 21 Sep 2022 19:23:58 +0000 Subject: [PATCH] [ci] format --- .../src/components/Counter.tsx | 2 +- packages/astro/test/preact-component.test.js | 1 - packages/astro/test/ssr-response.test.js | 2 +- packages/integrations/preact/src/client.ts | 14 +++-- packages/integrations/preact/src/context.ts | 4 +- packages/integrations/preact/src/server.ts | 16 ++++-- packages/integrations/preact/src/signals.ts | 57 ++++++++++--------- .../integrations/preact/src/static-html.ts | 2 +- packages/integrations/preact/src/types.ts | 2 +- 9 files changed, 56 insertions(+), 44 deletions(-) diff --git a/examples/framework-preact/src/components/Counter.tsx b/examples/framework-preact/src/components/Counter.tsx index c2b065e3c..5d702fb42 100644 --- a/examples/framework-preact/src/components/Counter.tsx +++ b/examples/framework-preact/src/components/Counter.tsx @@ -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 ( diff --git a/packages/astro/test/preact-component.test.js b/packages/astro/test/preact-component.test.js index 68822d10e..a18d3c97a 100644 --- a/packages/astro/test/preact-component.test.js +++ b/packages/astro/test/preact-component.test.js @@ -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); diff --git a/packages/astro/test/ssr-response.test.js b/packages/astro/test/ssr-response.test.js index d0dbdbff3..ffbc41aa5 100644 --- a/packages/astro/test/ssr-response.test.js +++ b/packages/astro/test/ssr-response.test.js @@ -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`); }); }); diff --git a/packages/integrations/preact/src/client.ts b/packages/integrations/preact/src/client.ts index c7a31d60c..4549e79f4 100644 --- a/packages/integrations/preact/src/client.ts +++ b/packages/integrations/preact/src/client.ts @@ -1,21 +1,25 @@ -import type { SignalLike } from './types'; import { h, render } from 'preact'; import StaticHtml from './static-html.js'; +import type { SignalLike } from './types'; const sharedSignalMap: Map = new Map(); export default (element: HTMLElement) => - async (Component: any, props: Record, { default: children, ...slotted }: Record) => { + async ( + Component: any, + props: Record, + { default: children, ...slotted }: Record + ) => { if (!element.hasAttribute('ssr')) return; for (const [key, value] of Object.entries(slotted)) { props[key] = h(StaticHtml, { value, name: key }); } let signalsRaw = element.dataset.preactSignals; - if(signalsRaw) { + if (signalsRaw) { const { signal } = await import('@preact/signals'); let signals: Record = JSON.parse(element.dataset.preactSignals as string); - for(const [propName, signalId] of Object.entries(signals)) { - if(!sharedSignalMap.has(signalId)) { + for (const [propName, signalId] of Object.entries(signals)) { + if (!sharedSignalMap.has(signalId)) { const signalValue = signal(props[propName]); sharedSignalMap.set(signalId, signalValue); } diff --git a/packages/integrations/preact/src/context.ts b/packages/integrations/preact/src/context.ts index 73c4402b0..c711017c4 100644 --- a/packages/integrations/preact/src/context.ts +++ b/packages/integrations/preact/src/context.ts @@ -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; diff --git a/packages/integrations/preact/src/server.ts b/packages/integrations/preact/src/server.ts index 2c1ac738a..212e183cf 100644 --- a/packages/integrations/preact/src/server.ts +++ b/packages/integrations/preact/src/server.ts @@ -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 } } -function renderToStaticMarkup(this: RendererContext, Component: any, props: Record, { default: children, ...slotted }: Record) { +function renderToStaticMarkup( + this: RendererContext, + Component: any, + props: Record, + { default: children, ...slotted }: Record +) { const ctx = getContext(this.result); const slots: Record> = {}; @@ -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. * diff --git a/packages/integrations/preact/src/signals.ts b/packages/integrations/preact/src/signals.ts index db62961aa..3fa1529f4 100644 --- a/packages/integrations/preact/src/signals.ts +++ b/packages/integrations/preact/src/signals.ts @@ -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; @@ -9,40 +9,45 @@ function isSignal(x: any): x is SignalLike { export function restoreSignalsOnProps(ctx: Context, props: Record) { // Restore signal props that were mutated for serialization let propMap: PropNameToSignalMap; - if(ctx.propsToSignals.has(props)) { - propMap = ctx.propsToSignals.get(props)! + if (ctx.propsToSignals.has(props)) { + propMap = ctx.propsToSignals.get(props)!; } else { propMap = new Map(); ctx.propsToSignals.set(props, propMap); } - for(const [key, signal] of propMap) { + for (const [key, signal] of propMap) { props[key] = signal; } return propMap; } -export function serializeSignals(ctx: Context, props: Record, attrs: AstroPreactAttrs, map: PropNameToSignalMap){ - // Check for signals - const signals: Record = {}; - for(const [key, value] of Object.entries(props)) { - if(isSignal(value)) { - // Set the value to the current signal value - // This mutates the props on purpose, so that it will be serialized correct. - props[key] = value.peek(); - map.set(key, value); - - let id: string; - if(ctx.signals.has(value)) { - id = ctx.signals.get(value)!; - } else { - id = incrementId(ctx); - ctx.signals.set(value, id); - } - signals[key] = id; - } - } +export function serializeSignals( + ctx: Context, + props: Record, + attrs: AstroPreactAttrs, + map: PropNameToSignalMap +) { + // Check for signals + const signals: Record = {}; + for (const [key, value] of Object.entries(props)) { + if (isSignal(value)) { + // Set the value to the current signal value + // This mutates the props on purpose, so that it will be serialized correct. + props[key] = value.peek(); + map.set(key, value); - if(Object.keys(signals).length) { - attrs['data-preact-signals'] = JSON.stringify(signals); + let id: string; + if (ctx.signals.has(value)) { + id = ctx.signals.get(value)!; + } else { + id = incrementId(ctx); + ctx.signals.set(value, id); + } + signals[key] = id; } + } + + if (Object.keys(signals).length) { + attrs['data-preact-signals'] = JSON.stringify(signals); + } } diff --git a/packages/integrations/preact/src/static-html.ts b/packages/integrations/preact/src/static-html.ts index e474caa5a..e1127d226 100644 --- a/packages/integrations/preact/src/static-html.ts +++ b/packages/integrations/preact/src/static-html.ts @@ -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 } }); }; diff --git a/packages/integrations/preact/src/types.ts b/packages/integrations/preact/src/types.ts index e5058f5f3..93f65bbc2 100644 --- a/packages/integrations/preact/src/types.ts +++ b/packages/integrations/preact/src/types.ts @@ -10,5 +10,5 @@ export type SignalLike = { export type PropNameToSignalMap = Map; export type AstroPreactAttrs = { - ['data-preact-signals']?: string + ['data-preact-signals']?: string; };