-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: inline
thingies
dependency (#1004)
* fix: inline `thingies` * fix: remove unneeded code * chore: don't apply prettier to `thingies` code * refactor: inline `tick` and `until` functions
- Loading branch information
Showing
16 changed files
with
102 additions
and
15 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 |
---|---|---|
|
@@ -11,3 +11,4 @@ package-lock.json | |
CHANGELOG.md | ||
|
||
src/json-joy | ||
src/thingies |
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 |
---|---|---|
|
@@ -121,7 +121,6 @@ | |
} | ||
}, | ||
"dependencies": { | ||
"thingies": "^1.11.1", | ||
"tslib": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
|
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
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
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
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
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
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
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
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,20 @@ | ||
/** | ||
* An externally resolvable/rejectable "promise". Use it to resolve/reject | ||
* promise at any time. | ||
* | ||
* ```ts | ||
* const future = new Defer(); | ||
* | ||
* future.promise.then(value => console.log(value)); | ||
* | ||
* future.resolve(123); | ||
* ``` | ||
*/ | ||
export class Defer<T> { | ||
public readonly resolve!: (data: T) => void; | ||
public readonly reject!: (error: any) => void; | ||
public readonly promise: Promise<T> = new Promise<T>((resolve, reject) => { | ||
(this as any).resolve = resolve; | ||
(this as any).reject = reject; | ||
}); | ||
} |
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,36 @@ | ||
import {go} from './go'; | ||
import type {Code} from './types'; | ||
|
||
class Task<T = unknown> { | ||
public readonly resolve!: (data: T) => void; | ||
public readonly reject!: (error: any) => void; | ||
public readonly promise = new Promise<T>((resolve, reject) => { | ||
(this as any).resolve = resolve; | ||
(this as any).reject = reject; | ||
}); | ||
constructor(public readonly code: Code<T>) {} | ||
} | ||
|
||
/** Limits concurrency of async code. */ | ||
export const concurrency = (limit: number) => { | ||
let workers = 0; | ||
const queue = new Set<Task>(); | ||
const work = async () => { | ||
const task = queue.values().next().value; | ||
if (task) queue.delete(task); | ||
else return; | ||
workers++; | ||
try { | ||
task.resolve(await task.code()); | ||
} catch (error) { | ||
task.reject(error); | ||
} finally { | ||
workers--, queue.size && go(work); | ||
} | ||
}; | ||
return async <T = unknown>(code: Code<T>): Promise<T> => { | ||
const task = new Task(code); | ||
queue.add(task as Task<unknown>); | ||
return workers < limit && go(work), task.promise; | ||
}; | ||
}; |
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,6 @@ | ||
import type {Code} from './types'; | ||
|
||
/** Executes code concurrently. */ | ||
export const go = <T>(code: Code<T>): void => { | ||
code().catch(() => {}); | ||
}; |
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 @@ | ||
export * from './concurrency'; | ||
export * from './Defer'; | ||
export * from './go'; | ||
export * from './of'; |
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,16 @@ | ||
/** | ||
* Given a promise awaits it and returns a 3-tuple, with the following members: | ||
* | ||
* - First entry is either the resolved value of the promise or `undefined`. | ||
* - Second entry is either the error thrown by promise or `undefined`. | ||
* - Third entry is a boolean, truthy if promise was resolved and falsy if rejected. | ||
* | ||
* @param promise Promise to convert to 3-tuple. | ||
*/ | ||
export const of = async <T, E = unknown>(promise: Promise<T>): Promise<[T | undefined, E | undefined, boolean]> => { | ||
try { | ||
return [await promise, undefined, true]; | ||
} catch (error: unknown) { | ||
return [undefined, error as E, false]; | ||
} | ||
}; |
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 @@ | ||
export type Code<T = unknown> = () => Promise<T>; |
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