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
1c2d8cea
Commit
1c2d8cea
authored
Mar 20, 2020
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rename `filter` to `mapper`
parent
48cf885c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
13 deletions
+16
-13
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/linear.kt
...ore/src/commonMain/kotlin/net.mamoe.mirai/event/linear.kt
+16
-13
No files found.
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/linear.kt
View file @
1c2d8cea
...
@@ -11,42 +11,45 @@ package net.mamoe.mirai.event
...
@@ -11,42 +11,45 @@ package net.mamoe.mirai.event
import
kotlinx.coroutines.*
import
kotlinx.coroutines.*
import
net.mamoe.mirai.utils.MiraiExperimentalAPI
import
net.mamoe.mirai.utils.MiraiExperimentalAPI
import
net.mamoe.mirai.utils.SinceMirai
import
kotlin.coroutines.CoroutineContext
import
kotlin.coroutines.CoroutineContext
import
kotlin.coroutines.EmptyCoroutineContext
import
kotlin.coroutines.EmptyCoroutineContext
/**
/**
* 挂起当前协程, 监听这个事件, 并尝试从这个事件中获取一个值.
* 挂起当前协程, 监听这个事件, 并尝试从这个事件中获取一个值.
*
*
* 若 [
filt
er] 抛出了一个异常, 本函数会立即抛出这个异常.
* 若 [
mapp
er] 抛出了一个异常, 本函数会立即抛出这个异常.
*
*
* @param timeoutMillis 超时. 单位为毫秒. `-1` 为不限制
* @param timeoutMillis 超时. 单位为毫秒. `-1` 为不限制
* @param
filter 过滤
器. 返回非 null 则代表得到了需要的值. [subscribingGet] 会返回这个值
* @param
mapper 过滤转换
器. 返回非 null 则代表得到了需要的值. [subscribingGet] 会返回这个值
*
*
* @see subscribingGetAsync 本函数的异步版本
* @see subscribingGetAsync 本函数的异步版本
*/
*/
@SinceMirai
(
"0.28.0"
)
@MiraiExperimentalAPI
@MiraiExperimentalAPI
suspend
inline
fun
<
reified
E
:
Event
,
R
:
Any
>
subscribingGet
(
suspend
inline
fun
<
reified
E
:
Event
,
R
:
Any
>
subscribingGet
(
timeoutMillis
:
Long
=
-
1
,
timeoutMillis
:
Long
=
-
1
,
noinline
filter
:
E
.(
E
)
->
R
?
// 不要 crossinline: crossinline 后 stacktrace 会不正常
noinline
mapper
:
suspend
E
.(
E
)
->
R
?
// 不要 crossinline: crossinline 后 stacktrace 会不正常
):
R
{
):
R
{
require
(
timeoutMillis
==
-
1L
||
timeoutMillis
>
0
)
{
"timeoutMillis must be -1 or > 0"
}
require
(
timeoutMillis
==
-
1L
||
timeoutMillis
>
0
)
{
"timeoutMillis must be -1 or > 0"
}
return
subscribingGetOrNull
(
timeoutMillis
,
filt
er
)
?:
error
(
"timeout subscribingGet"
)
return
subscribingGetOrNull
(
timeoutMillis
,
mapp
er
)
?:
error
(
"timeout subscribingGet"
)
}
}
/**
/**
* 挂起当前协程, 监听这个事件, 并尝试从这个事件中获取一个值.
* 挂起当前协程, 监听这个事件, 并尝试从这个事件中获取一个值.
*
*
* 若 [
filt
er] 抛出了一个异常, 本函数会立即抛出这个异常.
* 若 [
mapp
er] 抛出了一个异常, 本函数会立即抛出这个异常.
*
*
* @param timeoutMillis 超时. 单位为毫秒. `-1` 为不限制
* @param timeoutMillis 超时. 单位为毫秒. `-1` 为不限制
* @param
filter 过滤
器. 返回非 null 则代表得到了需要的值. [subscribingGet] 会返回这个值
* @param
mapper 过滤转换
器. 返回非 null 则代表得到了需要的值. [subscribingGet] 会返回这个值
*
*
* @see subscribingGetAsync 本函数的异步版本
* @see subscribingGetAsync 本函数的异步版本
*/
*/
@SinceMirai
(
"0.28.0"
)
@MiraiExperimentalAPI
@MiraiExperimentalAPI
suspend
inline
fun
<
reified
E
:
Event
,
R
:
Any
>
subscribingGetOrNull
(
suspend
inline
fun
<
reified
E
:
Event
,
R
:
Any
>
subscribingGetOrNull
(
timeoutMillis
:
Long
=
-
1
,
timeoutMillis
:
Long
=
-
1
,
noinline
filter
:
E
.(
E
)
->
R
?
// 不要 crossinline: crossinline 后 stacktrace 会不正常
noinline
mapper
:
suspend
E
.(
E
)
->
R
?
// 不要 crossinline: crossinline 后 stacktrace 会不正常
):
R
?
{
):
R
?
{
require
(
timeoutMillis
==
-
1L
||
timeoutMillis
>
0
)
{
"timeoutMillis must be -1 or > 0"
}
require
(
timeoutMillis
==
-
1L
||
timeoutMillis
>
0
)
{
"timeoutMillis must be -1 or > 0"
}
var
result
:
R
?
=
null
var
result
:
R
?
=
null
...
@@ -58,7 +61,7 @@ suspend inline fun <reified E : Event, R : Any> subscribingGetOrNull(
...
@@ -58,7 +61,7 @@ suspend inline fun <reified E : Event, R : Any> subscribingGetOrNull(
var
listener
:
Listener
<
E
>?
=
null
var
listener
:
Listener
<
E
>?
=
null
listener
=
this
.
subscribe
{
listener
=
this
.
subscribe
{
val
value
=
try
{
val
value
=
try
{
filt
er
.
invoke
(
this
,
it
)
mapp
er
.
invoke
(
this
,
it
)
}
catch
(
e
:
Exception
)
{
}
catch
(
e
:
Exception
)
{
resultThrowable
=
e
resultThrowable
=
e
return
@
subscribe
ListeningStatus
.
STOPPED
.
also
{
listener
!!
.
complete
()
}
return
@
subscribe
ListeningStatus
.
STOPPED
.
also
{
listener
!!
.
complete
()
}
...
@@ -76,7 +79,7 @@ suspend inline fun <reified E : Event, R : Any> subscribingGetOrNull(
...
@@ -76,7 +79,7 @@ suspend inline fun <reified E : Event, R : Any> subscribingGetOrNull(
@Suppress
(
"DuplicatedCode"
)
// for better performance
@Suppress
(
"DuplicatedCode"
)
// for better performance
listener
=
this
.
subscribe
{
listener
=
this
.
subscribe
{
val
value
=
try
{
val
value
=
try
{
filt
er
.
invoke
(
this
,
it
)
mapp
er
.
invoke
(
this
,
it
)
}
catch
(
e
:
Exception
)
{
}
catch
(
e
:
Exception
)
{
resultThrowable
=
e
resultThrowable
=
e
return
@
subscribe
ListeningStatus
.
STOPPED
.
also
{
listener
!!
.
complete
()
}
return
@
subscribe
ListeningStatus
.
STOPPED
.
also
{
listener
!!
.
complete
()
}
...
@@ -96,17 +99,17 @@ suspend inline fun <reified E : Event, R : Any> subscribingGetOrNull(
...
@@ -96,17 +99,17 @@ suspend inline fun <reified E : Event, R : Any> subscribingGetOrNull(
/**
/**
* 异步监听这个事件, 并尝试从这个事件中获取一个值.
* 异步监听这个事件, 并尝试从这个事件中获取一个值.
*
*
* 若 [
filt
er] 抛出的异常将会被传递给 [Deferred.await] 抛出.
* 若 [
mapp
er] 抛出的异常将会被传递给 [Deferred.await] 抛出.
*
*
* @param timeoutMillis 超时. 单位为毫秒. `-1` 为不限制
* @param timeoutMillis 超时. 单位为毫秒. `-1` 为不限制
* @param coroutineContext 额外的 [CoroutineContext]
* @param coroutineContext 额外的 [CoroutineContext]
* @param
filter 过滤
器. 返回非 null 则代表得到了需要的值. [subscribingGet] 会返回这个值
* @param
mapper 过滤转换
器. 返回非 null 则代表得到了需要的值. [subscribingGet] 会返回这个值
*/
*/
@MiraiExperimentalAPI
@MiraiExperimentalAPI
inline
fun
<
reified
E
:
Event
,
R
:
Any
>
CoroutineScope
.
subscribingGetAsync
(
inline
fun
<
reified
E
:
Event
,
R
:
Any
>
CoroutineScope
.
subscribingGetAsync
(
coroutineContext
:
CoroutineContext
=
EmptyCoroutineContext
,
coroutineContext
:
CoroutineContext
=
EmptyCoroutineContext
,
timeoutMillis
:
Long
=
-
1
,
timeoutMillis
:
Long
=
-
1
,
noinline
filter
:
E
.(
E
)
->
R
?
// 不要 crossinline: crossinline 后 stacktrace 会不正常
noinline
mapper
:
suspend
E
.(
E
)
->
R
?
// 不要 crossinline: crossinline 后 stacktrace 会不正常
):
Deferred
<
R
>
=
this
.
async
(
coroutineContext
)
{
):
Deferred
<
R
>
=
this
.
async
(
coroutineContext
)
{
subscribingGet
(
timeoutMillis
,
filt
er
)
subscribingGet
(
timeoutMillis
,
mapp
er
)
}
}
\ 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