@civet/events 1.2.0 → 2.0.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/dist/ConfigProvider.d.ts +8 -0
- package/dist/EventHandler.d.ts +27 -0
- package/dist/EventReceiver.d.ts +16 -0
- package/dist/composeHandlers.d.ts +1 -0
- package/dist/context.d.ts +10 -0
- package/dist/main.d.ts +8 -0
- package/dist/main.js +931 -0
- package/dist/useEventHandler.d.ts +25 -0
- package/package.json +53 -50
- package/lib/ConfigProvider.js +0 -26
- package/lib/EventHandler.js +0 -47
- package/lib/EventReceiver.js +0 -37
- package/lib/composeHandlers.js +0 -20
- package/lib/context.js +0 -12
- package/lib/index.js +0 -65
- package/lib/useEventHandler.js +0 -76
- package/src/ConfigProvider.jsx +0 -16
- package/src/EventHandler.jsx +0 -36
- package/src/EventReceiver.js +0 -24
- package/src/composeHandlers.js +0 -10
- package/src/context.js +0 -5
- package/src/index.js +0 -13
- package/src/useEventHandler.js +0 -70
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { GenericEventReceiver, InferEvent, InferOptions, InferResource } from './EventReceiver';
|
|
2
|
+
/**
|
|
3
|
+
* Enables automatic updating for a Resource component or useResource hook by subscribing to an EventReceiver.
|
|
4
|
+
*
|
|
5
|
+
* Necessary configuration that is not directly specified is taken from the ConfigContext and ResourceContext.
|
|
6
|
+
*
|
|
7
|
+
* onEvent can be used to directly access events allowing you to add custom event logic to your components.
|
|
8
|
+
*/
|
|
9
|
+
export default function useEventHandler<EventReceiverI extends GenericEventReceiver, ResourceI extends InferResource<EventReceiverI> = InferResource<EventReceiverI>, OptionsI extends InferOptions<EventReceiverI> = InferOptions<EventReceiverI>, EventI extends InferEvent<EventReceiverI> = InferEvent<EventReceiverI>>({ eventReceiver: eventReceiverProp, resource: resourceProp, disabled, options: optionsProp, onEvent, onNotify, }: {
|
|
10
|
+
/** EventReceiver to be used */
|
|
11
|
+
eventReceiver?: EventReceiverI;
|
|
12
|
+
/** ResourceContext to be used */
|
|
13
|
+
resource?: ResourceI;
|
|
14
|
+
/** Disables the event handler */
|
|
15
|
+
disabled?: boolean;
|
|
16
|
+
/** Options for the EventReceiver */
|
|
17
|
+
options?: OptionsI;
|
|
18
|
+
/** Callback to filter events and handle your own event logic - if true is returned, the event does not cause the resource to update */
|
|
19
|
+
onEvent?: (event: EventI) => boolean;
|
|
20
|
+
/** Provides information on when the resource has been requested to update - events contains the events that lead to the update */
|
|
21
|
+
onNotify?: (next: {
|
|
22
|
+
request: string;
|
|
23
|
+
revision: string;
|
|
24
|
+
}, events: EventI[]) => void;
|
|
25
|
+
}): void;
|
package/package.json
CHANGED
|
@@ -1,75 +1,78 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@civet/events",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Civet",
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
"exports": {
|
|
8
|
-
".": {
|
|
9
|
-
"import": "./src/index.js",
|
|
10
|
-
"require": "./lib/index.js",
|
|
11
|
-
"default": "./lib/index.js"
|
|
12
|
-
}
|
|
13
|
-
},
|
|
14
|
-
"files": [
|
|
15
|
-
"lib",
|
|
16
|
-
"src"
|
|
17
|
-
],
|
|
18
|
-
"scripts": {
|
|
19
|
-
"prebuild": "npm run build:clean",
|
|
20
|
-
"build:clean": "rimraf ./lib",
|
|
21
|
-
"build": "babel src -d lib",
|
|
22
|
-
"lint": "eslint",
|
|
23
|
-
"prepare": "npm run lint && npm run build",
|
|
24
|
-
"preversion": "git diff HEAD --name-only --exit-code || (echo -\\> unclean working directory && exit 1)",
|
|
25
|
-
"postversion": "cross-env git commit -am$npm_package_version",
|
|
26
|
-
"prettify": "prettier . --write"
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "Aaron Burmeister"
|
|
27
7
|
},
|
|
28
8
|
"repository": {
|
|
29
9
|
"type": "git",
|
|
30
|
-
"url": "
|
|
10
|
+
"url": "https://github.com/civet-org/events.git"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/civet-org/events/issues"
|
|
31
14
|
},
|
|
15
|
+
"homepage": "https://civet.js.org/",
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"type": "module",
|
|
32
18
|
"keywords": [
|
|
33
19
|
"civet",
|
|
34
20
|
"react",
|
|
35
21
|
"data",
|
|
36
22
|
"rest"
|
|
37
23
|
],
|
|
38
|
-
"
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"main": "./dist/main.js",
|
|
28
|
+
"types": "./dist/main.d.ts",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"import": "./dist/main.js"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"dev": "vite",
|
|
36
|
+
"build": "tsc -b ./tsconfig-build.json && vite build",
|
|
37
|
+
"lint": "eslint . --max-warnings 0",
|
|
38
|
+
"lint:fix": "npm run lint -- --fix",
|
|
39
|
+
"prettify": "prettier . --write",
|
|
40
|
+
"preview": "vite preview",
|
|
41
|
+
"prepublishOnly": "npm run build"
|
|
42
42
|
},
|
|
43
|
-
"homepage": "https://civet.js.org/",
|
|
44
43
|
"dependencies": {
|
|
45
44
|
"fast-deep-equal": "^3.1.3",
|
|
46
|
-
"
|
|
45
|
+
"object-hash": "^3.0.0",
|
|
46
|
+
"uuid": "^11.1.0"
|
|
47
47
|
},
|
|
48
48
|
"peerDependencies": {
|
|
49
|
-
"@civet/core": ">=
|
|
49
|
+
"@civet/core": ">=2.0",
|
|
50
50
|
"react": ">=18.0",
|
|
51
51
|
"react-dom": ">=18.0"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
|
-
"@
|
|
55
|
-
"@
|
|
56
|
-
"@
|
|
57
|
-
"@
|
|
58
|
-
"@
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"eslint": "^9.20.0",
|
|
62
|
-
"eslint-config-prettier": "^10.0.1",
|
|
54
|
+
"@civet/core": "^2.0.2",
|
|
55
|
+
"@eslint/js": "^9.32.0",
|
|
56
|
+
"@types/react": "^19.1.12",
|
|
57
|
+
"@types/react-dom": "^19.1.9",
|
|
58
|
+
"@vitejs/plugin-react": "^5.0.2",
|
|
59
|
+
"eslint": "^9.34.0",
|
|
60
|
+
"eslint-config-prettier": "^10.1.8",
|
|
63
61
|
"eslint-import-resolver-alias": "^1.1.2",
|
|
64
|
-
"eslint-
|
|
65
|
-
"eslint-plugin-
|
|
66
|
-
"eslint-plugin-
|
|
67
|
-
"eslint-plugin-react-hooks": "^5.2.0
|
|
68
|
-
"eslint-plugin-
|
|
69
|
-
"
|
|
70
|
-
"
|
|
71
|
-
"
|
|
72
|
-
"react
|
|
73
|
-
"
|
|
62
|
+
"eslint-import-resolver-typescript": "^4.4.4",
|
|
63
|
+
"eslint-plugin-import": "^2.32.0",
|
|
64
|
+
"eslint-plugin-prettier": "^5.5.4",
|
|
65
|
+
"eslint-plugin-react-hooks": "^5.2.0",
|
|
66
|
+
"eslint-plugin-react-refresh": "^0.4.20",
|
|
67
|
+
"eslint-plugin-unused-imports": "^4.2.0",
|
|
68
|
+
"globals": "^16.3.0",
|
|
69
|
+
"prettier": "^3.6.2",
|
|
70
|
+
"react": "^19.1.1",
|
|
71
|
+
"react-dom": "^19.1.1",
|
|
72
|
+
"typescript": "^5.9.2",
|
|
73
|
+
"typescript-eslint": "^8.41.0",
|
|
74
|
+
"vite": "^7.1.3",
|
|
75
|
+
"vite-plugin-dts": "^4.5.4",
|
|
76
|
+
"vite-tsconfig-paths": "^5.1.4"
|
|
74
77
|
}
|
|
75
78
|
}
|
package/lib/ConfigProvider.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports["default"] = void 0;
|
|
7
|
-
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
8
|
-
var _EventReceiver = require("./EventReceiver");
|
|
9
|
-
var _context = require("./context");
|
|
10
|
-
var _jsxRuntime = require("react/jsx-runtime");
|
|
11
|
-
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|
12
|
-
var ConfigProvider = function ConfigProvider(_ref) {
|
|
13
|
-
var eventReceiver = _ref.eventReceiver,
|
|
14
|
-
children = _ref.children;
|
|
15
|
-
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_context.ConfigContext.Provider, {
|
|
16
|
-
value: {
|
|
17
|
-
eventReceiver: eventReceiver
|
|
18
|
-
},
|
|
19
|
-
children: children
|
|
20
|
-
});
|
|
21
|
-
};
|
|
22
|
-
ConfigProvider.propTypes = {
|
|
23
|
-
eventReceiver: _EventReceiver.eventReceiverPropType,
|
|
24
|
-
children: _propTypes["default"].node
|
|
25
|
-
};
|
|
26
|
-
var _default = exports["default"] = ConfigProvider;
|
package/lib/EventHandler.js
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports["default"] = void 0;
|
|
7
|
-
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
8
|
-
var _EventReceiver = require("./EventReceiver");
|
|
9
|
-
var _useEventHandler = _interopRequireDefault(require("./useEventHandler"));
|
|
10
|
-
var _jsxRuntime = require("react/jsx-runtime");
|
|
11
|
-
var _excluded = ["children"];
|
|
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 */
|
|
17
|
-
eventReceiver: _EventReceiver.eventReceiverPropType,
|
|
18
|
-
/** ResourceContext to be used */
|
|
19
|
-
resource: _propTypes["default"].object,
|
|
20
|
-
/** Disables the event handler */
|
|
21
|
-
disabled: _propTypes["default"].bool,
|
|
22
|
-
/** Options for the EventReceiver */
|
|
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
|
-
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 */
|
|
27
|
-
onNotify: _propTypes["default"].func,
|
|
28
|
-
children: _propTypes["default"].node
|
|
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;
|
|
47
|
-
var _default = exports["default"] = EventHandler;
|
package/lib/EventReceiver.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.isEventReceiver = exports.eventReceiverPropType = exports["default"] = void 0;
|
|
7
|
-
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
8
|
-
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|
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(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
|
-
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
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
|
-
var EventReceiver = /*#__PURE__*/function () {
|
|
16
|
-
function EventReceiver() {
|
|
17
|
-
_classCallCheck(this, EventReceiver);
|
|
18
|
-
}
|
|
19
|
-
return _createClass(EventReceiver, [{
|
|
20
|
-
key: "subscribe",
|
|
21
|
-
value: function subscribe(resource, options, handler) {
|
|
22
|
-
if (typeof handler !== 'function') {
|
|
23
|
-
throw new Error('Handler must be a function');
|
|
24
|
-
}
|
|
25
|
-
var unsubscribe = this.handleSubscribe(resource, options, handler);
|
|
26
|
-
if (typeof unsubscribe !== 'function') {
|
|
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.');
|
|
28
|
-
}
|
|
29
|
-
return unsubscribe;
|
|
30
|
-
}
|
|
31
|
-
}]);
|
|
32
|
-
}();
|
|
33
|
-
var isEventReceiver = exports.isEventReceiver = function isEventReceiver(eventReceiver) {
|
|
34
|
-
return eventReceiver instanceof EventReceiver;
|
|
35
|
-
};
|
|
36
|
-
var eventReceiverPropType = exports.eventReceiverPropType = _propTypes["default"].instanceOf(EventReceiver);
|
|
37
|
-
var _default = exports["default"] = EventReceiver;
|
package/lib/composeHandlers.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports["default"] = void 0;
|
|
7
|
-
var composeHandlers = function composeHandlers() {
|
|
8
|
-
for (var _len = arguments.length, handlers = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
9
|
-
handlers[_key] = arguments[_key];
|
|
10
|
-
}
|
|
11
|
-
return function () {
|
|
12
|
-
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
|
13
|
-
args[_key2] = arguments[_key2];
|
|
14
|
-
}
|
|
15
|
-
return handlers.reduce(function (sum, handler) {
|
|
16
|
-
return sum || (typeof handler === 'function' ? handler.apply(void 0, args) : false);
|
|
17
|
-
}, false);
|
|
18
|
-
};
|
|
19
|
-
};
|
|
20
|
-
var _default = exports["default"] = composeHandlers;
|
package/lib/context.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.useConfigContext = exports.ConfigContext = void 0;
|
|
7
|
-
var _react = require("react");
|
|
8
|
-
var ConfigContext = exports.ConfigContext = /*#__PURE__*/(0, _react.createContext)({});
|
|
9
|
-
ConfigContext.displayName = 'Events.ConfigContext';
|
|
10
|
-
var useConfigContext = exports.useConfigContext = function useConfigContext() {
|
|
11
|
-
return (0, _react.useContext)(ConfigContext);
|
|
12
|
-
};
|
package/lib/index.js
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
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); }
|
|
4
|
-
Object.defineProperty(exports, "__esModule", {
|
|
5
|
-
value: true
|
|
6
|
-
});
|
|
7
|
-
exports.ConfigConsumer = void 0;
|
|
8
|
-
Object.defineProperty(exports, "ConfigProvider", {
|
|
9
|
-
enumerable: true,
|
|
10
|
-
get: function get() {
|
|
11
|
-
return _ConfigProvider["default"];
|
|
12
|
-
}
|
|
13
|
-
});
|
|
14
|
-
Object.defineProperty(exports, "EventHandler", {
|
|
15
|
-
enumerable: true,
|
|
16
|
-
get: function get() {
|
|
17
|
-
return _EventHandler["default"];
|
|
18
|
-
}
|
|
19
|
-
});
|
|
20
|
-
Object.defineProperty(exports, "EventReceiver", {
|
|
21
|
-
enumerable: true,
|
|
22
|
-
get: function get() {
|
|
23
|
-
return _EventReceiver["default"];
|
|
24
|
-
}
|
|
25
|
-
});
|
|
26
|
-
Object.defineProperty(exports, "composeHandlers", {
|
|
27
|
-
enumerable: true,
|
|
28
|
-
get: function get() {
|
|
29
|
-
return _composeHandlers["default"];
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
Object.defineProperty(exports, "eventReceiverPropType", {
|
|
33
|
-
enumerable: true,
|
|
34
|
-
get: function get() {
|
|
35
|
-
return _EventReceiver.eventReceiverPropType;
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
Object.defineProperty(exports, "isEventReceiver", {
|
|
39
|
-
enumerable: true,
|
|
40
|
-
get: function get() {
|
|
41
|
-
return _EventReceiver.isEventReceiver;
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
Object.defineProperty(exports, "useConfigContext", {
|
|
45
|
-
enumerable: true,
|
|
46
|
-
get: function get() {
|
|
47
|
-
return _context.useConfigContext;
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
Object.defineProperty(exports, "useEventHandler", {
|
|
51
|
-
enumerable: true,
|
|
52
|
-
get: function get() {
|
|
53
|
-
return _useEventHandler["default"];
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
var _context = require("./context");
|
|
57
|
-
var _ConfigProvider = _interopRequireDefault(require("./ConfigProvider"));
|
|
58
|
-
var _EventHandler = _interopRequireDefault(require("./EventHandler"));
|
|
59
|
-
var _EventReceiver = _interopRequireWildcard(require("./EventReceiver"));
|
|
60
|
-
var _composeHandlers = _interopRequireDefault(require("./composeHandlers"));
|
|
61
|
-
var _useEventHandler = _interopRequireDefault(require("./useEventHandler"));
|
|
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
|
-
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(e) { return e && e.__esModule ? e : { "default": e }; }
|
|
65
|
-
var ConfigConsumer = exports.ConfigConsumer = _context.ConfigContext.Consumer;
|
package/lib/useEventHandler.js
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports["default"] = void 0;
|
|
7
|
-
var _core = require("@civet/core");
|
|
8
|
-
var _fastDeepEqual = _interopRequireDefault(require("fast-deep-equal"));
|
|
9
|
-
var _react = require("react");
|
|
10
|
-
var _context = require("./context");
|
|
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
|
-
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(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
|
-
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(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
|
-
*/
|
|
25
|
-
function useEventHandler(_ref) {
|
|
26
|
-
var eventReceiverProp = _ref.eventReceiver,
|
|
27
|
-
resourceProp = _ref.resource,
|
|
28
|
-
disabled = _ref.disabled,
|
|
29
|
-
optionsProp = _ref.options,
|
|
30
|
-
onEvent = _ref.onEvent,
|
|
31
|
-
onNotify = _ref.onNotify;
|
|
32
|
-
var configContext = (0, _context.useConfigContext)();
|
|
33
|
-
var eventReceiver = eventReceiverProp || configContext.eventReceiver;
|
|
34
|
-
var resourceContext = (0, _core.useResourceContext)();
|
|
35
|
-
var currentResource = resourceProp || resourceContext;
|
|
36
|
-
var _useState = (0, _react.useState)(currentResource),
|
|
37
|
-
_useState2 = _slicedToArray(_useState, 2),
|
|
38
|
-
resource = _useState2[0],
|
|
39
|
-
setResource = _useState2[1];
|
|
40
|
-
if ((currentResource === null || currentResource === void 0 ? void 0 : currentResource.request) !== (resource === null || resource === void 0 ? void 0 : resource.request)) {
|
|
41
|
-
setResource(currentResource);
|
|
42
|
-
}
|
|
43
|
-
var _useState3 = (0, _react.useState)(optionsProp),
|
|
44
|
-
_useState4 = _slicedToArray(_useState3, 2),
|
|
45
|
-
options = _useState4[0],
|
|
46
|
-
setOptions = _useState4[1];
|
|
47
|
-
if (!(0, _fastDeepEqual["default"])(options, optionsProp)) {
|
|
48
|
-
setOptions(optionsProp);
|
|
49
|
-
}
|
|
50
|
-
var isDisabled = Boolean(disabled || (currentResource === null || currentResource === void 0 ? void 0 : currentResource.isEmpty));
|
|
51
|
-
(0, _react.useEffect)(function () {
|
|
52
|
-
if (eventReceiver == null || isDisabled) return undefined;
|
|
53
|
-
var unsubscribe = eventReceiver.subscribe(resource, options, function (data) {
|
|
54
|
-
if (((data === null || data === void 0 ? void 0 : data.length) || 0) === 0) return;
|
|
55
|
-
var unhandledEvents;
|
|
56
|
-
if (typeof onEvent === 'function') {
|
|
57
|
-
unhandledEvents = data.filter(function (event) {
|
|
58
|
-
return !onEvent(event);
|
|
59
|
-
});
|
|
60
|
-
} else {
|
|
61
|
-
unhandledEvents = data;
|
|
62
|
-
}
|
|
63
|
-
if (unhandledEvents.length === 0 || typeof (resource === null || resource === void 0 ? void 0 : resource.notify) !== 'function') {
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
var promise = resource.notify();
|
|
67
|
-
if (typeof onNotify === 'function') {
|
|
68
|
-
promise.then(function (result) {
|
|
69
|
-
return onNotify(result, unhandledEvents);
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
return unsubscribe;
|
|
74
|
-
}, [eventReceiver, isDisabled, resource, options, onEvent, onNotify]);
|
|
75
|
-
}
|
|
76
|
-
var _default = exports["default"] = useEventHandler;
|
package/src/ConfigProvider.jsx
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import PropTypes from 'prop-types';
|
|
2
|
-
import { eventReceiverPropType } from './EventReceiver';
|
|
3
|
-
import { ConfigContext } from './context';
|
|
4
|
-
|
|
5
|
-
const ConfigProvider = ({ eventReceiver, children }) => (
|
|
6
|
-
<ConfigContext.Provider value={{ eventReceiver }}>
|
|
7
|
-
{children}
|
|
8
|
-
</ConfigContext.Provider>
|
|
9
|
-
);
|
|
10
|
-
|
|
11
|
-
ConfigProvider.propTypes = {
|
|
12
|
-
eventReceiver: eventReceiverPropType,
|
|
13
|
-
children: PropTypes.node,
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
export default ConfigProvider;
|
package/src/EventHandler.jsx
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import PropTypes from 'prop-types';
|
|
2
|
-
import { eventReceiverPropType } from './EventReceiver';
|
|
3
|
-
import useEventHandler from './useEventHandler';
|
|
4
|
-
|
|
5
|
-
const propTypes = {
|
|
6
|
-
/** EventReceiver to be used */
|
|
7
|
-
eventReceiver: eventReceiverPropType,
|
|
8
|
-
/** ResourceContext to be used */
|
|
9
|
-
resource: PropTypes.object,
|
|
10
|
-
/** Disables the event handler */
|
|
11
|
-
disabled: PropTypes.bool,
|
|
12
|
-
/** Options for the EventReceiver */
|
|
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 */
|
|
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
|
-
onNotify: PropTypes.func,
|
|
18
|
-
children: PropTypes.node,
|
|
19
|
-
};
|
|
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
|
-
|
|
36
|
-
export default EventHandler;
|
package/src/EventReceiver.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import PropTypes from 'prop-types';
|
|
2
|
-
|
|
3
|
-
class EventReceiver {
|
|
4
|
-
subscribe(resource, options, handler) {
|
|
5
|
-
if (typeof handler !== 'function') {
|
|
6
|
-
throw new Error('Handler must be a function');
|
|
7
|
-
}
|
|
8
|
-
const unsubscribe = this.handleSubscribe(resource, options, handler);
|
|
9
|
-
if (typeof unsubscribe !== 'function') {
|
|
10
|
-
console.warn(
|
|
11
|
-
'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.',
|
|
12
|
-
);
|
|
13
|
-
}
|
|
14
|
-
return unsubscribe;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const isEventReceiver = (eventReceiver) =>
|
|
19
|
-
eventReceiver instanceof EventReceiver;
|
|
20
|
-
|
|
21
|
-
const eventReceiverPropType = PropTypes.instanceOf(EventReceiver);
|
|
22
|
-
|
|
23
|
-
export default EventReceiver;
|
|
24
|
-
export { isEventReceiver, eventReceiverPropType };
|
package/src/composeHandlers.js
DELETED
package/src/context.js
DELETED
package/src/index.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { ConfigContext, useConfigContext } from './context';
|
|
2
|
-
|
|
3
|
-
export const { Consumer: ConfigConsumer } = ConfigContext;
|
|
4
|
-
export { default as ConfigProvider } from './ConfigProvider';
|
|
5
|
-
export { default as EventHandler } from './EventHandler';
|
|
6
|
-
export {
|
|
7
|
-
default as EventReceiver,
|
|
8
|
-
eventReceiverPropType,
|
|
9
|
-
isEventReceiver,
|
|
10
|
-
} from './EventReceiver';
|
|
11
|
-
export { default as composeHandlers } from './composeHandlers';
|
|
12
|
-
export { default as useEventHandler } from './useEventHandler';
|
|
13
|
-
export { useConfigContext };
|
package/src/useEventHandler.js
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import { useResourceContext } from '@civet/core';
|
|
2
|
-
import deepEquals from 'fast-deep-equal';
|
|
3
|
-
import { useEffect, useState } from 'react';
|
|
4
|
-
import { useConfigContext } from './context';
|
|
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
|
-
*/
|
|
13
|
-
function useEventHandler({
|
|
14
|
-
/** EventReceiver to be used */
|
|
15
|
-
eventReceiver: eventReceiverProp,
|
|
16
|
-
/** ResourceContext to be used */
|
|
17
|
-
resource: resourceProp,
|
|
18
|
-
/** Disables the event handler */
|
|
19
|
-
disabled,
|
|
20
|
-
/** Options for the EventReceiver */
|
|
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 */
|
|
23
|
-
onEvent,
|
|
24
|
-
/** Provides information on when the resource has been requested to update - events contains the events that lead to the update */
|
|
25
|
-
onNotify,
|
|
26
|
-
}) {
|
|
27
|
-
const configContext = useConfigContext();
|
|
28
|
-
const eventReceiver = eventReceiverProp || configContext.eventReceiver;
|
|
29
|
-
|
|
30
|
-
const resourceContext = useResourceContext();
|
|
31
|
-
const currentResource = resourceProp || resourceContext;
|
|
32
|
-
|
|
33
|
-
const [resource, setResource] = useState(currentResource);
|
|
34
|
-
if (currentResource?.request !== resource?.request) {
|
|
35
|
-
setResource(currentResource);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const [options, setOptions] = useState(optionsProp);
|
|
39
|
-
if (!deepEquals(options, optionsProp)) {
|
|
40
|
-
setOptions(optionsProp);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const isDisabled = Boolean(disabled || currentResource?.isEmpty);
|
|
44
|
-
|
|
45
|
-
useEffect(() => {
|
|
46
|
-
if (eventReceiver == null || isDisabled) return undefined;
|
|
47
|
-
const unsubscribe = eventReceiver.subscribe(resource, options, (data) => {
|
|
48
|
-
if ((data?.length || 0) === 0) return;
|
|
49
|
-
let unhandledEvents;
|
|
50
|
-
if (typeof onEvent === 'function') {
|
|
51
|
-
unhandledEvents = data.filter((event) => !onEvent(event));
|
|
52
|
-
} else {
|
|
53
|
-
unhandledEvents = data;
|
|
54
|
-
}
|
|
55
|
-
if (
|
|
56
|
-
unhandledEvents.length === 0 ||
|
|
57
|
-
typeof resource?.notify !== 'function'
|
|
58
|
-
) {
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
const promise = resource.notify();
|
|
62
|
-
if (typeof onNotify === 'function') {
|
|
63
|
-
promise.then((result) => onNotify(result, unhandledEvents));
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
return unsubscribe;
|
|
67
|
-
}, [eventReceiver, isDisabled, resource, options, onEvent, onNotify]);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export default useEventHandler;
|