हम्म, यह सबसे आसान समाधान है जिसे मैं सोच सकता हूं। आपके लिए दिलचस्प हिस्सा awesomeDebounce()
फ़ंक्शन है जो उप-श्रृंखला बनाता है।
यह मूल रूप से सिर्फ throttle()
और debounceTime()
ऑपरेटरों को जोड़ती है:
const Rx = require('rxjs');
const chai = require('chai');
let scheduler = new Rx.TestScheduler((actual, expected) => {
chai.assert.deepEqual(actual, expected);
console.log(actual);
});
function awesomeDebounce(source, timeWindow = 1000, scheduler = Rx.Scheduler.async) {
let shared = source.share();
let notification = shared
.switchMap(val => Rx.Observable.of(val).delay(timeWindow, scheduler))
.publish();
notification.connect();
return shared
.throttle(() => notification)
.merge(shared.debounceTime(timeWindow, scheduler))
.distinctUntilChanged();
}
let sourceMarbles = '---a----b-c-d-----e-f---';
let expectedMarbles = '---a----b------d--e----f';
// Create the test Observable
let observable = scheduler
.createHotObservable(sourceMarbles)
.let(source => awesomeDebounce(source, 30, scheduler));
scheduler.expectObservable(observable).toBe(expectedMarbles);
scheduler.flush();
भीतरी notification
नमूदार तो मैं अपनी घड़ी मैन्युअल रूप से रीसेट कर सकते हैं जब मैं की जरूरत है throttle()
ऑपरेटर के लिए ही प्रयोग किया जाता है। throttle()
से आंतरिक सदस्यता पर स्वतंत्र होने के लिए मुझे इस पर्यवेक्षक को "गर्म" में भी बदलना पड़ा।
तो क्या आप एक समय खिड़की में पहली और आखिरी घटना को उत्सर्जित करना चाहते हैं? – paulpdaniels
हां, लेकिन समय खिड़की debounce के साथ व्यवहार करता है (यह खिड़की के अंदर प्रत्येक घटना के साथ बढ़ाया जाता है)। – Roman
यह वास्तविक उत्तर है! https://stackoverflow.com/a/30145789/684869 – cgatian