working shared alien-signals state

main
Laurin Weger 3 weeks ago
parent 5f3b898b42
commit bc17629a67
No known key found for this signature in database
GPG Key ID: 9B372BB0B792770F
  1. 2891
      package-lock.json
  2. 8
      package.json
  3. 7
      src/app/components/Highlight.astro
  4. 2
      src/app/components/SvelteRoot.svelte
  5. 6
      src/app/pages/index.astro
  6. 0
      src/frontends/objectState.ts
  7. 14
      src/frontends/react/HelloWorld.tsx
  8. 0
      src/frontends/react/SvelteWrapper.tsx
  9. 0
      src/frontends/react/VueWrapper.tsx
  10. 19
      src/frontends/svelte/HelloWorld.svelte
  11. 22
      src/frontends/svelte/mount.ts
  12. 23
      src/frontends/vue/HelloWorld.vue
  13. 8
      src/frontends/vue/mount.ts
  14. 45
      src/ng-mock/js-land/signalLibs/alienSignals.ts

2891
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -15,9 +15,17 @@
"@astrojs/react": "4.3.0", "@astrojs/react": "4.3.0",
"@astrojs/svelte": "7.1.0", "@astrojs/svelte": "7.1.0",
"@astrojs/vue": "^5.1.0", "@astrojs/vue": "^5.1.0",
"@gn8/alien-signals-react": "^0.1.1",
"@gn8/alien-signals-solid": "^0.1.1",
"@gn8/alien-signals-svelte": "^0.1.1",
"@gn8/alien-signals-vue": "^0.1.1",
"@types/react": "19.1.10", "@types/react": "19.1.10",
"@types/react-dom": "19.1.7", "@types/react-dom": "19.1.7",
"alien-deepsignals": "^0.1.0",
"alien-signals": "^2.0.7",
"astro": "5.13.2", "astro": "5.13.2",
"install": "^0.13.0",
"npm": "^11.5.2",
"react": "19.1.1", "react": "19.1.1",
"react-dom": "19.1.1", "react-dom": "19.1.1",
"svelte": "5.38.2", "svelte": "5.38.2",

@ -1,11 +1,4 @@
--- ---
interface Props {
react?: boolean;
svelte?: boolean;
vue?: boolean;
astro?: boolean;
}
const { react, svelte, vue, astro } = Astro.props; const { react, svelte, vue, astro } = Astro.props;
const frameworkName = Object.keys(Astro.props)[0]; const frameworkName = Object.keys(Astro.props)[0];

@ -1,7 +1,5 @@
<script lang="ts"> <script lang="ts">
import HelloWorld from "src/frontends/svelte/HelloWorld.svelte"; import HelloWorld from "src/frontends/svelte/HelloWorld.svelte";
//
</script> </script>
<HelloWorld /> <HelloWorld />

@ -11,14 +11,14 @@ const title = "Multi-framework app";
<Layout title={title}> <Layout title={title}>
<Highlight vue> <Highlight vue>
<VueRoot client:load /> <VueRoot client:only />
</Highlight> </Highlight>
<Highlight react> <Highlight react>
<ReactRoot client:visible /> <ReactRoot client:only="react" />
</Highlight> </Highlight>
<Highlight svelte> <Highlight svelte>
<SvelteRoot client:load /> <SvelteRoot client:only />
</Highlight> </Highlight>
</Layout> </Layout>

@ -1,9 +1,23 @@
import React from "react"; import React from "react";
import { useSignal } from "@gn8/alien-signals-react";
import {
deepSignalExample,
computedSignalFromExample,
} from "src/ng-mock/js-land/signalLibs/alienSignals";
export function HelloWorldReact() { export function HelloWorldReact() {
const [state] = useSignal(deepSignalExample);
const [computed] = useSignal(computedSignalFromExample);
return ( return (
<div> <div>
<p>Hello World from React!</p> <p>Hello World from React!</p>
<p>Here is a reactive object with {computed.get()} props.</p>
<pre>{JSON.stringify(state, null, 4)}</pre>
<button onClick={() => state.array.push(state.array.at(-1) + 1)}>
Click to add val
</button>
</div> </div>
); );
} }

