Skip to content

Commit 13de20a

Browse files
committed
Add examples
1 parent 1f20e9d commit 13de20a

File tree

4 files changed

+330
-0
lines changed

4 files changed

+330
-0
lines changed

JavaScript/1-sync.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
'use strict';
2+
3+
const checkAvailability = (items, state) => {
4+
console.log({ checkStart: items });
5+
for (const item of items) {
6+
const { id, quantity } = item;
7+
const count = state[id];
8+
if (quantity > count) {
9+
console.log({ checkEnd: false });
10+
return false;
11+
}
12+
}
13+
console.log({ checkEnd: true });
14+
return true;
15+
};
16+
17+
const processPayment = (payment) => {
18+
console.log({ payment });
19+
};
20+
21+
const shipGoods = (items, state) => {
22+
for (const { id, quantity } of items) {
23+
state[id] -= quantity;
24+
}
25+
console.log({ ship: items });
26+
};
27+
28+
const sendOrderConfirmation = (email) => {
29+
console.log({ email });
30+
};
31+
32+
const buy = (order, state) => {
33+
const available = checkAvailability(order.items, state);
34+
if (available) {
35+
processPayment(order.paymentDetails);
36+
shipGoods(order.items, state);
37+
sendOrderConfirmation(order.userEmail);
38+
}
39+
console.log({ state });
40+
};
41+
42+
const state = { 1722: 5 };
43+
const name = 'A4 Paper; 500 sheets; 75 Gsm';
44+
45+
setTimeout(() => {
46+
const order1 = {
47+
paymentDetails: { card: '**** **** **** 1234' },
48+
items: [{ id: '1722', name, price: 52, quantity: 3 }],
49+
userEmail: '[email protected]',
50+
};
51+
buy(order1, state);
52+
}, 100);
53+
54+
setTimeout(() => {
55+
const order2 = {
56+
paymentDetails: { card: '**** **** **** 1234' },
57+
items: [{ id: '1722', name, price: 52, quantity: 1 }],
58+
userEmail: '[email protected]',
59+
};
60+
buy(order2, state);
61+
}, 100);
62+
63+
setTimeout(() => {
64+
const order3 = {
65+
paymentDetails: { card: '**** **** **** 1234' },
66+
items: [{ id: '1722', name, price: 52, quantity: 2 }],
67+
userEmail: '[email protected]',
68+
};
69+
buy(order3, state);
70+
}, 100);

JavaScript/2-async.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
'use strict';
2+
3+
const timers = require('node:timers/promises');
4+
5+
const randomTimeout = () => timers.setTimeout(Math.random() * 500);
6+
7+
const checkAvailability = async (items, state) => {
8+
console.log({ checkStart: items });
9+
for (const item of items) {
10+
const { id, quantity } = item;
11+
const count = state[id];
12+
if (quantity > count) {
13+
console.log({ checkEnd: false });
14+
return false;
15+
}
16+
}
17+
console.log({ checkEnd: true });
18+
await randomTimeout();
19+
return true;
20+
};
21+
22+
const processPayment = async (payment) => {
23+
console.log({ payment });
24+
await randomTimeout();
25+
};
26+
27+
const shipGoods = async (items, state) => {
28+
for (const { id, quantity } of items) {
29+
state[id] -= quantity;
30+
}
31+
console.log({ ship: items });
32+
await randomTimeout();
33+
};
34+
35+
const sendOrderConfirmation = async (email) => {
36+
console.log({ email });
37+
await randomTimeout();
38+
};
39+
40+
const buy = async (order, state) => {
41+
const available = await checkAvailability(order.items, state);
42+
if (available) {
43+
await processPayment(order.paymentDetails);
44+
await shipGoods(order.items, state);
45+
await sendOrderConfirmation(order.userEmail);
46+
}
47+
console.log({ state });
48+
};
49+
50+
const state = { 1722: 5 };
51+
const name = 'A4 Paper; 500 sheets; 75 Gsm';
52+
53+
const main = async () => {
54+
const order1 = {
55+
paymentDetails: { card: '**** **** **** 1234' },
56+
items: [{ id: '1722', name, price: 52, quantity: 3 }],
57+
userEmail: '[email protected]',
58+
};
59+
await buy(order1, state);
60+
61+
const order2 = {
62+
paymentDetails: { card: '**** **** **** 1234' },
63+
items: [{ id: '1722', name, price: 52, quantity: 1 }],
64+
userEmail: '[email protected]',
65+
};
66+
await buy(order2, state);
67+
68+
const order3 = {
69+
paymentDetails: { card: '**** **** **** 1234' },
70+
items: [{ id: '1722', name, price: 52, quantity: 2 }],
71+
userEmail: '[email protected]',
72+
};
73+
await buy(order3, state);
74+
};
75+
76+
main();

