Namespace sync
January 5, 2018 ยท View on GitHub
Table of Contents
APIs
Namespace sync deal with synchronization between threads in Napa. Lock is provided for exclusive and shared mutex scenarios.
Interface Lock
Exclusive Lock, which is transportable across JavaScript threads.
Use napa.sync.createLock() to create a lock.
var lock = napa.sync.createLock();
lock.guardSync(func: (...params: any[]) => any, params?: any[]): any
Run input function synchronously and obtain the lock during its execution, returns what the function returns, or throws error if input function throws. Lock will be released once execution finishes.
try {
var value = lock.guardSync(() => {
// DoSomething may throw.
return DoSomething();
});
console.log(value);
}
catch(error) {
console.log(error);
}
An example Synchronized Loading demonstrated how to implement a shared, lazy-loading phone book.