Skip to content

Commit

Permalink
docs(ipc): use async (#1722)
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 authored and dead-horse committed Nov 29, 2017
1 parent 503b69b commit 1420682
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 58 deletions.
54 changes: 25 additions & 29 deletions docs/source/en/core/cluster-and-ipc.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,27 +332,25 @@ We put all logics that is used to interact with the remote data source into a Se

```js
// app/service/source.js
module.exports = app => {
let memoryCache = {};

return class Source extends app.Service {
get(key) {
return memoryCache[key];
}

* checkUpdate() {
// check if remote data source has changed
const updated = yield mockCheck();
this.ctx.logger.info('check update response %s', updated);
return updated;
}

* update() {
// update memory cache from remote
memoryCache = yield mockFetch();
this.ctx.logger.info('update memory cache from remote: %j', memoryCache);
}
};
let memoryCache = {};

class SourceService extends Service {
get(key) {
return memoryCache[key];
}

async checkUpdate() {
// check if remote data source has changed
const updated = await mockCheck();
this.ctx.logger.info('check update response %s', updated);
return updated;
}

async update() {
// update memory cache from remote
memoryCache = await mockFetch();
this.ctx.logger.info('update memory cache from remote: %j', memoryCache);
}
}
```

Expand All @@ -365,8 +363,8 @@ exports.schedule = {
type: 'all', // run in all workers
};

exports.task = function* (ctx) {
yield ctx.service.source.update();
exports.task = async ctx => {
await ctx.service.source.update();
ctx.app.lastUpdateBy = 'force';
};
```
Expand All @@ -380,8 +378,8 @@ exports.schedule = {
type: 'worker', // only run in one worker
};

exports.task = function* (ctx) {
const needRefresh = yield ctx.service.source.checkUpdate();
exports.task = async ctx => {
const needRefresh = await ctx.service.source.checkUpdate();
if (!needRefresh) return;

// notify all workers to update memory cache from `file`
Expand All @@ -398,10 +396,8 @@ module.exports = app => {
app.logger.info('start update by %s', by);
// create an anonymous context to access service
const ctx = app.createAnonymousContext();
// a convenient way to execute with generator function
// can replaced by `co`
ctx.runInBackground(function* () {
yield ctx.service.source.update();
ctx.runInBackground(async () => {
await ctx.service.source.update();
app.lastUpdateBy = by;
});
});
Expand Down
54 changes: 25 additions & 29 deletions docs/source/zh-cn/core/cluster-and-ipc.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,27 +334,25 @@ app.messenger.once(action, data => {

```js
// app/service/source.js
module.exports = app => {
let memoryCache = {};

return class Source extends app.Service {
get(key) {
return memoryCache[key];
}

* checkUpdate() {
// check if remote data source has changed
const updated = yield mockCheck();
this.ctx.logger.info('check update response %s', updated);
return updated;
}

* update() {
// update memory cache from remote
memoryCache = yield mockFetch();
this.ctx.logger.info('update memory cache from remote: %j', memoryCache);
}
};
let memoryCache = {};

class SourceService extends Service {
get(key) {
return memoryCache[key];
}

async checkUpdate() {
// check if remote data source has changed
const updated = await mockCheck();
this.ctx.logger.info('check update response %s', updated);
return updated;
}

async update() {
// update memory cache from remote
memoryCache = await mockFetch();
this.ctx.logger.info('update memory cache from remote: %j', memoryCache);
}
}
```

Expand All @@ -367,8 +365,8 @@ exports.schedule = {
type: 'all', // run in all workers
};

exports.task = function* (ctx) {
yield ctx.service.source.update();
exports.task = async ctx => {
await ctx.service.source.update();
ctx.app.lastUpdateBy = 'force';
};
```
Expand All @@ -382,8 +380,8 @@ exports.schedule = {
type: 'worker', // only run in one worker
};

exports.task = function* (ctx) {
const needRefresh = yield ctx.service.source.checkUpdate();
exports.task = async ctx => {
const needRefresh = await ctx.service.source.checkUpdate();
if (!needRefresh) return;

// notify all workers to update memory cache from `file`
Expand All @@ -400,10 +398,8 @@ module.exports = app => {
app.logger.info('start update by %s', by);
// create an anonymous context to access service
const ctx = app.createAnonymousContext();
// a convenient way to execute with generator function
// can be replaced by `co`
ctx.runInBackground(function* () {
yield ctx.service.source.update();
ctx.runInBackground(async () => {
await ctx.service.source.update();
app.lastUpdateBy = by;
});
});
Expand Down

0 comments on commit 1420682

Please sign in to comment.