-
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.
- Loading branch information
0 parents
commit 3185a91
Showing
13 changed files
with
8,580 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,2 @@ | ||
dist | ||
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,22 @@ | ||
module.exports = { | ||
"root": true, | ||
"extends": [ | ||
"plugin:@typescript-eslint/eslint-recommended", | ||
"plugin:@typescript-eslint/recommended" | ||
], | ||
"parser": "@typescript-eslint/parser", | ||
"plugins": ["@typescript-eslint"], | ||
"rules": { | ||
"no-console": "error" | ||
}, | ||
"overrides": [ | ||
{ | ||
"files": ["*.{ts,tsx}"], | ||
"parserOptions": { | ||
"tsconfigRootDir": __dirname, | ||
"project": "./tsconfig.json", | ||
}, | ||
"extends": ["plugin:@typescript-eslint/recommended-requiring-type-checking"], | ||
} | ||
] | ||
}; |
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,2 @@ | ||
node_modules | ||
/dist |
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 @@ | ||
{ | ||
"printWidth": 120, | ||
"semi": true, | ||
"singleQuote": true, | ||
"trailingComma": "es5" | ||
} |
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,9 @@ | ||
The MIT License | ||
|
||
Copyright 2023 Tulip Interfaces, Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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,72 @@ | ||
# Semaphore | ||
|
||
A Semaphore is like a Promise, with a jet-pack. Use it as a signal and to block asynchronous | ||
operations. | ||
|
||
Semaphores are great when you need to know the state of a promise from synchronous code. | ||
|
||
## API | ||
|
||
- `Semaphore.wrap(p: Promise<T>): Semaphore<T>` - wraps a promise as a Semaphore. | ||
- `.isReady(): boolean` - True if the semaphore was resolved. | ||
- `.state: State` - One of 'PENDING' | 'READY' | 'FAILED' | ||
- ... all the Promise methods `.then(...)`, `.catch(...)`, `.finally(...)` | ||
|
||
## Example 1 | ||
|
||
```typescript | ||
const data = new Semaphore<Data>(); | ||
fetchData().then(data.resolve, data.reject); | ||
|
||
function renderData() { | ||
switch (data.state) { | ||
case State.PENDING: | ||
return "Loading..."; | ||
case State.FAILED: | ||
return "Failed to fetch data"; | ||
case State.READY: | ||
return `Data: ${JSON.stringify(data)}`; | ||
} | ||
} | ||
setInterval(renderData, 1000); | ||
``` | ||
|
||
## Example 2 | ||
|
||
Say you have a class that needs to do some initialization with an asynchronous function. | ||
|
||
```typescript | ||
async doSomeAsyncSetup() { | ||
// .... | ||
} | ||
|
||
class Foo { | ||
|
||
private ready: Semaphore<void>; | ||
|
||
constructor() { | ||
this.ready = Semaphore.wrap(doSomeAsyncSetup()); | ||
} | ||
|
||
canFrobitz() { | ||
return ready.isReady(); | ||
} | ||
|
||
async someMethod() { | ||
await this.ready; | ||
|
||
// Ready to go... | ||
} | ||
} | ||
|
||
|
||
// ... elsewhere ... | ||
|
||
const foo = new Foo(); | ||
|
||
setTimeout(() => { | ||
if (!foo.canFrobitz()) { | ||
console.error("Canna do it!"); | ||
} | ||
}, timeoutTime); | ||
``` |
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 @@ | ||
{ | ||
"transform": { | ||
"^.+\\.(t|j)sx?$": "ts-jest" | ||
}, | ||
"testRegex": "\\.jest\\.tsx?$", | ||
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"] | ||
} |
Oops, something went wrong.