keferjs stream debounce, but not on keycode==13(return)

50 views Asked by At

I want to debounce on all key presses excluding return. I have tried the following but it doesn't debounce.

some_stream.flatMap((event) => {
                            if(event.keyCode == 13){
                                return Kefir.stream(emitter => {
                                    emitter.emit(event.target.value);
                                });
                            }else{
                                const debounced_stream = Kefir.stream(emitter => {
                                    emitter.emit(event.target.value);
                                }).debounce(1000)
                                return debounced_stream;
                            }

                        })
1

There are 1 answers

0
Tim.Willis On BEST ANSWER

I was able to solve this with the code block below which debounces on every keyCode except 13:

const search_stream = Kefir.fromEvents(self.search_keyword._tag.input, 'keyup');
                const debounced_search_stream = search_stream
                        .filter((event) => {
                            return event.keyCode != 13;
                        })
                        .map((event) => {
                            return event.target.value;
                        })
                        .debounce(1000);
                const not_debounced_search_stream = search_stream
                        .filter((event) => {
                            return event.keyCode == 13;
                        })
                        .map((event) => {
                            return event.target.value;
                        })
                Kefir.merge([debounced_search_stream, not_debounced_search_stream]).onValue(keyword => {
                    if(keyword !== null){
                        if (keyword) {
                            //do something
                        }
                    }
                })