reference 0.0.2  reference: ^0.0.2 copied to clipboard
reference: ^0.0.2 copied to clipboard
A library for building Flutter plugins that want to maintain access to object instances on separate threads/processes.
reference #
A library for building Flutter plugins that want to maintain access to object instances on separate threads/processes.
Overview #
This library works by managing pairs of references. Each pair consists of a LocalReference and a
RemoteReference. The pairs are stored and managed in a ReferencePairManager. Here are the basic
definitions of these classes:
- LocalReference - represents an object on the same thread/process.
- RemoteReference - represents an object on a different thread/process.
- ReferencePairManager - manages communication between objects represented by LocalReferences andRemoteReferences.
A LocalReference and RemoteReference pair is also maintained by a pair of
ReferencePairManagers. One ReferencePairManager is on the same thread/process as the object that
LocalReference represents and another ReferencePairManager is on the same thread/process as the
object that RemoteReference represents.
The labels of local and remote are relative to which thread one is on. A RemoteReference in a
ReferencePairManager will represent a LocalReference in another ReferencePairManager and vice
versa. This is shown in the diagram below:
 
It’s also important to note that the RemoteReference in both ReferencePairManagers are
considered equivalent values, so they can be used to identify paired LocalReferences.
For every reference pair, the ReferencePairManager’s role is to handle communication between the
objects represented by LocalReference and RemoteReference.
How ReferencePairManager Handles Communication #
ReferencePairManagers are responsible for creating pairs, disposing pairs, and calling methods on
paired References. Here are the relevant classes:
- TypeReference - represents a type. This type must be able to be represented by a
LocalReference.
- RemoteReferenceCommunicationHandler - handles communication with RemoteReferences for aReferencePairManager. This class communicates with otherReferencePairManagers to create, dispose, or execute methods onRemoteReferences.
- LocalReferenceCommunicationHandler - handles communication with LocalReferences for aReferencePairManager. This class handles communication from otherReferencePairManagers to create, dispose, or execute methods for aLocalReference.
Below is the typical flow for either creating a pair, disposing of a pair, or a LocalReference
calling a method on it’s paired RemoteReference. A detailed example follows.
 
To give a more detailed explanation, let’s assume we want to create a new pair. It’s also important
to remember that what is considered a LocalReference to one ReferencePairManager, is considered
a RemoteReference to another.
- An instance of LocalReferenceis created.
- LocalReferencetells ReferencePairManager1 to create a- RemoteReference.
- ReferencePairManager1 creates a RemoteReferenceand stores theRemoteReferenceand theLocalReferenceas a pair.
- ReferencePairManager1 tells its RemoteReferenceCommunicationHandlerto communicate with anotherReferencePairManagerto create aRemoteReferencefor the instance ofLocalReference.
- RemoteReferenceCommunicationHandlertells ReferencePairManager2 to make a- RemoteReference.
- ReferencePairManager2 tells its LocalReferenceCommunicationHandlerto create aLocalReference.
- LocalReferenceCommunicationHandlercreates and returns a- LocalReference.
- ReferencePairManager2 stores the RemoteReferenceand theLocalReferencefromLocalReferenceCommunicationHandleras a pair.
How ReferencePairManager Handles Arguments #
When creating a RemoteReference or executing a method, you have the option to pass arguments as
well. Before a ReferencePairManager hands off arguments to a
RemoteReferenceCommunicationHandler, it converts all LocalReferences to RemoteReferences and
before a ReferencePairManager hands off arguments to a LocalReferenceCommunicationHandler it
converts all RemoteReferences to LocalReferences.
Getting Started #
This project is a starting point for a Flutter plug-in package, a specialized package that includes platform-specific implementation code for Android and/or iOS.
For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.
To use this with your own plugin, you will have to extend ReferencePairManager and implement
RemoteReferenceCommunicationHandler and LocalReferenceCommunicationHandler. This needs to be
done in Dart and then on every platform that is wanted to be supported. (e.g. Java/Kotlin for
Android or Obj-C/Swift for iOS. This plugin allows you to use any system for IPC (e.g.
MethodChannel or dart:ffi), but it also provides a MethodChannelReferencePairManager that is a
partial implementation using MethodChannels. Here are the latest example implementations for
Dart
and Java.