Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API alignment: handler is always in options #111

Merged
merged 2 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ The file `EXAMPLE_CONFIG.json5` references the `zenoh-plugin-remote-api\EXAMPLE_
yarn build
```

The result are placed into the `zenoh-ts/dist` directory.
The result is placed into the `zenoh-ts/dist` directory.

This library is currently compatible with browsers, but not with NodeJS due to websocket library limitations.

Expand Down
24 changes: 13 additions & 11 deletions zenoh-ts/examples/chat/src/chat_session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,20 @@ export class ChatSession {
log(`[Session] Declare publisher on ${keyexpr}`);

this.message_subscriber = await this.session.declare_subscriber(KEYEXPR_CHAT_USER.join("*"),
(sample: Sample) => {
let message = deserialize_string(sample.payload().to_bytes());
log(`[Subscriber] Received message: ${message} from ${sample.keyexpr().toString()}`);
let user = ChatUser.fromKeyexpr(sample.keyexpr());
if (user) {
const timestamp = new Date().toISOString();
this.messages.push({ t: timestamp, u: user.username, m: message });
if (this.messageCallback) {
this.messageCallback(user, message);
{
handler: (sample: Sample) => {
let message = deserialize_string(sample.payload().to_bytes());
log(`[Subscriber] Received message: ${message} from ${sample.keyexpr().toString()}`);
let user = ChatUser.fromKeyexpr(sample.keyexpr());
if (user) {
const timestamp = new Date().toISOString();
this.messages.push({ t: timestamp, u: user.username, m: message });
if (this.messageCallback) {
this.messageCallback(user, message);
}
}
return Promise.resolve();
}
return Promise.resolve();
}
);
log(`[Session] Declare Subscriber on ${KEYEXPR_CHAT_USER.join("*").toString()}`);
Expand All @@ -142,7 +144,7 @@ export class ChatSession {

// Subscribe to changes of users presence
this.liveliness_subscriber = this.session.liveliness().declare_subscriber(KEYEXPR_CHAT_USER.join("*"), {
callback: (sample: Sample) => {
handler: (sample: Sample) => {
let keyexpr = sample.keyexpr();
let user = ChatUser.fromKeyexpr(keyexpr);
if (!user) {
Expand Down
2 changes: 1 addition & 1 deletion zenoh-ts/examples/deno/src/z_ping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { Encoding, CongestionControl, Config, Session } from "@eclipse-zenoh/zen
export async function main() {
const session = await Session.open(new Config("ws/127.0.0.1:10000"));

const sub = session.declare_subscriber("test/pong", new FifoChannel(256) );
const sub = session.declare_subscriber("test/pong", { handler: new FifoChannel(256) } );
const pub = session.declare_publisher(
"test/ping",
{
Expand Down
2 changes: 1 addition & 1 deletion zenoh-ts/examples/deno/src/z_pong.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function main() {
pub.put(sample.payload());
};

session.declare_subscriber("test/pong", subscriber_callback);
session.declare_subscriber("test/pong", { handler: subscriber_callback } );

let count = 0;
while (true) {
Expand Down
2 changes: 1 addition & 1 deletion zenoh-ts/examples/deno/src/z_sub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function main() {
const session = await Session.open(new Config("ws/127.0.0.1:10000"));
const key_expr = new KeyExpr(args.key);

const poll_subscriber: Subscriber = session.declare_subscriber(key_expr, new RingChannel(10));
const poll_subscriber: Subscriber = session.declare_subscriber(key_expr, { handler: new RingChannel(10) });

let sample = await poll_subscriber.receive();

Expand Down
2 changes: 1 addition & 1 deletion zenoh-ts/examples/deno/src/z_sub_thr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export async function main() {
console.warn("Declare subscriber");
session.declare_subscriber(
"test/thr",
subscriber_callback
{ handler: subscriber_callback }
);

let count = 0;
Expand Down
10 changes: 5 additions & 5 deletions zenoh-ts/src/liveliness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ function executeAsync(func: any) {
}

interface LivelinessSubscriberOptions {
callback?: (sample: Sample) => Promise<void>,
handler?: (sample: Sample) => Promise<void>, // TODO: add | Handler,
history: boolean,
}

interface LivelinessGetOptions {
callback?: (reply: Reply) => Promise<void>,
handler?: (reply: Reply) => Promise<void>, // TODO: add | Handler,
timeout?: TimeDuration,
}

Expand Down Expand Up @@ -61,8 +61,8 @@ export class Liveliness {
let remote_subscriber;
let callback_subscriber = false;

if (options?.callback !== undefined) {
let callback = options?.callback;
if (options?.handler !== undefined) {
let callback = options?.handler;
callback_subscriber = true;
const callback_conversion = async function (sample_ws: SampleWS,): Promise<void> {
let sample: Sample = Sample_from_SampleWS(sample_ws);
Expand Down Expand Up @@ -102,7 +102,7 @@ export class Liveliness {

let receiver = Receiver.new(chan);

let callback = options?.callback;
let callback = options?.handler;
if (callback !== undefined) {
executeAsync(async () => {
for await (const message of chan) {
Expand Down
17 changes: 14 additions & 3 deletions zenoh-ts/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ export interface PublisherOptions {
reliability?: Reliability,
}

/**
* Options for a Subscriber
* @prop handler - Callback function for this subscriber
*/
export interface SubscriberOptions {
handler?: ((sample: Sample) => Promise<void>) | Handler,
}

// ███████ ███████ ███████ ███████ ██ ██████ ███ ██
// ██ ██ ██ ██ ██ ██ ██ ████ ██
// ███████ █████ ███████ ███████ ██ ██ ██ ██ ██ ██
Expand Down Expand Up @@ -422,20 +430,23 @@ export class Session {
* If a Subscriber is created with a callback, it cannot be simultaneously polled for new values
*
* @param {IntoKeyExpr} key_expr - string of key_expression
* @param {((sample: Sample) => Promise<void>) | Handler} handler - Either a HandlerChannel or a Callback Function to be called for all samples
* @param {SubscriberOptions} subscriber_opts - Options for the subscriber, including a handler
*
* @returns Subscriber
*/
// Handler size : This is to match the API_DATA_RECEPTION_CHANNEL_SIZE of zenoh internally
declare_subscriber(
key_expr: IntoKeyExpr,
handler?: ((sample: Sample) => Promise<void>) | Handler
subscriber_opts?: SubscriberOptions
): Subscriber {
let _key_expr = new KeyExpr(key_expr);
let remote_subscriber: RemoteSubscriber;

let callback_subscriber = false;
if (handler === undefined) {
let handler;
if (subscriber_opts?.handler !== undefined) {
handler = subscriber_opts?.handler;
} else {
handler = new FifoChannel(256);
}
let [callback, handler_type] = check_handler_or_callback<Sample>(handler);
Expand Down