Meteor 的反应性在幕后如何运作? [英] How does Meteor's reactivity work behind the scenes?

查看:13
本文介绍了Meteor 的反应性在幕后如何运作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读 docs 并查看了 反应性背后的来源,但我不明白.

I have read the docs and looked at the source behind reactivity, but I don't understand it.

有人可以解释一下这在幕后是如何工作的,因为对我来说它看起来很神奇:)

Can someone explain how this works behind the scenes, as it looks like magic to me :).

推荐答案

所以它实际上相当简单,在基本层面上涉及两种类型的功能:

So it's actually rather straight forward, at a basic level there are 2 types of functions involved:

  1. 创建反应上下文的函数(反应函数)

  1. Functions that create a reactive context (reactive function)

使响应式上下文无效的函数(使函数无效)

Functions that invalidate a reactive context (invalidating function)

两者兼而有之的功能.(我撒谎有 3 个)

Functions that can do both. (I lied there are 3)

当您调用 reactive 函数 时,它会创建一个 context,meteor 全局存储该 reactive 函数 订阅 invalidation 的 context 回调.您传递给响应式函数的函数,或从其中运行的任何函数,可以是 invalidating function 并且可以获取当前的 context 并将其存储在本地.这些函数可以在任何时候,比如数据库更新或简单的定时器调用,使 context 无效.原始的 reactive 函数 会接收到该事件并重新评估自身.

When you call a reactive function it creates a context that meteor stores globally and to which the reactive function subscribes an invalidation callback. The function that you pass to a reactive function, or any functions that run from within it, can be an invalidating function and can grab the current context and store it locally. These functions can then at any time, like on a db update or simply a timer call, invalidate that context. The original reactive function would then receive that event and re-evaluate itself.

下面是使用meteor函数的步骤(注意Tracker.autorun以前被称为Deps.autorun):

Here's a step by step using meteor functions (note that Tracker.autorun used to be called Deps.autorun):

Tracker.autorun(function(){ 
  alert("Hello " + Session.get("name"));
});

Session.set("name", "Greg");

  1. autorun 以函数为参数
  2. 在 autorun 运行此函数之前,它会创建一个 context
  3. autorun 将回调附加到 context 的失效事件
  4. 此回调将重新运行传递给 autorun 的函数
  5. 然后该函数第一次在 context 中运行.
  6. Meteor 将此 context 全局存储为当前活动的 context
  7. 函数内部还有一个函数:Session.get()
  8. Session.get() 既是一个反应式函数,又是一个失效函数
  9. Session.get 设置它自己的context 并在内部将它与键名称"相关联
  10. Session.get 从meteor 全局检索当前上下文(自动运行的上下文)
  11. Session.get 注册到它自己的上下文的失效回调只会使它的封闭上下文(在本例中为 autorun 的上下文)无效
  12. 所以现在我们有 2 个上下文,autorun 的和 session.get 的
  13. 当这些函数返回时,meteor 会清理活动上下文全局变量

  1. autorun takes a function as its parameter
  2. before autorun runs this function, it creates a context
  3. autorun attaches a callback to the context's invalidation event
  4. This callback will re-run the function passed to autorun
  5. The function is then run in the context for the first time.
  6. Meteor stores this context globally as the currently active context
  7. Inside the function is another function: Session.get()
  8. Session.get() is both a reactive function and an invalidating function
  9. Session.get sets up it's own context and associates it internally with the key "name"
  10. Session.get retrieves the current context (autorun's context) globally from meteor
  11. The invalidation callback that Session.get registers to it's own context, will simply invalidate it's enclosing context (in this case, autorun's context)
  12. So now we have 2 contexts, autorun's and session.get's
  13. when these functions return, meteor cleans up the active context global variable

Session.set 是另一个能够使 context 失效的函数.

Session.set is another function capable of invalidating a context.

整个实现实际上也相当简单,您可以在这里看到:
https://github.com/meteor/meteor/blob/master/packages/tracker/跟踪器.js

可以在此处找到有关其工作原理的一个很好的示例:
https://github.com/meteor/meteor/blob/master/包/reactive-dict/reactive-dict.js

响应式编程实际上并不是针对meteor 或JS 的
你可以在这里阅读:http://en.wikipedia.org/wiki/Reactive_programming

这篇关于Meteor 的反应性在幕后如何运作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