Transloadit.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import async from 'async';
  2. export default class Transloadit {
  3. constructor(opts) {
  4. // Dictates in what order different plugin types are ran:
  5. this.types = [ 'presetter', 'selecter', 'uploader' ];
  6. // Container for different types of plugins
  7. this.plugins = {};
  8. }
  9. use(Plugin, opts) {
  10. // Instantiate
  11. var plugin = new Plugin(this, opts);
  12. // Save in plugin container
  13. // if (!this.plugins[plugin.type]) {
  14. // this.plugins[plugin.type] = [];
  15. // }
  16. this.plugins[plugin.type] = this.plugins[plugin.type] || [];
  17. this.plugins[plugin.type].push(plugin);
  18. return this;
  19. }
  20. setProgress(plugin, percentage) {
  21. // Any plugin can call this via `this.core.setProgress(this, precentage)`
  22. console.log(plugin.type + ' plugin ' + plugin.name + ' set the progress to ' + percentage);
  23. return this;
  24. }
  25. run() {
  26. // Walk over plugins in the order as defined by this.types.
  27. // var files = []
  28. // for (var j in this.types) {
  29. // var type = this.types[j];
  30. // // Walk over all plugins of this type, passing & modifying the files array as we go
  31. // for (var i in this.plugins[type]) {
  32. // var plugin = this.plugins[type][i];
  33. // console.log('--> Now running ' + plugin.type + ' plugin ' + plugin.name + ': ');
  34. // files = plugin.run(files);
  35. // console.dir(files);
  36. // console.log('');
  37. // }
  38. // }
  39. // console.log(this.plugins);
  40. // for (let plugin in this.plugins) {
  41. // console.log(this.plugins[plugin]);
  42. // this.plugins[plugin].run().then(function (text) {
  43. // console.log(text);
  44. // })
  45. // }
  46. // array of promises from all plugins
  47. // Promise.all(plugins).then(function(files) {
  48. // console.log(files);
  49. // });
  50. // Walk over plugins in the order as defined by this.types.
  51. // plugins.push(plugin.run.bind(plugin));
  52. var pluginTypePack = [];
  53. // function dummy(cb) {
  54. // cb(null, 'smth');
  55. // }
  56. // pluginTypePack.push(dummy);
  57. for (let j in this.types) {
  58. const type = this.types[j];
  59. const pluginPack = [];
  60. for (let i in this.plugins[type]) {
  61. const plugin = this.plugins[type][i];
  62. pluginPack.push(plugin.run.bind(plugin));
  63. }
  64. // console.log(pluginPack);
  65. const pluginTypePackExecuter = function (done) {
  66. async.parallel(pluginPack, function (err, files) {
  67. // console.log('parallel done');
  68. console.log(files);
  69. done(files);
  70. });
  71. };
  72. pluginTypePack.push(pluginTypePackExecuter);
  73. }
  74. // console.log(pluginTypePack);
  75. async.waterfall(pluginTypePack, function (result) {
  76. // console.log(result);
  77. });
  78. // console.log(plugins);
  79. // core.run is the final step and retuns the results (vs every other method, returning `this`)
  80. // for chainability
  81. // return files;
  82. }
  83. }