Skip to content

Commit

Permalink
feat(stark-core): update logging actions style
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
Due to an improvement on how actions are defined, the enum `StarkLoggingActionsTypes`
became obsolete so it has been removed.

As a result, the following actions have been changed:
- `StarkSetLoggingApplicationId(public applicationId: string)`
  -> `StarkLoggingActions.setLoggingApplicationId({ applicationId: string })`
- `StarkLogMessageAction(public message: StarkLogMessage)`
  -> `StarkLoggingActions.logMessage({ message: StarkLogMessage })`
- `StarkFlushLogMessages(public numberOfMessagesToFlush: number)`
  -> `StarkLoggingActions.flushLogMessages({ numberOfMessagesToFlush: number })`

And aso the previous union type has been replaced:
`StarkLoggingActions` -> `StarkLoggingActions.Types`.

Change in effect:

```typescript
// Before
@effect({ dispatch: false })
public starkLogMessageAction$(): Observable<void> {
    return this.actions$.pipe(
        ofType<StarkLogMessageAction>(StarkLoggingActionsTypes.LOG_MESSAGE),
        map((action: StarkLogMessageAction) => {
            // some logic
        })
    );
}

// After
public starkLogMessageAction$ = createEffect(
    () => this.actions$.pipe(
        ofType(StarkLoggingActions.logMessage),
        map((action) => {
            // some logic
        })
    ),
    { dispatch: false }
);
```

Change in `action` usage:

```typescript
// Before
this.store.dispatch(new StarkLogMessageAction(message));

// After
this.store.dispatch(StarkLoggingActions.logMessage({ message: message }));
```
  • Loading branch information
SuperITMan committed May 3, 2021
1 parent f1803a8 commit 3dd57d2
Show file tree
Hide file tree
Showing 14 changed files with 113 additions and 120 deletions.
1 change: 1 addition & 0 deletions packages/stark-core/src/modules/logging.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./logging/actions";
export * from "./logging/constants";
export * from "./logging/entities";
export * from "./logging/reducers";
export * from "./logging/services";
Expand Down
3 changes: 2 additions & 1 deletion packages/stark-core/src/modules/logging/actions.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./actions/logging.actions";
import * as StarkLoggingActions from "./actions/logging.actions";
export { StarkLoggingActions };
66 changes: 19 additions & 47 deletions packages/stark-core/src/modules/logging/actions/logging.actions.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,33 @@
import { Action } from "@ngrx/store";
import { createAction, props, union } from "@ngrx/store";
import { StarkLogMessage } from "../entities";

/**
* Actions related to {@link StarkLoggingService}
*/
export enum StarkLoggingActionTypes {
SET_LOGGING_APPLICATION_ID = "[StarkLogging] Set Logging Application Id",
LOG_MESSAGE = "[StarkLogging] Log Message",
FLUSH_LOG = "[StarkLogging] Flush Log"
}
import { starkLoggingStoreKey } from "../constants";

/**
* Add the Application Name to the logging object
*
* Parameter:
* - applicationId - The id of the application
*/
export class StarkSetLoggingApplicationId implements Action {
/**
* The type of action
*/
public readonly type: StarkLoggingActionTypes.SET_LOGGING_APPLICATION_ID = StarkLoggingActionTypes.SET_LOGGING_APPLICATION_ID;

/**
* Class constructor
* @param applicationId - The id of the application
*/
public constructor(public applicationId: string) {}
}
export const setLoggingApplicationId = createAction(`[${starkLoggingStoreKey}] Set Logging Application Id`, props<{ applicationId: string }>());

/**
* Store a debug/info/warning/error message
*
* Parameter:
* - message - The message to log
*/
export class StarkLogMessageAction implements Action {
/**
* The type of action
*/
public readonly type: StarkLoggingActionTypes.LOG_MESSAGE = StarkLoggingActionTypes.LOG_MESSAGE;

/**
* Class constructor
* @param message - The message to log
*/
public constructor(public message: StarkLogMessage) {}
}
export const logMessage = createAction(`[${starkLoggingStoreKey}] Log Message`, props<{ message: StarkLogMessage }>());

/**
* Persists the log messages in the redux store to the back-end
*
* Parameter:
* - numberOfMessagesToFlush - The number of messages to flush
*/
export class StarkFlushLogMessages implements Action {
/**
* The type of action
*/
public readonly type: StarkLoggingActionTypes.FLUSH_LOG = StarkLoggingActionTypes.FLUSH_LOG;

/**
* Class constructor
* @param numberOfMessagesToFlush - The number of messages to flush
*/
public constructor(public numberOfMessagesToFlush: number) {}
}
export const flushLogMessages = createAction(`[${starkLoggingStoreKey}] Flush Log`, props<{ numberOfMessagesToFlush: number }>());

