Skip to content

Commit

Permalink
refactor(main): write the main control flow in an async function (#129)
Browse files Browse the repository at this point in the history
* refactor(npm): rewrite getDownloads using async/await

* refactor(main): write the main control flow in an async function

* docs(main): write some explaining comments of the flow

* refactor(main): return instead of await
  • Loading branch information
Haroenv authored Apr 17, 2018
1 parent 412e002 commit 7c30925
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/__tests__/npm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ describe('getDownloads()', () => {
expect(angular).toBeGreaterThan(1900000);
expect(angular).toBeLessThan(3000000);

expect(holmes).toBeGreaterThan(250);
expect(holmes).toBeGreaterThan(200);
expect(holmes).toBeLessThan(550);
});

Expand Down
24 changes: 15 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,21 @@ const { index: mainIndex, client } = createAlgoliaIndex(c.indexName);
const { index: bootstrapIndex } = createAlgoliaIndex(c.bootstrapIndexName);
const stateManager = createStateManager(mainIndex);

Promise.resolve()
.then(() => setSettings(bootstrapIndex))
.then(() => stateManager.check())
.then(bootstrap)
.then(() => stateManager.get())
.then(replicate)
.then(() => stateManager.get())
.then(watch)
.catch(error);
async function main() {
// first we make sure the bootstrap index has the correct settings
await setSettings(bootstrapIndex);
// then we run the bootstrap
// after a bootstrap is done, it's moved to main (with settings)
// if it was already finished, we will set the settings on the main index
await bootstrap(await stateManager.check());
// then we figure out which updates we missed since
// the last time main index was updated
await replicate(await stateManager.get());
// then we watch 👀 for all changes happening in the ecosystem
return watch(await stateManager.get());
}

main().catch(error);

async function setSettings(index) {
await index.setSettings(c.indexSettings);
Expand Down

0 comments on commit 7c30925

Please sign in to comment.