@ -1,7 +1,24 @@
<script> <script>
// Simple Svelte component import { useSignal } from "@gn8/alien-signals-svelte";
import {
deepSignalExample,
computedSignalFromExample,
} from "src/ng-mock/js-land/signalLibs/alienSignals";
const nestedObject = useSignal(deepSignalExample);
const computed = useSignal(computedSignalFromExample);
</script> </script>
<div> <div>
<p>Hello World from Svelte!</p> <p>Hello World from Svelte!</p>
<p>Here is a reactive object with {$computed.get()} props.</p>
<pre>{JSON.stringify($nestedObject, null, 4)}</pre>
<button
on:click={() => {
$nestedObject.array.splice(0, 1);
}}
>
Click to remove first array element
</button>
</div> </div>

@ -1,22 +0,0 @@
// Simple Svelte-like component using vanilla JS
export function mountSvelteComponent(element: HTMLElement) {
// Create the DOM structure that would be created by the Svelte component
element.innerHTML = `
<div style="
padding: 20px;
border: 2px solid #000000ff;
border-radius: 8px;
margin: 10px;
background-color: #fff5f5;
">
<h2>Svelte Component</h2>
<p>Hello World from Svelte!</p>
</div>
`;
return {
$destroy: () => {
element.innerHTML = "";
},
};
}

@ -1,9 +1,22 @@
<script setup lang="ts">
import {useSignal} from '@gn8/alien-signals-vue';
import {
deepSignalExample,
computedSignalFromExample,
} from "src/ng-mock/js-land/signalLibs/alienSignals";
const state = useSignal(deepSignalExample);
const computed = useSignal(computedSignalFromExample);
</script>
<template> <template>
<div> <div>
<p>Hello World from Vue!</p> <p>Hello World from Vue!</p>
<p>Here is a reactive object with {{computed.get()}} props.</p>
<pre>{{JSON.stringify(state, null, 4)}}</pre>
<button @click="state.array.push(state.array.at(-1) + 1)">
Click to add val
</button>
</div> </div>
</template> </template>
<script setup lang="ts">
// Simple Vue component with composition API
</script>

@ -1,8 +0,0 @@
import { createApp } from "vue";
import HelloworldVue from "./HelloWorld.vue";
export function mountVueComponent(element: HTMLElement) {
const app = createApp(HelloworldVue);
app.mount(element);
return app;
}

@ -0,0 +1,45 @@
import { deepSignal, watch, computed } from "alien-deepsignals";
import { signal } from "alien-signals";
const deepSignalExample = deepSignal({
count: 0,
name: "John",
nested: {
deep: "value",
},
array: [1, 2, 3],
set: new Set(["el1"]),
});
const computedSignalFromExample = computed(() => {
return Object.keys(deepSignalExample).length;
});
/** Create a vanilla alien-signals Signal so that it can be normally used by universe-alien-signals. */
const wrapDeepIntoAlienSignal = <T extends object>(
nestedSignal: ReturnType<typeof deepSignal<T>>
) => {
const alienSignal = signal(nestedSignal);
// Watch deep signal and notify vanilla alien-signal on changes.
watch(
nestedSignal,
(value) => {
// We need to destructure because the object otherwise remained the same.
alienSignal({ ...nestedSignal });
},
{ deep: true }
);
return alienSignal;
};
const wrappedDeepSignalState = wrapDeepIntoAlienSignal(deepSignalExample);
const wrappedComputedPropertiesOfObj = wrapDeepIntoAlienSignal(
computedSignalFromExample
);
export {
wrappedDeepSignalState as deepSignalExample,
wrappedComputedPropertiesOfObj as computedSignalFromExample,
};
Loading…
Cancel
Save