File tree Expand file tree Collapse file tree 3 files changed +73
-0
lines changed
Expand file tree Collapse file tree 3 files changed +73
-0
lines changed Original file line number Diff line number Diff line change @@ -266,6 +266,7 @@ You can call one of the functions of that object:
266266| ` cmd .ofEither ` | Calls a function (sync or async) and maps the result into a message. |
267267| ` cmd .ofSuccess ` | Same as ` ofEither ` but ignores the error case. |
268268| ` cmd .ofError ` | Same as ` ofEither ` but ignores the success case. |
269+ | ` cmd .ofNone ` | Same as ` ofEither ` but ignores both, the success case and the error case. |
269270| ` cmd .ofSub ` | Use this function to trigger a command in a subscription. |
270271
271272### Dispatch a message
Original file line number Diff line number Diff line change @@ -283,4 +283,52 @@ describe("cmd", () => {
283283 expect ( message . name ) . toBe ( "error" ) ;
284284 } ) ;
285285 } ) ;
286+
287+ describe ( "ofNone" , ( ) => {
288+ it ( "runs a sync task" , ( ) => {
289+ // arrange
290+ const task = jest . fn ( ) ;
291+ const result = cmd . ofNone ( task ) ;
292+
293+ // act
294+ result [ 0 ] ?.( ( ) => null ) ;
295+
296+ // assert
297+ expect ( task ) . toHaveBeenCalledTimes ( 1 ) ;
298+ } ) ;
299+
300+ it ( "runs an async task" , ( ) => {
301+ // arrange
302+ const task = jest . fn ( asyncResolve ) ;
303+ const result = cmd . ofNone ( task ) ;
304+
305+ // act
306+ result [ 0 ] ?.( ( ) => null ) ;
307+
308+ // assert
309+ expect ( task ) . toHaveBeenCalledTimes ( 1 ) ;
310+ } ) ;
311+
312+ it ( "ignores the error for a sync task" , async ( ) => {
313+ // arrange
314+ const result = cmd . ofNone ( syncError ) ;
315+
316+ // act
317+ const succeeds = ( ) : void => result [ 0 ] ?.( jest . fn ( ) ) ;
318+
319+ // assert
320+ expect ( succeeds ) . not . toThrow ( ) ;
321+ } ) ;
322+
323+ it ( "ignores the error for an async task" , async ( ) => {
324+ // arrange
325+ const result = cmd . ofNone ( asyncReject ) ;
326+
327+ // act
328+ const succeeds = ( ) : void => result [ 0 ] ?.( jest . fn ( ) ) ;
329+
330+ // assert
331+ expect ( succeeds ) . not . toThrow ( ) ;
332+ } ) ;
333+ } ) ;
286334} ) ;
Original file line number Diff line number Diff line change @@ -108,6 +108,30 @@ const cmd = {
108108
109109 return [ bind ] ;
110110 } ,
111+
112+ /**
113+ * Creates a command out of a function and ignores both, the success case and the error case. This can also be an async function.
114+ * @param task The function to call.
115+ * @param args The parameters of the task.
116+ */
117+ ofNone < TErrorMessage extends Message , TArgs extends unknown [ ] > (
118+ task : ( ...args : TArgs ) => unknown ,
119+ ...args : TArgs
120+ ) : Cmd < TErrorMessage > {
121+ const bind = ( _dispatch : Dispatch < TErrorMessage > , fallback ?: FallbackHandler ) : void => {
122+ try {
123+ const taskResult = task ( ...args ) ;
124+
125+ Promise . resolve ( taskResult )
126+ . then ( ( ) => fallback ?.( ) )
127+ . catch ( ( ) => fallback ?.( ) ) ;
128+ } catch {
129+ fallback ?.( ) ;
130+ }
131+ } ;
132+
133+ return [ bind ] ;
134+ } ,
111135} ;
112136
113137function defaultFallbackHandler ( ) : void {
You can’t perform that action at this time.
0 commit comments