Skip to content

Commit

Permalink
feat(stark-core): update session 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 `StarkSessionActionsTypes`
became obsolete so it has been removed.

As a result, the following actions have been changed:
- `StarkChangeLanguage(public languageId: string)`
  -> `StarkSessionActions.changeLanguage({ languageId: string })`
- `StarkChangeLanguageSuccess(public languageId: string)`
  -> `StarkSessionActions.changeLanguageSuccess({ languageId: string })`
- `StarkChangeLanguageFailure(public error: any)`
  -> `StarkSessionActions.changeLanguageFailure({ error: any })`
- `StarkInitializeSession(public user: StarkUser)`
  -> `StarkSessionActions.initializeSession({ user: StarkUser })`
- `StarkInitializeSessionSuccess()` -> `StarkSessionActions.initializeSessionSuccess()`
- `StarkDestroySession()` -> `StarkSessionActions.destroySession()`
- `StarkDestroySessionSuccess()` -> `StarkSessionActions.destroySessionSuccess()`
- `StarkSessionTimeoutCountdownStart(public countdown: number)`
  -> `StarkSessionActions.sessionTimeoutCountdownStart({ countdown: number })`
- `StarkSessionTimeoutCountdownStop()` -> `StarkSessionActions.sessionTimeoutCountdownStop()`
- `StarkSessionTimeoutCountdownFinish()` -> `StarkSessionActions.sessionTimeoutCountdownFinish()`
- `StarkSessionLogout()` -> `StarkSessionActions.sessionLogout()`
- `StarkUserActivityTrackingPause()` -> `StarkSessionActions.userActivityTrackingPause()`
- `StarkUserActivityTrackingResume()` -> `StarkSessionActions.userActivityTrackingResume()`

And also the previous union type has been replaced:
`StarkSessionActions` -> `StarkSessionActions.Types`.

Change in effect:

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

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

Change in `action` usage:

```typescript
// Before
this.store.dispatch(new StarkChangeLanguageSuccess(languageId));

// After
this.store.dispatch(StarkSessionActions.changeLanguageSuccess({ languageId: languageId }));
```
  • Loading branch information
SuperITMan committed May 3, 2021
1 parent 727c244 commit 810bbc1
Show file tree
Hide file tree
Showing 17 changed files with 232 additions and 344 deletions.
3 changes: 2 additions & 1 deletion packages/stark-core/src/modules/session/actions.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./actions/session.actions";
import * as StarkSessionActions from "./actions/session.actions";
export { StarkSessionActions };
194 changes: 50 additions & 144 deletions packages/stark-core/src/modules/session/actions/session.actions.ts
Original file line number Diff line number Diff line change
@@ -1,101 +1,46 @@
import { Action } from "@ngrx/store";
import { createAction, props, union } from "@ngrx/store";
import { StarkUser } from "../../user/entities";

/**
* Actions related to {@link StarkSessionService}
*/
export enum StarkSessionActionTypes {
CHANGE_LANGUAGE = "[StarkSession] Change Language",
CHANGE_LANGUAGE_SUCCESS = "[StarkSession] Change Language Success",
CHANGE_LANGUAGE_FAILURE = "[StarkSession] Change Language Failure",
INITIALIZE_SESSION = "[StarkSession] Initialize Session",
INITIALIZE_SESSION_SUCCESS = "[StarkSession] Initialize Session Success",
DESTROY_SESSION = "[StarkSession] Destroy Session",
DESTROY_SESSION_SUCCESS = "[StarkSession] Destroy Session Success",
SESSION_TIMEOUT_COUNTDOWN_START = "[StarkSession] Session Timeout Countdown Start",
SESSION_TIMEOUT_COUNTDOWN_STOP = "[StarkSession] Session Timeout Countdown Stop",
SESSION_TIMEOUT_COUNTDOWN_FINISH = "[StarkSession] Session Timeout Countdown Finish",
SESSION_LOGOUT = "[StarkSession] Session Logout",
USER_ACTIVITY_TRACKING_PAUSE = "[StarkSession] User Activity Tracking Pause",
USER_ACTIVITY_TRACKING_RESUME = "[StarkSession] User Activity Tracking Resume"
}
import { starkSessionStoreKey } from "../constants";

