11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT license.
33
4- import { createSlice } from "@reduxjs/toolkit" ;
4+ import { createSlice , current } from "@reduxjs/toolkit" ;
55import _ from "lodash" ;
6+ import { ClasspathEntry , ProjectState } from "../../../types" ;
7+ import { ProjectType } from "../../../../utils/webview" ;
68
79export const classpathConfigurationViewSlice = createSlice ( {
810 name : "classpathConfig" ,
911 initialState : {
1012 activeProjectIndex : 0 ,
1113 projects : [ ] ,
12- activeVmInstallPath : "" ,
14+ activeVmInstallPath : [ ] as string [ ] ,
1315 vmInstalls : [ ] ,
14- projectType : undefined ,
15- sources : [ ] as string [ ] ,
16- output : "" ,
17- referencedLibraries : [ ] as string [ ] ,
16+ projectType : [ ] as ProjectType [ ] ,
17+ sources : [ ] as ClasspathEntry [ ] [ ] ,
18+ output : [ ] as string [ ] ,
19+ libraries : [ ] as ClasspathEntry [ ] [ ] ,
20+ projectState : [ ] as ProjectState [ ] ,
21+ loadingState : false ,
1822 exception : undefined ,
1923 } ,
2024 reducers : {
2125 listProjects : ( state , action ) => {
2226 state . projects = action . payload ;
2327 state . activeProjectIndex = 0 ;
28+ const projectNum = state . projects . length ;
29+ state . activeVmInstallPath = Array ( projectNum ) . fill ( "" ) ;
30+ state . projectType = Array ( projectNum ) . fill ( "" ) ;
31+ state . sources = Array ( projectNum ) . fill ( [ ] ) ;
32+ state . output = Array ( projectNum ) . fill ( "" ) ;
33+ state . libraries = Array ( projectNum ) . fill ( [ ] ) ;
34+ state . projectState = Array ( projectNum ) . fill ( ProjectState . Unloaded ) ;
2435 } ,
2536 listVmInstalls : ( state , action ) => {
2637 state . vmInstalls = action . payload ;
@@ -29,43 +40,54 @@ export const classpathConfigurationViewSlice = createSlice({
2940 state . activeProjectIndex = action . payload ;
3041 } ,
3142 loadClasspath : ( state , action ) => {
32- state . projectType = action . payload . projectType ;
33- state . output = action . payload . output ;
34- state . activeVmInstallPath = action . payload . activeVmInstallPath ;
43+ state . projectState [ state . activeProjectIndex ] = ProjectState . Loaded ;
44+ state . projectType [ state . activeProjectIndex ] = action . payload . projectType ;
45+ state . output [ state . activeProjectIndex ] = action . payload . output ;
46+ state . activeVmInstallPath [ state . activeProjectIndex ] = action . payload . activeVmInstallPath ;
3547 // Only update the array when they have different elements.
36- if ( isDifferentStringArray ( state . sources , action . payload . sources ) ) {
37- state . sources = action . payload . sources ;
48+ const currentSources = _ . sortBy ( current ( state . sources ) , [ "path" , "output" ] ) ;
49+ const newSources = _ . sortBy ( action . payload . sources , [ "path" , "output" ] ) ;
50+ if ( ! _ . isEqual ( currentSources , newSources ) ) {
51+ state . sources [ state . activeProjectIndex ] = action . payload . sources ;
3852 }
39- if ( isDifferentStringArray ( state . referencedLibraries , action . payload . referencedLibraries ) ) {
40- state . referencedLibraries = action . payload . referencedLibraries ;
53+
54+ const currentLibs = _ . sortBy ( current ( state . libraries ) , [ "path" ] ) ;
55+ const newLibs = _ . sortBy ( action . payload . libraries , [ "path" ] ) ;
56+ if ( ! _ . isEqual ( currentLibs , newLibs ) ) {
57+ state . libraries [ state . activeProjectIndex ] = action . payload . libraries ;
4158 }
4259 } ,
4360 updateSource : ( state , action ) => {
44- state . sources = action . payload ;
61+ state . sources [ state . activeProjectIndex ] = action . payload ;
4562 } ,
4663 setOutputPath : ( state , action ) => {
47- state . output = action . payload ;
64+ state . output [ state . activeProjectIndex ] = action . payload ;
4865 } ,
4966 setJdks : ( state , action ) => {
50- state . activeVmInstallPath = action . payload . activeVmInstallPath ;
67+ state . activeVmInstallPath [ state . activeProjectIndex ] = action . payload . activeVmInstallPath ;
5168 if ( action . payload . vmInstalls &&
5269 isDifferentStringArray ( state . vmInstalls , action . payload . vmInstalls ) ) {
5370 state . vmInstalls = action . payload . vmInstalls ;
5471 }
5572 } ,
5673 removeReferencedLibrary : ( state , action ) => {
5774 const removedIndex : number = action . payload as number ;
58- if ( removedIndex > - 1 && removedIndex < state . referencedLibraries . length ) {
59- state . referencedLibraries . splice ( removedIndex , 1 ) ;
75+ if ( removedIndex > - 1 && removedIndex < state . libraries [ state . activeProjectIndex ] . length ) {
76+ state . libraries [ state . activeProjectIndex ] . splice ( removedIndex , 1 ) ;
6077 }
6178 } ,
62- addReferencedLibraries : ( state , action ) => {
63- state . referencedLibraries . push ( ...action . payload ) ;
64- state . referencedLibraries = _ . uniq ( state . referencedLibraries ) ;
79+ addLibraries : ( state , action ) => {
80+ let newLibs = state . libraries [ state . activeProjectIndex ] ;
81+ newLibs . unshift ( ...action . payload ) ;
82+ newLibs = _ . uniq ( newLibs ) ;
83+ state . libraries [ state . activeProjectIndex ] = _ . uniq ( newLibs ) ;
6584 } ,
6685 catchException : ( state , action ) => {
6786 state . exception = action . payload ;
68- }
87+ } ,
88+ updateLoadingState : ( state , action ) => {
89+ state . loadingState = action . payload ;
90+ } ,
6991 } ,
7092} ) ;
7193
@@ -82,8 +104,9 @@ export const {
82104 setOutputPath,
83105 setJdks,
84106 removeReferencedLibrary,
85- addReferencedLibraries ,
107+ addLibraries ,
86108 catchException,
109+ updateLoadingState,
87110} = classpathConfigurationViewSlice . actions ;
88111
89112export default classpathConfigurationViewSlice . reducer ;
0 commit comments