简体   繁体   English

连续承诺/ A +加载

[英]Continuous Promise/A+ loading

So I'd like to have these continuous, asynchronous, data loading in browser. 因此,我想在浏览器中加载这些连续的,异步的数据。 I like Promises, they're good, aren't they? 我喜欢Promises,它们很好,不是吗? Fun to program, easy to read. 编程有趣,易于阅读。

In my problem, I won't to continuously update some resource in the browser, for example an image. 在我的问题中,我不会在浏览器中不断更新某些资源,例如图像。 There are two parts then: producer and consumer. 然后有两个部分:生产者和消费者。 In synchronous code it could look like this: 在同步代码中,它可能看起来像这样:

var p = producer(),
    data = null;

while (data = p.next()) {
    consumer.update(data)
}

How would this translate to asynchronous, Promise/A+ enabled code? 这将如何转换为支持Promise / A +的异步代码?

UPDATE 更新

I ended up using the following base class: 我最终使用了以下基类:

class Stream
    input  : null
    output : null
    # @param  {function} callback
    end    : (callback) -> @end_callback = callback
    # @param  {Stream} writable
    # @return {Stream}
    pipe   : (writable) -> writable.input = @ ; @output = writable
    # @param  {function} callback
    # @return {Stream}
    data   : (callback) ->
        setTimeout =>
            last = @
            chain = [@]
            while last.input != null
                last = last.input
                chain.push(last)
            last = chain.pop()
            next = =>
                thenable = last.read()
                if thenable is null and @end_callback
                    return @end_callback()
                thenable.then (value) =>
                    if not chain.length
                        callback(value)
                        @data(callback)
                    else
                        last = chain.pop()
                        last.write(value)
                        next()
            next()
        , 0
        @

Just implement read method, returning a Promise or null, or both read and write to create transforming stream. 刚刚实施read法,返回一个Promise或空,或两者readwrite创建转化流。

What you're talking of is more a Stream than Promises - maybe there's a better fitting library. 您所说的更多的是Stream,而不是Promises-也许有一个更好的拟合库。 If not, you might want to have a look at my "Lazy Promise Stream" implementation . 如果没有,您可能想看看我的“惰性承诺流”实现

How would this [polling] translate to asynchronous, Promise/A+ enabled code? 此[轮询]如何转换为支持Promise / A +的异步代码?

The producer() 's next() method would return promises. producer()next()方法将返回promise。 Then, you'd loop over them in a recursive manner: 然后,您将以递归方式遍历它们:

function getNext() {
    return p.next().then(function(data) {
         consumer.update(data);
         return getNext();
    });
}
getNext();

The return s could be omitted if you knew it was an infinite stream without errors, as getNext() would be always pending 如果您知道return s是无限流且没有错误,则可以省略return ,因为getNext()始终处于待处理状态

I think from your description you'd probably want to use 'Web Workers' if you have a producer consumer pair. 我认为根据您的描述,如果您有生产者消费者对,则可能要使用“ Web Workers”。

The 'Worker' would be sent work and then return messages at certain points, such as when it's complete. 将向“工作人员”发送工作,然后在某些时候(例如完成时)返回消息。 You can listen to the onmessage event in the consumer and decide what to do, such as send more work, or process the result if it's complete. 您可以在使用者中监听onmessage事件,并决定要做什么,例如发送更多工作,或者在结果完成后对其进行处理。

Have a look at the MDN guide and see if it fits your need: 查看MDN指南,看看它是否符合您的需求:

https://developer.mozilla.org/en-US/docs/Web/Guide/Performance/Using_web_workers https://developer.mozilla.org/zh-CN/docs/Web/Guide/Performance/Using_web_workers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM