Skip to content
This repository has been archived by the owner on Jul 28, 2020. It is now read-only.

Missing binding of first argument of task defined as functions #3

Closed
pylebecq opened this issue Oct 23, 2018 · 1 comment
Closed

Missing binding of first argument of task defined as functions #3

pylebecq opened this issue Oct 23, 2018 · 1 comment
Labels

Comments

@pylebecq
Copy link
Contributor

pylebecq commented Oct 23, 2018

The documentation states the following:

Note: when executing the provided function, this is binded to the first argument provided. Eg. in new SimpleTask({id: 4}), this is binded to {id: 4} when executing the task.

Considering the following example:

require("./client");

function timeout(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

const { Task, Workflow } = require("zenaton");

const saveInDb = Task('saveInDb', async (done) => {
    console.log(this);
    done(null, 0);
});

const workflow1 = Workflow("workflow1", {
    init(initData) {
        this.id = initData.id
        this.name = initData.name
        this.sandbox = initData.sandbox
        this.temporary = initData.temporary
    },
    handle() {
        new saveInDb(this).dispatch();
    }
});

if (process.env.LAUNCH) {
    new workflow1({
        id: 1,
        name: "Test",
        sandbox: true,
        temporary: true
    }).dispatch();
}

We would expect it to print

{id: 1, name: "Test", sandbox: true, temporary: true}

Actually, it prints

{}

To run this example easily, save it to a file issue-bind.js in the examples-node repository.
Then, you can run zenaton listen --boot issue-bind.js and to start the workflow, run LAUNCH=1 node issue-bind.js

Either the documentation is wrong, or the binding is not done correctly.

Note: using the init() method to define the task work as expected:

require("./client");

function timeout(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

const { Task, Workflow } = require("zenaton");

const saveInDb = Task('saveInDb', {
    init(data) {
        this.data = data
    },
    handle(done) {
        console.log(this);
        done(null, 0);
    }
});

const workflow1 = Workflow("workflow1", {
    init(initData) {
        this.id = initData.id
        this.name = initData.name
        this.sandbox = initData.sandbox
        this.temporary = initData.temporary
    },
    handle() {
        new saveInDb(this).dispatch();
    }
});

if (process.env.LAUNCH) {
    new workflow1({
        id: 1,
        name: "Test",
        sandbox: true,
        temporary: true
    }).dispatch();
}

This prints the following:

{ data: { id: 1, name: 'Test', sandbox: true, temporary: true } }
@pylebecq pylebecq added the bug label Oct 23, 2018
@geomagilles
Copy link
Contributor

I close this, as it's no more relevant with v0.7

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

No branches or pull requests

2 participants