рд╣реЗрд▓реЛ, рд╣реЗрдмреНрд░! рдореИрдВ рдЖрдкрдХреЗ рд▓рд┐рдП рдбрд┐рдбрд┐рдПрд░ рдмреЛрдПрд▓реЗрдиреНрд╕ рдХреЗ рд▓реЗрдЦ "рдлреНрдпреВрдЪрд░реНрд╕ - рдЖрдЗрд╕реЛрд▓реЗрдЯреНрд╕ - рдЗрд╡реЗрдВрдЯ рд▓реВрдк" рдХрд╛ рдЕрдиреБрд╡рд╛рдж рдЕрддреБрд▓реНрдпрдХрд╛рд▓рд┐рдХ рдФрд░ Dart
( Flutter
рд╡рд┐рд╢реЗрд╖ рд░реВрдк рд╕реЗ) рдореЗрдВ рдФрд░ рдмрд╣реБ-рдкреНрд░рд╕рд╛рд░ рдкрд░ рдкреНрд░рд╕реНрддреБрдд рдХрд░рддрд╛ рд╣реВрдВ ред
TLDR: , . Event Loop, Future async/await (, JavaScript), otlin, , . , .
рдФрд░ рд╢рд░реНрддреЛрдВ рдХреЗ рдЕрдиреБрд╡рд╛рдж рдХреЗ рдмрд╛рд░реЗ рдореЗрдВред рдмреЛрд▓реА рдЬрд╛рдиреЗ рд╡рд╛рд▓реА рднрд╛рд╖рд╛ рдореЗрдВрд╣рдо рдореИрдВ рдХрд╣рддрд╛ рд╣реВрдВ рдХрд┐ "рдзрд╛рд░рд╛", "рд╡реНрдпрд╛рдкрд╛рд░", "рд╡рд╛рдпрджрд╛", рдЖрджрд┐, рдЬрдмрдХрд┐ рдкреНрд░рд┐рдВрдЯ рдореЗрдВ рдпреЗ рд╢рдмреНрдж рдЕрдЬреАрдм рдФрд░ рдЕрдирд╛рдбрд╝реА рд▓рдЧрддреЗ рд╣реИрдВред
рдЙрд╕реА рд╕рдордп, рдпрд╣ Stream
рдПрдХ рд╕реНрдЯреНрд░реАрдо рд╣реИ, рдФрд░ Thread
- рдПрдХ рд╕реНрдЯреНрд░реАрдоред рдЗрд╕ рдорд╛рдорд▓реЗ рдореЗрдВ рд╕рдВрджрд░реНрдн рд╣рдореЗрд╢рд╛ рдирд╣реАрдВ рдмрдЪрд╛ рд╣реИред рд╡рд╣реА рдЖрдЗрд╕реЛрд▓реЗрдЯ, рдореЗрд░реЗ рд╕реНрд╡рд╛рдж рдХреЗ рд▓рд┐рдП, рд░реВрд╕реА рдореЗрдВ рд╕рд╛рдорд╛рдиреНрдп рд▓рдЧрддрд╛ рд╣реИ, рдЗрд╕рд▓рд┐рдП рдХрд╣реАрдВ рдореИрдВ рд╕рд┐рд░рд┐рд▓рд┐рдХ рдореЗрдВ рд╡рд░реНрддрдиреА рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рддрд╛ рд╣реВрдВ, рдФрд░ рдХрд╣реАрдВ рдореВрд▓ рдореЗрдВред
рдпрд╣рд╛рдБ рдФрд░ рд╡рд╣рд╛рдБ рдХреЛрд╖реНрдардХ рдореЗрдВ рд░реВрд╕реА рд╢рдмреНрджреЛрдВ рдХреЗ рдмрд╛рдж рдЙрдирдХреЗ рдЕрдВрдЧреНрд░реЗрдЬреА рдореВрд▓, рдХрднреА-рдХрднреА рдЗрд╕рдХреЗ рд╡рд┐рдкрд░реАрддред
рдкрд░рд┐рдЪрдп
рд╣рд╛рд▓ рд╣реА рдореЗрдВ, рдореИрдВ рдЕрдХреНрд╕рд░ рдХреА рдЕрд╡рдзрд╛рд░рдгрд╛рдУрдВ рд╕реЗ рд╕рдВрдмрдВрдзрд┐рдд рдкреНрд░рд╢реНрдиреЛрдВ рдорд┐рд▓ Future
, async
, await
, Isolate
рдФрд░ рдХреЛрдб рдХреЗ рд╕рдорд╛рдирд╛рдВрддрд░ рдирд┐рд╖реНрдкрд╛рджрдиред
рдЗрд╕рдХреЗ рдЕрд▓рд╛рд╡рд╛, рдХреБрдЫ рдбреЗрд╡рд▓рдкрд░реНрд╕ рдХреЛ рдЕрдкрдиреЗ рдХреЛрдб рдХреЗ рдирд┐рд╖реНрдкрд╛рджрди рдХреЗ рдЕрдиреБрдХреНрд░рдо рдХреЛ рд╕рдордЭрдиреЗ рдореЗрдВ рд╕рдорд╕реНрдпрд╛рдПрдВ рд╣реИрдВред
рдореИрдВрдиреЗ рдЕрд╡рд╕рд░ рдХреЛ рдЬрдмреНрдд рдХрд░рдиреЗ рдФрд░ рдЗрди рдореБрджреНрджреЛрдВ рдкрд░ рдПрдХ рд▓реЗрдЦ рд╕рдорд░реНрдкрд┐рдд рдХрд░рдиреЗ рдХрд╛ рдлреИрд╕рд▓рд╛ рдХрд┐рдпрд╛ред рдПрдХ рд╣реА рд╕рдордп рдореЗрдВ, рдореИрдВ рдХреА рдЕрд╡рдзрд╛рд░рдгрд╛рдУрдВ рдХреЗ рд╕рд╛рде рдореБрдЦреНрдп рд░реВрдк рд╕реЗ рдЬреБрдбрд╝реЗ рднреНрд░рдо рд╕реЗ рдЫреБрдЯрдХрд╛рд░рд╛ рдкрд╛рдиреЗ рдХреА рдХреЛрд╢рд┐рд╢ рдХрд░реЗрдВрдЧреЗ
рдФрд░
ред
Dart
- рдПрдХрд▓ рдкрд┐рд░реЛрдпрд╛ рднрд╛рд╖рд╛
, Dart
тАФ Flutter
Dart
.
Dart
. . , .
, ( ), , .
void myBigLoop(){
for (int i = 0; i < 1000000; i++){
_doSomethingSynchronously();
}
}
, myBigLoop()
. , - ,
, .
Dart
Dart
, , Event Loop
.
Flutter
- ( Dart
-), тАФ Thread
, тАФ
(Isolate
). , .
, :
- (
Queues
) MicroTask
()
Event
(), FIFO
(.: first in first out, .. ,
, ), main()
,Event Loop
( )
,
Event Loop
, : MicroTask
Event
.
Event Loop
"" ,
"". ,
Dart
-, "" :
void eventLoop(){
while (microTaskQueue.isNotEmpty){
fetchFirstMicroTaskFromQueue();
executeThisMicroTask();
return;
}
if (eventQueue.isNotEmpty){
fetchFirstEventFromQueue();
executeThisEventRelatedCode();
}
}
, MicroTask
Event
. ?
MicroTask
,
, - , Event Loop
.
. - , - :
MyResource myResource;
...
void closeAndRelease() {
scheduleMicroTask(_dispose);
_close();
}
void _close(){
// ,
//
...
}
void _dispose(){
// ,
// , _close()
//
}
-, , . ,
scheduleMicroTask()
7 . Event
.
Event
, :
, , Event
.
, MicroTask
, Event Loop
Event
.
, Futures
Event
.
Futures
Future
,
( ) - .
, Future
:
- ,
Dart
- ,
Future
,
Event
Future
(incomplete)- , (
Future
)
, Future
Event
,
Event Loop
.
( ) then()
catchError()
Future
.
, :
void main(){
print('Before the Future');
Future((){
print('Running the Future');
}).then((_){
print('Future is complete');
});
print('After the Future');
}
, :
Before the Future
After the Future
Running the Future
Future is complete
:
print('Before the Future')
(){print('Running the Future');}
Event
print('After the Future')
Event Loop
2- ,
then()
:
Future
, , Event Loop
(async)
async
, Dart
, :
- тАФ
Future
- ,
await
, Future
, await
, await
, [ Future], .
Event Loop
...
,
,
void main() async {
methodA();
await methodB();
await methodC('main');
methodD();
}
methodA(){
print('A');
}
methodB() async {
print('B start');
await methodC('B');
print('B end');
}
methodC(String from) async {
print('C start from $from');
Future((){ // <== -
print('C running Future from $from');
}).then((_){
print('C end of Future from $from');
});
print('C end from $from');
}
methodD(){
print('D');
}
:
A
B start
C start from B
C end from B
B end
C start from main
C end from main
D
C running Future from B
C end of Future from B
C running Future from main
C end of Future from main
, methodC()
. , , .
, methodD()
, :
void main() async {
methodA();
await methodB();
await methodC('main');
methodD();
}
methodA(){
print('A');
}
methodB() async {
print('B start');
await methodC('B');
print('B end');
}
methodC(String from) async {
print('C start from $from');
await Future((){ // <==
print('C running Future from $from');
}).then((_){
print('C end of Future from $from');
});
print('C end from $from');
}
methodD(){
print('D');
}
:
A
B start
C start from B
C running Future from B
C end of Future from B
C end from B
B end
C start from main
C running Future from main
C end of Future from main
C end from main
D
, await
Future
methodC()
.
:
, , Event Loop
. . method1
тАФ method2
? ?
void method1(){
List<String> myArray = <String>['a','b','c'];
print('before loop');
myArray.forEach((String value) async {
await delayedPrint(value);
});
print('end of loop');
}
void method2() async {
List<String> myArray = <String>['a','b','c'];
print('before loop');
for(int i=0; i<myArray.length; i++) {
await delayedPrint(myArray[i]);
}
print('end of loop');
}
Future<void> delayedPrint(String value) async {
await Future.delayed(Duration(seconds: 1));
print('delayedPrint: $value');
}
: .
, method1
forEach()
. , async
( Future
). await
, Event
. , : print('end of loop')
. Event Loop
3 .
method2
, ( ) тАФ .
, ,
Event Loop
...
, Flutter
? ?
, Isolate
().
, Isolate
Dart
Thread
().
Thread
.
Flutter
.
.
Event Loop
.
(MicroTask
Event
).
.
Dart
.
, .
1.
() API, Dart
.
1.1. 1:
,
, , "" ""
.
, ,
. SendPort
( , , /, ).
, "" " " , "". :
//
//
//
//
//
SendPort newIsolateSendPort;
//
//
//
Isolate newIsolate;
//
// ,
//
//
void callerCreateIsolate() async {
//
// ReceivePort
// SendPort
//
ReceivePort receivePort = ReceivePort();
//
//
//
newIsolate = await Isolate.spawn(
callbackFunction,
receivePort.sendPort,
);
//
// ""
//
newIsolateSendPort = await receivePort.first;
}
//
//
//
static void callbackFunction(SendPort callerSendPort){
//
// SendPort
// ""
//
ReceivePort newIsolateReceivePort = ReceivePort();
//
// "" SendPort
//
callerSendPort.send(newIsolateReceivePort.sendPort);
//
//
//
}
:
1.2. 2:
,
, :
//
// ,
//
//
// ""
// String ( )
//
Future<String> sendReceive(String messageToBeSent) async {
//
//
//
ReceivePort port = ReceivePort();
//
// ,
// ,
//
//
newIsolateSendPort.send(
CrossIsolatesMessage<String>(
sender: port.sendPort,
message: messageToBeSent,
)
);
//
//
//
return port.first;
}
//
// Callback-
//
static void callbackFunction(SendPort callerSendPort){
//
// SendPort
//
//
ReceivePort newIsolateReceivePort = ReceivePort();
//
// "" SendPort
//
callerSendPort.send(newIsolateReceivePort.sendPort);
//
// , ,
//
//
newIsolateReceivePort.listen((dynamic message){
CrossIsolatesMessage incomingMessage = message as CrossIsolatesMessage;
//
//
//
String newMessage = "complemented string " + incomingMessage.message;
//
//
//
incomingMessage.sender.send(newMessage);
});
}
//
//
//
class CrossIsolatesMessage<T> {
final SendPort sender;
final T message;
CrossIsolatesMessage({
@required this.sender,
this.message,
});
}
1.3. 3:
, :
void dispose(){
newIsolate?.kill(priority: Isolate.immediate);
newIsolate = null;
}
1.4. тАФ Stream
, ""
"" (Streams). Single-Listener Stream
( ).
2. (One-shot computation)
,
, Dart
compute
,
: ( ).
3.
, ,
(Platform-Channel communication)
(main isolate).
, .
,
,
.
Future
, :
, (user experiences lags), .
, :
- ,
( ) - ,
, тАФ Event Loop
Futures
- ,
, тАФ
, , , Futures
( async
-) тАФ Futures
Event Loop
"" . , ( - , ).
, Future
тАФ , .
:
- JSON, (HttpRequest),
=> compute
- : =>
- ( ..) =>
- :
, , .
, , Event Loop
.
рдпрд╣ рднреА рдорд╣рддреНрд╡рдкреВрд░реНрдг рд╣реИ рдХрд┐ рдЗрд╕реЗ рди рднреВрд▓реЗрдВ Flutter
( Dart
) рд╕рд┐рдВрдЧрд▓-рдереНрд░реЗрдбреЗрдб рд╣реИ, рдЗрд╕рд▓рд┐рдП, рдЙрдкрдпреЛрдЧрдХрд░реНрддрд╛рдУрдВ рдХреЛ рд╕рдВрддреБрд╖реНрдЯ рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП, рдбреЗрд╡рд▓рдкрд░реНрд╕ рдХреЛ рдпрд╣ рд╕реБрдирд┐рд╢реНрдЪрд┐рдд рдХрд░рдирд╛ рдЪрд╛рд╣рд┐рдП рдХрд┐ рдПрдкреНрд▓рд┐рдХреЗрд╢рди рдпрдерд╛рд╕рдВрднрд╡ рдЖрд╕рд╛рдиреА рд╕реЗ рдХрд╛рдо рдХрд░реЗрдЧрд╛ред Futures
рдФрд░
- рдЗрд╕ рд▓рдХреНрд╖реНрдп рдХреЛ рдкреНрд░рд╛рдкреНрдд рдХрд░рдиреЗ рдореЗрдВ рдорджрдж рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рд╢рдХреНрддрд┐рд╢рд╛рд▓реА рдЙрдкрдХрд░рдгред
рд╕реМрднрд╛рдЧреНрдп