ZipWithLatestForm *Wanted Operator* or Workaround











up vote
1
down vote

favorite












Lets consider following streams:



Stream1: 1  2  3  4  5  6  7  8  9                
Stream2: ab c d f


Output I would like to get with combining these streams:



[1, a]
[2, b]
[3, c]
[4, c]
[5, d]
[6, f]
[7, f]
[8, f]
[9, f]


Sample Code Idea: StackBlitz Editor Link



const input$ = fromEvent(document, 'keydown')
.pipe(
map((event: KeyboardEvent) => event.keyCode)
);
const source$ = interval(500).pipe(s$ => zip(s$, input$))
source$.subscribe(x => console.log(x));


So this is something like operators zip() and/or withLatestForm(), but I can't come up with solution




  • Main idea is that I have interval of 500ms and on every tick I want to zip it with Keyboard Input Observable, which works how I would like, but as long I am not emitting Keyboard Input Observable everything stops. I want to keep emitting interval ticks, but if keyboard events stop then take the latest value










share|improve this question
























  • Does the output at the beginning of your question actually represent your use case? I'm not at all sure that it does. In which case, there are two questions within your question.
    – cartant
    3 hours ago















up vote
1
down vote

favorite












Lets consider following streams:



Stream1: 1  2  3  4  5  6  7  8  9                
Stream2: ab c d f


Output I would like to get with combining these streams:



[1, a]
[2, b]
[3, c]
[4, c]
[5, d]
[6, f]
[7, f]
[8, f]
[9, f]


Sample Code Idea: StackBlitz Editor Link



const input$ = fromEvent(document, 'keydown')
.pipe(
map((event: KeyboardEvent) => event.keyCode)
);
const source$ = interval(500).pipe(s$ => zip(s$, input$))
source$.subscribe(x => console.log(x));


So this is something like operators zip() and/or withLatestForm(), but I can't come up with solution




  • Main idea is that I have interval of 500ms and on every tick I want to zip it with Keyboard Input Observable, which works how I would like, but as long I am not emitting Keyboard Input Observable everything stops. I want to keep emitting interval ticks, but if keyboard events stop then take the latest value










share|improve this question
























  • Does the output at the beginning of your question actually represent your use case? I'm not at all sure that it does. In which case, there are two questions within your question.
    – cartant
    3 hours ago













up vote
1
down vote

favorite









up vote
1
down vote

favorite











Lets consider following streams:



Stream1: 1  2  3  4  5  6  7  8  9                
Stream2: ab c d f


Output I would like to get with combining these streams:



[1, a]
[2, b]
[3, c]
[4, c]
[5, d]
[6, f]
[7, f]
[8, f]
[9, f]


Sample Code Idea: StackBlitz Editor Link



const input$ = fromEvent(document, 'keydown')
.pipe(
map((event: KeyboardEvent) => event.keyCode)
);
const source$ = interval(500).pipe(s$ => zip(s$, input$))
source$.subscribe(x => console.log(x));


So this is something like operators zip() and/or withLatestForm(), but I can't come up with solution




  • Main idea is that I have interval of 500ms and on every tick I want to zip it with Keyboard Input Observable, which works how I would like, but as long I am not emitting Keyboard Input Observable everything stops. I want to keep emitting interval ticks, but if keyboard events stop then take the latest value










share|improve this question















Lets consider following streams:



Stream1: 1  2  3  4  5  6  7  8  9                
Stream2: ab c d f


Output I would like to get with combining these streams:



[1, a]
[2, b]
[3, c]
[4, c]
[5, d]
[6, f]
[7, f]
[8, f]
[9, f]


Sample Code Idea: StackBlitz Editor Link



const input$ = fromEvent(document, 'keydown')
.pipe(
map((event: KeyboardEvent) => event.keyCode)
);
const source$ = interval(500).pipe(s$ => zip(s$, input$))
source$.subscribe(x => console.log(x));


So this is something like operators zip() and/or withLatestForm(), but I can't come up with solution




  • Main idea is that I have interval of 500ms and on every tick I want to zip it with Keyboard Input Observable, which works how I would like, but as long I am not emitting Keyboard Input Observable everything stops. I want to keep emitting interval ticks, but if keyboard events stop then take the latest value







javascript typescript rxjs reactive-programming rxjs6






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 4 hours ago









chazsolo

4,8921232




4,8921232










