open method
Future<MeshDocument>
open(
- String path, {
- bool create = true,
- Map<
String, dynamic> ? initialJson, - MeshSchema? schema,
Implementation
Future<MeshDocument> open(String path, {bool create = true, Map<String, dynamic>? initialJson, MeshSchema? schema}) async {
final normalizedPath = _normalizeSyncPath(path);
final closing = _closingDocuments[normalizedPath];
if (closing != null) {
await closing;
}
final pending = _connectingDocuments[normalizedPath];
if (pending != null) {
await pending;
}
if (_connectedDocuments[normalizedPath] != null) {
final connectedDoc = _connectedDocuments[normalizedPath];
connectedDoc!.count++;
return connectedDoc.ref;
}
final c = Completer<_RefCount<MeshDocument>>();
_connectingDocuments[normalizedPath] = c.future;
try {
final config = _SyncOpenDocumentConfig(create: create, schemaJson: schema?.toJson(), schemaPath: null);
final openResult = await _openStream(path: normalizedPath, config: config, vector: null, initialJson: initialJson);
schema = MeshSchema.fromJson(openResult.stateHeaders.schemaJson);
final doc = MeshDocument(
schema: schema,
sendChangesToBackend: (base64) {
final currentStream = _documentStreams[normalizedPath];
if (currentStream == null) {
_roomClientLogger.fine('dropping sync for disconnected document stream $normalizedPath');
return;
}
try {
currentStream.queueSync(Uint8List.fromList(utf8.encode(base64)));
} catch (error, stackTrace) {
_roomClientLogger.log(Level.FINE, 'dropping sync for closed document stream $normalizedPath', error, stackTrace);
}
},
);
final rc = _RefCount(doc);
_connectedDocuments[normalizedPath] = rc;
_documentConfigs[normalizedPath] = config;
_documentStreams[normalizedPath] = openResult.streamState;
_reconnectBaseVectors.remove(normalizedPath);
_applySyncPayload(rc, openResult.firstChunk.data);
_attachStreamConsumer(path: normalizedPath, doc: rc, streamState: openResult.streamState, iterator: openResult.iterator);
notifyListeners();
c.complete(rc);
await doc.synchronized;
return doc;
} catch (err) {
c.completeError(err);
rethrow;
} finally {
_connectingDocuments.remove(normalizedPath);
}
}