Browse Source

docs: document `util` inline

Jonas Gloning 2 years ago
parent
commit
8a4b76aa72
3 changed files with 43 additions and 49 deletions
  1. 1 45
      docs/api.json
  2. 2 2
      lib/exports.ts
  3. 40 2
      lib/util.ts

+ 1 - 45
docs/api.json

@@ -504,49 +504,5 @@
         "description": "For media connections, this is always <code>'media'</code>."
         "description": "For media connections, this is always <code>'media'</code>."
       }
       }
     ]
     ]
-  },
-  {
-    "name": "util",
-    "type": "object",
-    "tags": [
-      "utility"
-    ],
-    "description": "Provides a variety of helpful utilities.<span class='warn'>Only the utilities documented here are guaranteed to be present on <code>util</code>. Undocumented utilities can be removed without warning. We don't consider these to be 'breaking changes.'</span>",
-    "children": [
-      {
-        "name": ".browser",
-        "type": "string",
-        "snippet": "if (util.browser === 'firefox') { /* OK to peer with Firefox peers. */ }",
-        "description": "The current browser. This property can be useful in determining whether or not two peers can connect. For example, as of now data connections are not yet interoperable between major browsers. <code>util.browser</code> can currently have the values <code>'firefox'</code>, <code>'chrome'</code>, <code>'safari'</code>, <code>'edge'</code>, <code>'Not a supported browser.'</code>, or <code>'Not a browser.'</code> (unknown WebRTC-compatible agent)."
-      },
-      {
-        "name": ".supports",
-        "type": "object",
-        "snippet": "if (util.supports.data) { /* OK to start a data connection. */ }",
-        "description": "A hash of WebRTC features mapped to booleans that correspond to whether the feature is supported by the current browser.<span class='warn'>Only the properties documented here are guaranteed to be present on <code>util.supports</code>.</span>",
-        "children": [
-          {
-            "name": ".audioVideo",
-            "type": "boolean",
-            "description": "True if the current browser supports media streams and PeerConnection."
-          },
-          {
-            "name": ".data",
-            "type": "boolean",
-            "description": "True if the current browser supports DataChannel and PeerConnection."
-          },
-          {
-            "name": ".binary",
-            "type": "boolean",
-            "description": "True if the current browser supports binary DataChannels."
-          },
-          {
-            "name": ".reliable",
-            "type": "boolean",
-            "description": "True if the current browser supports reliable DataChannels."
-          }
-        ]
-      }
-    ]
   }
   }
-]
+]

+ 2 - 2
lib/exports.ts

@@ -1,4 +1,4 @@
-import { util } from "./util";
+export { util, type Util } from "./util";
 import { Peer } from "./peer";
 import { Peer } from "./peer";
 
 
 export type {
 export type {
@@ -19,5 +19,5 @@ export type {
 	ServerMessageType,
 	ServerMessageType,
 } from "./enums";
 } from "./enums";
 
 
-export { Peer, util };
+export { Peer };
 export default Peer;
 export default Peer;

+ 40 - 2
lib/util.ts

@@ -4,11 +4,33 @@ import BinaryPack from "peerjs-js-binarypack";
 import { Supports } from "./supports";
 import { Supports } from "./supports";
 
 
 export interface UtilSupportsObj {
 export interface UtilSupportsObj {
+	/**
+	 * The current browser.
+	 * This property can be useful in determining whether two peers can connect.
+	 *
+	 * ```ts
+	 * if (util.browser === 'firefox') {
+	 *  // OK to peer with Firefox peers.
+	 * }
+	 * ```
+	 *
+	 * `util.browser` can currently have the values
+	 * `'firefox', 'chrome', 'safari', 'edge', 'Not a supported browser.', 'Not a browser.' (unknown WebRTC-compatible agent).
+	 */
 	browser: boolean;
 	browser: boolean;
 	webRTC: boolean;
 	webRTC: boolean;
+	/**
+	 * True if the current browser supports media streams and PeerConnection.
+	 */
 	audioVideo: boolean;
 	audioVideo: boolean;
+	/**
+	 * True if the current browser supports DataChannel and PeerConnection.
+	 */
 	data: boolean;
 	data: boolean;
 	binaryBlob: boolean;
 	binaryBlob: boolean;
+	/**
+	 * True if the current browser supports reliable DataChannels.
+	 */
 	reliable: boolean;
 	reliable: boolean;
 }
 }
 
 
@@ -27,7 +49,7 @@ const DEFAULT_CONFIG = {
 	sdpSemantics: "unified-plan",
 	sdpSemantics: "unified-plan",
 };
 };
 
 
-class Util {
+export class Util {
 	noop(): void {}
 	noop(): void {}
 
 
 	readonly CLOUD_HOST = "0.peerjs.com";
 	readonly CLOUD_HOST = "0.peerjs.com";
@@ -43,7 +65,13 @@ class Util {
 	readonly browser = Supports.getBrowser();
 	readonly browser = Supports.getBrowser();
 	readonly browserVersion = Supports.getVersion();
 	readonly browserVersion = Supports.getVersion();
 
 
-	// Lists which features are supported
+	/**
+	 * A hash of WebRTC features mapped to booleans that correspond to whether the feature is supported by the current browser.
+	 *
+	 * :::caution
+	 * Only the properties documented here are guaranteed to be present on `util.supports`
+	 * :::
+	 */
 	readonly supports = (function () {
 	readonly supports = (function () {
 		const supported: UtilSupportsObj = {
 		const supported: UtilSupportsObj = {
 			browser: Supports.isBrowserSupported(),
 			browser: Supports.isBrowserSupported(),
@@ -171,4 +199,14 @@ class Util {
 		return location.protocol === "https:";
 		return location.protocol === "https:";
 	}
 	}
 }
 }
+
+/**
+ * Provides a variety of helpful utilities.
+ *
+ * :::caution
+ * Only the utilities documented here are guaranteed to be present on `util`.
+ * Undocumented utilities can be removed without warning.
+ * We don't consider these to be breaking changes.
+ * :::
+ */
 export const util = new Util();
 export const util = new Util();