-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
This reverts commit 25a5b9a.
- Loading branch information
Showing
21 changed files
with
340 additions
and
111 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,33 @@ | ||
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> | ||
) => { | ||
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 { PropNameToSignalMap, RendererContext, SignalLike } 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
Oops, something went wrong.