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
ee45bcb3
Commit
ee45bcb3
authored
Mar 21, 2020
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Revise `subscribingGet`, remove `coroutineContext` params
parent
cfd5946c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
39 deletions
+34
-39
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/linear.kt
...ore/src/commonMain/kotlin/net.mamoe.mirai/event/linear.kt
+34
-39
No files found.
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/linear.kt
View file @
ee45bcb3
...
@@ -29,11 +29,10 @@ import kotlin.coroutines.EmptyCoroutineContext
...
@@ -29,11 +29,10 @@ import kotlin.coroutines.EmptyCoroutineContext
@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
,
coroutineContext
:
CoroutineContext
=
EmptyCoroutineContext
,
noinline
mapper
:
suspend
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
,
coroutineContext
,
mapper
)
?:
error
(
"timeout subscribingGet"
)
return
subscribingGetOrNull
(
timeoutMillis
,
mapper
)
?:
error
(
"timeout subscribingGet"
)
}
}
/**
/**
...
@@ -50,52 +49,19 @@ suspend inline fun <reified E : Event, R : Any> subscribingGet(
...
@@ -50,52 +49,19 @@ suspend inline fun <reified E : Event, R : Any> subscribingGet(
@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
,
coroutineContext
:
CoroutineContext
=
EmptyCoroutineContext
,
noinline
mapper
:
suspend
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
resultThrowable
:
Throwable
?
=
null
if
(
timeoutMillis
==
-
1L
)
{
return
if
(
timeoutMillis
==
-
1L
)
{
@Suppress
(
"DuplicatedCode"
)
// for better performance
coroutineScope
{
coroutineScope
{
var
listener
:
Listener
<
E
>?
=
null
subscribingGetOrNullImpl
<
E
,
R
>(
this
,
mapper
)
listener
=
this
.
subscribe
(
coroutineContext
)
{
val
value
=
try
{
mapper
.
invoke
(
this
,
it
)
}
catch
(
e
:
Exception
)
{
resultThrowable
=
e
return
@
subscribe
ListeningStatus
.
STOPPED
.
also
{
listener
!!
.
complete
()
}
}
if
(
value
!=
null
)
{
result
=
value
return
@
subscribe
ListeningStatus
.
STOPPED
.
also
{
listener
!!
.
complete
()
}
}
else
return
@
subscribe
ListeningStatus
.
LISTENING
}
}
}
}
else
{
}
else
{
withTimeoutOrNull
(
timeoutMillis
)
{
withTimeoutOrNull
(
timeoutMillis
)
{
var
listener
:
Listener
<
E
>?
=
null
subscribingGetOrNullImpl
<
E
,
R
>(
this
,
mapper
)
@Suppress
(
"DuplicatedCode"
)
// for better performance
listener
=
this
.
subscribe
(
coroutineContext
)
{
val
value
=
try
{
mapper
.
invoke
(
this
,
it
)
}
catch
(
e
:
Exception
)
{
resultThrowable
=
e
return
@
subscribe
ListeningStatus
.
STOPPED
.
also
{
listener
!!
.
complete
()
}
}
if
(
value
!=
null
)
{
result
=
value
return
@
subscribe
ListeningStatus
.
STOPPED
.
also
{
listener
!!
.
complete
()
}
}
else
return
@
subscribe
ListeningStatus
.
LISTENING
}
}
}
}
}
resultThrowable
?.
let
{
throw
it
}
return
result
}
}
/**
/**
...
@@ -114,5 +80,34 @@ inline fun <reified E : Event, R : Any> CoroutineScope.subscribingGetAsync(
...
@@ -114,5 +80,34 @@ inline fun <reified E : Event, R : Any> CoroutineScope.subscribingGetAsync(
coroutineContext
:
CoroutineContext
=
EmptyCoroutineContext
,
coroutineContext
:
CoroutineContext
=
EmptyCoroutineContext
,
noinline
mapper
:
suspend
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
,
this
.
coroutineContext
,
mapper
)
subscribingGet
(
timeoutMillis
,
mapper
)
}
@PublishedApi
internal
suspend
inline
fun
<
reified
E
:
Event
,
R
>
subscribingGetOrNullImpl
(
coroutineScope
:
CoroutineScope
,
noinline
mapper
:
suspend
E
.(
E
)
->
R
?
):
R
{
var
result
:
Result
<
R
?
>
=
Result
.
success
(
null
)
// stub
var
listener
:
Listener
<
E
>?
=
null
@Suppress
(
"DuplicatedCode"
)
// for better performance
listener
=
coroutineScope
.
subscribe
{
val
value
=
try
{
mapper
.
invoke
(
this
,
it
)
}
catch
(
e
:
Exception
)
{
result
=
Result
.
failure
(
e
)
listener
!!
.
complete
()
return
@
subscribe
ListeningStatus
.
STOPPED
}
if
(
value
!=
null
)
{
result
=
Result
.
success
(
value
)
listener
!!
.
complete
()
return
@
subscribe
ListeningStatus
.
STOPPED
}
else
return
@
subscribe
ListeningStatus
.
LISTENING
}
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