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
47df80d3
Commit
47df80d3
authored
May 06, 2020
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement event priority for `syncFromEvent` and `nextEvent`
parent
15e6bb4b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
14 deletions
+23
-14
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/linear.kt
...ore/src/commonMain/kotlin/net.mamoe.mirai/event/linear.kt
+11
-6
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/nextEvent.kt
.../src/commonMain/kotlin/net.mamoe.mirai/event/nextEvent.kt
+12
-8
No files found.
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/linear.kt
View file @
47df80d3
...
@@ -33,17 +33,18 @@ import kotlin.reflect.KClass
...
@@ -33,17 +33,18 @@ import kotlin.reflect.KClass
@JvmSynthetic
@JvmSynthetic
suspend
inline
fun
<
reified
E
:
Event
,
R
:
Any
>
syncFromEvent
(
suspend
inline
fun
<
reified
E
:
Event
,
R
:
Any
>
syncFromEvent
(
timeoutMillis
:
Long
=
-
1
,
timeoutMillis
:
Long
=
-
1
,
priority
:
Listener
.
EventPriority
=
Listener
.
EventPriority
.
MONITOR
,
crossinline
mapper
:
suspend
E
.(
E
)
->
R
?
crossinline
mapper
:
suspend
E
.(
E
)
->
R
?
):
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
if
(
timeoutMillis
==
-
1L
)
{
return
if
(
timeoutMillis
==
-
1L
)
{
coroutineScope
{
coroutineScope
{
syncFromEventImpl
<
E
,
R
>(
E
::
class
,
this
,
mapper
)
syncFromEventImpl
<
E
,
R
>(
E
::
class
,
this
,
priority
,
mapper
)
}
}
}
else
{
}
else
{
withTimeout
(
timeoutMillis
)
{
withTimeout
(
timeoutMillis
)
{
syncFromEventImpl
<
E
,
R
>(
E
::
class
,
this
,
mapper
)
syncFromEventImpl
<
E
,
R
>(
E
::
class
,
this
,
priority
,
mapper
)
}
}
}
}
}
}
...
@@ -65,12 +66,13 @@ suspend inline fun <reified E : Event, R : Any> syncFromEvent(
...
@@ -65,12 +66,13 @@ suspend inline fun <reified E : Event, R : Any> syncFromEvent(
@JvmSynthetic
@JvmSynthetic
suspend
inline
fun
<
reified
E
:
Event
,
R
:
Any
>
syncFromEventOrNull
(
suspend
inline
fun
<
reified
E
:
Event
,
R
:
Any
>
syncFromEventOrNull
(
timeoutMillis
:
Long
,
timeoutMillis
:
Long
,
priority
:
Listener
.
EventPriority
=
Listener
.
EventPriority
.
MONITOR
,
crossinline
mapper
:
suspend
E
.(
E
)
->
R
?
crossinline
mapper
:
suspend
E
.(
E
)
->
R
?
):
R
?
{
):
R
?
{
require
(
timeoutMillis
>
0
)
{
"timeoutMillis must be > 0"
}
require
(
timeoutMillis
>
0
)
{
"timeoutMillis must be > 0"
}
return
withTimeoutOrNull
(
timeoutMillis
)
{
return
withTimeoutOrNull
(
timeoutMillis
)
{
syncFromEventImpl
<
E
,
R
>(
E
::
class
,
this
,
mapper
)
syncFromEventImpl
<
E
,
R
>(
E
::
class
,
this
,
priority
,
mapper
)
}
}
}
}
...
@@ -93,11 +95,12 @@ suspend inline fun <reified E : Event, R : Any> syncFromEventOrNull(
...
@@ -93,11 +95,12 @@ suspend inline fun <reified E : Event, R : Any> syncFromEventOrNull(
inline
fun
<
reified
E
:
Event
,
R
:
Any
>
CoroutineScope
.
asyncFromEventOrNull
(
inline
fun
<
reified
E
:
Event
,
R
:
Any
>
CoroutineScope
.
asyncFromEventOrNull
(
timeoutMillis
:
Long
,
timeoutMillis
:
Long
,
coroutineContext
:
CoroutineContext
=
EmptyCoroutineContext
,
coroutineContext
:
CoroutineContext
=
EmptyCoroutineContext
,
priority
:
Listener
.
EventPriority
=
Listener
.
EventPriority
.
MONITOR
,
crossinline
mapper
:
suspend
E
.(
E
)
->
R
?
crossinline
mapper
:
suspend
E
.(
E
)
->
R
?
):
Deferred
<
R
?
>
{
):
Deferred
<
R
?
>
{
require
(
timeoutMillis
==
-
1L
||
timeoutMillis
>
0
)
{
"timeoutMillis must be -1 or > 0"
}
require
(
timeoutMillis
==
-
1L
||
timeoutMillis
>
0
)
{
"timeoutMillis must be -1 or > 0"
}
return
this
.
async
(
coroutineContext
)
{
return
this
.
async
(
coroutineContext
)
{
syncFromEventOrNull
(
timeoutMillis
,
mapper
)
syncFromEventOrNull
(
timeoutMillis
,
priority
,
mapper
)
}
}
}
}
...
@@ -121,11 +124,12 @@ inline fun <reified E : Event, R : Any> CoroutineScope.asyncFromEventOrNull(
...
@@ -121,11 +124,12 @@ inline fun <reified E : Event, R : Any> CoroutineScope.asyncFromEventOrNull(
inline
fun
<
reified
E
:
Event
,
R
:
Any
>
CoroutineScope
.
asyncFromEvent
(
inline
fun
<
reified
E
:
Event
,
R
:
Any
>
CoroutineScope
.
asyncFromEvent
(
timeoutMillis
:
Long
=
-
1
,
timeoutMillis
:
Long
=
-
1
,
coroutineContext
:
CoroutineContext
=
EmptyCoroutineContext
,
coroutineContext
:
CoroutineContext
=
EmptyCoroutineContext
,
priority
:
Listener
.
EventPriority
=
Listener
.
EventPriority
.
MONITOR
,
crossinline
mapper
:
suspend
E
.(
E
)
->
R
?
crossinline
mapper
:
suspend
E
.(
E
)
->
R
?
):
Deferred
<
R
>
{
):
Deferred
<
R
>
{
require
(
timeoutMillis
==
-
1L
||
timeoutMillis
>
0
)
{
"timeoutMillis must be -1 or > 0"
}
require
(
timeoutMillis
==
-
1L
||
timeoutMillis
>
0
)
{
"timeoutMillis must be -1 or > 0"
}
return
this
.
async
(
coroutineContext
)
{
return
this
.
async
(
coroutineContext
)
{
syncFromEvent
(
timeoutMillis
,
mapper
)
syncFromEvent
(
timeoutMillis
,
priority
,
mapper
)
}
}
}
}
...
@@ -139,9 +143,10 @@ inline fun <reified E : Event, R : Any> CoroutineScope.asyncFromEvent(
...
@@ -139,9 +143,10 @@ inline fun <reified E : Event, R : Any> CoroutineScope.asyncFromEvent(
internal
suspend
inline
fun
<
E
:
Event
,
R
>
syncFromEventImpl
(
internal
suspend
inline
fun
<
E
:
Event
,
R
>
syncFromEventImpl
(
eventClass
:
KClass
<
E
>,
eventClass
:
KClass
<
E
>,
coroutineScope
:
CoroutineScope
,
coroutineScope
:
CoroutineScope
,
priority
:
Listener
.
EventPriority
,
crossinline
mapper
:
suspend
E
.(
E
)
->
R
?
crossinline
mapper
:
suspend
E
.(
E
)
->
R
?
):
R
=
suspendCancellableCoroutine
{
cont
->
):
R
=
suspendCancellableCoroutine
{
cont
->
coroutineScope
.
subscribe
(
eventClass
)
{
coroutineScope
.
subscribe
(
eventClass
,
priority
=
priority
)
{
try
{
try
{
cont
.
resumeWith
(
kotlin
.
runCatching
{
cont
.
resumeWith
(
kotlin
.
runCatching
{
mapper
.
invoke
(
this
,
it
)
?:
return
@
subscribe
ListeningStatus
.
LISTENING
mapper
.
invoke
(
this
,
it
)
?:
return
@
subscribe
ListeningStatus
.
LISTENING
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/nextEvent.kt
View file @
47df80d3
...
@@ -29,11 +29,12 @@ import kotlin.reflect.KClass
...
@@ -29,11 +29,12 @@ import kotlin.reflect.KClass
*/
*/
@JvmSynthetic
@JvmSynthetic
suspend
inline
fun
<
reified
E
:
Event
>
nextEvent
(
suspend
inline
fun
<
reified
E
:
Event
>
nextEvent
(
timeoutMillis
:
Long
=
-
1
timeoutMillis
:
Long
=
-
1
,
priority
:
Listener
.
EventPriority
=
Listener
.
EventPriority
.
MONITOR
):
E
{
):
E
{
require
(
timeoutMillis
==
-
1L
||
timeoutMillis
>
0
)
{
"timeoutMillis must be -1 or > 0"
}
require
(
timeoutMillis
==
-
1L
||
timeoutMillis
>
0
)
{
"timeoutMillis must be -1 or > 0"
}
return
withTimeoutOrCoroutineScope
(
timeoutMillis
)
{
return
withTimeoutOrCoroutineScope
(
timeoutMillis
)
{
nextEventImpl
(
E
::
class
,
this
)
nextEventImpl
(
E
::
class
,
this
,
priority
)
}
}
}
}
...
@@ -50,11 +51,12 @@ suspend inline fun <reified E : Event> nextEvent(
...
@@ -50,11 +51,12 @@ suspend inline fun <reified E : Event> nextEvent(
*/
*/
@JvmSynthetic
@JvmSynthetic
suspend
inline
fun
<
reified
E
:
BotEvent
>
Bot
.
nextEvent
(
suspend
inline
fun
<
reified
E
:
BotEvent
>
Bot
.
nextEvent
(
timeoutMillis
:
Long
=
-
1
timeoutMillis
:
Long
=
-
1
,
priority
:
Listener
.
EventPriority
=
Listener
.
EventPriority
.
MONITOR
):
E
{
):
E
{
require
(
timeoutMillis
==
-
1L
||
timeoutMillis
>
0
)
{
"timeoutMillis must be -1 or > 0"
}
require
(
timeoutMillis
==
-
1L
||
timeoutMillis
>
0
)
{
"timeoutMillis must be -1 or > 0"
}
return
withTimeoutOrCoroutineScope
(
timeoutMillis
)
{
return
withTimeoutOrCoroutineScope
(
timeoutMillis
)
{
nextBotEventImpl
(
this
@
nextEvent
,
E
::
class
,
this
)
nextBotEventImpl
(
this
@
nextEvent
,
E
::
class
,
this
,
priority
)
}
}
}
}
...
@@ -63,9 +65,10 @@ suspend inline fun <reified E : BotEvent> Bot.nextEvent(
...
@@ -63,9 +65,10 @@ suspend inline fun <reified E : BotEvent> Bot.nextEvent(
@PublishedApi
@PublishedApi
internal
suspend
inline
fun
<
E
:
Event
>
nextEventImpl
(
internal
suspend
inline
fun
<
E
:
Event
>
nextEventImpl
(
eventClass
:
KClass
<
E
>,
eventClass
:
KClass
<
E
>,
coroutineScope
:
CoroutineScope
coroutineScope
:
CoroutineScope
,
priority
:
Listener
.
EventPriority
):
E
=
suspendCancellableCoroutine
{
cont
->
):
E
=
suspendCancellableCoroutine
{
cont
->
coroutineScope
.
subscribe
(
eventClass
)
{
coroutineScope
.
subscribe
(
eventClass
,
priority
=
priority
)
{
try
{
try
{
cont
.
resume
(
this
)
cont
.
resume
(
this
)
}
catch
(
e
:
Exception
)
{
}
catch
(
e
:
Exception
)
{
...
@@ -79,9 +82,10 @@ internal suspend inline fun <E : Event> nextEventImpl(
...
@@ -79,9 +82,10 @@ internal suspend inline fun <E : Event> nextEventImpl(
internal
suspend
inline
fun
<
E
:
BotEvent
>
nextBotEventImpl
(
internal
suspend
inline
fun
<
E
:
BotEvent
>
nextBotEventImpl
(
bot
:
Bot
,
bot
:
Bot
,
eventClass
:
KClass
<
E
>,
eventClass
:
KClass
<
E
>,
coroutineScope
:
CoroutineScope
coroutineScope
:
CoroutineScope
,
priority
:
Listener
.
EventPriority
):
E
=
suspendCancellableCoroutine
{
cont
->
):
E
=
suspendCancellableCoroutine
{
cont
->
coroutineScope
.
subscribe
(
eventClass
)
{
coroutineScope
.
subscribe
(
eventClass
,
priority
=
priority
)
{
try
{
try
{
if
(
this
.
bot
==
bot
)
cont
.
resume
(
this
)
if
(
this
.
bot
==
bot
)
cont
.
resume
(
this
)
}
catch
(
e
:
Exception
)
{
}
catch
(
e
:
Exception
)
{
...
...
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