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

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

问题描述

我已阅读文档,并查看了

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 :).

推荐答案

所以实际上很简单,在基本级别上涉及2种类型的函数:

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 function时,它会创建一个context,该context会在流星上全局存储,并且reactive function会向其订阅invalidation回调.传递给反应性函数的函数或从该函数中运行的任何函数可以是invalidating function,并且可以获取当前的context并将其存储在本地.然后,这些函数可以在任何时候(例如在db更新中或仅通过计时器调用)使该context无效.原始的reactive function然后将收到该事件并重新评估.

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.

以下是使用流星函数的逐步说明(请注意,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. 自动运行将功能作为参数
  2. 在自动运行运行此功能之前,它会创建一个context
  3. 自动运行将回调添加到context的无效事件
  4. 此回调将重新运行传递给自动运行的功能
  5. 然后该函数首次在context中运行.
  6. 流星将此context全局存储为当前活动的context
  7. 该函数内部还有另一个函数:Session.get()
  8. Session.get()既是reactive function又是invalidating function
  9. Session.get设置它自己的context并将其与键名称"在内部关联
  10. Session.get从流星全局检索当前上下文(自动运行的上下文)
  11. Session.get注册到其自己的上下文中的失效回调将仅使它的封闭上下文(在这种情况下为autorun的上下文)失效
  12. 所以现在我们有2个上下文,分别是autorun和session.get.
  13. 这些函数返回时,流星清理活动上下文全局变量

  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/tracker.js

,这里提供了一个很好的示例:
https://github.com/meteor/meteor/blob/master/包/reactive-dict/reactive-dict.js

反应式编程实际上并不是流星或JS专用
您可以在此处阅读: http://en.wikipedia.org/wiki/Reactive_programming

Reactive programming is not actually meteor or JS specific
you can read about it here: http://en.wikipedia.org/wiki/Reactive_programming

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

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