-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
13 changed files
with
2,786 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
nodeLinker: node-modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "esnext", | ||
"outDir": "dist", | ||
"declaration": true, | ||
"declarationDir": "./types" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.