Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kubat committed Feb 15, 2023
0 parents commit 3185a91
Show file tree
Hide file tree
Showing 13 changed files with 8,580 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
node_modules
22 changes: 22 additions & 0 deletions .eslintrc.js
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"],
}
]
};
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
/dist
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"printWidth": 120,
"semi": true,
"singleQuote": true,
"trailingComma": "es5"
}
9 changes: 9 additions & 0 deletions LICENSE
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.
72 changes: 72 additions & 0 deletions README.md
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);
```
7 changes: 7 additions & 0 deletions jest.config.json
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"]
}
Loading

0 comments on commit 3185a91

Please sign in to comment.