-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support shared signals in Preact islands (#4763)
* Support signals in Preact islands * Add a changeset * Only add signals if we need them * Refactor signal logic into its own module * Keep track of the signals used
- Loading branch information
Showing
21 changed files
with
272 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
'@astrojs/preact': minor | ||
'astro': patch | ||
--- | ||
|
||
Shared state in Preact components with signals | ||
|
||
This makes it possible to share client state between Preact islands via signals. | ||
|
||
For example, you can create a signals in an Astro component and then pass it to multiple islands: | ||
|
||
```astro | ||
--- | ||
// Component Imports | ||
import Counter from '../components/Counter'; | ||
import { signal } from '@preact/signals'; | ||
const count = signal(0); | ||
--- | ||
<Count count={count} /> | ||
<Count count={count} /> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
packages/astro/test/fixtures/preact-component/src/components/Signals.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { h } from 'preact'; | ||
|
||
export default ({ count }) => { | ||
return <div class="preact-signal">{ count }</div> | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/astro/test/fixtures/preact-component/src/pages/signals.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
import Signals from '../components/Signals'; | ||
import { signal } from '@preact/signals'; | ||
const count = signal(1); | ||
--- | ||
<html> | ||
<head> | ||
<title>Testing</title> | ||
</head> | ||
<body> | ||
<Signals client:load count={count} /> | ||
<Signals client:load count={count} /> | ||
</body> | ||
</html> |
1 change: 1 addition & 0 deletions
1
packages/astro/test/fixtures/ssr-response/src/pages/some-header.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/integrations/preact/client-dev.js → ...ges/integrations/preact/src/client-dev.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
// @ts-ignore | ||
import 'preact/debug'; | ||
import clientFn from './client.js'; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type { SignalLike } from './types'; | ||
import { h, render } from 'preact'; | ||
import StaticHtml from './static-html.js'; | ||
|
||
const sharedSignalMap: Map<string, SignalLike> = new Map(); | ||
|
||
export default (element: HTMLElement) => | ||
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) { | ||
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)) { | ||
const signalValue = signal(props[propName]); | ||
sharedSignalMap.set(signalId, signalValue); | ||
} | ||
props[propName] = sharedSignalMap.get(signalId); | ||
} | ||
} | ||
render( | ||
h(Component, props, children != null ? h(StaticHtml, { value: children }) : children), | ||
element | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import type { RendererContext, SignalLike, PropNameToSignalMap } from './types'; | ||
|
||
export type Context = { | ||
id: string; | ||
c: number; | ||
signals: Map<SignalLike, string>; | ||
propsToSignals: Map<Record<string, any>, PropNameToSignalMap>; | ||
}; | ||
|
||
const contexts = new WeakMap<RendererContext['result'], Context>(); | ||
|
||
export function getContext(result: RendererContext['result']): Context { | ||
if (contexts.has(result)) { | ||
return contexts.get(result)!; | ||
} | ||
let ctx = { | ||
c: 0, | ||
get id() { | ||
return 'p' + this.c.toString(); | ||
}, | ||
signals: new Map(), | ||
propsToSignals: new Map() | ||
}; | ||
contexts.set(result, ctx); | ||
return ctx; | ||
} | ||
|
||
export function incrementId(ctx: Context): string { | ||
let id = ctx.id; | ||
ctx.c++; | ||
return id; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import type { AstroPreactAttrs, PropNameToSignalMap, SignalLike } from './types'; | ||
import type { Context } from './context'; | ||
import { incrementId } from './context.js'; | ||
|
||
function isSignal(x: any): x is SignalLike { | ||
return x != null && typeof x === 'object' && typeof x.peek === 'function' && 'value' in x; | ||
} | ||
|
||
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)! | ||
} else { | ||
propMap = new Map(); | ||
ctx.propsToSignals.set(props, 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); | ||
} | ||
signals[key] = id; | ||
} | ||
} | ||
|
||
if(Object.keys(signals).length) { | ||
attrs['data-preact-signals'] = JSON.stringify(signals); | ||
} | ||
} |
Oops, something went wrong.