/**
* Triggered when the [StarkSessionService setCurrentLanguage()]{@link StarkSessionService#setCurrentLanguage} method is called,
* just before changing the current session's language.
*
* Parameter:
* - languageId - The target language to change to.
*/
export class StarkChangeLanguage implements Action {
/**
* The type of action
*/
public readonly type: StarkSessionActionTypes.CHANGE_LANGUAGE = StarkSessionActionTypes.CHANGE_LANGUAGE;

/**
* Class constructor
* @param languageId - The target language to change to.
*/
public constructor(public languageId: string) {}
}
export const changeLanguage = createAction(`[${starkSessionStoreKey}] Change Language`, props<{ languageId: string }>());

/**
* Triggered when the current session's language has been successfully changed by calling
* the [StarkSessionService setCurrentLanguage()]{@link StarkSessionService#setCurrentLanguage} method.
*
* Parameter:
* - languageId - The target language that was successfully changed to.
*/
export class StarkChangeLanguageSuccess implements Action {
/**
* The type of action
*/
public readonly type: StarkSessionActionTypes.CHANGE_LANGUAGE_SUCCESS = StarkSessionActionTypes.CHANGE_LANGUAGE_SUCCESS;

/**
* Class constructor
* @param languageId - The target language that was successfully changed to.
*/
public constructor(public languageId: string) {}
}
export const changeLanguageSuccess = createAction(`[${starkSessionStoreKey}] Change Language Success`, props<{ languageId: string }>());

/**
* Triggered when the change of the current session's language failed.
*
* Parameter:
* - error - The error that caused the language change to fail.
*/
export class StarkChangeLanguageFailure implements Action {
/**
* The type of action
*/
public readonly type: StarkSessionActionTypes.CHANGE_LANGUAGE_FAILURE = StarkSessionActionTypes.CHANGE_LANGUAGE_FAILURE;

/**
* Class constructor
* @param error - The error that caused the language change to fail.
*/
public constructor(public error: any) {}
}
export const changeLanguageFailure = createAction(`[${starkSessionStoreKey}] Change Language Failure`, props<{ error: any }>());

/**
* Triggered by the [StarkSessionService login()]{@link StarkSessionService#login} method before the process to
* initialize the session for the given user starts.
*
* Parameter:
* - user - The user whose session will be initialized.
*/
export class StarkInitializeSession implements Action {
/**
* The type of action
*/
public readonly type: StarkSessionActionTypes.INITIALIZE_SESSION = StarkSessionActionTypes.INITIALIZE_SESSION;

/**
* Class constructor
* @param user - The user whose session will be initialized.
*/
public constructor(public user: StarkUser) {}
}
export const initializeSession = createAction(`[${starkSessionStoreKey}] Initialize Session`, props<{ user: StarkUser }>());

/**
* Triggered when the initialization of the user's session has finished successfully.
*/
export class StarkInitializeSessionSuccess implements Action {
/**
* The type of action
*/
public readonly type: StarkSessionActionTypes.INITIALIZE_SESSION_SUCCESS = StarkSessionActionTypes.INITIALIZE_SESSION_SUCCESS;
}
export const initializeSessionSuccess = createAction(`[${starkSessionStoreKey}] Initialize Session Success`);

/**
* Triggered before the process to destroy the user's session starts right after the HTTP logout call has been sent.
Expand All @@ -107,114 +52,75 @@ export class StarkInitializeSessionSuccess implements Action {
* 4. Being inactive for a certain period of time reaching the idle timeout defined by the
* application's [StarkApplicationConfig sessionTimeout]{@link StarkApplicationConfig#sessionTimeout}.
*/
export class StarkDestroySession implements Action {
/**
* The type of action
*/
public readonly type: StarkSessionActionTypes.DESTROY_SESSION = StarkSessionActionTypes.DESTROY_SESSION;
}
export const destroySession = createAction(`[${starkSessionStoreKey}] Destroy Session`);

/**
* Triggered when the destruction of the user's session has finished successfully.
* This action is the last action to be dispatched when the user is logged out (either manually by himself or automatically due to inactivity).
*/
export class StarkDestroySessionSuccess implements Action {
/**
* The type of action
*/
public readonly type: StarkSessionActionTypes.DESTROY_SESSION_SUCCESS = StarkSessionActionTypes.DESTROY_SESSION_SUCCESS;
}
export const destroySessionSuccess = createAction(`[${starkSessionStoreKey}] Destroy Session Success`);

