Skip to content

Commit 2290c54

Browse files
committed
Concurrent localization operation fixed.
`apply` in LocalizationAPI.kt now not inline. Signed-off-by: Pavel Erokhin (MairwunNx) <MairwunNx@gmail.com>
1 parent 1ea9149 commit 2290c54

File tree

1 file changed

+18
-19
lines changed
  • src/main/kotlin/com/mairwunnx/projectessentials/core/api/v1/localization

1 file changed

+18
-19
lines changed

src/main/kotlin/com/mairwunnx/projectessentials/core/api/v1/localization/LocalizationAPI.kt

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,17 @@ import kotlin.system.measureTimeMillis
2727
* @since 2.0.0-SNAPSHOT.1.
2828
*/
2929
object LocalizationAPI {
30-
val logger = LogManager.getLogger()!!
30+
private val logger = LogManager.getLogger()!!
31+
private val mutex = Mutex()
3132

3233
private val generalConfiguration by lazy {
3334
getConfigurationByName<GeneralConfiguration>("general")
3435
}
3536

36-
val mutex = Mutex()
37-
val localizations: HashMap<String, MutableList<HashMap<String, String>>> = hashMapOf()
37+
private val localizations: MutableMap<String, MutableList<HashMap<String, String>>> =
38+
Collections.synchronizedMap(mutableMapOf())
3839

39-
inline fun apply(clazz: Class<*>, crossinline entries: () -> List<String>) {
40+
fun apply(clazz: Class<*>, entries: () -> List<String>) {
4041
CoroutineScope(Dispatchers.Default).launch {
4142
async {
4243
entries().asSequence().forEach {
@@ -50,14 +51,7 @@ object LocalizationAPI {
5051
predicate != "_comment"
5152
}.forEach { key ->
5253
val value = jsonObject.get(key) as String
53-
mutex.withLock {
54-
val result = localizations[name]
55-
if (result == null) {
56-
localizations[name] = mutableListOf(hashMapOf(key to value))
57-
} else {
58-
result.add(hashMapOf(Pair(key, value)))
59-
}
60-
}
54+
change(name, key, value)
6155
}
6256
}
6357
}.also { time ->
@@ -70,19 +64,24 @@ object LocalizationAPI {
7064
}
7165
}
7266

67+
@Synchronized
68+
private suspend fun change(name: String, key: String, value: String) {
69+
mutex.withLock {
70+
val result = localizations[name]
71+
if (result == null) {
72+
localizations[name] = mutableListOf(hashMapOf(key to value))
73+
} else {
74+
result.add(hashMapOf(Pair(key, value)))
75+
}
76+
}
77+
}
78+
7379
/**
7480
* Applying localization, without (with since 2.0.1) processing. Thread safe.
75-
* **Apply only in setup event!**
76-
*
77-
* Deprecated, and redirects to [apply].
7881
*
7982
* @param localization localization data class instance.
8083
* @since 2.0.0-SNAPSHOT.1.
8184
*/
82-
@Deprecated(
83-
"Deprecated feature, but still existing for backward compatibility",
84-
ReplaceWith("apply { localization.sources }")
85-
)
8685
fun apply(localization: Localization) = apply(localization.sourceClass) { localization.sources }
8786

8887
/**

0 commit comments

Comments
 (0)