@techolution-ai/computer-vision
Version:
A JavaScript/TypeScript library for computer vision applications, providing tools for image processing, scanning, and MQTT-based messaging.
153 lines (152 loc) • 5.45 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/messages/messages-provider.tsx
var messages_provider_exports = {};
__export(messages_provider_exports, {
MessagesContext: () => MessagesContext,
default: () => MessagesProvider
});
module.exports = __toCommonJS(messages_provider_exports);
var import_mqtt = __toESM(require("mqtt"), 1);
var import_react = require("react");
var import_jsx_runtime = require("react/jsx-runtime");
var MessagesContext = (0, import_react.createContext)(null);
function MessagesProvider({
children,
url,
enableDebugging = false,
connectionConfig
}) {
const clientRef = (0, import_react.useRef)(null);
const listenersRef = (0, import_react.useRef)({});
const log = (0, import_react.useCallback)(
(...params) => {
if (!enableDebugging) return;
console.log("[mqtt]:", ...params);
},
[enableDebugging]
);
const subscribeTopics = (0, import_react.useCallback)(
(topic) => {
clientRef.current?.subscribe(topic, { qos: 0 });
log(`subscribed to topic:`, topic);
},
[clientRef.current]
);
const removeListener = (0, import_react.useCallback)(
(topic) => {
log(`removing the listener from topic: ${topic}`);
log(`unsubscribed to topic: ${topic}`);
clientRef.current?.unsubscribe(topic);
},
[clientRef.current]
);
const registerListener = (0, import_react.useCallback)(
(topic, listener) => {
listenersRef.current[topic] = {
queued: !clientRef.current || !clientRef.current.connected,
callback: listener
};
subscribeTopics(topic);
return () => removeListener(topic);
},
[subscribeTopics, removeListener]
);
const connect = (0, import_react.useCallback)(() => {
if (clientRef.current && clientRef.current.connected) return;
try {
const optionsMqtt = {
clientId: `computer-vision-${Math.random().toString(16).substring(2, 8)}`,
...connectionConfig
};
clientRef.current = import_mqtt.default.connect(url, optionsMqtt);
clientRef.current.on("connect", () => {
log("connected");
Object.keys(listenersRef.current).forEach((topic) => {
const listener = listenersRef.current[topic];
if (!listener) return;
if (listener.queued) {
subscribeTopics(topic);
listener.queued = false;
}
});
});
clientRef.current.on("error", (err) => {
console.error("Connection error:", err);
clientRef.current?.end();
});
clientRef.current.on("reconnect", () => {
console.log(`[mqtt]: reconnect`);
});
clientRef.current.on("disconnect", (packet) => {
console.log("[mqtt]: disconnected ", packet.reasonCode);
});
clientRef.current.on("message", (topic, message) => {
const listener = listenersRef.current[topic];
listener?.callback(topic, message.toString());
});
return clientRef.current;
} catch (error) {
log(`error: `, error);
}
}, [url, subscribeTopics]);
const disconnect = (0, import_react.useCallback)(() => {
clientRef.current?.end();
clientRef.current = null;
}, [clientRef.current]);
const options = (0, import_react.useMemo)(
() => ({
log,
connect,
client: clientRef.current,
registerListener,
removeListener,
disconnect
}),
[connect, clientRef.current, registerListener, removeListener, disconnect]
);
const renderMqttProvider = () => {
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(MessagesContext.Provider, { value: options, children });
};
(0, import_react.useEffect)(() => {
const client = clientRef.current || connect();
return () => {
if (client && client.connected) {
client.end();
clientRef.current = null;
log("disconnected: " + client.disconnected);
}
};
}, []);
return renderMqttProvider();
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
MessagesContext
});
//# sourceMappingURL=messages-provider.cjs.map