Skip to content

Instantly share code, notes, and snippets.

@glendaviesnz
Forked from ghetolay/ngrx_actions.ts
Created February 28, 2017 02:00
Show Gist options
  • Select an option

  • Save glendaviesnz/3a2f5d22454ee0de469008ba4b02734b to your computer and use it in GitHub Desktop.

Select an option

Save glendaviesnz/3a2f5d22454ee0de469008ba4b02734b to your computer and use it in GitHub Desktop.

Revisions

  1. @ghetolay ghetolay revised this gist Feb 27, 2017. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions ngrx_actions.ts
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    interface Action {
    type: string;
    interface Action<T> {
    type: T;
    }

    interface PayloadAction<T, R> {
    @@ -8,7 +8,7 @@ interface PayloadAction<T, R> {
    }

    interface ActionFactory<T> {
    (): Action;
    (): Action<T>;
    type: T;
    }

    @@ -27,7 +27,7 @@ function newActionFactory<T extends string, R>(type: T|'', payloadFunc?: (payloa
    type: <T>type
    };
    } :
    <ActionFactory<T>> function(): Action {
    <ActionFactory<T>> function(): Action<T> {
    return { type: <T>type };
    }
    ;
  2. @ghetolay ghetolay revised this gist Feb 27, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ngrx_actions.ts
    Original file line number Diff line number Diff line change
    @@ -47,6 +47,6 @@ const Action2 = newActionFactory('ACTION2', (p: number) => p);
    * This should be replaced with ts 2.3 with something like :
    * `type Actions = returnType Action1 | returnType Action2`
    */
    type Actions = Action<'ACTION1> | PayloadAction<'ACTION2', number>;
    type Actions = Action<'ACTION1'> | PayloadAction<'ACTION2', number>;


  3. @ghetolay ghetolay revised this gist Feb 27, 2017. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion ngrx_actions.ts
    Original file line number Diff line number Diff line change
    @@ -47,5 +47,6 @@ const Action2 = newActionFactory('ACTION2', (p: number) => p);
    * This should be replaced with ts 2.3 with something like :
    * `type Actions = returnType Action1 | returnType Action2`
    */
    types Actions = Action<'ACTION1> | PayloadAction<'ACTION2', number>;
    type Actions = Action<'ACTION1> | PayloadAction<'ACTION2', number>;


  4. @ghetolay ghetolay created this gist Feb 27, 2017.
    51 changes: 51 additions & 0 deletions ngrx_actions.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    interface Action {
    type: string;
    }

    interface PayloadAction<T, R> {
    readonly type: T;
    payload: R;
    }

    interface ActionFactory<T> {
    (): Action;
    type: T;
    }

    interface PayloadActionFactory<T, R> {
    (payload: R): PayloadAction<T, R>;
    type: T;
    }

    function newActionFactory<T extends string>(type: T|''): ActionFactory<T>;
    function newActionFactory<T extends string, R>(type: T|'', payloadFunc: (payload: R) => R): PayloadActionFactory<T, R>;
    function newActionFactory<T extends string, R>(type: T|'', payloadFunc?: (payload: R) => R): ActionFactory<T> | PayloadActionFactory<T, R> {
    const actionBuilder = payloadFunc ?
    <PayloadActionFactory<T, R>> function(payload: R): PayloadAction<T, R> {
    return {
    payload: payloadFunc(payload),
    type: <T>type
    };
    } :
    <ActionFactory<T>> function(): Action {
    return { type: <T>type };
    }
    ;

    actionBuilder.type = <T>type;

    return actionBuilder;
    }

    //usage

    const Action1 = newActionFactory('ACTION1');
    /* Here we have to use a function mostly for typings but also allow user to define some logic to build the payload */
    const Action2 = newActionFactory('ACTION2', (p: number) => p);

    /* Here we need to do some boilerplate by repeting the structure or our actions
    * This should be replaced with ts 2.3 with something like :
    * `type Actions = returnType Action1 | returnType Action2`
    */
    types Actions = Action<'ACTION1> | PayloadAction<'ACTION2', number>;