-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
初步建立起 RSC 的直观体验 #343
Comments
fetch("http://localhost:3000/rsc")
.then((response) => response.body)
.then((rb) => {
const reader = rb.getReader();
return new ReadableStream({
start(controller) {
// The following function handles each data chunk
let count = 1;
function push() {
// "done" is a Boolean and value a "Uint8Array"
reader.read().then(({ done, value }) => {
// If there is no more data to read
if (done) {
console.log("forward done");
controller.close();
return;
}
// Get the data and send it to the browser via the controller
controller.enqueue(value);
// Check chunks by logging to the console
console.log('forward chunk ' + count, new TextDecoder().decode(value));
count += 1;
push();
});
}
push();
},
});
})
.then((stream) =>
// Respond with our stream
new Response(stream, { headers: { "Content-Type": "text/plain" } }),
)
.then(async (result) => {
// Do things with result
console.log({result});
let count = 1;
for await (const chunk of (await result).body) {
console.log('receive chunk ' + count, new TextDecoder().decode(chunk));
count += 1;
}
console.log('receive done');
}); 上面的代码是先请求一个返回 stream 的接口,然后再用 stream 转发出去,作为 reponse 返回,然后再模拟接收。 可以看到 stream 真正的优势:一边接收,一边发送;一边接收,一边处理。 这是 RSC 背后最根本的技术支撑,背后依托的是网络层和服务端的新一代基础设施,如果不用 stream 处理,那 RSC 作为一个创新性的 idea,在实际业务中的表现和使用场景,将会受到极大限制,或许足以让这个 idea 本身变得不切实际。 用了 stream 之后,可以比较放心的使用 rsc 返回较大组件;以及,未来在服务端,有希望在多个 server 之间串联 RSC,做到跨语言,跨团队,跨基础设施,组合「UI 页面」。 RSC 提供的两个最基础能力:
RSC 之上的第三方工具库和全栈框架,需要解决更复杂微妙的问题:
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The text was updated successfully, but these errors were encountered: