rxviz icon indicating copy to clipboard operation
rxviz copied to clipboard

Support pipeable operators

Open Airblader opened this issue 7 years ago • 8 comments

Closing #4 was a little premature. We can write our own pipeable operators and use them now, but due to the way rxjs is imported in the sandbox we cannot use all the built-ins as pipeable operators. For example, ideally this would work:

const source$ = of(42);
source$.pipe(
  map(value => 2 * value)
)

This would be really useful to quickly copy-paste samples from code you're writing to debug them as well as allow using rxviz as a reference for answers on platforms such as StackOverflow.

Airblader avatar Jan 28 '18 12:01 Airblader

I'm happy to implement this, but I'm not sure what a good way to achieve this would be. Manually import all known operators in the sandbox? AFAIK there's no good way to automatically import all operators without listing them explicitly.

Airblader avatar Jan 28 '18 12:01 Airblader

I'm not it's a good idea to pollute the global scope with all the available operators.

You can just do:

const { of } = Rx.Observable;
const { map } = Rx.operators;
const source$ = of(42);

source$.pipe(
  map(value => 2 * value)
)

moroshko avatar Jan 29 '18 06:01 moroshko

In general I agree, especially considering that functionally there's nothing we can't do already. However, to provide some perspective, I've been using rxviz every day for a while now. I use I for my work, I use it for private work and I use it when answering questions on StackOverflow. The two issues I run into almost every time are:

  1. Having to use the Rx name space rather than writing Observable etc directly
  2. No option to directly write pipeable operators.

Both can be worked around the way you describe, but it's just not as convenient as it could be for copy-pasting snippets in either direction.

I guess one thing I could do is prepare a snippet which does all the global scope aliasing, save its URL and bookmark it to use it as my starter template. That might be good enough.

Airblader avatar Jan 29 '18 07:01 Airblader

@moroshko you should upgrade couple of examples to use lettable operators just to show this functionality. Prehaps add those already into comment block of the custom code page.

This tool is awesome, thanks for this.

MattiJarvinen avatar Feb 02 '18 08:02 MattiJarvinen

@Airblader: rather that polluting global scope, I would expect something like quickfix command in editor that adds the imports.

Maybe some visual toolbox, with all operators, creators etc. and their description would also help.

Liero avatar Jun 08 '19 06:06 Liero

@Liero That could be theoretically done using with keyword, but it is not recommended. And there is a problem that, there are non-ambiguous creators/operators. combineLatest is one example.

I would vote for import syntax. Imports are used in every app that uses RXJS, it solves ambiguities problem and is easily copy/paste able.

Akxe avatar Aug 29 '19 10:08 Akxe

Hello @Airblader ,

It's been a while but, as mentioned before, having them available in the global scope can be an issue, most notably with name clashes for things like combineLatest that exist in 2 forms.

However I can see how in its current state this is not very user-friendly. What do you think about:

Short-term

Automatically turn import statements into their rxviz equivalent, e.g when pasting:

import { combineLatest } from 'rxjs';
import { map } from 'rxjs/operators';

It becomes:

const { combineLatest } = Rx
const { map } = Rx.operators

Long-term

That would require far more work, but having something that provides auto-completion and adds imports should make the application far easier to use as an editor

rraziel avatar Aug 07 '20 16:08 rraziel

@rraziel For the long term, this should work https://github.com/cdr/code-server.

Stripped down VSCode would work wonders...

Akxe avatar Aug 13 '20 08:08 Akxe