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 pending = _connectingDocuments[path];

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

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

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

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

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

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

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