前端早晚报 - 包含了 Event Loop 是如何工作的和 Solid.js
本周的第一篇文章还是推荐一个查看 Event Loop 的直观工具
A React app to interactively visualize
JavaScript's Event Loop
1、client 端
https://github.com/Hopding/js-visualizer-9000-client
基于 react 的
"dependencies": {
"@material-ui/core": "^3.9.1",
"@material-ui/icons": "^3.0.2",
"brace": "^0.11.1",
"react": "^16.13.0",
"react-ace": "^6.3.2",
"react-dom": "^16.13.0",
"react-measure": "^2.2.4",
"react-pose": "^4.0.6",
"react-scripts": "2.1.8",
"uuid": "^3.3.2"
},
2、server 端
https://github.com/Hopding/js-visualizer-9000-server
基于 ws
const WebSocket = require('ws');
const { launchWorker } = require('./launchWorker');
const { reduceEvents } = require('./eventsReducer');
// Heroku provides a PORT env var that we have to use
const port = process.env.PORT || 8080;
const wss = new WebSocket.Server({ port });
console.log('Running server on port:', port);
const Messages = {
RunCode: 'RunCode',
};
wss.on('connection', (ws) => {
ws.on('message', (message) => {
console.log('Received:', message)
const { type, payload } = JSON.parse(message);
if (type === Messages.RunCode) {
let events = [];
let isFinished = false;
const worker = launchWorker(payload, evtString => {
if (!isFinished) {
const evt = JSON.parse(evtString);
events.push(evt);
if (evt.type === 'Done') {
const reducedEvents = reduceEvents(events);
console.log(reducedEvents.map(JSON.stringify))
ws.send(JSON.stringify(reducedEvents));
}
}
});
} else {
console.error('Unknown message type:', type);
}
});
});
2、Solid.js
其实早在 21 年就有一段时间的讨论,先看看它是啥
import { render } from "solid-js/web";
import { createSignal } from "solid-js";
function Counter() {
const [count, setCount] = createSignal(0);
const increment = () => setCount(count() + 1);
return (
<button type="button" onClick={increment}>
{count()}
</button>
);
}
render(() => <Counter />, document.getElementById("app"));
https://github.com/solidjs/solid
https://typeofnan.dev/solid-js-feels-like-what-i-always-wanted-react-to-be/