Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions donggil/EventLoop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Queue } from './Queue.js';

export class EventLoop {
#timer;
#queue;
#action;

constructor(action) {
this.#queue = new Queue();
this.#action = action;
}

init() {
this.#timer = setInterval(() => {
if (!this.#queue.isEmpty()) {
const value = this.#queue.dequeue();
this.#action(value);
}
}, 1000);
}

end() {
clearInterval(this.#timer);
}

add(value) {
this.#queue.enqueue(value);
}
}
16 changes: 16 additions & 0 deletions donggil/Printer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { EventLoop } from './EventLoop.js';

export class Printer {
#eventLoop;
constructor() {
const print = (value) => {
console.log(value);
};

this.#eventLoop = new EventLoop(print);
this.#eventLoop.init();
}
add(value) {
this.#eventLoop.add(value);
}
}
27 changes: 27 additions & 0 deletions donggil/Queue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export class Queue {
constructor() {
this.items = [];
}

enqueue(value) {
this.items.push(value);
}

dequeue() {
const cur = this.items.shift();
if (cur === undefined) throw new Error('큐가 비었습니다.');
return cur;
}

peek() {
return this.items[0];
}

getSize() {
return this.items.length;
}

isEmpty() {
return this.getSize() === 0;
}
}
12 changes: 12 additions & 0 deletions donggil/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { getInput } from './input.js';
import { Printer } from './Printer.js';

const pos = new Printer();

['첫번째 메시지 출력!!', '두번째 메시지 출력!!', '세번째 메시지 출력'].forEach(
(each) => pos.add(each)
);
while (1) {
const result = await getInput();
pos.add(result);
}
8 changes: 8 additions & 0 deletions donggil/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import readline from 'node:readline/promises';
import { stdin as input, stdout as output } from 'node:process';
const rl = readline.createInterface({ input, output });

export const getInput = async () => {
const result = await rl.question('> ');
return result;
};
13 changes: 13 additions & 0 deletions donggil/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "donggil",
"version": "1.0.0",
"description": "",
"main": "EventLoop.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"type":"module"
}