-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathnode_test.js
60 lines (45 loc) · 1.27 KB
/
node_test.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"use strict";
/** @type {import('./src/index.ts').threaded} */
const threaded = require("./dist/index.js").threaded;
/** @type {import('./src/index.ts').$unclaim}*/
const $unclaim = require("./dist/index.js").$unclaim;
/** @type {import('./src/index.ts').$claim}*/
const $claim = require("./dist/index.js").$claim;
function add(a, b) {
return a + b;
}
async function main() {
const add = threaded(function* (a, b) {
return a + b;
});
console.log(await add(5, 10)); // 15
// const user = {
// name: "john",
// balance: 100,
// };
// const addBalance = threaded(async function* (amount) {
// yield { user, add };
// await $claim(user);
// // Thread now has ownership over user and is
// // guaranteed not to change by other threads
// user.balance = add(user.balance, amount);
// $unclaim(user);
// return user;
// });
// await Promise.all([
// addBalance(10),
// addBalance(10),
// addBalance(10),
// addBalance(10),
// addBalance(10),
// addBalance(10),
// addBalance(10),
// addBalance(10),
// addBalance(10),
// addBalance(10),
// ]);
// console.assert(user.balance === 200, "Balance should be 200");
// console.log("Result in main:", user);
// addBalance.dispose();
}
main();