export type StarkLoggingActions = StarkSetLoggingApplicationId | StarkLogMessageAction | StarkFlushLogMessages;
/**
* @ignore
*/
const all = union({ setLoggingApplicationId, logMessage, flushLogMessages });
export type Types = typeof all;
1 change: 1 addition & 0 deletions packages/stark-core/src/modules/logging/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./constants/logging-store-key";
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* The store key will allow the application to find the reducer in the store
*/
export const starkLoggingStoreKey = "StarkLogging";
3 changes: 2 additions & 1 deletion packages/stark-core/src/modules/logging/logging.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { ModuleWithProviders, NgModule, Optional, SkipSelf } from "@angular/core";
import { StoreModule } from "@ngrx/store";
import { starkLoggingReducers } from "./reducers";
import { starkLoggingStoreKey } from "./constants";
import { STARK_LOGGING_SERVICE, StarkLoggingServiceImpl } from "./services";

@NgModule({
imports: [StoreModule.forFeature("StarkLogging", starkLoggingReducers)]
imports: [StoreModule.forFeature(starkLoggingStoreKey, starkLoggingReducers)]
})
export class StarkLoggingModule {
/**
Expand Down
4 changes: 2 additions & 2 deletions packages/stark-core/src/modules/logging/reducers.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "./reducers/index";
export * from "./reducers/logging.reducer";
export { selectStarkLogging, starkLoggingReducers, StarkLoggingState } from "./reducers/index";
export { loggingReducer } from "./reducers/logging.reducer";
9 changes: 5 additions & 4 deletions packages/stark-core/src/modules/logging/reducers/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ActionReducerMap, createFeatureSelector, createSelector, MemoizedSelector } from "@ngrx/store";
import { ActionReducerMap, createFeatureSelector, createSelector } from "@ngrx/store";
import { StarkLogging } from "../entities";
import { StarkLoggingActions } from "../actions";
import { loggingReducer } from "./logging.reducer";
import { starkLoggingStoreKey } from "../constants";

/**
* Defines the part of the state assigned to the {@link StarkLoggingModule}
Expand All @@ -16,7 +17,7 @@ export interface StarkLoggingState {
/**
* Reducers assigned to each property of the {@link StarkLoggingModule}'s state
*/
export const starkLoggingReducers: ActionReducerMap<StarkLoggingState, StarkLoggingActions> = {
export const starkLoggingReducers: ActionReducerMap<StarkLoggingState, StarkLoggingActions.Types> = {
/**
* Reducer assigned to the state's `logging` property
*/
Expand All @@ -26,7 +27,7 @@ export const starkLoggingReducers: ActionReducerMap<StarkLoggingState, StarkLogg
/**
* NGRX Selector for the {@link StarkLoggingModule}'s state
*/
export const selectStarkLogging: MemoizedSelector<object, StarkLogging> = createSelector(
createFeatureSelector<StarkLoggingState>("StarkLogging"),
export const selectStarkLogging = createSelector(
createFeatureSelector<StarkLoggingState>(starkLoggingStoreKey),
(state: StarkLoggingState) => state.logging
);
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*tslint:disable:completed-docs*/
import { StarkLogging, StarkLogMessageImpl, StarkLogMessageType } from "../entities";
import { loggingReducer } from "./logging.reducer";
import { StarkFlushLogMessages, StarkLogMessageAction, StarkSetLoggingApplicationId } from "../actions";
import { StarkLoggingActions } from "../actions";

const deepFreeze: Function = require("deep-freeze-strict");

Expand All @@ -26,7 +26,7 @@ describe("Reducer: LoggingReducer", () => {
deepFreeze(initialState); // Enforce immutability
const changedState: StarkLogging = loggingReducer(
initialState,
new StarkLogMessageAction(new StarkLogMessageImpl(StarkLogMessageType.DEBUG, "Message N", ""))
StarkLoggingActions.logMessage({ message: new StarkLogMessageImpl(StarkLogMessageType.DEBUG, "Message N", "") })
);

expect(changedState.messages.length).toBe(3);
Expand All @@ -38,7 +38,7 @@ describe("Reducer: LoggingReducer", () => {
it("should add the given messages to the array even if the state is not defined", () => {
const changedState: StarkLogging = loggingReducer(
<any>undefined,
new StarkLogMessageAction(new StarkLogMessageImpl(StarkLogMessageType.DEBUG, "Message N", ""))
StarkLoggingActions.logMessage({ message: new StarkLogMessageImpl(StarkLogMessageType.DEBUG, "Message N", "") })
);

expect(changedState.messages.length).toBe(1);
Expand All @@ -61,7 +61,10 @@ describe("Reducer: LoggingReducer", () => {
expect(initialState.messages.length).toBe(5);

deepFreeze(initialState); // Enforce immutability
const changedState: StarkLogging = loggingReducer(initialState, new StarkFlushLogMessages(3));
const changedState: StarkLogging = loggingReducer(
initialState,
StarkLoggingActions.flushLogMessages({ numberOfMessagesToFlush: 3 })
);

expect(changedState.messages.length).toBe(2);
expect(changedState.messages[0].message).toBe("Message 4");
Expand All @@ -76,12 +79,18 @@ describe("Reducer: LoggingReducer", () => {
initialState.applicationId = "whatever";

deepFreeze(initialState); // Enforce immutability
const changedState: StarkLogging = loggingReducer(initialState, new StarkSetLoggingApplicationId("new appID"));
const changedState: StarkLogging = loggingReducer(
initialState,
StarkLoggingActions.setLoggingApplicationId({ applicationId: "new appID" })
);

expect(changedState.applicationId).toBe("new appID");
});
it("should set the application id even if the state is not defined", () => {
const changedState: StarkLogging = loggingReducer(<any>undefined, new StarkSetLoggingApplicationId("new appID"));
const changedState: StarkLogging = loggingReducer(
<any>undefined,
StarkLoggingActions.setLoggingApplicationId({ applicationId: "new appID" })
);

expect(changedState.applicationId).toBe("new appID");
});
Expand Down
48 changes: 21 additions & 27 deletions packages/stark-core/src/modules/logging/reducers/logging.reducer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { StarkLoggingActions, StarkLoggingActionTypes } from "../actions";
import { StarkLoggingActions } from "../actions";
import { StarkLogging, StarkLogMessage } from "../entities";

/**
* The store key will allow the application to find the reducer in the store
*/
export const starkLoggingStoreKey = "starkLogging";
import { createReducer, on } from "@ngrx/store";

/**
* Defines the initial state of the reducer
Expand All @@ -15,33 +11,31 @@ const INITIAL_LOGGING_STATE: Readonly<StarkLogging> = {
messages: []
};

/**
* Definition of the reducer using `createReducer` method.
*/
const reducer = createReducer<StarkLogging, StarkLoggingActions.Types>(
INITIAL_LOGGING_STATE,
on(StarkLoggingActions.logMessage, (state, action) => ({ ...state, messages: [...state.messages, action.message] })),
on(StarkLoggingActions.flushLogMessages, (state, action) => {
const numberOfMessagesToFlush: number = action.numberOfMessagesToFlush;
const numberOfMessages: number = state.messages.length;
const messages: StarkLogMessage[] = state.messages.slice(numberOfMessagesToFlush, numberOfMessages);

return { ...state, messages: [...messages] };
}),
on(StarkLoggingActions.setLoggingApplicationId, (state, action) => ({ ...state, applicationId: action.applicationId }))
);

/**
* Definition of the `logging` reducer.
* @param state - The state of the reducer
* @param action - The action to perform
* @returns The new `StarkLogging` state
*/
export function loggingReducer(
state: Readonly<StarkLogging> = INITIAL_LOGGING_STATE,
action: Readonly<StarkLoggingActions>
state: Readonly<StarkLogging> | undefined,
action: Readonly<StarkLoggingActions.Types>
): Readonly<StarkLogging> {
// the new state will be calculated from the data coming in the actions
switch (action.type) {
case StarkLoggingActionTypes.LOG_MESSAGE:
const message: StarkLogMessage = action.message;
return { ...state, messages: [...state.messages, message] };

case StarkLoggingActionTypes.FLUSH_LOG:
const numberOfMessagesToFlush: number = action.numberOfMessagesToFlush;
const numberOfMessages: number = state.messages.length;
const messages: StarkLogMessage[] = state.messages.slice(numberOfMessagesToFlush, numberOfMessages);

return { ...state, messages: [...messages] };

case StarkLoggingActionTypes.SET_LOGGING_APPLICATION_ID:
return { ...state, applicationId: action.applicationId };

default:
return state;
}
return reducer(state, action);
}
Loading

0 comments on commit 3dd57d2

Please sign in to comment.