Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arguments passed to subscribe - pass through to constructor fn? #188

Open
bradennapier opened this issue May 23, 2018 · 0 comments
Open

Comments

@bradennapier
Copy link

bradennapier commented May 23, 2018

First - Love the proposal! I was just playing with it a bit and can't wait for this to be implemented.

This may have been brought up already but my quick search didn't bring anything. Perhaps this is breaking some major concept or something but my first thought with this is that we end up with a situation that it would be nice to receive pass-through parameters when subscribing to an observable.

Below is probably not exactly correct as I didn't check the syntax to make sure what im doing even works with the keydown api but it gives the general idea:

(I also know that this specific example makes no sense but i wanted to keep with the example in the docs so people didn't have to infer what is happening -- my example need is for a redux-like connect where we have a single params given then each "subscriber" would pass the component to reference)

export default function subscribe(eventName) {
  return new Observable((observer, keys) => {
    // Create an event handler which sends data to the sink
    const handler = event => keys.some(key => event.key === key) && observer.next(event);

    // Attach the event handler
    element.addEventListener(eventName, handler, true);

    // Return a cleanup function which will cancel the event stream
    return () => {
      // Detach the event handler from the element
      element.removeEventListener(eventName, handler, true);
    };
  });
}

const subscriber = subscribe('keydown');

subscriber.subscribe(
  {
    next: evt => {},
  },
  ['a'],
);

where

interface ExtendedSubscriber {
    subscriber(Observer, ...args: Array<any>) 
}

Just thinking it helps make observable more composable and ends up not requiring the wrapping function in many situations. It shouldn't really make much of a different in the implementation I would think to do it this way unless I am missing something.

Feels like this is also a way that the user can compose their own map and other functionality in a fairly straight forward and clean way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant