Skip to content

Commit 3bd0207

Browse files
author
Mendes Hugo
committed
chore(producer-consumer): add producer-consumer definitions
#3
1 parent aaedf51 commit 3bd0207

File tree

8 files changed

+122
-1
lines changed

8 files changed

+122
-1
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from "./exceptions";
22
export * from "./mutex";
3+
export * from "./producer-consumer";
34
export * from "./semaphore";
45
export * from "./synchronizer.interface";
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./producer-consumer.exception";
2+
export * from "./producer-consumer.invalid-read-parameter.exception";
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { ConcurrencyException } from "../../exceptions";
2+
3+
/**
4+
* Any exception related only to a [ProducerConsumer]{@link ProducerConsumer}.
5+
*/
6+
export abstract class ProducerConsumerException extends ConcurrencyException {}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { ProducerConsumerException } from "./producer-consumer.exception";
2+
3+
/**
4+
* Exception when a parameter is invalid for a read function
5+
*/
6+
export class ProducerConsumerInvalidReadParameterException extends ProducerConsumerException {}

src/producer-consumer/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./exceptions";
2+
export * from "./producer-consumer";

src/producer-consumer/producer-consumer.spec.ts

Whitespace-only changes.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { Synchronizer } from "../synchronizer.interface";
2+
3+
export class ProducerConsumer<T> implements Synchronizer {
4+
/**
5+
* The data available for instant reading
6+
*/
7+
private readonly items: T[];
8+
9+
/**
10+
* @returns the number of waiting "Threads" on any read
11+
*/
12+
public get queueLength(): number {
13+
throw new Error("Not implemented yet");
14+
}
15+
16+
/**
17+
* Create a ProducerConsumer
18+
*
19+
* @param initialItems the initial available values
20+
*/
21+
public constructor(initialItems: readonly T[] = []) {
22+
this.items = initialItems.slice();
23+
}
24+
25+
/**
26+
* Reads a given number of items:
27+
* Wait until the given number of items are available.
28+
*
29+
* @param nItem the number of items to read
30+
* @throws {ProducerConsumerInvalidReadParameterException} when the number of items to read is invalid
31+
* @throws {ConcurrencyInterruptedException} when the producer-consumer is interrupted
32+
* @returns a promise with the read results
33+
*/
34+
public read(nItem: number): Promise<T[]> {
35+
throw new Error("Not implemented yet");
36+
}
37+
38+
/**
39+
* Reads one item:
40+
* Wait until the item is available.
41+
*
42+
* @throws {ConcurrencyInterruptedException} when the producer-consumer is interrupted
43+
* @returns a promise with the read results
44+
*/
45+
public readOne(): Promise<T> {
46+
return this.read(1).then(([data]) => data);
47+
}
48+
49+
/**
50+
* Reads a given number of items:
51+
* Wait until the given number of items are available.
52+
*
53+
* Throws an error if the given time exceeds
54+
* and re-establish the state as if the method was not called.
55+
*
56+
* @param timeout maximum time (in ms) to read the items
57+
* @param nItem the number of items to read
58+
* @throws {ProducerConsumerInvalidReadParameterException} when the number of items to read is invalid
59+
* @throws {ConcurrencyInterruptedException} when the producer-consumer is interrupted
60+
* @returns a promise with the read results
61+
*/
62+
public tryRead(timeout: number, nItem: number): Promise<T[]> {
63+
throw new Error("Not implemented yet");
64+
}
65+
66+
/**
67+
* Reads one item:
68+
* Wait until the item is available.
69+
*
70+
* Throws an error if the given time exceeds
71+
* and re-establish the state as if the method was not called.
72+
*
73+
* @param timeout maximum time (in ms) to read the items
74+
* @throws {ConcurrencyInterruptedException} when the producer-consumer is interrupted
75+
* @returns a promise with the read result
76+
*/
77+
public tryReadOne(timeout: number): Promise<T> {
78+
return this.tryRead(timeout, 1).then(([data]) => data);
79+
}
80+
81+
/**
82+
* Write some items to the producer-consumer buffer.
83+
* It releases the await readings or store the data.
84+
*
85+
* @param items the items to write
86+
*/
87+
public write(items: T | T[]) {
88+
if (!Array.isArray(items)) {
89+
items = [items];
90+
}
91+
92+
throw new Error("Not implemented yet");
93+
}
94+
95+
/**
96+
* Interrupts all awaiting "threads" with an [exception]{@link ConcurrencyInterruptedException}.
97+
*
98+
* @param reason The reason why this producer-consumer is being interrupted
99+
* @param items the items to set once everything has been interrupted
100+
*/
101+
public interrupt(reason: unknown, items: readonly T[] = []) {
102+
throw new Error("Not implemented yet");
103+
}
104+
}

src/semaphore/semaphore.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ export class Semaphore implements Synchronizer {
226226
}
227227

228228
/**
229-
* Interrupts all awaiting "Threads" with an [exception]{@link ConcurrencyInterruptedException}.
229+
* Interrupts all awaiting "threads" with an [exception]{@link ConcurrencyInterruptedException}.
230230
*
231231
* @param reason The reason why this semaphore is being interrupted
232232
* @param permits The permits to set to this semaphore once everything has been interrupted

0 commit comments

Comments
 (0)