Skip to content
This repository has been archived by the owner on Sep 25, 2020. It is now read-only.

Latest commit

 

History

History
140 lines (105 loc) · 4.26 KB

redux-action.md

File metadata and controls

140 lines (105 loc) · 4.26 KB

@ReduxAction

Marks a class method as redux action dispatcher. Every time the decorated method is called, it will dispatch a new redux action. The return value of the decorated method is used as payload.

@ReduxAction({
  type?: string;
  contextClass?: {},
  onDispatchSuccess?: Function;
})

Configuration Options

type

Overwrites the type of the redux action.

default value The name of the decorated method
type string
mandatory no
class SomeClass {                           //  This will dispatch the following action:
                                            //
  @ReduxAction({type: 'some-action-type'})  //  {
  public someMethod(): string {             //    "type": "some-action-type",
    return 'some-return-value';             //    "payload": "some-return-value"
  }                                         //  }

}

contextClass

Defines the context class which is used to resolve the @ReduxActionContext.

default value this class
type class
mandatory no
@ReduxActionContext({prefix: 'some-prefix/')
class SomeContextClass {

}

class SomeClass {                                 //  This will dispatch the following action:
                                                  //
  @ReduxAction({contextClass: SomeContextClass})  //  {
  public someMethod(): string {                   //    "type": "some-prefix/someMethod",
    return 'some-return-value';                   //    "payload": "some-return-value"
  }                                               //  }

}

The example above will dispatch the following redux action:

{
  "type": "some-prefix/someMethod",
  "payload": "some-return-value"
}

onDispatchSuccess

Defines a callback function which is called as soon as the action is dispatched. The this context of the decorated method is bound to the callback function. That means that you can use this within the callback function.

default value this class
type class
mandatory no
class SomeClass {

  protected someProperty: string;

  @ReduxAction({
    onDispatchSuccess: SomeClass.prototype.someMethodDispatchSuccess
  })
  public someMethod(): string {
    this.someProperty = '123';
    return 'some-return-value';
  }

  public someMethodDispatchSuccess() {
    console.log(this.someProperty); // will log 123
  }

}

Special return values

Promise

If the method returns a Promise, the redux action will not be fired until the Promise is resolved successfully.

class SomeClass {                                 //  This will dispatch the following action:
                                                  //
  @ReduxAction()                                  //  {
  public someMethod(): Promise<string> {          //    "type": "someMethod",
    return Promise.resolve('some-return-value');  //    "payload": "some-return-value"
  }                                               //  }

}

Observable

If the method returns a Observable, the redux action will not be fired until the Observable is completed. The last value in the event stream is used as the payload.

class SomeClass {                                   //  This will dispatch the following action:
                                                    //
  @ReduxAction()                                    //  {
  public someMethod(): Observable<string> {         //    "type": "someMethod",
    return Observable.from([                        //    "payload": "some-return-value" 
      'not-used-as-payload',                        //  }
      'some-return-value'
    ]);
  }

}