gulpjs / undertaker

Task registry that allows composition through series/parallel methods.
MIT License
200 stars 31 forks source link

Sharing Functionalities example not working for `serial` and `parallel` tasks. #87

Open Smesharikable opened 5 years ago

Smesharikable commented 5 years ago

I tried to use Sharing Functionalities from README as an example to configure my tasks but it seems to not working, because this in function arguments of series and parallel stay bound to the Global object.

There is a gulpfile.js to reproduce:

var util = require('util');

var gulp = require('gulp');
var DefaultRegistry = require('undertaker-registry');

function ConfigRegistry(config){
  DefaultRegistry.call(this);
  this.config = config;
}

util.inherits(ConfigRegistry, DefaultRegistry);

ConfigRegistry.prototype.set = function set(name, fn) {
  // The `DefaultRegistry` uses `this._tasks` for storage.
  var task = this._tasks[name] = fn.bind(this.config);
  return task;
};

// `taker.registry` will reset each task in the registry with
// `ConfigRegistry.prototype.set` which will bind them to the config object.
gulp.registry(new ConfigRegistry({
  config: {
    src: './src',
    build: './build',
    bindTo: '0.0.0.0:8888'
  }
}));

gulp.task('dummy', function(cb) {
  // OK
  console.log('Config: ' + JSON.stringify(this.config));
  cb();
})

gulp.task('test-series', gulp.series('dummy', function(cb) {
  // undefined
  console.log('Config: ' + JSON.stringify(this.config));
  cb();
}));

gulp.task('test-parallel', gulp.parallel('dummy', function(cb) {
  // undefined
  console.log('Config: ' + JSON.stringify(this.config));
  cb();
}))
phated commented 5 years ago

When passing a function into a series or parallel call, it avoids the task system. This is something I've been thinking about a lot and don't have a solution for currently (for example, every anonymous function would be registered as "anonymous" in the task system, breaking all sorts of things).