Skip to content

Commit

Permalink
Revert "[ci] format"
Browse files Browse the repository at this point in the history
This reverts commit e3c78c5.
  • Loading branch information
matthewp committed Sep 22, 2022
1 parent 43a030a commit 1e7adaf
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 55 deletions.
2 changes: 1 addition & 1 deletion examples/framework-preact/src/components/Counter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
1 change: 1 addition & 0 deletions packages/astro/test/preact-component.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ 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);

Expand Down
2 changes: 1 addition & 1 deletion packages/astro/test/ssr-response.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
});
});
14 changes: 5 additions & 9 deletions packages/integrations/preact/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
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 });
}
let signalsRaw = element.dataset.preactSignals;
if (signalsRaw) {
if(signalsRaw) {
const { signal } = await import('@preact/signals');
let signals: Record<string, string> = 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);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/integrations/preact/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PropNameToSignalMap, RendererContext, SignalLike } from './types';
import type { RendererContext, SignalLike, PropNameToSignalMap } from './types';

export type Context = {
id: string;
Expand All @@ -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;
Expand Down
16 changes: 6 additions & 10 deletions packages/integrations/preact/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Component as BaseComponent, h } from 'preact';
import type { AstroPreactAttrs, RendererContext } from './types';
import { h, Component as BaseComponent } 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());

Expand Down Expand Up @@ -38,12 +38,7 @@ 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>> = {};
Expand All @@ -65,10 +60,11 @@ function renderToStaticMarkup(
);
return {
attrs,
html,
html
};
}


/**
* Reduces console noise by filtering known non-problematic errors.
*
Expand Down
55 changes: 25 additions & 30 deletions packages/integrations/preact/src/signals.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,45 +9,40 @@ function isSignal(x: any): x is SignalLike {
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)!;
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<string, any>,
attrs: AstroPreactAttrs,
map: PropNameToSignalMap
) {
// Check for signals
const signals: Record<string, string> = {};
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);
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)) {
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;
}
signals[key] = id;
}
}

if (Object.keys(signals).length) {
attrs['data-preact-signals'] = JSON.stringify(signals);
}
if(Object.keys(signals).length) {
attrs['data-preact-signals'] = JSON.stringify(signals);
}
}
2 changes: 1 addition & 1 deletion packages/integrations/preact/src/static-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } });
};
Expand Down
2 changes: 1 addition & 1 deletion packages/integrations/preact/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ export type SignalLike = {
export type PropNameToSignalMap = Map<string, SignalLike>;

export type AstroPreactAttrs = {
['data-preact-signals']?: string;
['data-preact-signals']?: string
};

0 comments on commit 1e7adaf

Please sign in to comment.