Переглянути джерело

docs: document `DataConnection` inline

Jonas Gloning 2 роки тому
батько
коміт
68ff68d1f9
3 змінених файлів з 33 додано та 107 видалено
  1. 0 103
      docs/api.json
  2. 32 3
      lib/dataconnection.ts
  3. 1 1
      lib/exports.ts

+ 0 - 103
docs/api.json

@@ -318,108 +318,5 @@
     "name": "peer.destroyed",
     "type": "boolean",
     "description": "<code>true</code> if this peer and all of its connections can no longer be used."
-  },
-  {
-    "name": "DataConnection",
-    "type": "class",
-    "description": "Wraps WebRTC's DataChannel. To get one, use <a href='#peerconnect'><code>peer.connect</code></a> or listen for the <a href='#peeron-connect'><code>connect</code></a> event.",
-    "children": [
-      {
-        "name": ".send",
-        "type": "method",
-        "snippet": "dataConnection.send(data);",
-        "description": "<code>data</code> is serialized by BinaryPack by default and sent to the remote peer.",
-        "children": {
-          "name": "data",
-          "description": "You can send any type of data, including objects, strings, and blobs."
-        }
-      },
-      {
-        "name": ".close",
-        "type": "method",
-        "snippet": "dataConnection.close();",
-        "description": "Closes the data connection gracefully, cleaning up underlying DataChannels and PeerConnections."
-      },
-      {
-        "name": ".on",
-        "type": "method",
-        "snippet": "dataConnection.on(event, callback);",
-        "description": "Set listeners for data connection events.",
-        "children": [
-          {
-            "name": "'data'",
-            "type": "event",
-            "snippet": "dataConnection.on('data', function(data) { ... });",
-            "description": "Emitted when data is received from the remote peer."
-          },
-          {
-            "name": "'open'",
-            "type": "event",
-            "snippet": "dataConnection.on('open', function() { ... });",
-            "description": "Emitted when the connection is established and ready-to-use."
-          },
-          {
-            "name": "'close'",
-            "type": "event",
-            "snippet": "dataConnection.on('close', function() { ... });",
-            "description": "Emitted when either you or the remote peer closes the data connection.<span class='warn'>Firefox does not yet support this event.</span>"
-          },
-          {
-            "name": "'error'",
-            "type": "event",
-            "snippet": "dataConnection.on('error', function(err) { ... });"
-          }
-        ]
-      },
-      {
-        "name": ".dataChannel",
-        "type": "object",
-        "description": "A reference to the RTCDataChannel object associated with the connection."
-      },
-      {
-        "name": ".label",
-        "type": "string",
-        "description": "The optional label passed in or assigned by PeerJS when the connection was initiated."
-      },
-      {
-        "name": ".metadata",
-        "description": "Any type of metadata associated with the connection, passed in by whoever initiated the connection."
-      },
-      {
-        "name": ".open",
-        "type": "boolean",
-        "description": "This is true if the connection is open and ready for read/write."
-      },
-      {
-        "name": ".peerConnection",
-        "type": "object",
-        "description": "A reference to the RTCPeerConnection object associated with the connection."
-      },
-      {
-        "name": ".peer",
-        "type": "string",
-        "description": "The ID of the peer on the other end of this connection."
-      },
-      {
-        "name": ".reliable",
-        "type": "boolean",
-        "description": "Whether the underlying data channels are reliable; defined when the connection was initiated."
-      },
-      {
-        "name": ".serialization",
-        "type": "string",
-        "description": "The serialization format of the data sent over the connection. Can be <code>binary</code> (default), <code>binary-utf8</code>, <code>json</code>, or <code>none</code>."
-      },
-      {
-        "name": ".type",
-        "type": "string",
-        "description": "For data connections, this is always <code>'data'</code>."
-      },
-      {
-        "name": ".bufferSize",
-        "type": "number",
-        "description": "The number of messages queued to be sent once the browser buffer is no longer full."
-      }
-    ]
   }
 ]

+ 32 - 3
lib/dataconnection.ts

@@ -8,19 +8,28 @@ import { ServerMessage } from "./servermessage";
 import { EncodingQueue } from "./encodingQueue";
 import type { DataConnection as IDataConnection } from "./dataconnection";
 
-type DataConnectionEvents = {
+export type DataConnectionEvents = {
 	/**
 	 * Emitted when data is received from the remote peer.
+	 *
+	 * ```ts
+	 * dataConnection.on('data', (data) => { ... });
+	 * ```
 	 */
 	data: (data: unknown) => void;
 	/**
 	 * Emitted when the connection is established and ready-to-use.
+	 *
+	 * ```ts
+	 * dataConnection.on('open', () => { ... });
+	 * ```
 	 */
 	open: () => void;
 };
 
 /**
- * Wraps a DataChannel between two Peers.
+ * Wraps WebRTC's DataChannel.
+ * To get one, use {@apilink Peer.connect} or listen for the {@apilink PeerEvents | `connect`} event.
  */
 export class DataConnection
 	extends BaseConnection<DataConnectionEvents>
@@ -30,8 +39,18 @@ export class DataConnection
 	private static readonly MAX_BUFFERED_AMOUNT = 8 * 1024 * 1024;
 
 	private _negotiator: Negotiator<DataConnectionEvents, DataConnection>;
+	/**
+	 * The optional label passed in or assigned by PeerJS when the connection was initiated.
+	 */
 	readonly label: string;
+	/**
+	 * The serialization format of the data sent over the connection.
+	 * {@apilink SerializationType | possible values}
+	 */
 	readonly serialization: SerializationType;
+	/**
+	 * Whether the underlying data channels are reliable; defined when the connection was initiated.
+	 */
 	readonly reliable: boolean;
 	stringify: (data: any) => string = JSON.stringify;
 	parse: (data: string) => any = JSON.parse;
@@ -41,6 +60,9 @@ export class DataConnection
 	}
 
 	private _buffer: any[] = [];
+	/**
+	 * The number of messages queued to be sent once the browser buffer is no longer full.
+	 */
 	private _bufferSize = 0;
 	private _buffering = false;
 	private _chunkedData: {
@@ -54,6 +76,9 @@ export class DataConnection
 	private _dc: RTCDataChannel;
 	private _encodingQueue = new EncodingQueue();
 
+	/**
+	 * A reference to the RTCDataChannel object associated with the connection.
+	 */
 	get dataChannel(): RTCDataChannel {
 		return this._dc;
 	}
@@ -234,7 +259,11 @@ export class DataConnection
 		super.emit("close");
 	}
 
-	/** Allows user to send data. */
+	/**
+	 * `data` is serialized and sent to the remote peer.
+	 * @param data You can send any type of data, including objects, strings, and blobs.
+	 * @returns
+	 */
 	send(data: any, chunked?: boolean): void {
 		if (!this.open) {
 			super.emit(

+ 1 - 1
lib/exports.ts

@@ -9,7 +9,7 @@ export type {
 	CallOption,
 } from "./optionInterfaces";
 export type { UtilSupportsObj } from "./util";
-export type { DataConnection } from "./dataconnection";
+export type { DataConnection, DataConnectionEvents } from "./dataconnection";
 export type { MediaConnection, MediaConnectionEvents } from "./mediaconnection";
 export type { LogLevel } from "./logger";
 export type {