-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor runningServices usecase into serviceManager
- Loading branch information
Showing
7 changed files
with
191 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from "./state"; | ||
export * from "./thunks"; | ||
export * from "./selectors"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import type { State as RootState } from "core/core"; | ||
import { createSelector } from "@reduxjs/toolkit"; | ||
|
||
import { name, type RunningService } from "./state"; | ||
|
||
const runningServices = (rootState: RootState): RunningService[] | undefined => { | ||
const { runningServices } = rootState[name]; | ||
|
||
if (runningServices === undefined) { | ||
return undefined; | ||
} | ||
|
||
return [...runningServices].sort((a, b) => b.startedAt - a.startedAt); | ||
}; | ||
|
||
const isUpdating = (rootState: RootState): boolean => { | ||
const { isUpdating } = rootState[name]; | ||
return isUpdating; | ||
}; | ||
|
||
const deletableRunningServices = createSelector(runningServices, runningServices => | ||
(runningServices ?? []).filter(({ isOwned }) => isOwned) | ||
); | ||
|
||
const isThereNonOwnedServices = createSelector( | ||
runningServices, | ||
runningServices => | ||
(runningServices ?? []).find(({ isOwned }) => !isOwned) !== undefined | ||
); | ||
|
||
const isThereOwnedSharedServices = createSelector( | ||
runningServices, | ||
runningServices => | ||
(runningServices ?? []).find(({ isOwned, isShared }) => isOwned && isShared) !== | ||
undefined | ||
); | ||
|
||
export const selectors = { | ||
runningServices, | ||
deletableRunningServices, | ||
isUpdating, | ||
isThereNonOwnedServices, | ||
isThereOwnedSharedServices | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { assert } from "tsafe/assert"; | ||
import { createSlice } from "@reduxjs/toolkit"; | ||
import type { PayloadAction } from "@reduxjs/toolkit"; | ||
import { id } from "tsafe/id"; | ||
|
||
type State = { | ||
isUpdating: boolean; | ||
runningServices: undefined | RunningService[]; | ||
}; | ||
|
||
export type RunningService = RunningService.Owned | RunningService.NotOwned; | ||
|
||
export declare namespace RunningService { | ||
export type Common = { | ||
id: string; | ||
packageName: string; | ||
friendlyName: string; | ||
logoUrl: string | undefined; | ||
monitoringUrl: string | undefined; | ||
isStarting: boolean; | ||
startedAt: number; | ||
/** Undefined if the service don't use the token */ | ||
vaultTokenExpirationTime: number | undefined; | ||
s3TokenExpirationTime: number | undefined; | ||
urls: string[]; | ||
postInstallInstructions: string | undefined; | ||
env: Record<string, string>; | ||
}; | ||
|
||
export type Owned = Common & { | ||
isShared: boolean; | ||
isOwned: true; | ||
}; | ||
|
||
export type NotOwned = Common & { | ||
isShared: true; | ||
isOwned: false; | ||
ownerUsername: string; | ||
}; | ||
} | ||
|
||
export const name = "serviceManager"; | ||
|
||
export const { reducer, actions } = createSlice({ | ||
name, | ||
"initialState": id<State>({ | ||
"isUpdating": false, | ||
"runningServices": undefined | ||
}), | ||
"reducers": { | ||
"updateStarted": state => { | ||
state.isUpdating = true; | ||
}, | ||
"updateCompleted": ( | ||
_state, | ||
{ payload }: PayloadAction<{ runningServices: RunningService[] }> | ||
) => { | ||
const { runningServices } = payload; | ||
|
||
return id<State>({ | ||
"isUpdating": false, | ||
runningServices | ||
}); | ||
}, | ||
"serviceStarted": ( | ||
state, | ||
{ | ||
payload | ||
}: PayloadAction<{ | ||
serviceId: string; | ||
doOverwriteStaredAtToNow: boolean; | ||
}> | ||
) => { | ||
const { serviceId, doOverwriteStaredAtToNow } = payload; | ||
const { runningServices } = state; | ||
|
||
assert(runningServices !== undefined); | ||
|
||
const runningService = runningServices.find(({ id }) => id === serviceId); | ||
|
||
if (runningService === undefined) { | ||
return; | ||
} | ||
|
||
runningService.isStarting = false; | ||
|
||
if (doOverwriteStaredAtToNow) { | ||
//NOTE: Harmless hack to improve UI readability. | ||
runningService.startedAt = Date.now(); | ||
} | ||
}, | ||
"serviceStopped": (state, { payload }: PayloadAction<{ serviceId: string }>) => { | ||
const { serviceId } = payload; | ||
|
||
const { runningServices } = state; | ||
assert(runningServices !== undefined); | ||
|
||
runningServices.splice( | ||
runningServices.findIndex(({ id }) => id === serviceId), | ||
1 | ||
); | ||
} | ||
} | ||
}); |
Oops, something went wrong.