Skip to content

Commit 5370c41

Browse files
author
Gerard Moreno-Torres Bertran
committed
add example
1 parent 68006c8 commit 5370c41

File tree

7 files changed

+186
-11
lines changed

7 files changed

+186
-11
lines changed

.vscode/launch.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Launch Program",
11+
"program": "${workspaceFolder}/src/index.js"
12+
}
13+
]
14+
}

dist/example.js

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/example.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,18 @@
33
"version": "0.1.4",
44
"main": "./src/index.js",
55
"dependencies": {
6-
"react-reconciler": "^0.22.2"
7-
},
8-
"peerDependencies": {
6+
"react-reconciler": "^0.22.2",
97
"react": "^16.10.2"
108
},
119
"eslintConfig": {
1210
"extends": "react-app"
1311
},
1412
"babel": {
15-
"presets": [
16-
"react-app"
17-
]
13+
"presets": []
1814
},
1915
"devDependencies": {
2016
"@babel/core": "^7.6.4",
17+
"@babel/runtime": "^7.6.3",
2118
"babel-preset-react-app": "^9.0.2",
2219
"parcel-bundler": "^1.12.4"
2320
}

src/ReactJSON.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Reconciler from "react-reconciler";
22

3-
const createHostConfig = () => ({
3+
const createHostConfig = callback => ({
44
now: Date.now,
55
getRootHostContext: () => {
66
let rootContext = {};
@@ -24,7 +24,7 @@ const createHostConfig = () => ({
2424
acc[key] = newProps[key];
2525
return acc;
2626
},
27-
{ type }
27+
{ type, children: [] }
2828
);
2929
},
3030
appendChildToContainer: (parent, child) => {
@@ -57,7 +57,9 @@ const createHostConfig = () => ({
5757
commitTextUpdate: (textInstance, oldText, newText) => {
5858
textInstance.value = newText;
5959
},
60-
commitUpdate: () => {},
60+
commitUpdate: () => {
61+
callback();
62+
},
6163
appendChild: (parentInstance, child) => {
6264
parentInstance.children.push(child);
6365
},
@@ -87,7 +89,7 @@ const createHostConfig = () => ({
8789
const ReactJSON = {
8890
mount(element, renderDom, callback) {
8991
const isAsync = false;
90-
const reconcilerInstance = Reconciler(createHostConfig());
92+
const reconcilerInstance = Reconciler(createHostConfig(callback));
9193
const container = reconcilerInstance.createContainer(renderDom, isAsync);
9294

9395
const parentComponent = null;

src/example.js

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import React, { useEffect, useState } from "react";
2+
import ReactJSON from "./ReactJSON";
3+
4+
const root = {};
5+
6+
const usePlayerState = () => {
7+
const [hand, setHand] = useState([]);
8+
const [graveyard, setGraveyard] = useState([]);
9+
10+
return {
11+
hand,
12+
setHand,
13+
graveyard,
14+
setGraveyard
15+
};
16+
};
17+
18+
const Events = createEvents();
19+
20+
function createEvents() {
21+
const subscriptions = {};
22+
23+
return {
24+
subscribe(key, callback) {
25+
if (!subscriptions[key]) {
26+
subscriptions[key] = [];
27+
}
28+
29+
const subscriptionsForKey = subscriptions[key];
30+
31+
const callbackWithUniqueHash = () => callback();
32+
33+
subscriptionsForKey.push(callbackWithUniqueHash);
34+
35+
return {
36+
cancel: () => {
37+
subscriptionsForKey.splice(
38+
subscriptionsForKey.indexOf(callbackWithUniqueHash),
39+
1
40+
);
41+
}
42+
};
43+
},
44+
inform(key) {
45+
const subscriptionsForKey = subscriptions[key];
46+
47+
if (!subscriptionsForKey || !subscriptionsForKey.length) {
48+
return;
49+
}
50+
51+
subscriptionsForKey.forEach(callback => callback());
52+
}
53+
};
54+
}
55+
56+
const useDeckState = () => {
57+
const [cards, setCards] = useState([
58+
"Ace of hearts",
59+
"Tow of hearts",
60+
"Three of hearts",
61+
"Four of hearts",
62+
"Five of hearts",
63+
"Six of hearts",
64+
"Seven of hearts",
65+
"Eight of hearts",
66+
"Nine of hearts",
67+
"Ten of hearts",
68+
"Jack of hearts",
69+
"Queen of hearts",
70+
"King of hearts"
71+
]);
72+
73+
return {
74+
draw: () => {
75+
const poppedCard = cards.pop();
76+
setCards(cards);
77+
return poppedCard;
78+
},
79+
cards
80+
};
81+
};
82+
83+
const App = () => {
84+
const [turn, setTurn] = useState(0);
85+
86+
const deckState = useDeckState();
87+
88+
const firstPlayerState = usePlayerState();
89+
const secondPlayerState = usePlayerState();
90+
91+
const activePlayer = turn % 2 === 0 ? firstPlayerState : secondPlayerState;
92+
93+
useEffect(() => {
94+
activePlayer.setHand(hand => [...hand, deckState.draw()]);
95+
}, [turn]);
96+
97+
useEffect(() => {
98+
const subscription = Events.subscribe("NEXT_TURN", () => {
99+
setTurn(turn => turn + 1);
100+
});
101+
102+
return () => subscription.cancel();
103+
}, []);
104+
105+
return (
106+
<root>
107+
<firstPlayer>
108+
<hand>
109+
{firstPlayerState.hand.map((card, index) => (
110+
<card key={index}>{card}</card>
111+
))}
112+
</hand>
113+
</firstPlayer>
114+
<secondPlayer>
115+
<hand>
116+
{secondPlayerState.hand.map((card, index) => (
117+
<card key={index}>{card}</card>
118+
))}
119+
</hand>
120+
</secondPlayer>
121+
<deck>
122+
{deckState.cards.map((card, index) => (
123+
<card key={index}>{card}</card>
124+
))}
125+
</deck>
126+
</root>
127+
);
128+
};
129+
130+
const log = () => {
131+
console.log(JSON.stringify(root, null, 2));
132+
console.log("----------");
133+
};
134+
135+
ReactJSON.mount(<App />, root, log);
136+
137+
setTimeout(log, 1000);
138+
139+
Events.inform("NEXT_TURN");
140+
141+
setTimeout(log, 1000);

0 commit comments

Comments
 (0)