use-propagate
Advanced tools
Comparing version 0.1.1-main.c03d10e to 0.1.1-main.d9ba316
@@ -15,3 +15,2 @@ "use strict"; | ||
var init = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var rendering = false; | ||
var allowPropagateDuringRender = init.allowPropagateDuringRender; | ||
@@ -25,25 +24,13 @@ var listeners = new _set.default(); | ||
}; | ||
var usePropagate = function usePropagate() { | ||
rendering = true; | ||
(0, _react.useLayoutEffect)(function () { | ||
rendering = false; | ||
}); | ||
return function (value) { | ||
if (rendering && !allowPropagateDuringRender) { | ||
return console.warn('use-propagate: The propagate callback function should not be called while rendering, ignoring the call.'); | ||
} | ||
(0, _forEach.default)(listeners).call(listeners, function (listener) { | ||
return listener(value); | ||
}); | ||
}; | ||
}; | ||
var rendering = false; | ||
return { | ||
useListen: function useListen(listener) { | ||
var listenerRef = (0, _useRefFrom.useRefFrom)(listener); | ||
var wrappingListener = (0, _react.useCallback)(function (value) { | ||
return listenerRef.current(value); | ||
var wrappingListener = (0, _react.useMemo)(function () { | ||
var wrappingListener = function wrappingListener(value) { | ||
return listenerRef.current(value); | ||
}; | ||
addListener(wrappingListener); | ||
return wrappingListener; | ||
}, [listenerRef]); | ||
(0, _react.useMemo)(function () { | ||
return addListener(wrappingListener); | ||
}, [wrappingListener]); | ||
(0, _react.useEffect)(function () { | ||
@@ -55,5 +42,18 @@ return function () { | ||
}, | ||
usePropagate: usePropagate | ||
usePropagate: function usePropagate() { | ||
rendering = true; | ||
(0, _react.useLayoutEffect)(function () { | ||
rendering = false; | ||
}); | ||
return function (value) { | ||
if (rendering && !allowPropagateDuringRender) { | ||
return console.warn('use-propagate: The propagate callback function should not be called while rendering, ignoring the call.'); | ||
} | ||
(0, _forEach.default)(listeners).call(listeners, function (listener) { | ||
return listener(value); | ||
}); | ||
}; | ||
} | ||
}; | ||
} | ||
//# sourceMappingURL=createPropagation.js.map |
import _Set from "@babel/runtime-corejs3/core-js-stable/set"; | ||
import _forEachInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/for-each"; | ||
import { useCallback, useEffect, useLayoutEffect, useMemo } from 'react'; | ||
import { useEffect, useLayoutEffect, useMemo } from 'react'; | ||
import { useRefFrom } from 'use-ref-from'; | ||
export default function createPropagation() { | ||
var init = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var rendering = false; | ||
var allowPropagateDuringRender = init.allowPropagateDuringRender; | ||
@@ -16,25 +15,13 @@ var listeners = new _Set(); | ||
}; | ||
var usePropagate = function usePropagate() { | ||
rendering = true; | ||
useLayoutEffect(function () { | ||
rendering = false; | ||
}); | ||
return function (value) { | ||
if (rendering && !allowPropagateDuringRender) { | ||
return console.warn('use-propagate: The propagate callback function should not be called while rendering, ignoring the call.'); | ||
} | ||
_forEachInstanceProperty(listeners).call(listeners, function (listener) { | ||
return listener(value); | ||
}); | ||
}; | ||
}; | ||
var rendering = false; | ||
return { | ||
useListen: function useListen(listener) { | ||
var listenerRef = useRefFrom(listener); | ||
var wrappingListener = useCallback(function (value) { | ||
return listenerRef.current(value); | ||
var wrappingListener = useMemo(function () { | ||
var wrappingListener = function wrappingListener(value) { | ||
return listenerRef.current(value); | ||
}; | ||
addListener(wrappingListener); | ||
return wrappingListener; | ||
}, [listenerRef]); | ||
useMemo(function () { | ||
return addListener(wrappingListener); | ||
}, [wrappingListener]); | ||
useEffect(function () { | ||
@@ -46,5 +33,18 @@ return function () { | ||
}, | ||
usePropagate: usePropagate | ||
usePropagate: function usePropagate() { | ||
rendering = true; | ||
useLayoutEffect(function () { | ||
rendering = false; | ||
}); | ||
return function (value) { | ||
if (rendering && !allowPropagateDuringRender) { | ||
return console.warn('use-propagate: The propagate callback function should not be called while rendering, ignoring the call.'); | ||
} | ||
_forEachInstanceProperty(listeners).call(listeners, function (listener) { | ||
return listener(value); | ||
}); | ||
}; | ||
} | ||
}; | ||
} | ||
//# sourceMappingURL=createPropagation.js.map |
{ | ||
"name": "use-propagate", | ||
"version": "0.1.1-main.c03d10e", | ||
"version": "0.1.1-main.d9ba316", | ||
"description": "Propagates an event to multiple subscribers using React hooks.", | ||
@@ -59,3 +59,3 @@ "files": [ | ||
"homepage": "https://github.com/compulim/use-propagate#readme", | ||
"switch:react:16": { | ||
"switch:react-16": { | ||
"devDependencies": { | ||
@@ -69,3 +69,3 @@ "@testing-library/react": "^12", | ||
}, | ||
"switch:react:17": { | ||
"switch:react-17": { | ||
"devDependencies": { | ||
@@ -79,3 +79,3 @@ "@testing-library/react": "^12", | ||
}, | ||
"switch:react:18": { | ||
"switch:react-18": { | ||
"devDependencies": { | ||
@@ -110,5 +110,5 @@ "@types/react": "^18", | ||
"@babel/runtime-corejs3": "^7.24.1", | ||
"use-propagate": "^0.1.1-main.c03d10e", | ||
"use-propagate": "^0.1.1-main.d9ba316", | ||
"use-ref-from": "^0.0.3" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24376