react-barcode-scanner
Version:
A barcode scanner base on Barcode Detector
28 lines (27 loc) • 799 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createAtom = createAtom;
exports.useAtom = useAtom;
var react_1 = require("react");
function createAtom() {
var subscriptions = [];
function set(newValue) {
setTimeout(function () {
subscriptions.forEach(function (c) { c(newValue); });
});
}
return {
set: set,
subscriptions: subscriptions
};
}
function useAtom(atom, initialState) {
var _a = (0, react_1.useState)(initialState), state = _a[0], setState = _a[1];
(0, react_1.useEffect)(function () {
var index = atom.subscriptions.push(setState);
return function () {
atom.subscriptions.splice(index, 1);
};
}, [atom]);
return [state, atom.set];
}