JavaScript/3-unsafe.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
'use strict';
2+
3+
const timers = require('node:timers/promises');
4+
5+
const randomTimeout = () => timers.setTimeout(Math.random() * 500);
6+
7+
const checkAvailability = async (items, state) => {
8+
console.log({ checkStart: items });
9+
for (const item of items) {
10+
const { id, quantity } = item;
11+
const count = state[id];
12+
if (quantity > count) {
13+
console.log({ checkEnd: false });
14+
return false;
15+
}
16+
}
17+
console.log({ checkEnd: true });
18+
await randomTimeout();
19+
return true;
20+
};
21+
22+
const processPayment = async (payment) => {
23+
console.log({ payment });
24+
await randomTimeout();
25+
};
26+
27+
const shipGoods = async (items, state) => {
28+
for (const { id, quantity } of items) {
29+
state[id] -= quantity;
30+
}
31+
console.log({ ship: items });
32+
await randomTimeout();
33+
};
34+
35+
const sendOrderConfirmation = async (email) => {
36+
console.log({ email });
37+
await randomTimeout();
38+
};
39+
40+
const buy = async (order, state) => {
41+
const available = await checkAvailability(order.items, state);
42+
if (available) {
43+
await processPayment(order.paymentDetails);
44+
await shipGoods(order.items, state);
45+
await sendOrderConfirmation(order.userEmail);
46+
}
47+
console.log({ state });
48+
};
49+
50+
const state = { 1722: 5 };
51+
const name = 'A4 Paper; 500 sheets; 75 Gsm';
52+
53+
setTimeout(() => {
54+
const order1 = {
55+
paymentDetails: { card: '**** **** **** 1234' },
56+
items: [{ id: '1722', name, price: 52, quantity: 3 }],
57+
userEmail: '[email protected]',
58+
};
59+
buy(order1, state);
60+
}, 10);
61+
62+
setTimeout(() => {
63+
const order2 = {
64+
paymentDetails: { card: '**** **** **** 1234' },
65+
items: [{ id: '1722', name, price: 52, quantity: 1 }],
66+
userEmail: '[email protected]',
67+
};
68+
buy(order2, state);
69+
}, 10);
70+
71+
setTimeout(() => {
72+
const order3 = {
73+
paymentDetails: { card: '**** **** **** 1234' },
74+
items: [{ id: '1722', name, price: 52, quantity: 2 }],
75+
userEmail: '[email protected]',
76+
};
77+
buy(order3, state);
78+
}, 10);

JavaScript/4-actor.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
'use strict';
2+
3+
const timers = require('node:timers/promises');
4+
const randomTimeout = () => timers.setTimeout(Math.random() * 500);
5+
6+
class Actor {
7+
#queue = [];
8+
#processing = false;
9+
#behavior = null;
10+
#state = null;
11+
12+
constructor(behavior, state) {
13+
this.#behavior = behavior;
14+
this.#state = state;
15+
}
16+
17+
async send(message) {
18+
this.#queue.push(message);
19+
await this.#process();
20+
}
21+
22+
async #process() {
23+
if (this.#processing) return;
24+
this.#processing = true;
25+
26+
while (this.#queue.length) {
27+
const message = this.#queue.shift();
28+
await this.#behavior(message, this.#state);
29+
}
30+
31+
this.#processing = false;
32+
}
33+
}
34+
35+
// Usage
36+
37+
const checkAvailability = async (items, state) => {
38+
console.log({ checkStart: items });
39+
for (const item of items) {
40+
const { id, quantity } = item;
41+
const count = state[id];
42+
if (quantity > count) {
43+
console.log({ checkEnd: false });
44+
return false;
45+
}
46+
}
47+
console.log({ checkEnd: true });
48+
await randomTimeout();
49+
return true;
50+
};
51+
52+
const processPayment = async (payment) => {
53+
console.log({ payment });
54+
await randomTimeout();
55+
};
56+
57+
const shipGoods = async (items, state) => {
58+
for (const { id, quantity } of items) {
59+
state[id] -= quantity;
60+
}
61+
await randomTimeout();
62+
console.log({ ship: items });
63+
};
64+
65+
const sendOrderConfirmation = async (email) => {
66+
console.log({ email });
67+
await randomTimeout();
68+
};
69+
70+
const buy = async (order, state) => {
71+
const available = await checkAvailability(order.items, state);
72+
if (available) {
73+
await processPayment(order.paymentDetails);
74+
await shipGoods(order.items, state);
75+
await sendOrderConfirmation(order.userEmail);
76+
}
77+
console.log({ state });
78+
};
79+
80+
const main = async () => {
81+
const orderActor = new Actor(buy, { 1722: 5 });
82+
const name = 'A4 Paper; 500 sheets; 75 Gsm';
83+
84+
const order1 = {
85+
paymentDetails: { card: '**** **** **** 1234' },
86+
items: [{ id: '1722', name, price: 52, quantity: 3 }],
87+
userEmail: '[email protected]',
88+
};
89+
await orderActor.send(order1);
90+
91+
const order2 = {
92+
paymentDetails: { card: '**** **** **** 1234' },
93+
items: [{ id: '1722', name, price: 52, quantity: 1 }],
94+
userEmail: '[email protected]',
95+
};
96+
await orderActor.send(order2);
97+
98+
const order3 = {
99+
paymentDetails: { card: '**** **** **** 1234' },
100+
items: [{ id: '1722', name, price: 52, quantity: 2 }],
101+
userEmail: '[email protected]',
102+
};
103+
await orderActor.send(order3);
104+
};
105+
106+
main();

0 commit comments

Comments
 (0)