@helenejs/react
Version:
Real-time Web Apps for Node.js
37 lines • 1.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useFind = useFind;
const react_1 = require("react");
const use_object_1 = require("./use-object");
const data_1 = require("@helenejs/data");
function useFind(collection, filter, sort, projection, limit, skip) {
const [data, setData] = (0, react_1.useState)([]);
(0, react_1.useEffect)(() => {
if (!collection)
return;
async function onUpdated() {
const result = await collection
.find(filter, projection)
.sort(sort)
.limit(limit)
.skip(skip);
setData(result);
}
onUpdated().catch(console.error);
collection.on(data_1.CollectionEvent.READY, onUpdated);
collection.on(data_1.CollectionEvent.UPDATED, onUpdated);
return () => {
collection.off(data_1.CollectionEvent.READY, onUpdated);
collection.off(data_1.CollectionEvent.UPDATED, onUpdated);
};
}, [
collection,
limit,
skip,
(0, use_object_1.useObject)(filter),
(0, use_object_1.useObject)(projection),
(0, use_object_1.useObject)(sort),
]);
return data;
}
//# sourceMappingURL=use-find.js.map