From 1f44d86a9b1ddb0311c1936df921b9338af4fea4 Mon Sep 17 00:00:00 2001 From: Charles Schleich Date: Thu, 24 Oct 2024 16:38:54 +0200 Subject: [PATCH] Use JS Constructors for Parameters, Query, Queryable, Reply, Selector --- zenoh-ts/src/query.ts | 143 +++++++++++++++------------------------- zenoh-ts/src/session.ts | 14 ++-- 2 files changed, 60 insertions(+), 97 deletions(-) diff --git a/zenoh-ts/src/query.ts b/zenoh-ts/src/query.ts index 14a7d46..6ca25b6 100644 --- a/zenoh-ts/src/query.ts +++ b/zenoh-ts/src/query.ts @@ -56,7 +56,8 @@ export class Queryable { Queryable.registry.unregister(this); } /** - * @hidden user must use new function + * Returns a Queryable + * Note! : user must use declare_queryable on a session */ constructor(remote_queryable: RemoteQueryable, callback_queryable: boolean) { this._remote_queryable = remote_queryable; @@ -97,12 +98,6 @@ export class Queryable { Queryable.registry.unregister(this); } - /** - * @hidden : user must use declare_queryable - */ - static new(remote_queryable: RemoteQueryable, callback_queryable: boolean): Queryable { - return new Queryable(remote_queryable, callback_queryable); - } } /** @@ -116,7 +111,7 @@ export function QueryWS_to_Query( let key_expr: KeyExpr = new KeyExpr(query_ws.key_expr); let payload: ZBytes | undefined = undefined; let attachment: ZBytes | undefined = undefined; - let parameters: Parameters = Parameters.new(query_ws.parameters); + let parameters: Parameters = new Parameters(query_ws.parameters); let encoding: Encoding | undefined = undefined; if (query_ws.payload != null) { @@ -129,7 +124,7 @@ export function QueryWS_to_Query( encoding = Encoding.from_str(query_ws.encoding); } - return Query.new( + return new Query( query_ws.query_uuid, key_expr, parameters, @@ -160,9 +155,11 @@ export class Query { private _reply_tx: SimpleChannel; /** - * @hidden - */ - private constructor( + * New Function Used to Construct Query, + * Note: Users should not need to call this function + * But will receieve 'Query's from Queryables + */ + constructor( query_id: UUIDv4, key_expr: KeyExpr, parameters: Parameters, @@ -185,7 +182,7 @@ export class Query { * @returns Selector */ selector() { - return Selector.new(this._key_expr, this._parameters) + return new Selector(this._key_expr, this._parameters) } /** * gets the KeyExpr of Query @@ -276,31 +273,6 @@ export class Query { this.reply_ws(qr_variant); } - /** - * New Function Used to Construct Query, - * Note: Users should not need to call this function - * But will receieve 'Query's from Queryables - * @hidden - */ - static new( - query_id: UUIDv4, - key_expr: KeyExpr, - parameters: Parameters, - payload: ZBytes | undefined, - attachment: ZBytes | undefined, - encoding: Encoding | undefined, - reply_tx: SimpleChannel, - ) { - return new Query( - query_id, - key_expr, - parameters, - payload, - attachment, - encoding, - reply_tx, - ); - } } @@ -318,10 +290,30 @@ export class Parameters { private _params: Map; - private constructor(p: Map) { - this._params = p; + // constructor(p: Map) { + // this._params = p; + // } + + constructor(p: IntoParameters) { + if (p instanceof Parameters) { + this._params = p._params + } else if (p instanceof Map) { + this._params = p; + } else { + const params = new Map(); + if (p.length != 0) { + for (const pair of p.split(";") || []) { + const [key, value] = pair.split("="); + if (key != undefined && value != undefined) { + params.set(key, value); + } + } + } + this._params = params; + } } + /** * removes a key from the parameters * @returns void @@ -383,7 +375,7 @@ export class Parameters { * @returns void */ extend(other: IntoParameters) { - let other_params = Parameters.new(other); + let other_params = new Parameters(other); for (let [key, value] of other_params._params) { this._params.set(key, value) } @@ -403,24 +395,6 @@ export class Parameters { return output_string; } - static new(p: IntoParameters): Parameters { - if (p instanceof Parameters) { - return p - } else if (p instanceof Map) { - return new Parameters(p); - } else { - const params = new Map(); - if (p.length != 0) { - for (const pair of p.split(";") || []) { - const [key, value] = pair.split("="); - if (key != undefined && value != undefined) { - params.set(key, value); - } - } - } - return new Parameters(params); - } - } } @@ -448,20 +422,17 @@ export class ReplyError { return this._encoding; } - private constructor(payload: ZBytes, encoding: Encoding) { - this._encoding = encoding; - this._payload = payload; - } - /** * ReplyError gets created by the reply of a `get` on a session - * @hidden + * */ - static new(reply_err_ws: ReplyErrorWS): ReplyError { - let zbytes = new ZBytes(reply_err_ws.payload); + constructor(reply_err_ws: ReplyErrorWS) { + let payload = new ZBytes(reply_err_ws.payload); let encoding = Encoding.from_str(reply_err_ws.encoding); - return new ReplyError(zbytes, encoding); + this._encoding = encoding; + this._payload = payload; } + } /** @@ -481,22 +452,15 @@ export class Reply { /** * @hidden */ - private constructor(result: Sample | ReplyError) { - this._result = result; - } - - /** - * @hidden - */ - static new(reply_ws: ReplyWS): Reply { + constructor(reply_ws: ReplyWS) { if ("Ok" in reply_ws.result) { let sample_ws = reply_ws.result["Ok"]; let sample = Sample_from_SampleWS(sample_ws); - return new Reply(sample); + this._result = sample; } else { let sample_ws_err: ReplyErrorWS = reply_ws.result["Err"]; - let reply_error = ReplyError.new(sample_ws_err); - return new Reply(reply_error); + let reply_error = new ReplyError(sample_ws_err); + this._result = reply_error; } } } @@ -536,7 +500,7 @@ export class Selector { */ parameters(): Parameters { if (this._parameters == undefined) { - return Parameters.new(""); + return new Parameters(""); } else { return this._parameters; } @@ -546,29 +510,28 @@ export class Selector { return this._key_expr.toString + "?" + this._parameters?.toString() } - private constructor(keyexpr: KeyExpr, parameters?: Parameters) { - this._key_expr = keyexpr; - this._parameters = parameters; - } - /** * New Function to create a selector from Selector / KeyExpr and Parameters * @returns Selector */ - static new(selector: IntoSelector, parameters?: IntoParameters): Selector { + constructor(selector: IntoSelector, parameters?: IntoParameters) { let key_expr: KeyExpr; if (selector instanceof Selector) { - return selector; + this._key_expr = selector._key_expr; + this._parameters = selector._parameters; + return ; } else if (selector instanceof KeyExpr) { key_expr = selector; } else { key_expr = new KeyExpr(selector); } + this._key_expr = key_expr; if (parameters == undefined) { - return new Selector(key_expr, Parameters.new("")); + this._parameters = new Parameters("") } else { - return new Selector(key_expr, Parameters.new(parameters)); + this._parameters = new Parameters(parameters); } } -} + +} \ No newline at end of file diff --git a/zenoh-ts/src/session.ts b/zenoh-ts/src/session.ts index 2e77557..312c79b 100644 --- a/zenoh-ts/src/session.ts +++ b/zenoh-ts/src/session.ts @@ -312,16 +312,16 @@ export class Session { let split_string = into_selector.split("?") if (split_string.length == 1) { key_expr = new KeyExpr(into_selector); - selector = Selector.new(key_expr); + selector = new Selector(key_expr); } else if (split_string.length == 2 && split_string[0] != undefined && split_string[1] != undefined) { key_expr = new KeyExpr(split_string[0]); - let parameters: Parameters = Parameters.new(split_string[1]); - selector = Selector.new(key_expr, parameters); + let parameters: Parameters = new Parameters(split_string[1]); + selector = new Selector(key_expr, parameters); } else { throw "Error: Invalid Selector, expected format ?"; } } else { - selector = Selector.new(into_selector); + selector = new Selector(into_selector); } let [callback, handler_type] = this.check_handler_or_callback(handler); @@ -362,7 +362,7 @@ export class Session { for await (const message of chan) { // This horribleness comes from SimpleChannel sending a 0 when the channel is closed if (message != undefined && (message as unknown as number) != 0) { - let reply = Reply.new(message); + let reply = new Reply(message); if (callback != undefined) { callback(reply); } @@ -475,7 +475,7 @@ export class Session { ); } - let queryable = Queryable.new(remote_queryable, callback_queryable); + let queryable = new Queryable(remote_queryable, callback_queryable); return queryable; } @@ -595,7 +595,7 @@ export class Receiver { return RecvErr.Disconnected; } else if (isReplyWS(channel_msg)) { // Handle the ReplyWS case - let opt_reply = Reply.new(channel_msg); + let opt_reply = new Reply(channel_msg); if (opt_reply == undefined) { return RecvErr.MalformedReply; } else {