Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Borewit committed Nov 3, 2023
0 parents commit 84ba8e2
Show file tree
Hide file tree
Showing 9 changed files with 2,844 additions and 0 deletions.
233 changes: 233 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
{
"env": {
"browser": true,
"es6": true,
"node": true
},
"extends": [
"prettier"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "./tsconfig.json",
"sourceType": "module"
},
"plugins": [
"eslint-plugin-import",
"eslint-plugin-jsdoc",
"eslint-plugin-unicorn",
"@typescript-eslint"
],
"ignorePatterns": ["src/**/*.d.ts"],
"rules": {
"@typescript-eslint/adjacent-overload-signatures": "error",
"@typescript-eslint/array-type": [
"error",
{
"default": "array"
}
],
"@typescript-eslint/ban-types": [
"error",
{
"types": {
"Object": {
"message": "Avoid using the `Object` type. Did you mean `object`?"
},
"Function": {
"message": "Avoid using the `Function` type. Prefer a specific function type, like `() => void`."
},
"Boolean": {
"message": "Avoid using the `Boolean` type. Did you mean `boolean`?"
},
"Number": {
"message": "Avoid using the `Number` type. Did you mean `number`?"
},
"String": {
"message": "Avoid using the `String` type. Did you mean `string`?"
},
"Symbol": {
"message": "Avoid using the `Symbol` type. Did you mean `symbol`?"
}
}
}
],
"@typescript-eslint/consistent-type-assertions": "error",
"@typescript-eslint/dot-notation": "error",
"@typescript-eslint/indent": [
"error",
2,
{
"ObjectExpression": "first",
"FunctionDeclaration": {
"parameters": "first"
},
"FunctionExpression": {
"parameters": "first"
},
"SwitchCase": 1
}
],
"@typescript-eslint/naming-convention": "off",
"@typescript-eslint/no-empty-function": [
"error",
{
"allow": [
"constructors"
]
}
],
"@typescript-eslint/no-empty-interface": "error",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-misused-new": "error",
"@typescript-eslint/no-namespace": "error",
"@typescript-eslint/no-parameter-properties": "off",
"@typescript-eslint/no-shadow": [
"error",
{
"hoist": "all"
}
],
"@typescript-eslint/no-this-alias": "error",
"@typescript-eslint/no-unused-expressions": "error",
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/no-var-requires": "error",
"@typescript-eslint/prefer-for-of": "off",
"@typescript-eslint/prefer-function-type": "error",
"@typescript-eslint/prefer-namespace-keyword": "error",
"@typescript-eslint/quotes": "off",
"@typescript-eslint/semi": [
"error",
"always"
],
"@typescript-eslint/triple-slash-reference": [
"error",
{
"path": "always",
"types": "prefer-import",
"lib": "always"
}
],
"@typescript-eslint/unified-signatures": "error",
"arrow-parens": [
"error",
"as-needed"
],
"comma-dangle": "error",
"complexity": "off",
"constructor-super": "error",
"curly": "off",
"default-case": "off",
"dot-notation": "error",
"eqeqeq": [
"error",
"smart"
],
"guard-for-in": "off",
"id-denylist": [
"error",
"any",
"Number",
"number",
"String",
"string",
"Boolean",
"boolean",
"Undefined",
"undefined"
],
"id-match": "error",
"import/no-internal-modules": "off",
"import/order": "off",
"import/no-unresolved": ["error", {"caseSensitiveStrict": true }],
"indent": "off",
"jsdoc/check-alignment": "error",
"jsdoc/check-indentation": "error",
"jsdoc/newline-after-description": "off",
"max-classes-per-file": "off",
"max-len": [
"error",
{
"code": 200
}
],
"new-parens": "error",
"no-bitwise": "off",
"no-caller": "error",
"no-cond-assign": "error",
"no-console": "error",
"no-debugger": "error",
"no-duplicate-case": "error",
"no-duplicate-imports": "error",
"no-empty": "error",
"no-empty-function": "off",
"no-eval": "error",
"no-extra-bind": "error",
"no-fallthrough": "off",
"no-invalid-this": "off",
"no-new-func": "error",
"no-new-wrappers": "error",
"no-redeclare": "error",
"no-return-await": "error",
"no-sequences": "error",
"no-shadow": "off",
"no-sparse-arrays": "error",
"no-template-curly-in-string": "error",
"no-throw-literal": "error",
"no-trailing-spaces": "error",
"no-undef-init": "error",
"no-underscore-dangle": "error",
"no-unsafe-finally": "error",
"no-unused-expressions": "error",
"no-unused-labels": "error",
"no-use-before-define": "off",
"no-var": "error",
"object-shorthand": "error",
"one-var": [
"error",
"never"
],
"prefer-const": "error",
"prefer-object-spread": "error",
"quote-props": [
"error",
"as-needed"
],
"quotes": "off",
"radix": "error",
"semi": "error",
"space-in-parens": [
"error",
"never"
],
"spaced-comment": [
"error",
"always",
{
"markers": [
"/"
]
}
],
"unicorn/prefer-ternary": "error",
"use-isnan": "error",
"valid-typeof": "off"
},
"settings": {
"import/parsers": {
"@typescript-eslint/parser": [
".ts",
".tsx"
]
},
"import/resolver": {
"typescript": {
"alwaysTryTypes": true,
"project": [
"lib/tsconfig.json",
"test/tsconfig.json"
]
}
}
}
}
37 changes: 37 additions & 0 deletions .github/workflows/nodejs-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Node.js CI
on: push