/**
* Triggered when the countdown to automatically destroy the user's session due to inactivity starts.
*
* The countdown is defined in the application's [StarkApplicationConfig sessionTimeoutWarningPeriod]{@link StarkApplicationConfig#sessionTimeoutWarningPeriod}.
*
* By default is set to 15 seconds.
*
* Parameter:
* - countdown - The countdown until the session will be automatically destroyed.
*/
export class StarkSessionTimeoutCountdownStart implements Action {
/**
* The type of action
*/
public readonly type: StarkSessionActionTypes.SESSION_TIMEOUT_COUNTDOWN_START = StarkSessionActionTypes.SESSION_TIMEOUT_COUNTDOWN_START;

/**
* Class constructor
* @param countdown - The countdown until the session will be automatically destroyed.
*/
public constructor(public countdown: number) {}
}
export const sessionTimeoutCountdownStart = createAction(`[${starkSessionStoreKey}] Session Timeout Countdown Start`, props<{ countdown: number }>());

/**
* Triggered when the countdown to automatically destroy the user's session due to inactivity stops.
*
* This countdown stops automatically when the user is active again and no longer idle.
*/
export class StarkSessionTimeoutCountdownStop implements Action {
/**
* The type of action
*/
public readonly type: StarkSessionActionTypes.SESSION_TIMEOUT_COUNTDOWN_STOP = StarkSessionActionTypes.SESSION_TIMEOUT_COUNTDOWN_STOP;
}
export const sessionTimeoutCountdownStop = createAction(`[${starkSessionStoreKey}] Session Timeout Countdown Stop`);

/**
* Triggered when the countdown to automatically destroy the user's session has finished. In this case the user will be automatically logged out.
*/
export class StarkSessionTimeoutCountdownFinish implements Action {
/**
* Defines the type of NGRX action to perform.
*/
public readonly type: StarkSessionActionTypes.SESSION_TIMEOUT_COUNTDOWN_FINISH =
StarkSessionActionTypes.SESSION_TIMEOUT_COUNTDOWN_FINISH;
}
export const sessionTimeoutCountdownFinish = createAction(`[${starkSessionStoreKey}] Session Timeout Countdown Finish`);

/**
* Triggered when the user is about to be logged out and the HTTP logout call to be sent.
* This action is called before the process to destroy the user's session starts.
*
* This action is dispatched by the [StarkSessionService logout()]{@link StarkSessionService#logout} method or in case the browser tab was closed and is dispatched before
* the {@link StarkDestroySession} action.
* the {@link destroySession} action.
*/
export class StarkSessionLogout implements Action {
/**
* The type of action
*/
public readonly type: StarkSessionActionTypes.SESSION_LOGOUT = StarkSessionActionTypes.SESSION_LOGOUT;
}
export const sessionLogout = createAction(`[${starkSessionStoreKey}] Session Logout`);

/**
* Triggered by the [StarkSessionService pauseUserActivityTracking()]{@link StarkSessionService#pauseUserActivityTracking} method
* when the user activity tracking is paused (automatically done by the {@link StarkSessionService}).
*/
export class StarkUserActivityTrackingPause implements Action {
/**
* The type of action
*/
public readonly type: StarkSessionActionTypes.USER_ACTIVITY_TRACKING_PAUSE = StarkSessionActionTypes.USER_ACTIVITY_TRACKING_PAUSE;
}
export const userActivityTrackingPause = createAction(`[${starkSessionStoreKey}] User Activity Tracking Pause`);

/**
* Triggered by the [StarkSessionService resumeUserActivityTracking()]{@link StarkSessionService#resumeUserActivityTracking} method
* when the user activity tracking is resumed (automatically done by the {@link StarkSessionService}).
*/
export class StarkUserActivityTrackingResume implements Action {
/**
* The type of action
*/
public readonly type: StarkSessionActionTypes.USER_ACTIVITY_TRACKING_RESUME = StarkSessionActionTypes.USER_ACTIVITY_TRACKING_RESUME;
}
export const userActivityTrackingResume = createAction(`[${starkSessionStoreKey}] User Activity Tracking Resume`);

