|
1 | 1 | # AsyncHandler |
| 2 | + |
| 3 | +[](https://www.npmjs.com/package/asynchandler) |
| 4 | + |
| 5 | +This library provides a generic interface which you can use for classes, |
| 6 | +so they can easily be combined using composite patterns. |
| 7 | +An `AsyncHandler` implementation has 3 functions: `canHandle`, `handle`, and `handleSafe`. |
| 8 | + |
| 9 | +`canHandle` is used to determine if a class supports a given input. |
| 10 | +In case it does not, it should throw an error. |
| 11 | +If a `canHandle` call succeeds, the `handle` function can be called to perform the necessary action. |
| 12 | +`handleSafe` is a utility function that combines the above two steps into one. |
| 13 | + |
| 14 | +For example, assume you want to handle an HTTP request, |
| 15 | +and have a separate class for every HTTP method. |
| 16 | +In the `canHandle` call for each of those, |
| 17 | +they would check if the method is the correct one and throw an error if this is not the case. |
| 18 | +In the `handle` call, they could then perform the necessary action for this method, |
| 19 | +knowing that the request has the correct method. |
| 20 | + |
| 21 | +This library comes with several utility composite handlers that can be used to combine such handlers into one. |
| 22 | +These have the same interface, so from the point of view of the caller, |
| 23 | +a single HTTP request class, and a composite handler that combines all the HTTP request classes, |
| 24 | +look and perform the same. |
| 25 | + |
| 26 | +## Composite handlers |
| 27 | + |
| 28 | +Below is an overview of all composite handlers provided in this library. |
| 29 | + |
| 30 | +### AsyncHandler |
| 31 | + |
| 32 | +The main interface. Although in practice this is actually an abstract class. |
| 33 | +This way it can provide a default `canHandle` implementation that always succeeds, |
| 34 | +and a `handleSafe` implementation that calls `canHandle`, and then `handle` if the first succeeds. |
| 35 | + |
| 36 | +### BooleanHandler |
| 37 | + |
| 38 | +Takes as input one or more handlers that return a boolean. |
| 39 | +This handler will return `true` if any of the input handlers can handle the input and return `true`. |
| 40 | + |
| 41 | +### CachedHandler |
| 42 | + |
| 43 | +Caches the result of the source handler in a [WeakMap](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap). |
| 44 | +The input is used as the key to cache with. |
| 45 | +Input field names can also be provided to only use a subset of the input. |
| 46 | + |
| 47 | +### ParallelHandler |
| 48 | + |
| 49 | +Runs the `handleSafe` function of all its source handlers simultaneously. |
| 50 | +The `canHandle` of this handler will always succeed. |
| 51 | + |
| 52 | +### ProcessHandler |
| 53 | + |
| 54 | +A handler made for worker threads. |
| 55 | +It can be used to only run the source handler depending on if it is executed on the primary or a worker thread. |
| 56 | + |
| 57 | +### SequenceHandler |
| 58 | + |
| 59 | +Runs all its handlers sequentially and returns the result of the last one that can handle the input. |
| 60 | +The `canHandle` of this handler will always succeed. |
| 61 | + |
| 62 | +### StaticHandler |
| 63 | + |
| 64 | +A handler that always succeeds and returns the provided value. |
| 65 | + |
| 66 | +### StaticThrowHandler |
| 67 | + |
| 68 | +A handler that always throws the provided error. |
| 69 | +It can be configured to always succeed `canHandle` and only throw when `handle` is called, |
| 70 | +or to throw in both cases. |
| 71 | + |
| 72 | +### UnionHandler |
| 73 | + |
| 74 | +An abstract class that combines the results of all its source handlers in some way. |
| 75 | +How these are combined is determined by the abstract `combine` function. |
| 76 | +It can be configured to ignore handlers that do not support the input, |
| 77 | +and to ignore errors. |
| 78 | + |
| 79 | +#### ArrayUnionHandler |
| 80 | + |
| 81 | +A `UnionHandler` that combines and flattens the results of its source handlers into a single array. |
| 82 | + |
| 83 | +### WaterfallHandler |
| 84 | + |
| 85 | +This handler will call the `handle` function of the first source handler where the `canHandle` call succeeds. |
| 86 | + |
| 87 | +## Utility |
| 88 | + |
| 89 | +Besides the composite handlers, some utility functions and classes are provided. |
| 90 | + |
| 91 | +### findHandler |
| 92 | + |
| 93 | +A function that returns the first handler from a list of handlers where `canHandle` succeeds. |
| 94 | +Throws an error if no such handler can be found. |
| 95 | + |
| 96 | +### filterHandlers |
| 97 | + |
| 98 | +Filters a list of handlers to only return those where `canHandle` succeeds. |
| 99 | +Throws an error if this would be an empty list. |
| 100 | + |
| 101 | +### ErrorFactory |
| 102 | + |
| 103 | +An interface for a factory that can create errors. |
| 104 | +This can be used in combination with a `StaticThrowHandler`. |
| 105 | + |
| 106 | +#### BaseErrorFactory |
| 107 | + |
| 108 | +An `ErrorFactory` that creates a standard Error. |
| 109 | + |
| 110 | +#### ClassErrorFactory |
| 111 | + |
| 112 | +An `ErrorFactory` that reuses the constructor of the input Error. |
0 commit comments