1.0.0 • Published 7 years ago

egg-bridge v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

egg-bridge

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Install

$ npm i egg-bridge --save

Usage

// {app_root}/config/plugin.js
exports.bridge = {
  enable: true,
  package: "egg-bridge"
};

Configuration

// {app_root}/config/config.default.js
exports.bridge = {
  timeout: 3000
};
// {app_root}/app/bridge/handle.js
const { BaseContextClass } = require("egg");
class Handle extends BaseContextClass {
  async bridge(...args) {
    return args;
  }
}
module.exports = Handle;

see config/config.default.js for more detail.

API

call from worker process:

// wait agent process handle result
await app.bridge().handle()
// wait agent process handle result,with ctx
await app.bridge(ctx).handle()
// just execute agent process handle
app.bridge('nowait').handle()
// just execute agent process handle,with ctx
app.bridge(ctx,'nowait').handle()
// execlute handle in this process
await app.bridge('self').handle()
// execlute handle in this process,with ctx
await app.bridge(ctx,'self').handle()
// wait agent process handle result,in package
await app.bridge().package.subpackage.handle()

call from agent process:

// wait random worker process handle result
await agent.bridge().handle()
// wait random worker process handle result,with ctx
await agent.bridge(ctx).handle()
// just execute random worker process handle
agent.bridge('nowait').handle()
// just execute random worker process handle,with ctx
agent.bridge(ctx,'nowait').handle()
// execlute handle in agent process
await agent.bridge('self').handle()
// execlute handle in agent process,with ctx
await agent.bridge(ctx,'self').handle()
// wait random worker process handle result,in package
await agent.bridge().package.subpackage.handle()

Example

// {app_root}/app/bridge/hello.js
const { BaseContextClass } = require('egg');
class Hello extends BaseContextClass {
  // optional
  static get timeout(){
    return 2000;
  }
  bridge(name) {
    return this.service.echo.hello(name);
  }
}

module.exports = Hello

// {app_root}/app/service/echo.js
const { Service } = require('egg');
const sleep = time => new Promise(resolve => setTimeout(resolve, time));
class EchoService extends Service {
  async hello(name) {
    await sleep(1500);
    return `hello ${name}`;
  }
}

module.exports = EchoService;


// agent.js
module.exports = agent => {
  agent.messenger.on('egg-ready', async () => {
    const ret = await agent.bridge().hello('world');
    console.log(ret);
  });
};

see example/app/bridge for more detail.

Questions & Suggestions

Please open an issue here.

License

MIT