Newer
Older
import { CallbackOptions, MakeFn, SubscribeRecipe } from "../../types";
Marin Karamihalev
committed
import { uniq } from "../util";
const subscriptionPath = "Device.LocalAgent.Subscription.";
const make: MakeFn =
(call, on): SubscribeRecipe =>
async (opts, callback) => {
const id = "NOTIFY@" + (opts.id || uniq());
const refList = Array.isArray(opts.reference)
? opts.reference.join(",")
: opts.reference;
const newSubPath = await call("ADD", {
path: subscriptionPath,
value: {
Enable: true,
ID: id,
NotifType: opts.notif,
NotifRetry: "retry" in opts ? opts.retry : true,
ReferenceList: refList,
Persistent: false,
},
});
// from https://usp.technology/specification/07-index-messages.html#sec:responses-and-retry
const respond = () => call("NOTIFY_RESP", { subscriptionId: id });
const clear = on(id, (msg, fullMsg, opts) => {
opts?.sendResp && respond();
callback(msg, fullMsg);
});
return () => {
clear();
return call("DELETE", { paths: newSubPath });
};
};
export default {
name: "subscribe",
make,
};