11using System ;
2+ using System . Collections . Concurrent ;
23using System . Collections . Generic ;
34using System . Linq ;
45using System . Reflection ;
@@ -16,7 +17,11 @@ namespace Flow.Launcher.Core.Plugin
1617{
1718 public static class PluginsLoader
1819 {
19- public static List < PluginPair > Plugins ( List < PluginMetadata > metadatas , PluginsSettings settings )
20+ private static readonly ConcurrentQueue < PluginPair > Plugins = new ( ) ;
21+
22+ private static readonly ConcurrentQueue < string > ErroredPlugins = new ( ) ;
23+
24+ public static async Task < List < PluginPair > > PluginsAsync ( List < PluginMetadata > metadatas , PluginsSettings settings )
2025 {
2126 var dotnetPlugins = DotNetPlugins ( metadatas ) ;
2227
@@ -46,17 +51,34 @@ public static List<PluginPair> Plugins(List<PluginMetadata> metadatas, PluginsSe
4651 . Concat ( executablePlugins )
4752 . Concat ( executableV2Plugins )
4853 . ToList ( ) ;
49- return plugins ;
54+
55+ await Task . WhenAll ( plugins ) ;
56+
57+ if ( ! ErroredPlugins . IsEmpty )
58+ {
59+ var errorPluginString = String . Join ( Environment . NewLine , ErroredPlugins ) ;
60+
61+ var errorMessage = "The following "
62+ + ( ErroredPlugins . Count > 1 ? "plugins have " : "plugin has " )
63+ + "errored and cannot be loaded:" ;
64+
65+ _ = Task . Run ( ( ) =>
66+ {
67+ MessageBox . Show ( $ "{ errorMessage } { Environment . NewLine } { Environment . NewLine } " +
68+ $ "{ errorPluginString } { Environment . NewLine } { Environment . NewLine } " +
69+ $ "Please refer to the logs for more information", "" ,
70+ MessageBoxButtons . OK , MessageBoxIcon . Warning ) ;
71+ } ) ;
72+ }
73+
74+ return Plugins . ToList ( ) ;
5075 }
5176
52- public static IEnumerable < PluginPair > DotNetPlugins ( List < PluginMetadata > source )
77+ public static IEnumerable < Task > DotNetPlugins ( List < PluginMetadata > source )
5378 {
54- var erroredPlugins = new List < string > ( ) ;
55-
56- var plugins = new List < PluginPair > ( ) ;
5779 var metadatas = source . Where ( o => AllowedLanguage . IsDotNet ( o . Language ) ) ;
5880
59- foreach ( var metadata in metadatas )
81+ return metadatas . Select ( metadata => Task . Run ( ( ) =>
6082 {
6183 var milliseconds = Stopwatch . Debug (
6284 $ "|PluginsLoader.DotNetPlugins|Constructor init cost for { metadata . Name } ", ( ) =>
@@ -100,53 +122,42 @@ public static IEnumerable<PluginPair> DotNetPlugins(List<PluginMetadata> source)
100122
101123 if ( plugin == null )
102124 {
103- erroredPlugins . Add ( metadata . Name ) ;
125+ ErroredPlugins . Enqueue ( metadata . Name ) ;
104126 return ;
105127 }
106128
107- plugins . Add ( new PluginPair { Plugin = plugin , Metadata = metadata } ) ;
129+ Plugins . Enqueue ( new PluginPair { Plugin = plugin , Metadata = metadata } ) ;
108130 } ) ;
109131 metadata . InitTime += milliseconds ;
110- }
111-
112- if ( erroredPlugins . Count > 0 )
113- {
114- var errorPluginString = String . Join ( Environment . NewLine , erroredPlugins ) ;
115-
116- var errorMessage = "The following "
117- + ( erroredPlugins . Count > 1 ? "plugins have " : "plugin has " )
118- + "errored and cannot be loaded:" ;
119-
120- _ = Task . Run ( ( ) =>
121- {
122- MessageBox . Show ( $ "{ errorMessage } { Environment . NewLine } { Environment . NewLine } " +
123- $ "{ errorPluginString } { Environment . NewLine } { Environment . NewLine } " +
124- $ "Please refer to the logs for more information", "" ,
125- MessageBoxButtons . OK , MessageBoxIcon . Warning ) ;
126- } ) ;
127- }
128-
129- return plugins ;
132+ } ) ) ;
130133 }
131134
132- public static IEnumerable < PluginPair > ExecutablePlugins ( IEnumerable < PluginMetadata > source )
135+ public static IEnumerable < Task > ExecutablePlugins ( IEnumerable < PluginMetadata > source )
133136 {
134137 return source
135138 . Where ( o => o . Language . Equals ( AllowedLanguage . Executable , StringComparison . OrdinalIgnoreCase ) )
136- . Select ( metadata => new PluginPair
139+ . Select ( metadata => Task . Run ( ( ) =>
137140 {
138- Plugin = new ExecutablePlugin ( metadata . ExecuteFilePath ) , Metadata = metadata
139- } ) ;
141+ return new PluginPair
142+ {
143+ Plugin = new ExecutablePlugin ( metadata . ExecuteFilePath ) ,
144+ Metadata = metadata
145+ } ;
146+ } ) ) ;
140147 }
141148
142- public static IEnumerable < PluginPair > ExecutableV2Plugins ( IEnumerable < PluginMetadata > source )
149+ public static IEnumerable < Task > ExecutableV2Plugins ( IEnumerable < PluginMetadata > source )
143150 {
144151 return source
145152 . Where ( o => o . Language . Equals ( AllowedLanguage . ExecutableV2 , StringComparison . OrdinalIgnoreCase ) )
146- . Select ( metadata => new PluginPair
153+ . Select ( metadata => Task . Run ( ( ) =>
147154 {
148- Plugin = new ExecutablePluginV2 ( metadata . ExecuteFilePath ) , Metadata = metadata
149- } ) ;
155+ return new PluginPair
156+ {
157+ Plugin = new ExecutablePluginV2 ( metadata . ExecuteFilePath ) ,
158+ Metadata = metadata
159+ } ;
160+ } ) ) ;
150161 }
151162 }
152163}
0 commit comments