gpt4 book ai didi

javascript - 在设置 RxJs 订阅后,是否有一种干净的方法可以立即执行函数?

转载 作者:行者123 更新时间:2023-12-04 07:50:30 26 4
gpt4 key购买 nike

我想做什么:

  • 向 websocket 服务器发送“请求”事件。
  • 接收与一些附加数据捆绑在一起的即时响应事件。
  • 随着时间的推移不断收到回复。

  • 我的问题是:
  • 有没有更干净的方式来做我在下面做的事情?不使用 setTimeout。

  • 首先,看看这个简化的工作示例:

    const { Subject, defer, interval, of} = rxjs;
    const EventEmitter = EventEmitter3;


    const emitter = new EventEmitter();
    const subject = new Subject();

    // The next lines mock a websocket server message listener, imagine this being present on server side
    emitter.on("request", () => {
    subject.next(`Immediate response`);
    interval(1000).subscribe((index) =>
    subject.next(`Delayed response...${index}`)
    );
    });



    // Imagine the following code being present in the browser
    function getData() {
    return defer(() => {
    // The next line mocks a websocket client sent event
    setTimeout(() => emitter.emit("request"), 0);
    return subject;
    });
    }

    getData().subscribe((data) => console.log(data));
    <script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.5.3/rxjs.umd.min.js"></script>
    <script src="https://unpkg.com/eventemitter3@latest/umd/eventemitter3.min.js"></script>

    现在看一个非工作示例。在这里,客户端不会立即得到响应,因为在设置订阅之前发送了“请求”事件。

    const { Subject, defer, interval, of} = rxjs;
    const EventEmitter = EventEmitter3;


    const emitter = new EventEmitter();
    const subject = new Subject();

    // The next lines mock a websocket server message listener, imagine this being present on server side
    emitter.on("request", () => {
    subject.next(`Immediate response`);
    interval(1000).subscribe((index) =>
    subject.next(`Delayed response...${index}`)
    );
    });


    // Imagine the following code being present in the browser
    function getData() {
    return defer(() => {
    emitter.emit('request');
    return subject;
    });
    }

    getData().subscribe((data) => console.log(data));
    <script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.5.3/rxjs.umd.min.js"></script>
    <script src="https://unpkg.com/eventemitter3@latest/umd/eventemitter3.min.js"></script>

    最佳答案

    您可以使用 BehaviorSubject 而不是 Subject :

    const { BehaviorSubject, defer, interval, of } = rxjs;
    const EventEmitter = EventEmitter3;


    const emitter = new EventEmitter();
    const subject = new BehaviorSubject();

    // The next lines mock a websocket server message listener, imagine this being present on server side
    emitter.on("request", () => {
    // the timing of this synchronous response would be impossible in a real socket
    subject.next(`Immediate response`);
    interval(1000).subscribe((index) =>
    subject.next(`Delayed response...${index}`)
    );
    });


    // Imagine the following code being present in the browser
    function getData() {
    return defer(() => {
    emitter.emit('request');
    return subject;
    });
    }

    getData().subscribe((data) => console.log(data));
    <script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.5.3/rxjs.umd.min.js"></script>
    <script src="https://unpkg.com/eventemitter3@latest/umd/eventemitter3.min.js"></script>

    请记住,在发送套接字请求后,服务器的响应不可能以与您为该演示实现模拟的方式相同的方式同步返回,因此 Subject这里不会产生预期的结果,它可以在真正的套接字上正常工作。

    关于javascript - 在设置 RxJs 订阅后,是否有一种干净的方法可以立即执行函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67010835/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com