Skip to content

Latest commit

 

History

History
91 lines (78 loc) · 3.26 KB

sync.md

File metadata and controls

91 lines (78 loc) · 3.26 KB

Namespace sync

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.