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: support some new features #5

Merged
merged 1 commit into from Jan 12, 2017
Merged

Conversation

gxcsoccer
Copy link
Member

@gxcsoccer gxcsoccer commented Jan 11, 2017

  • 支持 this.ready(err)
    // 客户端内部
    const err = new Error('some error');
    this.ready(err);
    
    // 调用者
    try {
       yield client.ready();  
    } catch (err) {
       // 会抛异常
    }
    
    client.ready(err => {
      if (err) {
        // 异常处理
        return;
      }
      // 启动后的逻辑
      // ...
    })
  • 支持 initMethod
    class Client extends SDKBase {
      constructor() {
        super({
          initMethod: 'init',
        });
      }
    
      * init() {
        // your initial code
      }
    }  
    
    class SDKBase extends EventEmitter {
      constructor(options) {
        super()
        // sdk-base 封装异步初始化逻辑,统一 co 引用
        if (options.initMethod) {
          co(this[options.initMethod].bind(this))
          .then(() => this.ready(true))
          .catch(err => this.ready(err)) ;
        }
      }
    }
  • 支持事件监听传入 generator
    // 一般形式
    client.on('data', data => {
      //  处理逻辑
      //  ...
    });
    
    // 异步数据处理
    client.on('data', function* (data) {
       yield callService(data);
       // ...
    });

close eggjs/egg#175

@mention-bot
Copy link

@gxcsoccer, thanks for your PR! By analyzing the history of the files in this pull request, we identified @fengmk2 and @dead-horse to be potential reviewers.

@codecov-io
Copy link

codecov-io commented Jan 11, 2017

Current coverage is 100% (diff: 100%)

No coverage report found for master at 14c8287.

Powered by Codecov. Last update 14c8287...680ed64

.then(() => this.ready(true))
.catch(err => this.ready(err));
}
this._ready = false;
Copy link
Member

Choose a reason for hiding this comment

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

ready 放到 get-ready 实现吧?

Copy link
Member Author

Choose a reason for hiding this comment

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

感觉没太大必要了,而且现在在 this.ready(err) 会同时 emit error,这就要求 client 是一个 EventEmitter,所以放 get-ready 反而有点不合适了

@@ -0,0 +1,21 @@
MIT License

Copyright (c) dead_horse and other contributors
Copy link
Member

Choose a reason for hiding this comment

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

Copyright (c) node-modules and other contributors

return;
}
console.error('\n[%s][pid: %s][%s][%s] %s: %s \nError Stack:\n %s',
Date(), process.pid, this.constructor.name, __filename, err.name,
Copy link
Member

Choose a reason for hiding this comment

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

__filename 其实不准确的。。。

Copy link
Member

Choose a reason for hiding this comment

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

__filename 可以不打印


var ready = require('get-ready');
Copy link
Member

Choose a reason for hiding this comment

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

为啥不在 get-ready 实现 ready 的逻辑?

Copy link
Member Author

Choose a reason for hiding this comment

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

以后就都 extends SDKBase 吧,我看了下 get-ready 其实也就是我们的几个模块在用

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

ok

Copy link
Member

@fengmk2 fengmk2 left a comment

Choose a reason for hiding this comment

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

+1

@fengmk2 fengmk2 merged commit 2a047c5 into master Jan 12, 2017
@fengmk2 fengmk2 deleted the support-some-new-features branch January 12, 2017 08:39
@fengmk2
Copy link
Member

fengmk2 commented Jan 12, 2017

3.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sdk-base 功能优化
5 participants