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

bug: Problem with the object prototype when piping #12

Open
hrajchert opened this issue May 14, 2019 · 2 comments
Open

bug: Problem with the object prototype when piping #12

hrajchert opened this issue May 14, 2019 · 2 comments
Assignees
Milestone

Comments

@hrajchert
Copy link
Contributor

When we use a custom operator like the tap defined below, the input loses its prototype

import { Task, UnknownError } from '@ts-task/task';
import { map } from '@ts-task/task/dist/lib/src/operators';

const tap = <T, E> (fn: (t: T) => unknown) => {
    return function (input: Task<T, E>): Task<T, E | UnknownError> {
        return input.map(val => {
            fn(val);
            return val;
        });
    };
};

Task.resolve(1)
    .pipe(
        map(x => x + 1),
        tap(x => console.log('waaa', x))
    )
    .fork(
        err => console.error('buu', err),
        val => console.log(val)
    );
buu Error: UnknownError (input.map is not a function)
    at fns.reduce (/Users/test/myapp/node_modules/@ts-task/task/dist/task.umd.js:194:40)
    at Array.reduce (<anonymous>)
    at Task [as resolver] (/Users/test/myapp/node_modules/@ts-task/task/dist/task.umd.js:189:33)
    at Promise (/Users/test/myapp/node_modules/@ts-task/task/dist/task.umd.js:42:22)
    at new Promise (<anonymous>)
    at Task.fork (/Users/test/myapp/node_modules/@ts-task/task/dist/task.umd.js:41:13)
    at Object.<anonymous> (/Users/test/myapp/dist/index.js:28:6)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
--------------
TypeError: input.map is not a function
    at /Users/test/myapp/dist/index.js:7:22
    at fns.reduce (/Users/test/myapp/node_modules/@ts-task/task/dist/task.umd.js:191:28)
    at Array.reduce (<anonymous>)
    at Task [as resolver] (/Users/test/myapp/node_modules/@ts-task/task/dist/task.umd.js:189:33)
    at Promise (/Users/test/myapp/node_modules/@ts-task/task/dist/task.umd.js:42:22)
    at new Promise (<anonymous>)
    at Task.fork (/Users/test/myapp/node_modules/@ts-task/task/dist/task.umd.js:41:13)
    at Object.<anonymous> (/Users/test/myapp/dist/index.js:28:6)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
@hrajchert
Copy link
Contributor Author

hrajchert commented May 14, 2019

Found out that the problem is because i'm using

import { map } from '@ts-task/task/dist/lib/src/operators';

instead of

import { operators, Task, UnknownError } from '@ts-task/task';
const {map} = operators;

Need to figure out how to improve exporting/importing
From rxjs, seen that package.json main should point to a commonjs module instead of umd

@hrajchert hrajchert added this to the Version 2.0 milestone May 14, 2019
@hrajchert
Copy link
Contributor Author

This is going to be a breaking change in the imports name.
Instead of importing operators like this:

import { Task, operators } from '@ts-task/task';

const { map } = operators;

or the unforeseeing (but kind of works excepts for the problem of only having fork)

import { map } from '@ts-task/task/dist/lib/src/operators';

The idea is to modify the build system to mimic how rxjs exposes its operator and do something like

import { Task } from '@ts-task/task';
import { map } from '@ts-task/task/operators';

@hrajchert hrajchert self-assigned this May 14, 2019
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