69 lines
2.4 KiB
Markdown
69 lines
2.4 KiB
Markdown
# Utils
|
|
|
|
The utils only use standard JavaScript, so they can be used in Browser and NodeJS.
|
|
|
|
Also they are very small. Uncompressed and unminified only 3kb.
|
|
|
|
The utils have TypeScript definitions and are compatible with ES2015.
|
|
|
|
## Lock
|
|
|
|
Lock is a very simple promise based Locking mechanism for JavaScript.
|
|
|
|
Usage:
|
|
``` typescript
|
|
import { Lock } from "@hibas123/utils";
|
|
|
|
const lock = new Lock();
|
|
|
|
async doThingThatNeedsLocking() {
|
|
let release = await lock.getLock(); // This will wait till lock from others is released
|
|
|
|
// Do stuff that requires lock
|
|
|
|
release(); // Release lock
|
|
}
|
|
```
|
|
|
|
## Observable
|
|
|
|
Very simple and light weight Observable.
|
|
|
|
Usage:
|
|
``` typescript
|
|
import { Observable } from "@hibas123/util";
|
|
|
|
const server = new Observable(); // Get new Observable Server
|
|
|
|
// Server can only send, not subscribe to messages
|
|
// Receiving is only possible via the public API
|
|
const public = server.getPublicApi();
|
|
|
|
const func = (data)=>{
|
|
console.log(data);
|
|
}
|
|
|
|
// func will be callen when a message is available
|
|
public.subscribe(func);
|
|
|
|
server.send("Hello World");
|
|
|
|
// This will unsubscribe the function. Please note, that it can
|
|
// only unsubscribe the exact function, that is used in subscribe
|
|
public.unsubscribe(func);
|
|
|
|
// This now won't call func anymore
|
|
server.send("Hello World2");
|
|
```
|
|
|
|
## License
|
|
|
|
MIT
|
|
|
|
Copyright (c) 2019 Fabian Stamm
|
|
|
|
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. |