-
Notifications
You must be signed in to change notification settings - Fork 113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(ferry): add IsolateClient #405
Merged
Merged
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
292170e
feat(ferry): add IsolateClient
b871dff
chore(pokemon_explorer): format
4005856
feat(ferry): add more methods to IsolateClient
0fc2a6f
feat(ferry): refactor IsolateClient, add more commands
d5363b9
feat(ferry): more IsolateClient features
1b15ae8
chore(ferry): format
8376820
feat(ferry): refactor single response commands
086c831
docs(ferry): add documentation for IsolateClient in website
2e53420
test(ferry): add tests
e701d3d
chore(ferry): format
7972d25
refactor(ferry): cleanup request response communication
c96eaa9
chore(ferry): format
e29c6df
refactor(ferry): refactore handleCommand
d58ebef
refactor(ferry): refactore handleCommand
685db93
docs(ferry): add example for token refresh over isolate
4458c88
refactor(ferry): make params map non null in pokemon example
4d5b490
chore(ferry): format
ccc13ed
chore(ferry): remove ignored lint, add unawaited
944d1b1
refactor(ferry): use a generic type parameter for the init params ins…
25cb800
docs(ferry): mention isolates in README
d481ebb
chore(release): publish packages
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import 'package:ferry/ferry.dart'; | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:get_it/get_it.dart'; | ||
import 'package:pokemon_explorer/src/app.dart'; | ||
|
||
import 'src/client_isolate.dart'; | ||
|
||
// example for running the client in a separate isolate | ||
void main() async { | ||
WidgetsFlutterBinding.ensureInitialized(); | ||
final client = await initIsolateClient(); | ||
GetIt.I.registerLazySingleton<TypedLink>(() => client); | ||
runApp(App()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import 'dart:isolate'; | ||
import 'dart:ui'; | ||
|
||
import 'package:ferry/ferry.dart'; | ||
import 'package:ferry/ferry_isolate.dart'; | ||
import 'package:ferry_hive_store/ferry_hive_store.dart'; | ||
import 'package:gql_http_link/gql_http_link.dart'; | ||
import 'package:hive_flutter/hive_flutter.dart'; | ||
import 'package:path_provider/path_provider.dart'; | ||
import 'package:pokemon_explorer/main.dart'; | ||
|
||
Future<IsolateClient> initIsolateClient() async { | ||
final client = IsolateClient.create(_initClientIsolate, params: { | ||
'hivePath': (await getApplicationDocumentsDirectory()).path, | ||
'url': apiUrl, | ||
}); | ||
|
||
return client; | ||
} | ||
|
||
// this is called on the new isolate | ||
// if you passed a messageHandler to the IsolateClient, you can use the sendPort | ||
// to send arbitrary messages to the main isolate. | ||
Future<Client> _initClientIsolate(Map<String, dynamic>? params, SendPort? sendPort) async { | ||
// don't use Hive.initFlutter to avoid dealing with method channels in the isolate | ||
// instead, call getApplicationDocumentsDirectory() on the main isolate | ||
// and pass the result to the ferry isolate | ||
Hive.init(params!["hivePath"]); | ||
|
||
final box = await Hive.openBox<Map<String, dynamic>>("graphql"); | ||
|
||
await box.clear(); | ||
|
||
final store = HiveStore(box); | ||
|
||
final cache = Cache(store: store); | ||
|
||
final link = HttpLink(params["url"]); | ||
|
||
final client = Client( | ||
link: link, | ||
cache: cache, | ||
); | ||
|
||
return client; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may as well make params
required
if we expect a hivePath and a url.Actually, it should be a class rather than a Map.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to Map to be non-nullable now.
Good point. Currently, the signature of the function that creates a the client on the new Isolate must be
do you think we should make it generic, like
and change the create() method of the IsolateClient to
This would be typesafe, which is definitely a big plus.
However, it could lead to users trying the send objects which cannot be serialized over isolates (like SharedPreferences, HiveBoxes, Closures... basically anything that is not just data), while Map<String, dynamic> is already typically used for json-like data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, Isolate.spawn is generic so I'd say it's fair to do the same.
We can always add an API comment with
///
to say "Make sure InitParams is serializable as it will be passed to the isolate".There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True. I'll update it.