@civet/events 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/lib/ConfigProvider.js +6 -4
- package/lib/EventHandler.js +28 -10
- package/lib/EventReceiver.js +7 -5
- package/lib/index.js +1 -1
- package/lib/useEventHandler.js +16 -7
- package/package.json +29 -26
- package/src/ConfigProvider.jsx +3 -1
- package/src/EventHandler.jsx +22 -7
- package/src/EventReceiver.js +5 -2
- package/src/composeHandlers.js +2 -1
- package/src/index.js +5 -1
- package/src/useEventHandler.js +20 -2
package/LICENSE
CHANGED
package/lib/ConfigProvider.js
CHANGED
|
@@ -7,15 +7,17 @@ exports["default"] = void 0;
|
|
|
7
7
|
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
8
8
|
var _EventReceiver = require("./EventReceiver");
|
|
9
9
|
var _context = require("./context");
|
|
10
|
-
|
|
10
|
+
var _jsxRuntime = require("react/jsx-runtime");
|
|
11
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|
11
12
|
var ConfigProvider = function ConfigProvider(_ref) {
|
|
12
13
|
var eventReceiver = _ref.eventReceiver,
|
|
13
14
|
children = _ref.children;
|
|
14
|
-
return /*#__PURE__*/
|
|
15
|
+
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_context.ConfigContext.Provider, {
|
|
15
16
|
value: {
|
|
16
17
|
eventReceiver: eventReceiver
|
|
17
|
-
}
|
|
18
|
-
|
|
18
|
+
},
|
|
19
|
+
children: children
|
|
20
|
+
});
|
|
19
21
|
};
|
|
20
22
|
ConfigProvider.propTypes = {
|
|
21
23
|
eventReceiver: _EventReceiver.eventReceiverPropType,
|
package/lib/EventHandler.js
CHANGED
|
@@ -7,23 +7,41 @@ exports["default"] = void 0;
|
|
|
7
7
|
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
8
8
|
var _EventReceiver = require("./EventReceiver");
|
|
9
9
|
var _useEventHandler = _interopRequireDefault(require("./useEventHandler"));
|
|
10
|
+
var _jsxRuntime = require("react/jsx-runtime");
|
|
10
11
|
var _excluded = ["children"];
|
|
11
|
-
function _interopRequireDefault(
|
|
12
|
-
function _objectWithoutProperties(
|
|
13
|
-
function _objectWithoutPropertiesLoose(
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
props = _objectWithoutProperties(_ref, _excluded);
|
|
17
|
-
(0, _useEventHandler["default"])(props);
|
|
18
|
-
return /*#__PURE__*/React.createElement(React.Fragment, null, children);
|
|
19
|
-
}
|
|
20
|
-
EventHandler.propTypes = {
|
|
12
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|
13
|
+
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
|
|
14
|
+
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
|
|
15
|
+
var propTypes = {
|
|
16
|
+
/** EventReceiver to be used */
|
|
21
17
|
eventReceiver: _EventReceiver.eventReceiverPropType,
|
|
18
|
+
/** ResourceContext to be used */
|
|
22
19
|
resource: _propTypes["default"].object,
|
|
20
|
+
/** Disables the event handler */
|
|
23
21
|
disabled: _propTypes["default"].bool,
|
|
22
|
+
/** Options for the EventReceiver */
|
|
24
23
|
options: _propTypes["default"].object,
|
|
24
|
+
/** Callback to filter events and handle your own event logic - if true is returned, the event does not cause the resource to update */
|
|
25
25
|
onEvent: _propTypes["default"].func,
|
|
26
|
+
/** Provides information on when the resource has been requested to update - events contains the events that lead to the update */
|
|
26
27
|
onNotify: _propTypes["default"].func,
|
|
27
28
|
children: _propTypes["default"].node
|
|
28
29
|
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Enables automatic updating for a Resource component or useResource hook by subscribing to an EventReceiver.
|
|
33
|
+
*
|
|
34
|
+
* Necessary configuration that is not directly specified is taken from the ConfigContext and ResourceContext.
|
|
35
|
+
*
|
|
36
|
+
* onEvent can be used to directly access events allowing you to add custom event logic to your components.
|
|
37
|
+
*/
|
|
38
|
+
function EventHandler(_ref) {
|
|
39
|
+
var children = _ref.children,
|
|
40
|
+
props = _objectWithoutProperties(_ref, _excluded);
|
|
41
|
+
(0, _useEventHandler["default"])(props);
|
|
42
|
+
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
|
|
43
|
+
children: children
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
EventHandler.propTypes = propTypes;
|
|
29
47
|
var _default = exports["default"] = EventHandler;
|
package/lib/EventReceiver.js
CHANGED
|
@@ -5,11 +5,11 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.isEventReceiver = exports.eventReceiverPropType = exports["default"] = void 0;
|
|
7
7
|
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
8
|
-
function _interopRequireDefault(
|
|
8
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|
9
9
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
10
|
-
function _classCallCheck(
|
|
11
|
-
function _defineProperties(
|
|
12
|
-
function _createClass(
|
|
10
|
+
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
|
|
11
|
+
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
|
|
12
|
+
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
|
|
13
13
|
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
14
14
|
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
15
15
|
var EventReceiver = /*#__PURE__*/function () {
|
|
@@ -19,7 +19,9 @@ var EventReceiver = /*#__PURE__*/function () {
|
|
|
19
19
|
return _createClass(EventReceiver, [{
|
|
20
20
|
key: "subscribe",
|
|
21
21
|
value: function subscribe(resource, options, handler) {
|
|
22
|
-
if (typeof handler !== 'function')
|
|
22
|
+
if (typeof handler !== 'function') {
|
|
23
|
+
throw new Error('Handler must be a function');
|
|
24
|
+
}
|
|
23
25
|
var unsubscribe = this.handleSubscribe(resource, options, handler);
|
|
24
26
|
if (typeof unsubscribe !== 'function') {
|
|
25
27
|
console.warn('EventReceiver.handleSubscribe should return a callback to cancel the subscription. Ignoring this warning may result in the execution of obsolete handlers and potential memory leaks.');
|
package/lib/index.js
CHANGED
|
@@ -61,5 +61,5 @@ var _composeHandlers = _interopRequireDefault(require("./composeHandlers"));
|
|
|
61
61
|
var _useEventHandler = _interopRequireDefault(require("./useEventHandler"));
|
|
62
62
|
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
|
|
63
63
|
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
|
|
64
|
-
function _interopRequireDefault(
|
|
64
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|
65
65
|
var ConfigConsumer = exports.ConfigConsumer = _context.ConfigContext.Consumer;
|
package/lib/useEventHandler.js
CHANGED
|
@@ -8,13 +8,20 @@ var _core = require("@civet/core");
|
|
|
8
8
|
var _fastDeepEqual = _interopRequireDefault(require("fast-deep-equal"));
|
|
9
9
|
var _react = require("react");
|
|
10
10
|
var _context = require("./context");
|
|
11
|
-
function _interopRequireDefault(
|
|
12
|
-
function _slicedToArray(
|
|
11
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|
12
|
+
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
|
|
13
13
|
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
14
|
-
function _unsupportedIterableToArray(
|
|
15
|
-
function _arrayLikeToArray(
|
|
14
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
15
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
16
16
|
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
17
|
-
function _arrayWithHoles(
|
|
17
|
+
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
|
|
18
|
+
/**
|
|
19
|
+
* Enables automatic updating for a Resource component or useResource hook by subscribing to an EventReceiver.
|
|
20
|
+
*
|
|
21
|
+
* Necessary configuration that is not directly specified is taken from the ConfigContext and ResourceContext.
|
|
22
|
+
*
|
|
23
|
+
* onEvent can be used to directly access events allowing you to add custom event logic to your components.
|
|
24
|
+
*/
|
|
18
25
|
function useEventHandler(_ref) {
|
|
19
26
|
var eventReceiverProp = _ref.eventReceiver,
|
|
20
27
|
resourceProp = _ref.resource,
|
|
@@ -40,7 +47,7 @@ function useEventHandler(_ref) {
|
|
|
40
47
|
if (!(0, _fastDeepEqual["default"])(options, optionsProp)) {
|
|
41
48
|
setOptions(optionsProp);
|
|
42
49
|
}
|
|
43
|
-
var isDisabled =
|
|
50
|
+
var isDisabled = Boolean(disabled || (currentResource === null || currentResource === void 0 ? void 0 : currentResource.isEmpty));
|
|
44
51
|
(0, _react.useEffect)(function () {
|
|
45
52
|
if (eventReceiver == null || isDisabled) return undefined;
|
|
46
53
|
var unsubscribe = eventReceiver.subscribe(resource, options, function (data) {
|
|
@@ -53,7 +60,9 @@ function useEventHandler(_ref) {
|
|
|
53
60
|
} else {
|
|
54
61
|
unhandledEvents = data;
|
|
55
62
|
}
|
|
56
|
-
if (unhandledEvents.length === 0 || typeof (resource === null || resource === void 0 ? void 0 : resource.notify) !== 'function')
|
|
63
|
+
if (unhandledEvents.length === 0 || typeof (resource === null || resource === void 0 ? void 0 : resource.notify) !== 'function') {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
57
66
|
var promise = resource.notify();
|
|
58
67
|
if (typeof onNotify === 'function') {
|
|
59
68
|
promise.then(function (result) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@civet/events",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Civet",
|
|
5
|
+
"type": "module",
|
|
5
6
|
"main": "./lib/index.js",
|
|
6
7
|
"exports": {
|
|
7
8
|
".": {
|
|
@@ -15,14 +16,14 @@
|
|
|
15
16
|
"src"
|
|
16
17
|
],
|
|
17
18
|
"scripts": {
|
|
18
|
-
"build:clean": "rimraf ./lib",
|
|
19
19
|
"prebuild": "npm run build:clean",
|
|
20
|
+
"build:clean": "rimraf ./lib",
|
|
20
21
|
"build": "babel src -d lib",
|
|
21
|
-
"lint": "eslint
|
|
22
|
+
"lint": "eslint",
|
|
22
23
|
"prepare": "npm run lint && npm run build",
|
|
23
24
|
"preversion": "git diff HEAD --name-only --exit-code || (echo -\\> unclean working directory && exit 1)",
|
|
24
25
|
"postversion": "cross-env git commit -am$npm_package_version",
|
|
25
|
-
"prettify": "prettier --write
|
|
26
|
+
"prettify": "prettier . --write"
|
|
26
27
|
},
|
|
27
28
|
"repository": {
|
|
28
29
|
"type": "git",
|
|
@@ -40,33 +41,35 @@
|
|
|
40
41
|
"url": "https://github.com/civet-org/events/issues"
|
|
41
42
|
},
|
|
42
43
|
"homepage": "https://civet.js.org/",
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"@babel/eslint-parser": "^7.22.15",
|
|
47
|
-
"@babel/preset-env": "^7.24.4",
|
|
48
|
-
"@babel/preset-react": "^7.18.6",
|
|
49
|
-
"@civet/core": "^1.3.0",
|
|
50
|
-
"cross-env": "^7.0.3",
|
|
51
|
-
"eslint": "^8.53.0",
|
|
52
|
-
"eslint-config-prettier": "^9.0.0",
|
|
53
|
-
"eslint-plugin-import": "^2.29.0",
|
|
54
|
-
"eslint-plugin-prettier": "^5.0.1",
|
|
55
|
-
"eslint-plugin-react": "^7.33.2",
|
|
56
|
-
"eslint-plugin-react-hooks": "^4.6.0",
|
|
57
|
-
"eslint-plugin-unused-imports": "^3.0.0",
|
|
58
|
-
"prettier": "^3.2.5",
|
|
59
|
-
"react": "^18.2.0",
|
|
60
|
-
"react-dom": "^18.2.0",
|
|
61
|
-
"rimraf": "^5.0.5"
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"fast-deep-equal": "^3.1.3",
|
|
46
|
+
"prop-types": "^15.8.1"
|
|
62
47
|
},
|
|
63
48
|
"peerDependencies": {
|
|
64
49
|
"@civet/core": ">=1.3.0",
|
|
65
50
|
"react": ">=18.0",
|
|
66
51
|
"react-dom": ">=18.0"
|
|
67
52
|
},
|
|
68
|
-
"
|
|
69
|
-
"
|
|
70
|
-
"
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@babel/cli": "^7.26.4",
|
|
55
|
+
"@babel/core": "^7.26.10",
|
|
56
|
+
"@babel/eslint-parser": "^7.26.10",
|
|
57
|
+
"@babel/preset-env": "^7.26.9",
|
|
58
|
+
"@babel/preset-react": "^7.26.3",
|
|
59
|
+
"@civet/core": "^1.4.1",
|
|
60
|
+
"cross-env": "^7.0.3",
|
|
61
|
+
"eslint": "^9.20.0",
|
|
62
|
+
"eslint-config-prettier": "^10.0.1",
|
|
63
|
+
"eslint-import-resolver-alias": "^1.1.2",
|
|
64
|
+
"eslint-plugin-import": "^2.31.0",
|
|
65
|
+
"eslint-plugin-prettier": "^5.2.3",
|
|
66
|
+
"eslint-plugin-react": "^7.37.4",
|
|
67
|
+
"eslint-plugin-react-hooks": "^5.2.0-canary-8759c5c8-20250207",
|
|
68
|
+
"eslint-plugin-unused-imports": "^4.1.4",
|
|
69
|
+
"globals": "^16.0.0",
|
|
70
|
+
"prettier": "^3.5.3",
|
|
71
|
+
"react": "^18.3.1",
|
|
72
|
+
"react-dom": "^18.3.1",
|
|
73
|
+
"rimraf": "^6.0.1"
|
|
71
74
|
}
|
|
72
75
|
}
|
package/src/ConfigProvider.jsx
CHANGED
|
@@ -3,7 +3,9 @@ import { eventReceiverPropType } from './EventReceiver';
|
|
|
3
3
|
import { ConfigContext } from './context';
|
|
4
4
|
|
|
5
5
|
const ConfigProvider = ({ eventReceiver, children }) => (
|
|
6
|
-
<ConfigContext.Provider value={{ eventReceiver }}>
|
|
6
|
+
<ConfigContext.Provider value={{ eventReceiver }}>
|
|
7
|
+
{children}
|
|
8
|
+
</ConfigContext.Provider>
|
|
7
9
|
);
|
|
8
10
|
|
|
9
11
|
ConfigProvider.propTypes = {
|
package/src/EventHandler.jsx
CHANGED
|
@@ -2,20 +2,35 @@ import PropTypes from 'prop-types';
|
|
|
2
2
|
import { eventReceiverPropType } from './EventReceiver';
|
|
3
3
|
import useEventHandler from './useEventHandler';
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
return <>{children}</>;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
EventHandler.propTypes = {
|
|
5
|
+
const propTypes = {
|
|
6
|
+
/** EventReceiver to be used */
|
|
12
7
|
eventReceiver: eventReceiverPropType,
|
|
8
|
+
/** ResourceContext to be used */
|
|
13
9
|
resource: PropTypes.object,
|
|
10
|
+
/** Disables the event handler */
|
|
14
11
|
disabled: PropTypes.bool,
|
|
12
|
+
/** Options for the EventReceiver */
|
|
15
13
|
options: PropTypes.object,
|
|
14
|
+
/** Callback to filter events and handle your own event logic - if true is returned, the event does not cause the resource to update */
|
|
16
15
|
onEvent: PropTypes.func,
|
|
16
|
+
/** Provides information on when the resource has been requested to update - events contains the events that lead to the update */
|
|
17
17
|
onNotify: PropTypes.func,
|
|
18
18
|
children: PropTypes.node,
|
|
19
19
|
};
|
|
20
20
|
|
|
21
|
+
/**
|
|
22
|
+
* Enables automatic updating for a Resource component or useResource hook by subscribing to an EventReceiver.
|
|
23
|
+
*
|
|
24
|
+
* Necessary configuration that is not directly specified is taken from the ConfigContext and ResourceContext.
|
|
25
|
+
*
|
|
26
|
+
* onEvent can be used to directly access events allowing you to add custom event logic to your components.
|
|
27
|
+
*/
|
|
28
|
+
function EventHandler({ children, ...props }) {
|
|
29
|
+
useEventHandler(props);
|
|
30
|
+
|
|
31
|
+
return <>{children}</>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
EventHandler.propTypes = propTypes;
|
|
35
|
+
|
|
21
36
|
export default EventHandler;
|
package/src/EventReceiver.js
CHANGED
|
@@ -2,7 +2,9 @@ import PropTypes from 'prop-types';
|
|
|
2
2
|
|
|
3
3
|
class EventReceiver {
|
|
4
4
|
subscribe(resource, options, handler) {
|
|
5
|
-
if (typeof handler !== 'function')
|
|
5
|
+
if (typeof handler !== 'function') {
|
|
6
|
+
throw new Error('Handler must be a function');
|
|
7
|
+
}
|
|
6
8
|
const unsubscribe = this.handleSubscribe(resource, options, handler);
|
|
7
9
|
if (typeof unsubscribe !== 'function') {
|
|
8
10
|
console.warn(
|
|
@@ -13,7 +15,8 @@ class EventReceiver {
|
|
|
13
15
|
}
|
|
14
16
|
}
|
|
15
17
|
|
|
16
|
-
const isEventReceiver = (eventReceiver) =>
|
|
18
|
+
const isEventReceiver = (eventReceiver) =>
|
|
19
|
+
eventReceiver instanceof EventReceiver;
|
|
17
20
|
|
|
18
21
|
const eventReceiverPropType = PropTypes.instanceOf(EventReceiver);
|
|
19
22
|
|
package/src/composeHandlers.js
CHANGED
|
@@ -2,7 +2,8 @@ const composeHandlers =
|
|
|
2
2
|
(...handlers) =>
|
|
3
3
|
(...args) =>
|
|
4
4
|
handlers.reduce(
|
|
5
|
-
(sum, handler) =>
|
|
5
|
+
(sum, handler) =>
|
|
6
|
+
sum || (typeof handler === 'function' ? handler(...args) : false),
|
|
6
7
|
false,
|
|
7
8
|
);
|
|
8
9
|
|
package/src/index.js
CHANGED
|
@@ -3,7 +3,11 @@ import { ConfigContext, useConfigContext } from './context';
|
|
|
3
3
|
export const { Consumer: ConfigConsumer } = ConfigContext;
|
|
4
4
|
export { default as ConfigProvider } from './ConfigProvider';
|
|
5
5
|
export { default as EventHandler } from './EventHandler';
|
|
6
|
-
export {
|
|
6
|
+
export {
|
|
7
|
+
default as EventReceiver,
|
|
8
|
+
eventReceiverPropType,
|
|
9
|
+
isEventReceiver,
|
|
10
|
+
} from './EventReceiver';
|
|
7
11
|
export { default as composeHandlers } from './composeHandlers';
|
|
8
12
|
export { default as useEventHandler } from './useEventHandler';
|
|
9
13
|
export { useConfigContext };
|
package/src/useEventHandler.js
CHANGED
|
@@ -3,12 +3,25 @@ import deepEquals from 'fast-deep-equal';
|
|
|
3
3
|
import { useEffect, useState } from 'react';
|
|
4
4
|
import { useConfigContext } from './context';
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Enables automatic updating for a Resource component or useResource hook by subscribing to an EventReceiver.
|
|
8
|
+
*
|
|
9
|
+
* Necessary configuration that is not directly specified is taken from the ConfigContext and ResourceContext.
|
|
10
|
+
*
|
|
11
|
+
* onEvent can be used to directly access events allowing you to add custom event logic to your components.
|
|
12
|
+
*/
|
|
6
13
|
function useEventHandler({
|
|
14
|
+
/** EventReceiver to be used */
|
|
7
15
|
eventReceiver: eventReceiverProp,
|
|
16
|
+
/** ResourceContext to be used */
|
|
8
17
|
resource: resourceProp,
|
|
18
|
+
/** Disables the event handler */
|
|
9
19
|
disabled,
|
|
20
|
+
/** Options for the EventReceiver */
|
|
10
21
|
options: optionsProp,
|
|
22
|
+
/** Callback to filter events and handle your own event logic - if true is returned, the event does not cause the resource to update */
|
|
11
23
|
onEvent,
|
|
24
|
+
/** Provides information on when the resource has been requested to update - events contains the events that lead to the update */
|
|
12
25
|
onNotify,
|
|
13
26
|
}) {
|
|
14
27
|
const configContext = useConfigContext();
|
|
@@ -27,7 +40,7 @@ function useEventHandler({
|
|
|
27
40
|
setOptions(optionsProp);
|
|
28
41
|
}
|
|
29
42
|
|
|
30
|
-
const isDisabled =
|
|
43
|
+
const isDisabled = Boolean(disabled || currentResource?.isEmpty);
|
|
31
44
|
|
|
32
45
|
useEffect(() => {
|
|
33
46
|
if (eventReceiver == null || isDisabled) return undefined;
|
|
@@ -39,7 +52,12 @@ function useEventHandler({
|
|
|
39
52
|
} else {
|
|
40
53
|
unhandledEvents = data;
|
|
41
54
|
}
|
|
42
|
-
if (
|
|
55
|
+
if (
|
|
56
|
+
unhandledEvents.length === 0 ||
|
|
57
|
+
typeof resource?.notify !== 'function'
|
|
58
|
+
) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
43
61
|
const promise = resource.notify();
|
|
44
62
|
if (typeof onNotify === 'function') {
|
|
45
63
|
promise.then((result) => onNotify(result, unhandledEvents));
|