From 4b1acbf3c325b935c37d055306c89dbb6eddc058 Mon Sep 17 00:00:00 2001 From: David Reed Date: Tue, 13 Jun 2023 23:06:33 -0400 Subject: [PATCH] higher quality types for client methods --- src/client/index.js | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/src/client/index.js b/src/client/index.js index 03ec699..4f93149 100644 --- a/src/client/index.js +++ b/src/client/index.js @@ -19,6 +19,17 @@ import StyleApi from './dom/style.js'; import IDBApi from './idb.js'; import WebSocketApi from './requests/websocket.js'; +/** + * @template {Function} [T=Function] + * @typedef {(fn: T, that: any, args: any[]) => {}} WrapFun + */ + +/** + * @typedef {object} WrapPropertyDescriptor + * @property {WrapFun} [get] + * @property {WrapFun} [set] + */ + class UVClient extends EventEmitter { /** * @@ -73,9 +84,9 @@ class UVClient extends EventEmitter { /** * * @param {*} obj - * @param {*} prop + * @param {PropertyKey} prop * @param {WrapFun} wrapper - * @param {*} construct + * @param {boolean} [construct] * @returns */ override(obj, prop, wrapper, construct) { @@ -84,26 +95,30 @@ class UVClient extends EventEmitter { obj[prop] = wrapped; return wrapped; } + /** + * + * @param {*} obj + * @param {PropertyKey} prop + * @param {WrapPropertyDescriptor} [wrapObj] + * @returns + */ overrideDescriptor(obj, prop, wrapObj = {}) { const wrapped = this.wrapDescriptor(obj, prop, wrapObj); if (!wrapped) return {}; this.nativeMethods.defineProperty(obj, prop, wrapped); return wrapped; } - /** - * @template {Function} [T=Function] - * @typedef {(fn: T, that: any, args: any[]) => {}} WrapFun - */ + /** * * @template T * @param {*} obj - * @param {*} prop + * @param {PropertyKey} prop * @param {WrapFun} wrap - * @param {boolean} construct + * @param {boolean} [construct] * @returns {T} */ - wrap(obj, prop, wrap, construct) { + wrap(obj, prop, wrap, construct = false) { const fn = obj[prop]; if (!fn) return fn; const wrapped = @@ -122,10 +137,17 @@ class UVClient extends EventEmitter { wrapped.prototype.constructor = wrapped; } - this.emit('wrap', fn, wrapped, !!construct); + this.emit('wrap', fn, wrapped, construct); return wrapped; } + /** + * + * @param {*} obj + * @param {PropertyKey} prop + * @param {WrapPropertyDescriptor} [wrapObj] + * @returns + */ wrapDescriptor(obj, prop, wrapObj = {}) { const descriptor = this.nativeMethods.getOwnPropertyDescriptor( obj,