nextflow中文文档|nextflow js中文教程|解析

npm npmdoc 2年前 (2021-12-29) 677次浏览

nextflow中文文档|nextflow js中文教程|解析

安装命令:npm i nextflow

Node.js – NextFlow

一个简单的 Node.js 控制流库,面向 CoffeeScript 开发人员。它也是 JavaScript 友好的。

为什么?

看看最著名的 JavaScript 控制流库:Async.jsStepSeq如果您要在 CoffeeScript 中使用这些库,您的代码将变得一团糟。

Async.js / CoffeeScript

async = require('async')
 
async.series(
  (->
    #first function 
  ),
  (->
    #second function 
  )
)
 

步骤 / CoffeeScript

Step = require('step')
 
Step(
  (->
    #first function 
  ),
  (->
    #second function 
  )
)

序列/CoffeeScript

Seq = require('seq')
 
Seq().seq(->
  #first function 
).seq(->
  #second function 
)

哎呀。如果您使用 JavaScript 进行编程,那么所有这些都是非常有用的解决方案。此外,公平地说,他们比 NextFlow 做得更多。但是 NextFlow 用 CoffeeScript 程序看起来要好得多。

关于异步

有人对我对 的批评发表了一些评论async理所当然。当我编写 NextFlow 时,我不知道异步的瀑布和对象传递功能。然而,这些方法仍然有其不足之处。我仍然认为 NextFlow 是一个轻量级的库,与 async 的无所不能相比,我也认为 NextFlow 的语法更令人愉快,即使对于 JavaScript 开发也是如此。

安装

npm install nextflow

用法

如果您愿意next()依次调用该函数,将参数传递给next()

next = require('nextflow')
 
vals = []
= 0
 
next flow =
  1: ->
    vals.push(1)
    @next()
  2: ->
    vals.push(2)
    = Math.random()
    @next(x)
  3: (num) ->
    vals.push(num)
    @next()
  4: ->
    vals.push(4)
    @next()
  5: ->
    console.log vals[0#is 1 
    console.log vals[1#is 2 
    console.log vals[2#is x 
    console.log vals[3#is 4 
 
 

通过标签调用函数,也传递参数:

vals = []
= 0
 
next flow =
  a1: ->
    vals.push(1)
    @a2()
  a2: ->
    vals.push(2)
    = Math.random()
    @a3(x)
  a3: (num) ->
    vals.push(num)
    @a4()
  a4: ->
    vals.push(4)
    @a5()
  a5: ->
    console.log vals[0#is 1 
    console.log vals[1#is 2 
    console.log vals[2#is x 
    console.log vals[3#is 4 
 

调用next()或调用标签:

vals = []
= 0
= 0
 
next flow =
  a1: ->
    vals.push(1)
    @a2()
  a2: ->
    vals.push(2)
    = Math.random()
    @a3(x)
  a3: (num) ->
    vals.push(num)
    = Math.random()
    @next(y)
  a4: (num) ->
    vals.push(num)
    @a5()
  a5: ->
    console.log vals[0#is 1 
    console.log vals[1#is 2 
    console.log vals[2#is x 
    console.log vals[3#is y 
 
 

错误处理

在一个函数中处理错误。标记它error:ERROR:ErRoR大小写无所谓。

next flow = 
  error: (err) ->
    console.log err.message
  1: ->
    throw new Error('some error')

通过将它们作为 @next 回调的第一个参数传递来处理错误:

next flow = 
  error: (err) ->
    console.log err.message #ENOENT, open '/tmp/this_file_hopefully_does_not_exist' 
  1: ->
    nonExistentFile = '/tmp/this_file_hopefully_does_not_exist'
    fs.readFile nonExistentFile@next

如果需要,手动调用错误函数

next flow = 
  error: (err) ->
    console.log err.message #"I feel like calling an error." 
  a1: ->
    @error(new Error("I feel like calling an error."))

JavaScript 友好

Rock 中提取的示例还使用BatchFlow

next({
    ERROR: function(err) {
        console.error(err);
    },
    isRepoPathLocal: function() {
        fs.exists(repoPath, this.next);
    },
    copyIfLocal: function(itsLocal) {
        if (itsLocal) {
            fs.copy(repoPath, projectPath, this.gitDirExist);
        } else {
            this.next();
        }
    },
    execGit: function() {
        exec(util.format("git clone %s %s", repoPath, projectPath), this.next);
    },
    gitDirExist: function(params) {
        fs.exists(path.join(projectPath, '.git'), this.next);
    },
    removeGitDir: function(gdirExists) {
        if (gdirExists)
            fs.remove(path.join(projectPath, '.git'), this.next);
        else
            this.next();
    },
    checkRockConf: function() {
        fs.exists(projectRockConf, this.next);
    },
    loadRockConf: function(rockConfExists) {
        if (rockConfExists)
            fs.readFile(projectRockConf, this.next);
        else
            this.next();
    },
    walkFiles: function(err, data) {
        var files = [], self = this; ignoreDirs = [];
 
        if (data) {
            projectRockObj = JSON.parse(data.toString());
            ignoreDirs = projectRockObj.ignoreDirs;
            if (ignoreDirs) {
                for (var i = 0; i < ignoreDirs.length; ++i) {
                    ignoreDirs[i] = path.resolve(projectPath, ignoreDirs[i]);
                }
            } else {
                ignoreDirs = [];
            }
        }
 
        walker(projectPath)
          .filterDir(function(dir, stat) { 
            if (dir === projectRockPath)
                return false;
            else
                if (ignoreDirs.indexOf(dir) >= 0) 
                    return false;
                else
                    return true;
          })
          .on('file', function(file) { files.push(file) })
          .on('end', function() { self.next(files); });
    },
    tweezeFiles: function(files) {
        tweezers.readFilesAndExtractUniq(files, this.next);
    },
    promptUser: function(err, tokenObj) {
        var replacements = {}, self = this;
 
        var rl = readline.createInterface({input: process.stdin, output: process.stdout})
        
        batch(tokenObj.tokens).seq().each(function(i, token, done) { 
            if (_(getSystemTokens()).keys().indexOf(token) === -1) {
                rl.question(token + '', function(input){
                    replacements[token] = input.trim();
                    done();
                });
            } else {
                replacements[token] = getSystemTokens()[token];
                done();
            }
        }).end(function(){
            rl.close();
            endCallback();
        });
    }
});

浏览器兼容性

我还没有让这个浏览器兼容,但是你可以通过一个简单的附加nextwindow对象的修改来做到这一点不过,我提醒您要彻底测试,因为此模块取决于插入对象的顺序的稳定性。如果违反了这一点,您就会遇到问题。据我了解,尽管大多数浏览器都遵守这一点,但这不是 ECMA 标准的一部分。

阅读此讨论以了解更多信息:http :
//code.google.com/p/v8/issues/detail?id=164

执照

麻省理工学院许可

版权所有 (c) 2012 JP Richardson

项目贡献人员列表:


极客公园 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:nextflow中文文档|nextflow js中文教程|解析
喜欢 (0)
.excerpt .focus {display:none}