Skip to content

Commit

Permalink
Update xState eg with release candidate, new syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
loganvolkers authored Nov 24, 2023
1 parent da38095 commit bf08553
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion website/src/components/CodeEditor.astro
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ to all the elements in Sandpack's UI -->
dependencies: {
...dependencies,
// Must exist
bunshi: "2.0.2",
bunshi: "v2.1.0-rc.1",
},
}}
files={files}
Expand Down
18 changes: 12 additions & 6 deletions website/src/source-examples/xstate/molecules.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { molecule } from "bunshi";
import { molecule, onMount } from "bunshi";
import { assign, createMachine, interpret } from "xstate";

interface CounterContext {
Expand All @@ -11,14 +11,20 @@ type CounterEvent = {

export const CountMolecule = molecule(() => {
const countMachine = createMachine<CounterContext, CounterEvent>({
id: 'counter',
id: "counter",
context: { count: 0 },
on: {
INCREMENT: {
actions: assign({ count: (ctx) => ctx.count + 1 })
}
}
actions: assign({ count: (ctx) => ctx.count + 1 }),
},
},
});

return interpret(countMachine).start();
const actor = interpret(countMachine);

onMount(() => {
actor.start();
return () => actor.stop();
});
return actor;
});
14 changes: 10 additions & 4 deletions website/src/source-examples/xstate/molecules2.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ComponentScope, molecule } from "bunshi";
import { ComponentScope, molecule, onMount, use } from "bunshi";
import { assign, createMachine, interpret } from "xstate";

interface CounterContext {
Expand All @@ -9,8 +9,8 @@ type CounterEvent = {
type: "INCREMENT";
};

export const CountMolecule = molecule((_, scope) => {
scope(ComponentScope);
export const CountMolecule = molecule(() => {
use(ComponentScope);

const countMachine = createMachine<CounterContext, CounterEvent>({
id: "counter",
Expand All @@ -22,5 +22,11 @@ export const CountMolecule = molecule((_, scope) => {
},
});

return interpret(countMachine).start();
const actor = interpret(countMachine);

onMount(() => {
actor.start();
return () => actor.stop();
});
return actor;
});

0 comments on commit bf08553

Please sign in to comment.