open method

Future<MeshDocument> open(
  1. String path, {
  2. bool create = true,
  3. Map<String, dynamic>? initialJson,
  4. MeshSchema? schema,
})

Implementation

Future<MeshDocument> open(String path, {bool create = true, Map<String, dynamic>? initialJson, MeshSchema? schema}) async {
  final normalizedPath = _normalizeSyncPath(path);
  final pending = _connectingDocuments[normalizedPath];

  if (pending != null) {
    await pending;
  }

  if (_connectedDocuments[normalizedPath] != null) {
    final connectedDoc = _connectedDocuments[normalizedPath];
    connectedDoc!.count++;
    return connectedDoc.ref;
  }

  // todo: add support for state vector / partial updates
  // todo: initial bytes loading

  final c = Completer<_RefCount<MeshDocument>>();
  _connectingDocuments[normalizedPath] = c.future;
  try {
    final result =
        (await room.sendRequest("room.connect", {
              "path": normalizedPath,
              "create": create,
              "initial_json": initialJson,
              "schema": schema?.toJson(),
            }))
            as JsonContent;

    schema = MeshSchema.fromJson(result.json["schema"]);

    final doc = MeshDocument(
      schema: schema,
      sendChangesToBackend: (base64) => _changesToSync.sink.add(_QueuedSync(path: normalizedPath, base64: base64)),
    );
    final rc = _RefCount(doc);
    _connectedDocuments[normalizedPath] = rc;
    notifyListeners();

    c.complete(rc);
    return doc;
  } catch (err) {
    c.completeError(err);
    rethrow;
  } finally {
    _connectingDocuments.remove(normalizedPath);
  }
}