asked 4 hours ago









Goga Koreli

1139




1139












  • Does the output at the beginning of your question actually represent your use case? I'm not at all sure that it does. In which case, there are two questions within your question.
    – cartant
    3 hours ago


















  • Does the output at the beginning of your question actually represent your use case? I'm not at all sure that it does. In which case, there are two questions within your question.
    – cartant
    3 hours ago
















Does the output at the beginning of your question actually represent your use case? I'm not at all sure that it does. In which case, there are two questions within your question.
– cartant
3 hours ago




Does the output at the beginning of your question actually represent your use case? I'm not at all sure that it does. In which case, there are two questions within your question.
– cartant
3 hours ago












1 Answer
1






active

oldest

votes

















up vote
0
down vote













You can just use withLatestFrom



import { of, fromEvent, interval } from 'rxjs'; 
import { map, withLatestFrom } from 'rxjs/operators';

const input$ = fromEvent(document, 'keydown').pipe( map((event: KeyboardEvent) => event.keyCode));
const timer$ = interval(1000);
const source$ = timer$.pipe(withLatestFrom(input$));

source$.subscribe(x => console.log(x));


Try out this live demo at stackblitz -> https://stackblitz.com/edit/rxjs-bmx7hg



As soon as you focus the page and press some key, the stream will start and you will get an emitted value on every tick, no matter if you clicked a button since the last tick.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400784%2fzipwithlatestform-wanted-operator-or-workaround%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    You can just use withLatestFrom



    import { of, fromEvent, interval } from 'rxjs'; 
    import { map, withLatestFrom } from 'rxjs/operators';

    const input$ = fromEvent(document, 'keydown').pipe( map((event: KeyboardEvent) => event.keyCode));
    const timer$ = interval(1000);
    const source$ = timer$.pipe(withLatestFrom(input$));

    source$.subscribe(x => console.log(x));


    Try out this live demo at stackblitz -> https://stackblitz.com/edit/rxjs-bmx7hg



    As soon as you focus the page and press some key, the stream will start and you will get an emitted value on every tick, no matter if you clicked a button since the last tick.






    share|improve this answer



























      up vote
      0
      down vote













      You can just use withLatestFrom



      import { of, fromEvent, interval } from 'rxjs'; 
      import { map, withLatestFrom } from 'rxjs/operators';

      const input$ = fromEvent(document, 'keydown').pipe( map((event: KeyboardEvent) => event.keyCode));
      const timer$ = interval(1000);
      const source$ = timer$.pipe(withLatestFrom(input$));

      source$.subscribe(x => console.log(x));


      Try out this live demo at stackblitz -> https://stackblitz.com/edit/rxjs-bmx7hg



      As soon as you focus the page and press some key, the stream will start and you will get an emitted value on every tick, no matter if you clicked a button since the last tick.






      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        You can just use withLatestFrom



        import { of, fromEvent, interval } from 'rxjs'; 
        import { map, withLatestFrom } from 'rxjs/operators';

        const input$ = fromEvent(document, 'keydown').pipe( map((event: KeyboardEvent) => event.keyCode));
        const timer$ = interval(1000);
        const source$ = timer$.pipe(withLatestFrom(input$));

        source$.subscribe(x => console.log(x));


        Try out this live demo at stackblitz -> https://stackblitz.com/edit/rxjs-bmx7hg



        As soon as you focus the page and press some key, the stream will start and you will get an emitted value on every tick, no matter if you clicked a button since the last tick.






        share|improve this answer














        You can just use withLatestFrom



        import { of, fromEvent, interval } from 'rxjs'; 
        import { map, withLatestFrom } from 'rxjs/operators';

        const input$ = fromEvent(document, 'keydown').pipe( map((event: KeyboardEvent) => event.keyCode));
        const timer$ = interval(1000);
        const source$ = timer$.pipe(withLatestFrom(input$));

        source$.subscribe(x => console.log(x));


        Try out this live demo at stackblitz -> https://stackblitz.com/edit/rxjs-bmx7hg



        As soon as you focus the page and press some key, the stream will start and you will get an emitted value on every tick, no matter if you clicked a button since the last tick.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 3 hours ago

























        answered 3 hours ago









        Bene

        28229




        28229






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400784%2fzipwithlatestform-wanted-operator-or-workaround%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Sphinx de Gizeh

            Dijon

            Langue