- PHP Reactive Programming
- Martin Sikora
- 162字
- 2021-07-09 19:06:18
Event loop and RxPHP
Applying event loop to Observables would work in a similar way. We'd create Observables, start an event loop and periodically check their progress. Luckily for us, RxPHP is prepared for this. In combination with the ReactPHP library ( https://github.com/reactphp/react ), we can use a Scheduler that's designed exactly for what we need.
As an example, we can have a look at IntervalObservable
that periodically emits values:
// rxphp_eventloop.php $loop = new ReactEventLoopStreamSelectLoop(); $scheduler = new RxSchedulerEventLoopScheduler($loop); RxObservable::interval(1000, $scheduler) ->take(3) ->subscribe(new DebugSubject()); $loop->run();
This prints three values with 1s delays:
$ php rxphp_eventloop.php 23:12:44 [] onNext: 0 (integer) 23:12:45 [] onNext: 1 (integer) 23:12:46 [] onNext: 2 (integer) 23:12:46 [] onCompleted
Note
In RxPHP 2, using event loops has been simplified and, most of the time, we don't even need to worry about starting the loop ourselves. We'll talk about differences between RxPHP 1.x and RxPHP 2 regarding event loops in Chapter 6, PHP Streams API and Higher-Order Observables.
推薦閱讀
- Power Up Your PowToon Studio Project
- Oracle Database In-Memory(架構(gòu)與實踐)
- Spring Boot+Spring Cloud+Vue+Element項目實戰(zhàn):手把手教你開發(fā)權(quán)限管理系統(tǒng)
- 精通網(wǎng)絡(luò)視頻核心開發(fā)技術(shù)
- 正則表達(dá)式經(jīng)典實例(第2版)
- 零基礎(chǔ)趣學(xué)C語言
- FPGA Verilog開發(fā)實戰(zhàn)指南:基于Intel Cyclone IV(進(jìn)階篇)
- 計算機(jī)應(yīng)用基礎(chǔ)案例教程
- iOS自動化測試實戰(zhàn):基于Appium、Python與Pytest
- C# Multithreaded and Parallel Programming
- RESTful Web Clients:基于超媒體的可復(fù)用客戶端
- UX Design for Mobile
- AI自動化測試:技術(shù)原理、平臺搭建與工程實踐
- Python第三方庫開發(fā)應(yīng)用實戰(zhàn)
- 一覽眾山小:ASP.NET Web開發(fā)修行實錄