Skip to content

Commit

Permalink
feat: promises delivery
Browse files Browse the repository at this point in the history
doublelam committed Dec 13, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent a19365b commit 733de64
Showing 13 changed files with 2,786 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true

[*.{js,json,yml}]
charset = utf-8
indent_style = space
indent_size = 2
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.yarn/** linguist-vendored
/.yarn/releases/* binary
/.yarn/plugins/**/* binary
/.pnp.* binary linguist-generated
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions

# Swap the comments on the following lines if you wish to use zero-installs
# In that case, don't forget to run `yarn config set enableGlobalCache false`!
# Documentation here: https://yarnpkg.com/features/caching#zero-installs

#!.yarn/cache
.pnp.*
node_modules
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"eslint.debug": true,
"eslint.format.enable": true,
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
}
1 change: 1 addition & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodeLinker: node-modules
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# promises-delivery
```ts

const delivery = new Delivery<string>();

const start = async () => {

[1,2,3,4,5,6,7,8,9,10].forEach(async v => {
const val = await delivery.register(`key-${v}`);
console.log('------',`key-${v}`, val);
})
}

start();

[1,2,3,4,5,6,7,8,9,10].forEach(v => {
setTimeout(() => {
delivery.resolve(`key-${v}`, `Key: key-${v} resolved`)
}, 1000 * v)
});
```
34 changes: 34 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Delivery {
promises = {};
register(key) {
if (this.promises[key]) {
throw new Error(`Promise with Key: ${key} is already registered`);
}
const state = {
promise: null,
resolve: null,
reject: null,
};
state.promise = new Promise((resolve, reject) => {
state.resolve = resolve;
state.reject = reject;
});
this.promises[key] = state;
return this.promises[key].promise;
}
resolve(key, value) {
if (!this.promises[key]) {
throw new Error(`Promise with Key: ${key} is not found`);
}
this.promises[key].resolve(value);
delete this.promises[key];
}
reject(key, reason) {
if (!this.promises[key]) {
throw new Error(`Promise with Key: ${key} is not found`);
}
this.promises[key].reject(reason);
delete this.promises[key];
}
}
export default Delivery;
57 changes: 57 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* eslint-disable @typescript-eslint/no-require-imports */
const { FlatCompat } = require('@eslint/eslintrc');
const typeScriptEsLintPlugin = require('@typescript-eslint/eslint-plugin');

const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: typeScriptEsLintPlugin.configs['recommended'],
});

module.exports = [
...compat.config({
env: { node: true },
extends: [
'plugin:@typescript-eslint/recommended',
'prettier',
'plugin:prettier/recommended',
],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaFeatures: {
jsx: true,
modules: true,
},
},
plugins: ['@typescript-eslint'],
}),
{
rules: {
'require-jsdoc': 'off',
'no-invalid-this': 'off',
// "import/no-unresolved": 0,
'prettier/prettier': [
'error',
{
singleQuote: true,
trailingComma: 'all',
arrowParens: 'avoid',
endOfLine: 'auto',
},
],
'max-len': [
2,
{
code: 140,
ignoreStrings: true,
ignoreUrls: true,
},
],
'no-shadow': 'off',
'global-require': 0,
'no-console': 1,
'@typescript-eslint/no-shadow': 'error',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/no-explicit-any': 1,
},
},
];
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "promises-delivery",
"version": "1.0.0",
"main": "./dist/index.js",
"type": "module",
"types": "./types/index.d.ts",
"source": "./src/index.ts",
"packageManager": "[email protected]",
"devDependencies": {
"@eslint/compat": "1.1.1",
"@eslint/eslintrc": "3.2.0",
"@typescript-eslint/eslint-plugin": "8.3.0",
"@typescript-eslint/parser": "8.3.0",
"eslint": "9.9.1",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-jsx-a11y": "6.9.0",
"eslint-plugin-prettier": "5.2.1",
"prettier": "3.3.3",
"typescript": "5.5.4"
}
}
54 changes: 54 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
class Delivery<T> {
private promises: {
[key: string]: {
promise: Promise<T>;
resolve: (value: T) => void;
reject: (reason: string) => void;
};
} = {};
public register(key: string) {
if (this.promises[key]) {
throw new Error(`Promise with Key: ${key} is already registered`);
}
const state: {
promise: Promise<T> | null;
resolve: ((value: T) => void) | null;
reject: ((reason: string) => void) | null;
} = {
promise: null,
resolve: null,
reject: null,
};

state.promise = new Promise((resolve, reject) => {
state.resolve = resolve;
state.reject = reject;
});

this.promises[key] = state as {
promise: Promise<T>;
resolve: (value: T) => void;
reject: (reason: string) => void;
};

return this.promises[key].promise;
}

public resolve(key: string, value: T) {
if (!this.promises[key]) {
throw new Error(`Promise with Key: ${key} is not found`);
}
this.promises[key].resolve(value);
delete this.promises[key];
}

public reject(key: string, reason: string) {
if (!this.promises[key]) {
throw new Error(`Promise with Key: ${key} is not found`);
}
this.promises[key].reject(reason);
delete this.promises[key];
}
}

export default Delivery;
8 changes: 8 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"target": "esnext",
"outDir": "dist",
"declaration": true,
"declarationDir": "./types"
}
}
7 changes: 7 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
declare class Delivery<T> {
private promises;
register(key: string): Promise<T>;
resolve(key: string, value: T): void;
reject(key: string, reason: string): void;
}
export default Delivery;
Loading

0 comments on commit 733de64

Please sign in to comment.