Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
M
Mirai
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
Mirai
Commits
d94b1d32
Commit
d94b1d32
authored
Apr 21, 2020
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rename `subscribingGet` to `syncFromEvent`, `subscribingGetAsync` to `asyncFromEvent`; Improve docs
parent
52342267
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
65 additions
and
38 deletions
+65
-38
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/linear.kt
...ore/src/commonMain/kotlin/net.mamoe.mirai/event/linear.kt
+65
-38
No files found.
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/linear.kt
View file @
d94b1d32
...
...
@@ -7,47 +7,53 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
@
file
:
Suppress
(
"unused"
)
package
net.mamoe.mirai.event
import
kotlinx.coroutines.*
import
net.mamoe.mirai.utils.MiraiExperimentalAPI
import
net.mamoe.mirai.utils.SinceMirai
import
kotlin.coroutines.CoroutineContext
import
kotlin.coroutines.EmptyCoroutineContext
import
kotlin.coroutines.suspendCoroutine
import
kotlin.jvm.JvmSynthetic
import
kotlin.reflect.KClass
/**
* 挂起当前协程, 监听
这个事件, 并尝试从这个事件中获取一个值.
* 挂起当前协程, 监听
事件 [E], 并尝试从这个事件中**同步**一个值, 在超时时抛出 [TimeoutCancellationException]
*
* 若 [mapper] 抛出了一个异常, 本函数会立即抛出这个异常.
* @param timeoutMillis 超时. 单位为毫秒. `-1` 为不限制.
* @param mapper 过滤转换器. 返回非 null 则代表得到了需要的值. [syncFromEvent] 会返回这个值
*
* @param timeoutMillis 超时. 单位为毫秒. `-1` 为不限制
* @param mapper 过滤转换器. 返回非 null 则代表得到了需要的值. [subscribingGet] 会返回这个值
* @see asyncFromEvent 本函数的异步版本
*
* @see subscribingGetAsync 本函数的异步版本
* @throws TimeoutCancellationException 在超时后抛出.
* @throws Throwable 当 [mapper] 抛出任何异常时, 本函数会抛出该异常
*/
@
SinceMirai
(
"0.29.0"
)
@
MiraiExperimentalAPI
suspend
inline
fun
<
reified
E
:
Event
,
R
:
Any
>
s
ubscribingGe
t
(
@
JvmSynthetic
@
SinceMirai
(
"0.38.0"
)
suspend
inline
fun
<
reified
E
:
Event
,
R
:
Any
>
s
yncFromEven
t
(
timeoutMillis
:
Long
=
-
1
,
noinline
mapper
:
suspend
E
.(
E
)
->
R
?
// 不要 crossinline: crossinline 后 stacktrace 会不正常
):
R
{
require
(
timeoutMillis
==
-
1L
||
timeoutMillis
>
0
)
{
"timeoutMillis must be -1 or > 0"
}
return
s
ubscribingGetOrNull
(
timeoutMillis
,
mapper
)
?:
error
(
"timeout subscribingGe
t"
)
return
s
yncFromEventOrNull
(
timeoutMillis
,
mapper
)
?:
error
(
"timeout $timeoutMillis ms asyncFromEven
t"
)
}
/**
* 挂起当前协程, 监听这个事件, 并尝试从这个事件中获取一个值.
*
* 若 [mapper] 抛出了一个异常, 本函数会立即抛出这个异常.
* 挂起当前协程, 监听这个事件, 并尝试从这个事件中获取一个值, 在超时时返回 `null`
*
* @param timeoutMillis 超时. 单位为毫秒. `-1` 为不限制
* @param mapper 过滤转换器. 返回非 null 则代表得到了需要的值. [subscribingGet] 会返回这个值
* @param mapper 过滤转换器. 返回非 null 则代表得到了需要的值.
*
* @return 超时返回 `null`, 否则返回 [mapper] 返回的第一个非 `null` 值.
*
* @see subscribingGetAsync 本函数的异步版本
* @see asyncFromEvent 本函数的异步版本
* @throws Throwable 当 [mapper] 抛出任何异常时, 本函数会抛出该异常
*/
@
SinceMirai
(
"0.29.0"
)
@
MiraiExperimentalAPI
suspend
inline
fun
<
reified
E
:
Event
,
R
:
Any
>
s
ubscribingGe
tOrNull
(
@
JvmSynthetic
@
SinceMirai
(
"0.38.0"
)
suspend
inline
fun
<
reified
E
:
Event
,
R
:
Any
>
s
yncFromEven
tOrNull
(
timeoutMillis
:
Long
=
-
1
,
noinline
mapper
:
suspend
E
.(
E
)
->
R
?
// 不要 crossinline: crossinline 后 stacktrace 会不正常
):
R
?
{
...
...
@@ -55,11 +61,11 @@ suspend inline fun <reified E : Event, R : Any> subscribingGetOrNull(
return
if
(
timeoutMillis
==
-
1L
)
{
coroutineScope
{
s
ubscribingGetOrNullImpl
<
E
,
R
>(
this
,
mapper
)
s
yncFromEventOrNullImpl
<
E
,
R
>(
E
::
class
,
this
,
mapper
)
}
}
else
{
withTimeoutOrNull
(
timeoutMillis
)
{
s
ubscribingGetOrNullImpl
<
E
,
R
>(
this
,
mapper
)
s
yncFromEventOrNullImpl
<
E
,
R
>(
E
::
class
,
this
,
mapper
)
}
}
}
...
...
@@ -71,16 +77,42 @@ suspend inline fun <reified E : Event, R : Any> subscribingGetOrNull(
*
* @param timeoutMillis 超时. 单位为毫秒. `-1` 为不限制
* @param coroutineContext 额外的 [CoroutineContext]
* @param mapper 过滤转换器. 返回非
null 则代表得到了需要的值. [subscribingGe
t] 会返回这个值
* @param mapper 过滤转换器. 返回非
`null` 则代表得到了需要的值. [syncFromEven
t] 会返回这个值
*/
@S
inceMirai
(
"0.29.0
"
)
@
MiraiExperimentalAPI
inline
fun
<
reified
E
:
Event
,
R
:
Any
>
CoroutineScope
.
subscribingGetAsync
(
@S
uppress
(
"DeferredIsResult
"
)
@
SinceMirai
(
"0.38.0"
)
inline
fun
<
reified
E
:
Event
,
R
:
Any
>
CoroutineScope
.
asyncFromEventOrNull
(
timeoutMillis
:
Long
=
-
1
,
coroutineContext
:
CoroutineContext
=
EmptyCoroutineContext
,
noinline
mapper
:
suspend
E
.(
E
)
->
R
?
// 不要 crossinline: crossinline 后 stacktrace 会不正常
):
Deferred
<
R
>
=
this
.
async
(
coroutineContext
)
{
subscribingGet
(
timeoutMillis
,
mapper
)
):
Deferred
<
R
?
>
{
require
(
timeoutMillis
==
-
1L
||
timeoutMillis
>
0
)
{
"timeoutMillis must be -1 or > 0"
}
return
this
.
async
(
coroutineContext
)
{
syncFromEventOrNull
(
timeoutMillis
,
mapper
)
}
}
/**
* 异步监听这个事件, 并尝试从这个事件中获取一个值.
*
* 若 [mapper] 抛出的异常将会被传递给 [Deferred.await] 抛出.
*
* @param timeoutMillis 超时. 单位为毫秒. `-1` 为不限制
* @param coroutineContext 额外的 [CoroutineContext]
* @param mapper 过滤转换器. 返回非 null 则代表得到了需要的值. [syncFromEvent] 会返回这个值
*/
@Suppress
(
"DeferredIsResult"
)
@SinceMirai
(
"0.38.0"
)
inline
fun
<
reified
E
:
Event
,
R
:
Any
>
CoroutineScope
.
asyncFromEvent
(
timeoutMillis
:
Long
=
-
1
,
coroutineContext
:
CoroutineContext
=
EmptyCoroutineContext
,
noinline
mapper
:
suspend
E
.(
E
)
->
R
?
// 不要 crossinline: crossinline 后 stacktrace 会不正常
):
Deferred
<
R
>
{
require
(
timeoutMillis
==
-
1L
||
timeoutMillis
>
0
)
{
"timeoutMillis must be -1 or > 0"
}
return
this
.
async
(
coroutineContext
)
{
syncFromEvent
(
timeoutMillis
,
mapper
)
}
}
...
...
@@ -88,24 +120,19 @@ inline fun <reified E : Event, R : Any> CoroutineScope.subscribingGetAsync(
//// internal
//////////////
@PublishedApi
internal
suspend
inline
fun
<
reified
E
:
Event
,
R
>
subscribingGetOrNullImpl
(
internal
suspend
fun
<
E
:
Event
,
R
>
syncFromEventOrNullImpl
(
eventClass
:
KClass
<
E
>,
coroutineScope
:
CoroutineScope
,
noinline
mapper
:
suspend
E
.(
E
)
->
R
?
):
R
?
{
var
result
:
Result
<
R
?
>
=
Result
.
success
(
null
)
// stub
mapper
:
suspend
E
.(
E
)
->
R
?
):
R
?
=
suspendCoroutine
{
cont
->
var
listener
:
Listener
<
E
>?
=
null
@Suppress
(
"DuplicatedCode"
)
// for better performance
listener
=
coroutineScope
.
subscribe
{
result
=
kotlin
.
runCatching
{
listener
=
coroutineScope
.
subscribe
(
eventClass
)
{
cont
.
resumeWith
(
kotlin
.
runCatching
{
mapper
.
invoke
(
this
,
it
)
?:
return
@
subscribe
ListeningStatus
.
LISTENING
}
}
)
listener
!!
.
complete
()
return
@
subscribe
ListeningStatus
.
STOPPED
}
listener
.
join
()
return
result
.
getOrThrow
()
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment