forked from fanruan/design
Browse Source
Merge in DESIGN/design from ~STARRYI/design:feature/x to feature/x * commit '2d490cacb658cac0054e36e9db7871756cdf4309': REPORT-57974 【主题切换】新建模板时,没按照设置的模板主题来 REPORT-57952 【主题切换】新建模板主题标题显示有问题 REPORT-57943 【主题切换】10.0自定义的预定义样式,模板放11.0上,配置丢失 REPORT-51919 主题切换 REPORT-51919 主题切换 REPORT-57934 【主题切换】新建主题时,未填写主题名称不能设置细节配置research/11.0
starryi
3 years ago
12 changed files with 222 additions and 86 deletions
@ -0,0 +1,86 @@
|
||||
package com.fr.design.mainframe.theme; |
||||
|
||||
import com.fr.base.theme.TemplateTheme; |
||||
import com.fr.base.theme.TemplateThemeConfig; |
||||
import com.fr.concurrent.NamedThreadFactory; |
||||
import com.fr.module.ModuleContext; |
||||
|
||||
import java.util.concurrent.ExecutorService; |
||||
|
||||
/** |
||||
* @author Starryi |
||||
* @version 1.0 |
||||
* Created by Starryi on 2021/8/24 |
||||
*/ |
||||
public class AsyncThemeFetcher<T extends TemplateTheme> { |
||||
|
||||
private final ExecutorService executorService; |
||||
private final TemplateThemeConfig<T> config; |
||||
|
||||
public AsyncThemeFetcher(int threads, TemplateThemeConfig<T> config) { |
||||
this.executorService = ModuleContext.getExecutor().newFixedThreadPool( |
||||
threads, |
||||
new NamedThreadFactory(this.getClass().getName()) |
||||
); |
||||
this.config = config; |
||||
} |
||||
|
||||
public void shutdown() { |
||||
executorService.shutdown(); |
||||
} |
||||
|
||||
public boolean isShutdown() { |
||||
return executorService.isShutdown(); |
||||
} |
||||
|
||||
public void submit(String themeName, AsyncThemeFetchCallback<T> callback) { |
||||
callback.beforeCachedFetch(); |
||||
executorService.submit(new Runnable() { |
||||
@Override |
||||
public void run() { |
||||
if (executorService.isShutdown()) { |
||||
return; |
||||
} |
||||
T theme = config.cachedFetch(themeName, new TemplateThemeConfig.CacheCondition<T>() { |
||||
@Override |
||||
public boolean shouldCacheTheme(T theme) { |
||||
return callback.shouldCache(AsyncThemeFetcher.this, theme); |
||||
} |
||||
}); |
||||
if (executorService.isShutdown()) { |
||||
return; |
||||
} |
||||
callback.afterCachedFetch(theme); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
public interface AsyncThemeFetchCallback<T extends TemplateTheme> { |
||||
void beforeCachedFetch(); |
||||
boolean shouldCache(AsyncThemeFetcher<T> fetcher, T theme); |
||||
void afterCachedFetch(T theme); |
||||
} |
||||
|
||||
public static class AsyncThemeFetchCallbackAdapter<T extends TemplateTheme> implements AsyncThemeFetchCallback<T> { |
||||
|
||||
@Override |
||||
public void beforeCachedFetch() { |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public boolean shouldCache(AsyncThemeFetcher<T> fetcher, T theme) { |
||||
// 如果Fetcher已经关闭就不放进缓存里了
|
||||
// 因为可切换工作目录,所以submit时的工作目录环境与最终获取到主题数据时的工作目录环境可能不是同一个,
|
||||
// 如果仍然放进缓存中,会污染当前工作目录环境的主题缓存.
|
||||
// TODO: 除了根据asyncThemeFetch的关闭情况来判断是否缓存主题,也可以更加精细的判断前后的工作目录环境是否时同一个
|
||||
// TODO: 后续看情况再优化吧.
|
||||
return !fetcher.isShutdown(); |
||||
} |
||||
|
||||
@Override |
||||
public void afterCachedFetch(T theme) { |
||||
|
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue