什么是*像*一个承诺,但可以解决多个时间? [英] What is *like* a promise but can resolve mutliple times?

查看:102
本文介绍了什么是*像*一个承诺,但可以解决多个时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个行为像承诺的pub / sub机制,但可以多次解析,并且表现得像一个事件,除非您在通知发生后订阅,并触发最新的值。

I am looking for a pub/sub mechanism that behaves like a promise but can resolve multiple times, and behaves like an event except if you subscribe after a notification has happened it triggers with the most recent value.

我知道 通知 ,但 deferred.notify 对订单敏感,所以这样做就像一个事件。例如:

I am aware of notify, but deferred.notify is order-sensitive, so in that way it behaves just like an event. eg:

d.notify('notify before'); // Not observed :(
d.promise.progress(function(data){ console.log(data) });
d.notify('notify after');  // Observed
setTimeout(function(){ d.notify('notify much later') }, 100); // Also Observed

小提琴: http://jsfiddle.net/foLhag3b/

我想要的通知系统非常适合应该更新的UI组件,以反映其后面的数据的状态,在这种情况下,您不想关心数据已经到达或不存在,并且您想要更新进入。

The notification system I'd like is a good fit for a UI component that should update to reflect the state of the data behind it. In these cases, you don't want to care about whether the data has arrived yet or not, and you want updates when they come in.

也许这与立即模式用户界面,但是因为它是基于消息的而是不同的。

Maybe this is similar to Immediate mode UIs, but is distinct because it is message based.

基于消息的UI更新的最新技术,据我所知,是使用承诺或回调来初始化的东西,然后还绑定更新事件:

The state of the art for message based UI updating, as far as I'm aware, is something which uses a promise or callback to initialize, then also binds an update event:

myUIComponent.gotData(model.data);
model.onUpdate(myUIComponent.gotData); // doing two things is 2x teh workz :(

我不想同时做我不认为任何人都应该有这个用例,这个用例很常见,可以抽象出来。

I don't want to have to do both. I don't think anyone should have to, the use case is common enough to abstract.

model.whenever(myUIComponent.gotData); // yay one intention === one line of code

我可以建立一个机制做我想要的,但是我想看看这样一个pub / sub机制是否已经存在,很多聪明人在CS中做了很多工作,我认为这个概念必须存在,我只是在寻找它的名字。

I could build a mechanism to do what I want, but I wanted to see if a pub/sub mechanism like this already exists. A lot of smart people have done a lot in CS and I figure this concept must exist, I just am looking for the name of it.

要清楚,我不是想改变整个框架,对Angular或React说,我只在寻找一个酒吧/子机制,最好是实现一个已知的概念,具有像 notifr lemme-kno touch-基础

To be clear, I'm not looking to change an entire framework, say to Angular or React. I'm looking only for a pub/sub mechanism. Preferably an implementation of a known concept with a snazzy name like notifr or lemme-kno or touch-base.

推荐答案

你会想看看(功能)无效编程。您正在寻找的概念被称为FRP中的行为信号。它随着时间的推移模拟价值的变化,并且可以随时检查(连续保持一个值,与离散触发事件的流相反)。

You'll want to have a look at (functional) reactive programming. The concept you are looking for is known as a Behavior or Signal in FRP. It models the change of a value over time, and can be inspected at any time (continuously holds a value, in contrast to a stream that discretely fires events).

var ui = state.map(render); // whenever state updates, so does ui with render() result

JS中的一些流行的库是< a href =https://github.com/baconjs/bacon.js =nofollow>培根和 Rx ,然后使用自己的术语:你会发现属性可观察

Some popular libraries in JS are Bacon and Rx, which use their own terminology however: you'll find properties and observables.

这篇关于什么是*像*一个承诺,但可以解决多个时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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