jobs:

build:
name: "Build module"
runs-on: ubuntu-latest

steps:

- uses: actions/checkout@v2

- uses: actions/setup-node@v1
with:
node-version: 16.x

- name: Install production dependencies, check node engine compatibility
run: yarn install --production=true

- name: Install development dependencies
run: yarn install --production=false --ignore-engines

- name: Build & Code analysis
run: yarn run lint

- name: Build
run: yarn run build

- name: Upload build
uses: actions/upload-artifact@v2
with:
name: build
path: |
src/**/*.js
src/**/*.js.map
src/**/*.d.ts
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# IntelliJ IDEA project files
.idea
*.iml

# NPM
node_modules

# Yarn
yarn-error.log
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
[![Node.js CI](https://github.com/Borewit/bounded-queue/actions/workflows/nodejs-ci.yml/badge.svg)](https://github.com/Borewit/bounded-queue/actions/workflows/nodejs-ci.yml)

# bounded-queue

`bounded-queue` helps you to solve the [Producer–consumer problem](https://en.wikipedia.org/wiki/Producer%E2%80%93consumer_problem).

## Introduction

Imagine you have read records from a database and write those to another database.
A simple wait to that. is with the following code:

```js
import {queue} from 'bounded-queue';

async function convertDatabaseRecords() {

while(dbA.moreRecorrdsAviable) {
const record = await dbA.readRecord();
// Consumer
await dbB.writeRecord(record); // expenive async write (consume) operation
}
}
```
In the previous example, we either read from database A, or write to database B. Would be faster if read from database A, while we write to database B at the same time.
As `dbA.readRecord()` and dbB.readRecord()` are `async` functions, there is no need to introduce threading to accomplish that.

The `bounded-queue` helps you with that. The following example used a `bounded-queue` with a maximum of 3 queued records.:

```js
import {queue} from 'bounded-queue';

async function convertDatabaseRecords() {

await queue(3, () => {
// Producer
return readItemFromOneDatabase(); // expenive async read (produce) operation
}, record => {
// Consumer
writeRecordToAnotherDataBase(record); // expenive async write (consume) operation
});
}
```

## Installation

```shell
npm install bounded-queue
```
65 changes: 65 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"name": "bounded-queue",
"version": "0.1.0",
"description": "Bounded batch queue, where items are produced and consumed based on user specified functions",
"main": "index.js",
"type": "module",
"license": "MIT",
"private": false,
"engines": {
"node": ">= 14.0.0",
"npm": ">= 6.0.0"
},
"exports": {
".": "./src/batch-queue.js"
},
"types": "src/batch-queue.d.ts",
"files": [
"src/**/*.js",
"src/**/*.d.ts"
],
"homepage": "https://github.com/Borewit/bounded-queue",
"repository": {
"type": "git",
"url": "https://github.com/Borewit/bounded-queue.git"
},
"funding": {
"type": "github",
"url": "https://github.com/sponsors/Borewit"
},
"bugs": "https://github.com/Borewit/bounded-queue/issues",
"author": {
"name": "Borewit",
"url": "https://github.com/Borewit"
},
"keywords": [
"queue",
"bounded",
"buffer",
"producer",
"consumer",
"promise",
"async"
],
"contributors": [],
"scripts": {
"clean": "del-cli src/**/*.js src/**/*.js.map src/**/*.d.ts",
"compile-src": "tsc -p src",
"build": "npm run clean && npm run compile-src",
"lint": "eslint '**/*.ts'"
},
"devDependencies": {
"@types/node": "^20.8.10",
"@typescript-eslint/eslint-plugin": "^6.9.1",
"@typescript-eslint/parser": "^6.9.1",
"del-cli": "^5.1.0",
"eslint": "^8.52.0",
"eslint-config-prettier": "^9.0.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.29.0",
"eslint-plugin-jsdoc": "^46.8.2",
"eslint-plugin-unicorn": "^49.0.0",
"npm-run-all": "^4.1.5",
"typescript": "^5.2.2"
}
}
Loading

0 comments on commit 84ba8e2

Please sign in to comment.