@furystack/utils
General utilities, forked from @furystack/utils
Disposable
You can implement disposable resources and use them with a using() or usingAsync() syntax.
Example:
class Resource implements IDisposable {
dispose() {
}
}
using(new Resource(), (resource) => {
})
usingAsync(new Resource(), async (resource) => {
})
ObservableValue and ValueObservers
You can track value changes using with this simple Observable implementation.
Example:
const observableValue = new ObservableValue<number>(0)
const observer = observableValue.subscribe((newValue) => {
console.log('Value changed:', newValue)
})
observableValue.setValue(Math.random())
observer[Symbol.dispose]()
observableValue[Symbol.dispose]()
PathHelper
The class contains small helper methods for path transformation and manipulation.
Retrier
Retrier is a utility that can keep trying an operation until it succeeds, times out or reach a specified retry limit.
const funcToRetry: () => Promise<boolean> = async () => {
const hasSucceeded = false
return hasSucceeded
}
const retrierSuccess = await Retrier.create(funcToRetry)
.setup({
Retries: 3,
RetryIntervalMs: 1,
timeoutMs: 1000,
})
.run()
Trace
Trace is an utility that can be used to track method calls, method returns and errors
const methodTracer: IDisposable = Trace.method({
object: myObjectInstance,
method: myObjectInstance.method,
isAsync: true,
onCalled: (traceData) => {
console.log('Method called:', traceData)
},
onFinished: (traceData) => {
console.log('Method call finished:', traceData)
},
onError: (traceData) => {
console.log('Method throwed an error:', traceData)
},
})
methodTracer[Symbol.dispose]()