sounisi5011 / vec-draw

[WIP] SVGを書きやすくするDSL
0 stars 0 forks source link

unifiedを導入する #24

Closed sounisi5011 closed 5 years ago

sounisi5011 commented 5 years ago

unistのような、有志が規格化したASTを導入する。 https://github.com/sounisi5011/vec-draw/issues/15 で提案された作業を試みる。

sounisi5011 commented 5 years ago

unifiedのTransformerの第三引数nextはどうすれば良いんだろう。

If the signature of a transformer includes next (third argument), the function may finish asynchronous, and must invoke next().

この引数が渡されたら、next関数を呼び出さないといけないってこと…?

sounisi5011 commented 5 years ago

unifiedのTransformerの第三引数nextはどうすれば良いんだろう。

If the signature of a transformer includes next (third argument), the function may finish asynchronous, and must invoke next().

この引数が渡されたら、next関数を呼び出さないといけないってこと…?

コードをたどったら判明した。Transformer関数内で非同期処理をする場合は、

// Promise<Unist.Node>を返す
function transformer(node, file) {
  return new Promise((resolve, reject) => {
    node2node(node, file, (error, resultNode) => {
      if (error) {
        reject(error);
      } else {
        resolve(resultNode);
      }
    });
  });
}

// 第三引数 next コールバック関数を使用する
function transformer(node, file, next) {
  node2node(node, file, (error, resultNode) => {
    next(error || null, resultNode, file);
  });
}

このどちらの書き方もできるらしい。

unifiedが使用しているtroughは、Transformer関数の引数の数でこの判定を行っているみたいだ。 つまり、Promiseスタイルでやっている限り、第三引数nextを関数の定義に含める必要は無い。

sounisi5011 commented 5 years ago

buildしたものにhast.d.tsが含まれていない… 含むようにしたほうが良いんだろうか…?

sounisi5011 commented 5 years ago

エラー時の処理等が気になるけれど、とりあえずいったん終わりにしよう。