0no-co / wonka

🎩 A tiny but capable push & pull stream library for TypeScript and Flow
MIT License
709 stars 29 forks source link

Cannot reference operators, sources, sinks without opening Wonka. #25

Closed parkerziegler closed 5 years ago

parkerziegler commented 5 years ago

In the upgrade from v2 to v3, we lost the ability to just reference wonka sources, sinks, and operators directly (i.e. as Wonka.subscribe or Wonka.map). Instead, it now only works if you open Wonka at the top of your .re file and additionally reference sources, sinks, and operators in their Wonka.<name> form.

Reproduction

  1. Init a new project: bsb -init wonka-test -theme basic-reason (this assumes you already have bs-platform installed globally).
  2. Paste this code in the root Reason file (I believe it's Demo.re in the boilerplate):
let source = Wonka.fromArray([|1, 2, 3|]);

source
|> Wonka.map((. _val) => Wonka.interval(_val * 1000) |> Wonka.take(3))
|> Wonka.concatAll
|> Wonka.subscribe((. _val) => print_int(_val));
  1. Run yarn build.
  2. You should see error output like the following:
➜  wonka-test yarn build
yarn run v1.16.0
$ bsb -make-world
[1/1] Building Wonka.cmi
ninja: no work to do.
[1/1] Building src/Demo-WonkaTest.cmj

  We've found a bug for you!
  /wonka-test/src/Demo.re 1:14-28

  1 │ let source = Wonka.fromArray([|1, 2, 3|]);
  2 │ 
  3 │ source

  The value fromArray can't be found in Wonka

Similarly, if you just open Wonka and don't reference the module further, you'll get an error. For example, following steps 3 and 4 above with this code:

open Wonka;
let source = fromArray([|1, 2, 3|]);

source
|> map((. _val) => interval(_val * 1000) |> take(3))
|> concatAll
|> subscribe((. _val) => print_int(_val));

will yield the following error:

➜  wonka-test yarn build
yarn run v1.16.0
$ bsb -make-world
[1/1] Building Wonka.cmi
ninja: no work to do.
[1/1] Building src/Demo-WonkaTest.cmj

  We've found a bug for you!
  /Users/parkerziegler/Documents/repos/OSS/wonka-test/src/Demo.re 2:14-22

  1 │ open Wonka;
  2 │ let source = fromArray([|1, 2, 3|]);
  3 │ 
  4 │ source

  The value fromArray can't be found

I'm thinking there's something about how modules are included that's going awry here, but I'm not 100% sure what it is. But I'll do some investigating when I can and try to see if I can figure out a solution. IIRC this worked fine in v2 so if we need to move back to a structure like that we maybe could? Not sure yet how that affects native stuff.

parkerziegler commented 5 years ago

Examining this more, I think I uncovered the source of things. In the change from v2 to v3, the namespace flag in bsconfig was turned to true (it defaults to false for backwards compatibility: https://bucklescript.github.io/docs/en/build-configuration#name-namespace). This means that the project, by default, is under the Wonka namespace. So, in order to access everything in wonka.ml, you actually have to use Wonka.Wonka (the first Wonka coming from the namespace, the second coming from wonka.ml).

A solution would be to revert the namespacing back to false (i.e. remove it from bsconfig.json). This seems ok to me, as all of the modules built in this project are prefixed with Wonka_ anyway, so it's very unlikely they'd conflict with any consumer code. Another possibility would be to not include anything in wonka.ml, but in that case I'm not sure how we'd do the conditional logic around including WonkaJs in web builds only 🤔

kitten commented 5 years ago

@parkerziegler ah yea that was definitely not intentional. I’ll reverse that :)