question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Binding a context to task functions referenced in series and parallel

See original GitHub issue

I have a config registry that’s basically the same thing as what’s in the docs. I also have about a dozen functions that represent different steps of a build process I have. I only expose three tasks to the actual gulp CLI tool (clean, build and serve).

My build task looks similar to this

gulp.task('build', gulp
    .series(cleanBuild, gulp
        .parallel(copyHtml, copyImages, babelCompile, prepareAppIndex, copyVendorFiles, lessCompile)));

Now my problem is that the functions (copyHtml, copyImages, etc) are not registered as tasks with my ConfigRegistry. That makes sense, they’re not really “tasks”. At least not ones I want to expose directly because that would just cause static/cruft for devs who just want an easy to use build process. So what happens is the functions try to access this.config but it’s never been binded because the individual functions are not registered as tasks so I get a classic “Cannot read property ‘config’ of undefined” TypeError.

Any ideas how I can keep this elegant or how undertaker could be updated to make this easier? The only way I can see to do it somewhat elegantly right now is to wrap each of my functions in a factory function. The function would then close (in the closure sense) over the factory function’s scope which would contain a config object I pass in (or I could just bind the config to the function).

function copyHtmlFactory(config) {
    function copyHtml() {
        // do something with the config parameter or this.config if using bind
    }

    return copyHtml; // or return copyHtml.bind({config: config});
}

I think another way I could do it would be to bind the function references (without the use of a factory function) before they are passed to .series() and .parallel().

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
phatedcommented, Sep 15, 2015

A custom registry can do tons of things.

How I would handle this:

  • Come up with a convention for internal tasks (like prefix with double underscore, e.g. __copyHtml)
  • Inside the custom registry’s .set() method, check if the name has the double underscore, and if it does, add it to a separate object attached to your custom registry. Also, make sure to bind the context and return the newly bound function in this method.
  • Inside the custom registry’s .get() method, check the name for double underscores and fetch out of the correct object.
  • Inside the custom registry’s .tree() method, only return public tasks. This method is used for exposing things to the CLI.
  • Inside the custom registry’s .init() method, register all tasks (public and private).

I think the above solution solves all your problems. The implementation for custom registries had a ton of thought behind it to allow you to implement these types of things but you need to think thoroughly about the solution. This is a highly advanced feature for gulp and you can do lots of wonderful and scary things.

0reactions
mikehaas763commented, Sep 23, 2015

No it’s not.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Executive Resources and Item-Context Binding
Executive Resources and Item-Context Binding: Exploring the Influence of Concurrent Inhibition, Updating, and Shifting Tasks on Context Memory.
Read more >
Guidance for developing Azure Functions | Microsoft Learn
The function.json file defines the function's trigger, bindings, ... A function app provides an execution context in Azure in which your ...
Read more >
Function binding
The sayHi is a “bound” function, that can be called alone or passed to setTimeout – doesn't matter, the context will be right....
Read more >
Input binding in function other than Azure Function within ...
Responses based on my opinion: Can input binding be done only in functions being Azure Functions? I believe yes it can only done...
Read more >
Scaling and Parallel Processing
In addition to any limits placed by the task executor (such as whether it is backed by a thread pool), the tasklet configuration...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found