首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Bacon.js EventStream值

Bacon.js EventStream值
EN

Stack Overflow用户
提问于 2016-07-03 16:57:55
回答 3查看 174关注 0票数 1

假设我的代码中有一个任意值: var arbitraryValue = null;

我的代码中有多个地方可以更改此值。我可以根据这个值的变化创建一个EventStream吗?换句话说,如果arbitraryValue被更改,新值将作为EventStream发送给订阅者?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-07-04 16:14:50

谢谢你的回答!也许Bacon.js不是我想要的,所以我决定创建自己的“反应性编程”库。它立即计算新值,而不需要使用setTimeout检查值。

代码语言:javascript
复制
var = createReactiveValue = function(initialValue) {
return {
    "observers": [], // Call these functions with a new value when it has been set
    "value":initialValue,
    "set": function(newValue) {
        this.value = newValue;

        this.observers.forEach(function(observer) {
            // Call observers asynchronously
            setTimeout(function() { observer(this.value) }, 0);
        })
    },
    "get": function() {
        return this.value;
    },
    "addObserver": function(observer) {
        this.observers.push(observer);
    }
};
};

// If any of the dependant values change, sets reactive value 'value' to the result of calling 'result'
var = makeReaction = function(value, dependencies, result) {
    // Start watching changes in dependencies
    dependencies.forEach(function(dependency) {
        dependency.addObserver(function(newValue) {
            var newResult = result(value, dependencies);
            value.set(newResult);
        });
    });
};


// When 'reactiveValue' is changed, calls the given 'result' function with the new value
var = watchReaction = function(reactiveValue, result) {
    // Start watching changes in reactive value
    reactiveValue.addObserver(function(newValue) {
        result(newValue);
    });
};

var a = createReactiveValue(2);
var b = createReactiveValue(1);
var c = createReactiveValue(null);

// We want c to depend on both a and b. If either a or b change, c should    automatically recalculate it's value
makeReaction(c, [a, b], function(c, [a, b]) {
    return a.get() + b.get();
});

// If c is changed, for whatever reason, log it
watchReaction(c, function(newC) {
    console.log("Reaction completed: " + a.get() + " + " + b.get() + " = " + c.get());
});

// Test it by resetting value a to random
setInterval(function()
{
    var newValue = Math.random();
    a.set(newValue); },
1000); // Logs the new result imm

编辑:现在可以作为一个库:https://github.com/Jarzka/ReaktioJS

票数 1
EN

Stack Overflow用户

发布于 2016-07-04 13:40:03

使用Bus而不是普通值:

代码语言:javascript
复制
var arbitraryValueBus = Bacon.Bus()

现在可以使用Bus.push设置值了。

代码语言:javascript
复制
arbitraryValueBus.push(newValue)

并且您可以通过订阅Bus来侦听值的更改。

代码语言:javascript
复制
arbitraryValueBus.forEach(newValue => console.log(newValue))

注意,使用一个简单的Bus,您的订阅者将不会获得在forEach调用之前设置的值。因此,如果要添加“当前值”,并立即使用当前值调用回调,则应使用Property

代码语言:javascript
复制
var b = Bacon.Bus()
var p = arbitraryValueBus.toProperty()
p.forEach() // to make sure it's updated before adding subscribers
b.push("first value")
p.subscribe(x => console.log(x))

==> outputs "first value"

现在,如果有,您的订阅者将立即获得当前值。

票数 2
EN

Stack Overflow用户

发布于 2016-07-03 19:28:49

检查值是否更改

在js中,检查变量的值是否更改没有那么难。

代码语言:javascript
复制
function checkValue() {
    var newValue = value;
    if (newValue != value) {
        // Value has changed
        // .... (Code that needs to be executed)
    }
}

通过阅读您的问题,我得出结论,您需要不断检查值是否发生了变化。为此,我建议使用以下功能:

代码语言:javascript
复制
function checkValue(interval) {
    var checkInterval = setInterval(function () {
        var newValue = value;
        if (newValue != value) {
            // Value has changed
            // .... (Code that needs to be executed)
        }
    }, interval); 
}

在代码开头的某个地方调用此函数将检查您的值是否每隔一毫秒就有更改一次。要停止这个过程,您所需要做的就是清除间隔。注意,我在变量的内部放置了区间,以使清除间隔成为可能。

清除间隔很容易,就像:

代码语言:javascript
复制
clearInterval(checkInterval);

注意:请设置间隔,以便它每一秒左右检查一次。(1000毫秒)它可能不需要降低,而更频繁的检查只是给浏览器带来不必要的工作。

我希望这能回答你的问题!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38172243

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档