export type StarkSessionActions =
| StarkChangeLanguage
| StarkChangeLanguageSuccess
| StarkChangeLanguageFailure
| StarkInitializeSession
| StarkInitializeSessionSuccess
| StarkDestroySession
| StarkDestroySessionSuccess
| StarkSessionTimeoutCountdownStart
| StarkSessionTimeoutCountdownStop
| StarkSessionTimeoutCountdownFinish
| StarkSessionLogout
| StarkUserActivityTrackingPause
| StarkUserActivityTrackingResume;
/**
* @ignore
*/
const all = union({
changeLanguage,
changeLanguageSuccess,
changeLanguageFailure,
initializeSession,
initializeSessionSuccess,
destroySession,
destroySessionSuccess,
sessionTimeoutCountdownStart,
sessionTimeoutCountdownStop,
sessionTimeoutCountdownFinish,
sessionLogout,
userActivityTrackingPause,
userActivityTrackingResume
});
export type Types = typeof all;
46 changes: 2 additions & 44 deletions packages/stark-core/src/modules/session/constants.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,2 @@
/**
* Name of the initialization states of the application
*/
export const starkAppInitStateName = "starkAppInit";
/**
* Name of the exit states of the application
*/
export const starkAppExitStateName = "starkAppExit";

/**
* Name of the login state of the application
*/
export const starkLoginStateName: string = starkAppInitStateName + ".starkLogin";
/**
* URL of the login state of the application
*/
export const starkLoginStateUrl = "/starkLogin";

/**
* Name of the Preloading state of the application
*/
export const starkPreloadingStateName: string = starkAppInitStateName + ".starkPreloading";
/**
* URL of the Preloading state of the application
*/
export const starkPreloadingStateUrl = "/starkPreloading";

/**
* Name of the SessionExpired state of the application
*/
export const starkSessionExpiredStateName: string = starkAppExitStateName + ".starkSessionExpired";
/**
* URL of the SessionExpired state of the application
*/
export const starkSessionExpiredStateUrl = "/starkSessionExpired";

/**
* Name of the SessionLogout state of the application
*/
export const starkSessionLogoutStateName: string = starkAppExitStateName + ".starkSessionLogout";
/**
* URL of the SessionLogout state of the application
*/
export const starkSessionLogoutStateUrl = "/starkSessionLogout";
export * from "./constants/session-states";
export * from "./constants/session-store-key";
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Name of the initialization states of the application
*/
export const starkAppInitStateName = "starkAppInit";
/**
* Name of the exit states of the application
*/
export const starkAppExitStateName = "starkAppExit";

/**
* Name of the login state of the application
*/
export const starkLoginStateName: string = starkAppInitStateName + ".starkLogin";
/**
* URL of the login state of the application
*/
export const starkLoginStateUrl = "/starkLogin";

/**
* Name of the Preloading state of the application
*/
export const starkPreloadingStateName: string = starkAppInitStateName + ".starkPreloading";
/**
* URL of the Preloading state of the application
*/
export const starkPreloadingStateUrl = "/starkPreloading";

/**
* Name of the SessionExpired state of the application
*/
export const starkSessionExpiredStateName: string = starkAppExitStateName + ".starkSessionExpired";
/**
* URL of the SessionExpired state of the application
*/
export const starkSessionExpiredStateUrl = "/starkSessionExpired";

/**
* Name of the SessionLogout state of the application
*/
export const starkSessionLogoutStateName: string = starkAppExitStateName + ".starkSessionLogout";
/**
* URL of the SessionLogout state of the application
*/
export const starkSessionLogoutStateUrl = "/starkSessionLogout";
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Key defined to find the service in a store
*/
export const starkSessionStoreKey = "StarkSession";
4 changes: 2 additions & 2 deletions packages/stark-core/src/modules/session/reducers.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "./reducers/index";
export * from "./reducers/session.reducer";
export { selectStarkSession, starkSessionReducers, StarkSessionState } from "./reducers/index";
export { sessionReducer } from "./reducers/session.reducer";

0 comments on commit 810bbc1

Please sign in to comment.