This example implements a shared phone book component. The component will not load data until the first lookup request happens. When it starts to load data, it ensures the loading will run only once.
The component is implemented using lazy-loading pattern with the use of napa.sync.Lock
.
- Go to directory of "examples/tutorial/synchronized-loading"
- Run "npm install" to install napajs
- Run "node synchronized-loading.js"
The output below shows one possible result. The sequence of the output may be different but the data loading will always run once only.
[lookupPhoneNumber] Start to lookup phone number of david.
[lookupPhoneNumber] Start to lookup phone number of wade.
[load_data] loading...
[lookupPhoneNumber] Start to lookup phone number of lisa.
[lookupPhoneNumber] wade : <not found>.
[lookupPhoneNumber] lisa : 567-888-9999.
[lookupPhoneNumber] david : 123-444-5555.
[run] All operations are completed.