forked from microsoft/napajs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
synchronized-loading.js
42 lines (33 loc) · 1.32 KB
/
synchronized-loading.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
const napa = require("napajs");
// Change this value to control number of napa workers initialized.
const NUMBER_OF_WORKERS = 4;
// Create a napa zone with number_of_workers napa workers.
const zone = napa.zone.create('zone', { workers: NUMBER_OF_WORKERS });
function run() {
// Initialize the phone book component
const phoneBook = require('./phone-book');
phoneBook.initialize();
// Setup all workers with functions to be executed.
zone.broadcast(' \
var napa = require("napajs"); \
var zone = napa.zone.get("zone"); \
var phoneBook = require("./phone-book"); \
');
zone.broadcast(lookupPhoneNumber.toString());
var tasks = [];
tasks.push(zone.execute('', 'lookupPhoneNumber', ['david']));
tasks.push(zone.execute('', 'lookupPhoneNumber', ['lisa']));
tasks.push(zone.execute('', 'lookupPhoneNumber', ['wade']));
Promise.all(tasks).then(function () {
console.log('[run] All operations are completed.');
});
}
function lookupPhoneNumber(name) {
console.log(`[lookupPhoneNumber] Start to lookup phone number of ${name}.`);
var n = phoneBook.lookup(name);
console.log(`[lookupPhoneNumber] ${name} : ${n ? n : '<not found>'}.`);
}
// Run program.
run();