Giter VIP home page Giter VIP logo

rxjs-groupby-basic's Introduction

rxjs-groupby-basic

Basic demo of groupBy for RxJS Live.

Each branch demonstrates a different operator: mergeMap, switchMap, concatMap, exhaustMap, groupBy, and a custom operator implementation of groupBy.

Authors

Sam Julien & Mike Ryan

rxjs-groupby-basic's People

Contributors

mikeryandev avatar samjulien avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

rxjs-groupby-basic's Issues

toggleStatus should write to database after the delay

Cool demo, food for thought... But I'm not sure if I'm understanding the setup you've used.

  • Why does the toggleStatus function update its local state BEFORE the delay? That's fine for the setButtonEmoji, but why have a database-tracking variable that doesn't follow the db.

If it wouldn't change your demo to alter this, I think it'd be more believable to do this..

  • Why route everything through the dispatcher to begin with? I understand in the real world you might have an already-combined stream, so that's fair. Just curious.

Thanks for the discussion..

Auto detect group completion for removal

Since the custom operator knows everything it need to know when groups can safely be removed, there should be no need for a fixed 15 second timeout.

Instead it could detect if the inner observable inside the switchMap has completed. If so, it can terminate the group.

I gave it a stab in my own branch here.

export function switchMapByKey<T, K, O extends ObservableInput<any>>(
  keySelector: (value: T) => K,
  project: (value: T, index: number) => O
): OperatorFunction<T, ObservedValueOf<O>> {
  return (source) => {
    let vault: any = {}; 
    return source.pipe(
      groupBy(
      (v) => {
        const key = keySelector(v);
        if (!vault[key]) vault[key] = new Subject<void>();
        return key;
      },
      item => item,
      group => vault[group.key]
      ),
      mergeMap((group) =>
        group.pipe(
          switchMap((e, i) =>
            from(project(e, i)).pipe(
              tap({ complete: () => { vault[group.key].complete(); }})
             )
          ),
          finalize(() => { delete vault[group.key]; })
        )
      ) as OperatorFunction<GroupedObservable<K, T>, ObservedValueOf<O>>
    )
  }
}

Edit: This was working in my browser, but TS keeps complaining about types, so it's not perfect yet.
Edit2: I gave TS some hints and now it's happy.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.