Skip to content

Commit

Permalink
Merge pull request #23 from Maksimka101/fix/web
Browse files Browse the repository at this point in the history
Fix: `Combine.spawn` on web
  • Loading branch information
Maksimka101 authored Aug 30, 2023
2 parents dcd7d09 + 1409455 commit 505b75e
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 0.5.6
- Fix the `Combine.spawn` method on web.

## 0.5.5
- Fix the `BackgroundIsolateBinaryMessenger` initialization.

Expand Down
18 changes: 9 additions & 9 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ packages:
dependency: transitive
description:
name: collection
sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a
sha256: f092b211a4319e98e5ff58223576de6c2803db36221657b46c82574721240687
url: "https://pub.dev"
source: hosted
version: "1.18.0"
version: "1.17.2"
combine:
dependency: "direct main"
description:
path: ".."
relative: true
source: path
version: "0.5.5"
version: "0.5.6"
cupertino_icons:
dependency: "direct main"
description:
Expand Down Expand Up @@ -264,18 +264,18 @@ packages:
dependency: transitive
description:
name: stack_trace
sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b"
sha256: c3c7d8edb15bee7f0f74debd4b9c5f3c2ea86766fe4178eb2a18eb30a0bdaed5
url: "https://pub.dev"
source: hosted
version: "1.11.1"
version: "1.11.0"
stream_channel:
dependency: transitive
description:
name: stream_channel
sha256: ba2aa5d8cc609d96bbb2899c28934f9e1af5cddbd60a827822ea467161eb54e7
sha256: "83615bee9045c1d322bbbd1ba209b7a749c2cbcdcb3fdd1df8eb488b3279c1c8"
url: "https://pub.dev"
source: hosted
version: "2.1.2"
version: "2.1.1"
string_scanner:
dependency: transitive
description:
Expand All @@ -296,10 +296,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b"
sha256: "75760ffd7786fffdfb9597c35c5b27eaeec82be8edfb6d71d32651128ed7aab8"
url: "https://pub.dev"
source: hosted
version: "0.6.1"
version: "0.6.0"
vector_math:
dependency: transitive
description:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ class IsolatedBinaryMessengerMiddleware extends BinaryMessenger {
ByteData? data,
ui.PlatformMessageResponseCallback? callback,
) async {
await _binaryMessenger.handlePlatformMessage(channel, data, callback);
ui.channelBuffers.push(channel, data, (data) {
if (callback != null) {
callback(data);
}
});
}

@override
Expand Down
3 changes: 2 additions & 1 deletion lib/src/combine_singleton.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:combine/src/combine_worker_singleton.dart';
import 'package:combine/src/isolate_factory/effective_isolate_factory.dart';
import 'package:combine/src/isolate_factory/isolate_factory.dart';
import 'package:combine/src/isolate_messenger/isolate_messenger.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

/// {@template combine_singleton}
Expand Down Expand Up @@ -86,7 +87,7 @@ class Combine {
argument: argument,
errorsAreFatal: errorsAreFatal,
debugName: debugName,
isolateToken: isolateToken ?? RootIsolateToken.instance,
isolateToken: isolateToken ?? getRootIsolateToken(isWeb: kIsWeb),
);
}
}
5 changes: 5 additions & 0 deletions lib/src/isolate_factory/effective_isolate_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:combine/src/isolate_factory/isolate_factory.dart';
import 'package:combine/src/isolate_factory/native_isolate_factory.dart'
if (dart.library.html) 'package:combine/src/isolate_factory/web_isolate_factory.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

@visibleForTesting
void setTestIsolateFactory(IsolateFactory isolateFactory) {
Expand All @@ -19,3 +20,7 @@ IsolateFactory? _testIsolateFactory;
IsolateFactory get effectiveIsolateFactory {
return _testIsolateFactory ?? IsolateFactoryImpl();
}

RootIsolateToken? getRootIsolateToken({required bool isWeb}) {
return isWeb ? null : RootIsolateToken.instance;
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: combine
description: A Flutter package which allows you to work with
MethodChannels in Isolate and provides simplified Isolate and Thread Pool API.
version: 0.5.5
version: 0.5.6
homepage: https://github.com/Maksimka101/combine
repository: https://github.com/Maksimka101/combine

Expand Down
50 changes: 50 additions & 0 deletions test/combine_test.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import 'package:combine/src/binary_messenger_middleware/ui_binary_messenger_middleware.dart';
import 'package:combine/src/combine_singleton.dart';
import 'package:combine/src/isolate_factory/effective_isolate_factory.dart';
import 'package:combine/src/isolate_factory/native_isolate_factory.dart';
import 'package:combine/src/isolate_factory/web_isolate_factory.dart';
import 'package:combine/src/isolate_messenger/internal_isolate_messenger/internal_isolate_messenger.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';

import 'combine_spawners/arguments_resend_combine_spawner.dart';
import 'combine_spawners/counter_combine_spawners.dart';
import 'mocks/mock_isolate_factory.dart';

void main() {
setUpAll(() {
Expand Down Expand Up @@ -55,6 +59,52 @@ void main() {
);
},
);

test(
"'Combine.spawn' calls 'effectiveIsolateFactory.create' correctly",
() async {
final isolateFactory = MockIsolateFactory();
registerFallbackValue(FakeRootIsolateToken());
setTestIsolateFactory(isolateFactory);

void entryPoint(_) {}
const errorsAreFatal = false;
const debugName = 'debugName';

when(
() => isolateFactory.create(
entryPoint,
argument: debugName,
debugName: debugName,
errorsAreFatal: errorsAreFatal,
isolateToken: any(named: "isolateToken"),
),
).thenAnswer((_) async => FakeCombineInfo());

await Combine().spawn(
entryPoint,
errorsAreFatal: errorsAreFatal,
debugName: debugName,
argument: debugName,
// Don't pass isolate token because this test tests isolate token creation.
// isolateToken:
);

verify(
() => isolateFactory.create(
entryPoint,
errorsAreFatal: errorsAreFatal,
debugName: debugName,
argument: debugName,
isolateToken: getRootIsolateToken(isWeb: kIsWeb),
),
).called(1);
},
);

test("'getRootIsolateToken' returns null on web", () {
expect(getRootIsolateToken(isWeb: true), isNull);
});
}

void commonCombineTest() {
Expand Down
13 changes: 13 additions & 0 deletions test/mocks/mock_isolate_factory.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:combine/src/combine_info.dart';
import 'package:combine/src/combine_isolate/combine_isolate.dart';
import 'package:combine/src/isolate_factory/isolate_factory.dart';
import 'package:flutter/services.dart';
import 'package:mocktail/mocktail.dart';

class MockIsolateFactory extends Mock implements IsolateFactory {}

class FakeCombineIsolate extends Fake implements CombineIsolate {}

class FakeCombineInfo extends Fake implements CombineInfo {}

class FakeRootIsolateToken extends Fake implements RootIsolateToken {}

0 comments on commit 505b75e

Please sign in to comment.