next-resolver 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- next_resolver-0.0.1/LICENSE +21 -0
- next_resolver-0.0.1/PKG-INFO +74 -0
- next_resolver-0.0.1/README.md +54 -0
- next_resolver-0.0.1/next_resolver.egg-info/PKG-INFO +74 -0
- next_resolver-0.0.1/next_resolver.egg-info/SOURCES.txt +8 -0
- next_resolver-0.0.1/next_resolver.egg-info/dependency_links.txt +1 -0
- next_resolver-0.0.1/next_resolver.egg-info/top_level.txt +1 -0
- next_resolver-0.0.1/pyproject.toml +29 -0
- next_resolver-0.0.1/python/__init__.py +105 -0
- next_resolver-0.0.1/setup.cfg +4 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright © 2025-today, Andrea Giammarchi, @WebReflection
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the “Software”), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software
|
|
10
|
+
is furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included
|
|
13
|
+
in all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
21
|
+
IN THE SOFTWARE.
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: next-resolver
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A utility to simpify the repeated unique ID + promise dance (asyncio).
|
|
5
|
+
Author: Andrea Giammarchi
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/WebReflection/next-resolver#readme
|
|
8
|
+
Project-URL: Repository, https://github.com/WebReflection/next-resolver
|
|
9
|
+
Project-URL: Issues, https://github.com/WebReflection/next-resolver/issues
|
|
10
|
+
Keywords: promise,async,asyncio,unique-id,resolver
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
16
|
+
Requires-Python: >=3.8
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Dynamic: license-file
|
|
20
|
+
|
|
21
|
+
# next-resolver
|
|
22
|
+
|
|
23
|
+
[](https://coveralls.io/github/WebReflection/next-resolver?branch=main)
|
|
24
|
+
|
|
25
|
+
<sup>**Social Media Photo by [Jan Genge](https://unsplash.com/@jan_genge) on [Unsplash](https://unsplash.com/)**</sup>
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
A utility to simpify the repeated *unique ID* + *promise* dance.
|
|
29
|
+
|
|
30
|
+
```js
|
|
31
|
+
import nextResolver from 'next-resolver';
|
|
32
|
+
|
|
33
|
+
const [next, resolve] = nextResolver();
|
|
34
|
+
|
|
35
|
+
// next unique identifier and its promise
|
|
36
|
+
const [id, promise] = next();
|
|
37
|
+
|
|
38
|
+
// pass the promise around, hold the id ...
|
|
39
|
+
// ... so that whenever it's done:
|
|
40
|
+
if (condition) {
|
|
41
|
+
resolve(id, value);
|
|
42
|
+
|
|
43
|
+
// or reject via
|
|
44
|
+
resolve(id, null, new Error('reason'));
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
The default `nextResolver()` accepts an optional callback that will receive a unique identifier that can be used to return something else:
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
// use strings instead of numbers as IDs
|
|
52
|
+
const [next, resolve] = nextResolver(String);
|
|
53
|
+
|
|
54
|
+
const [id, promise] = next();
|
|
55
|
+
typeof id; // string
|
|
56
|
+
|
|
57
|
+
// use any Map key variant
|
|
58
|
+
const [next, resolve] = nextResolver(id => {
|
|
59
|
+
// make it stronger (not really useful)
|
|
60
|
+
return `${id}-${crypto.randomUUID()}`;
|
|
61
|
+
// make it a unique ref
|
|
62
|
+
return { id };
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Please note that if the returned value is still awaited, the next `id` will be passed to create a new identifier (so don't put too much logic within the `id` creation, it's already granted to be unique per that logic/session).
|
|
67
|
+
|
|
68
|
+
This is mostly needed in case you want to brand your async operations, so that something like a prefix would be enough:
|
|
69
|
+
|
|
70
|
+
```js
|
|
71
|
+
const [next, resolve] = nextResolver(id => `my-logic-${id}`);
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
And that's all folks 🥕
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# next-resolver
|
|
2
|
+
|
|
3
|
+
[](https://coveralls.io/github/WebReflection/next-resolver?branch=main)
|
|
4
|
+
|
|
5
|
+
<sup>**Social Media Photo by [Jan Genge](https://unsplash.com/@jan_genge) on [Unsplash](https://unsplash.com/)**</sup>
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
A utility to simpify the repeated *unique ID* + *promise* dance.
|
|
9
|
+
|
|
10
|
+
```js
|
|
11
|
+
import nextResolver from 'next-resolver';
|
|
12
|
+
|
|
13
|
+
const [next, resolve] = nextResolver();
|
|
14
|
+
|
|
15
|
+
// next unique identifier and its promise
|
|
16
|
+
const [id, promise] = next();
|
|
17
|
+
|
|
18
|
+
// pass the promise around, hold the id ...
|
|
19
|
+
// ... so that whenever it's done:
|
|
20
|
+
if (condition) {
|
|
21
|
+
resolve(id, value);
|
|
22
|
+
|
|
23
|
+
// or reject via
|
|
24
|
+
resolve(id, null, new Error('reason'));
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
The default `nextResolver()` accepts an optional callback that will receive a unique identifier that can be used to return something else:
|
|
29
|
+
|
|
30
|
+
```js
|
|
31
|
+
// use strings instead of numbers as IDs
|
|
32
|
+
const [next, resolve] = nextResolver(String);
|
|
33
|
+
|
|
34
|
+
const [id, promise] = next();
|
|
35
|
+
typeof id; // string
|
|
36
|
+
|
|
37
|
+
// use any Map key variant
|
|
38
|
+
const [next, resolve] = nextResolver(id => {
|
|
39
|
+
// make it stronger (not really useful)
|
|
40
|
+
return `${id}-${crypto.randomUUID()}`;
|
|
41
|
+
// make it a unique ref
|
|
42
|
+
return { id };
|
|
43
|
+
});
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Please note that if the returned value is still awaited, the next `id` will be passed to create a new identifier (so don't put too much logic within the `id` creation, it's already granted to be unique per that logic/session).
|
|
47
|
+
|
|
48
|
+
This is mostly needed in case you want to brand your async operations, so that something like a prefix would be enough:
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
const [next, resolve] = nextResolver(id => `my-logic-${id}`);
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
And that's all folks 🥕
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: next-resolver
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A utility to simpify the repeated unique ID + promise dance (asyncio).
|
|
5
|
+
Author: Andrea Giammarchi
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/WebReflection/next-resolver#readme
|
|
8
|
+
Project-URL: Repository, https://github.com/WebReflection/next-resolver
|
|
9
|
+
Project-URL: Issues, https://github.com/WebReflection/next-resolver/issues
|
|
10
|
+
Keywords: promise,async,asyncio,unique-id,resolver
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
16
|
+
Requires-Python: >=3.8
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Dynamic: license-file
|
|
20
|
+
|
|
21
|
+
# next-resolver
|
|
22
|
+
|
|
23
|
+
[](https://coveralls.io/github/WebReflection/next-resolver?branch=main)
|
|
24
|
+
|
|
25
|
+
<sup>**Social Media Photo by [Jan Genge](https://unsplash.com/@jan_genge) on [Unsplash](https://unsplash.com/)**</sup>
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
A utility to simpify the repeated *unique ID* + *promise* dance.
|
|
29
|
+
|
|
30
|
+
```js
|
|
31
|
+
import nextResolver from 'next-resolver';
|
|
32
|
+
|
|
33
|
+
const [next, resolve] = nextResolver();
|
|
34
|
+
|
|
35
|
+
// next unique identifier and its promise
|
|
36
|
+
const [id, promise] = next();
|
|
37
|
+
|
|
38
|
+
// pass the promise around, hold the id ...
|
|
39
|
+
// ... so that whenever it's done:
|
|
40
|
+
if (condition) {
|
|
41
|
+
resolve(id, value);
|
|
42
|
+
|
|
43
|
+
// or reject via
|
|
44
|
+
resolve(id, null, new Error('reason'));
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
The default `nextResolver()` accepts an optional callback that will receive a unique identifier that can be used to return something else:
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
// use strings instead of numbers as IDs
|
|
52
|
+
const [next, resolve] = nextResolver(String);
|
|
53
|
+
|
|
54
|
+
const [id, promise] = next();
|
|
55
|
+
typeof id; // string
|
|
56
|
+
|
|
57
|
+
// use any Map key variant
|
|
58
|
+
const [next, resolve] = nextResolver(id => {
|
|
59
|
+
// make it stronger (not really useful)
|
|
60
|
+
return `${id}-${crypto.randomUUID()}`;
|
|
61
|
+
// make it a unique ref
|
|
62
|
+
return { id };
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Please note that if the returned value is still awaited, the next `id` will be passed to create a new identifier (so don't put too much logic within the `id` creation, it's already granted to be unique per that logic/session).
|
|
67
|
+
|
|
68
|
+
This is mostly needed in case you want to brand your async operations, so that something like a prefix would be enough:
|
|
69
|
+
|
|
70
|
+
```js
|
|
71
|
+
const [next, resolve] = nextResolver(id => `my-logic-${id}`);
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
And that's all folks 🥕
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
next_resolver
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "next-resolver"
|
|
7
|
+
version = "0.0.1"
|
|
8
|
+
description = "A utility to simpify the repeated unique ID + promise dance (asyncio)."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { text = "MIT" }
|
|
11
|
+
requires-python = ">=3.8"
|
|
12
|
+
authors = [{ name = "Andrea Giammarchi" }]
|
|
13
|
+
keywords = ["promise", "async", "asyncio", "unique-id", "resolver"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 4 - Beta",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
[project.urls]
|
|
23
|
+
Homepage = "https://github.com/WebReflection/next-resolver#readme"
|
|
24
|
+
Repository = "https://github.com/WebReflection/next-resolver"
|
|
25
|
+
Issues = "https://github.com/WebReflection/next-resolver/issues"
|
|
26
|
+
|
|
27
|
+
[tool.setuptools]
|
|
28
|
+
package-dir = { "next_resolver" = "python" }
|
|
29
|
+
packages = ["next_resolver"]
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
|
|
3
|
+
__all__ = ["Promise", "next_resolver"]
|
|
4
|
+
|
|
5
|
+
def resolver(value, wait=True):
|
|
6
|
+
if asyncio.iscoroutine(value):
|
|
7
|
+
task = asyncio.create_task(value)
|
|
8
|
+
if wait:
|
|
9
|
+
async def wait(resolve, reject):
|
|
10
|
+
try:
|
|
11
|
+
resolve(await task)
|
|
12
|
+
except Exception as e:
|
|
13
|
+
reject(e)
|
|
14
|
+
|
|
15
|
+
value = Promise(wait)
|
|
16
|
+
|
|
17
|
+
return value
|
|
18
|
+
|
|
19
|
+
class PromiseWithResolvers:
|
|
20
|
+
def __init__(self):
|
|
21
|
+
self.resolve = None
|
|
22
|
+
self.reject = None
|
|
23
|
+
self.promise = None
|
|
24
|
+
|
|
25
|
+
class Promise(asyncio.Future):
|
|
26
|
+
|
|
27
|
+
async def value(promise):
|
|
28
|
+
while isinstance(promise, Promise):
|
|
29
|
+
promise = await promise
|
|
30
|
+
|
|
31
|
+
return promise
|
|
32
|
+
|
|
33
|
+
def with_resolvers():
|
|
34
|
+
ref = PromiseWithResolvers()
|
|
35
|
+
|
|
36
|
+
def fn(resolve, reject):
|
|
37
|
+
ref.resolve = resolve
|
|
38
|
+
ref.reject = reject
|
|
39
|
+
|
|
40
|
+
ref.promise = Promise(fn)
|
|
41
|
+
return ref
|
|
42
|
+
|
|
43
|
+
def __init__(self, fn):
|
|
44
|
+
super().__init__()
|
|
45
|
+
this = self
|
|
46
|
+
|
|
47
|
+
def resolve(value):
|
|
48
|
+
this.set_result(value)
|
|
49
|
+
|
|
50
|
+
def reject(reason):
|
|
51
|
+
this.set_exception(reason)
|
|
52
|
+
|
|
53
|
+
resolver(fn(resolve, reject), wait=False)
|
|
54
|
+
|
|
55
|
+
def then(self, resolve, reject=None):
|
|
56
|
+
p = Promise.with_resolvers()
|
|
57
|
+
|
|
58
|
+
def done(future):
|
|
59
|
+
try:
|
|
60
|
+
result = future.result()
|
|
61
|
+
if resolve:
|
|
62
|
+
p.resolve(resolver(resolve(result)))
|
|
63
|
+
|
|
64
|
+
except Exception as e:
|
|
65
|
+
if reject:
|
|
66
|
+
p.reject(resolver(reject(e)))
|
|
67
|
+
|
|
68
|
+
else:
|
|
69
|
+
raise e
|
|
70
|
+
|
|
71
|
+
self.add_done_callback(done)
|
|
72
|
+
|
|
73
|
+
return p.promise
|
|
74
|
+
|
|
75
|
+
def catch(self, on_rejected):
|
|
76
|
+
return self.then(None, on_rejected)
|
|
77
|
+
|
|
78
|
+
def next_resolver(typed = int):
|
|
79
|
+
map = {}
|
|
80
|
+
id = 0
|
|
81
|
+
|
|
82
|
+
def next():
|
|
83
|
+
nonlocal id
|
|
84
|
+
|
|
85
|
+
while True:
|
|
86
|
+
uid = typed(id)
|
|
87
|
+
id += 1
|
|
88
|
+
if not uid in map:
|
|
89
|
+
break
|
|
90
|
+
|
|
91
|
+
wr = Promise.with_resolvers()
|
|
92
|
+
map[uid] = wr
|
|
93
|
+
return [uid, wr.promise]
|
|
94
|
+
|
|
95
|
+
def resolver(uid, value, error=None):
|
|
96
|
+
if uid in map:
|
|
97
|
+
wr = map[uid]
|
|
98
|
+
del map[uid]
|
|
99
|
+
|
|
100
|
+
if error:
|
|
101
|
+
wr.reject(error)
|
|
102
|
+
else:
|
|
103
|
+
wr.resolve(value)
|
|
104
|
+
|
|
105
|
+
return [next, resolver]
|