RxJS与last相比并发出

时间:2015-12-05 17:43:14

标签: javascript reactive-programming rxjs

我对ReactiveJS很新,我想实现以下目标:

如果它与最后一个时间戳(updated_at)具有不同的时间戳(updated_at),则仅发出值(对象)。

import UIKit
import XCPlayground

import Photos
import PhotosUI

class PLView: UIView {

    let image: UIImage
    let imageURL: NSURL
    let videoURL: NSURL

    let liveView: PHLivePhotoView

    init(image: UIImage, imageURL: NSURL, videoURL: NSURL) {
        self.image = image
        self.imageURL = imageURL
        self.videoURL = videoURL
        let rect = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
        self.liveView = PHLivePhotoView(frame: rect)
        super.init(frame: rect)
        self.addSubview(self.liveView)
    }

    func prepareLivePhoto() {
        makeLivePhotoFromItems { (livePhoto) in
            self.liveView.livePhoto = livePhoto
            print("\nReady! Click on the LivePhoto in the Assistant Editor panel!\n")
        }
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        print("\nClicked! Wait for it...\n")
        self.liveView.startPlaybackWithStyle(.Full)
    }

    private func makeLivePhotoFromItems(completion: (PHLivePhoto) -> Void) {
        PHLivePhoto.requestLivePhotoWithResourceFileURLs([imageURL, videoURL], placeholderImage: image, targetSize: CGSizeZero, contentMode: .AspectFit) {
            (livePhoto, infoDict) -> Void in
            // This "canceled" condition is just to avoid redundant passes in the Playground preview panel.
            if let canceled = infoDict[PHLivePhotoInfoCancelledKey] as? Int where canceled == 0 {
                if let livePhoto = livePhoto {
                    completion(livePhoto)
                }
            }
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}



XCPlaygroundPage.currentPage.needsIndefiniteExecution = true

let plview = PLView(image: UIImage(named: "IMG_0001.JPG")!,
                 imageURL: NSBundle.mainBundle().URLForResource("IMG_0001", withExtension: "JPG")!,
                 videoURL: NSBundle.mainBundle().URLForResource("IMG_0001", withExtension: "mov")!)

XCPlaygroundPage.currentPage.liveView = plview

plview.prepareLivePhoto()

2 个答案:

答案 0 :(得分:4)

请阅读有关 distinctUntilChanged()的文档。 http://reactivex.io/documentation/operators/distinct.html

我想以下代码就是问题的答案。(仅添加1行)

var checkJobsRx = new Rx.Subject();
  checkJobsRx
 .dosomethinghere()
 .distinctUntilChanged(function(job) { return job.updated_at })
 .subscribe(function (data) {})

答案 1 :(得分:1)

您可以使用distinctUntilChanged检查重复项的发布值:

checkJobsRx
  .map(fooBar)
  .distinctUntilChanged((item) => item.updated_at)
  .subscribe((data) => { })

检查the documentation