Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: first implement #2

Merged
merged 16 commits into from Feb 9, 2017
Merged

feat: first implement #2

merged 16 commits into from Feb 9, 2017

Conversation

popomore
Copy link
Member

@popomore popomore commented Feb 8, 2017

support require index or files

const mkdirp = require('mz-modules').mkdirp;
const mkdirp = require('mz-modules/mkdirp');

modules

  • mz-modules/mkdirp wrapped mkdirp
  • mz-modules/rimraf wrapped rimraf
  • mz-modules/glob wrapped glob
  • mz-modules/sleep wrapped ko-sleep
  • mz-modules/nextTick wrapped process.nextTick
  • mz-modules/setImmediate wrapped setImmediate

Closes #1

support require index or files

```js
const mkdirp = require('mz-modules').mkdirp;
const mkdirp = require('mz-modules/mkdirp');
```

modules

- mkdirp
- rimraf
- sleep
- nexttick
- glob
@codecov
Copy link

codecov bot commented Feb 8, 2017

Codecov Report

❗ No coverage uploaded for pull request base (master@7626020). Click here to learn what that means.

@@           Coverage Diff           @@
##             master     #2   +/-   ##
=======================================
  Coverage          ?   100%           
=======================================
  Files             ?      8           
  Lines             ?     24           
  Branches          ?      0           
=======================================
  Hits              ?     24           
  Misses            ?      0           
  Partials          ?      0
Impacted Files Coverage Δ
nextTick.js 100% <100%> (ø)
mkdirp.js 100% <100%> (ø)
glob.js 100% <100%> (ø)
lib/wrap.js 100% <100%> (ø)
rimraf.js 100% <100%> (ø)
setImmediate.js 100% <100%> (ø)
index.js 100% <100%> (ø)
sleep.js 100% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7626020...1b544ef. Read the comment docs.

'use strict';

module.exports = {
mkdirp: require('./mkdirp'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

写成 getter 吧,要不然就变成只使用 sleep,然后所有模块都 require 进来了。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个不改了吧,提供两种方式

nexttick.js Outdated
@@ -0,0 +1,7 @@
'use strict';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是不是也要一个 setImmediate ?

@popomore
Copy link
Member Author

popomore commented Feb 8, 2017 via email

@popomore
Copy link
Member Author

popomore commented Feb 8, 2017 via email

nexttick.js Outdated

module.exports = () => {
return new Promise(resolve => {
process.nextTick(resolve);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

呃,我记得 Promise 默认就是自动加 nextTick 的,所以这里直接 return new Promise(resolve => { resolve(); }); 就好

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nodejs/node#2736 (comment) Promise 默认在 nextTick 队列之后触发

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nextTick 感觉可以不提供了,需要的人直接写 yield Promise.resolve() 即可

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以这样写 module.exports = () => Promise.resolve();

实际测试 Promise.then() 好像是用的 setImmediate 而不是 process.nextTick

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

promise 没法做到 nextTick 的,感觉这个会有点误导?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那还是用真的 nextTick 吧,不要 hack 了

@popomore
Copy link
Member Author

popomore commented Feb 9, 2017

nextTick.js 我改成大写吧,保持一致

@popomore
Copy link
Member Author

popomore commented Feb 9, 2017

写了个公用方法,再看看

@popomore
Copy link
Member Author

popomore commented Feb 9, 2017

改完了去各个仓库安利

@popomore
Copy link
Member Author

popomore commented Feb 9, 2017

过了

@@ -0,0 +1,3 @@
'use strict';

module.exports = () => new Promise(resolve => setImmediate(resolve));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个好像无解,只能这样。其实这里是 nextTick 然后再一次 setImmediate

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

promise 用的是 setImmediate,不是 nextTick,所以是 nextTick 无解。。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

process.nextTick(() => console.log(1));
Promise.resolve().then(() => console.log(2));
process.nextTick(() => console.log(3));
// =>
// 1
// 3
// 2

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setImmediate(() => console.log(1));
Promise.resolve().then(() => console.log(2));
setImmediate(() => console.log(3));
// =>
// 2
// 1
// 3

额,都不是,尴尬了。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Promise.resolve 在 nextTick 之后,setImmediate 之前。所以相对于 nextTick,setImmediate 了

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的,这里要不换一个没这么歧义的名字?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

把这个用例加上

@popomore
Copy link
Member Author

popomore commented Feb 9, 2017

用例加了

@popomore
Copy link
Member Author

popomore commented Feb 9, 2017

npminstall 跑太慢,测试过了

@popomore popomore merged commit af94cb8 into master Feb 9, 2017
@popomore popomore deleted the first branch February 9, 2017 10:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants