@canonical/react-components 2.3.1 → 2.4.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.
@@ -0,0 +1,51 @@
1
+ import { FC, ReactNode } from "react";
2
+ import React from "react";
3
+ export interface EventCallback<T> {
4
+ onSuccess: (event: T) => void;
5
+ onFailure: (msg: string) => void;
6
+ onFinish?: () => void;
7
+ }
8
+ export interface EventQueue<T> {
9
+ get: (operationId: string) => EventCallback<T> | undefined;
10
+ set: (operationId: string, onSuccess: (event: T) => void, onFailure: (msg: string) => void, onFinish?: () => void) => void;
11
+ remove: (operationId: string) => void;
12
+ }
13
+ /**
14
+ * This provides an event queue system for managing callbacks associated with
15
+ * asynchronous operations (e.g., API calls) in an application.
16
+ *
17
+ * It allows components to register success, failure,
18
+ * and optional finish handlers for a given operation ID, and later retrieve or remove them.
19
+ *
20
+ * This is useful for handling side effects when dealing
21
+ * with multiple operations that need to be tracked and updated independently.
22
+ *
23
+ * The `createEventQueue` function should be used to create a single provider and context that are shared throughout your application.
24
+ * The returned `EventQueueProvider` and `useEventQueue` hook should be exported
25
+ * from a shared module and reused across the app to ensure a single
26
+ * context instance is used.
27
+ *
28
+ * Usage pattern:
29
+ * // eventQueue.ts
30
+ * export const { EventQueueProvider, useEventQueue } = createEventQueue<EventType>();
31
+ *
32
+ * // App.tsx
33
+ * import { EventQueueProvider } from "./eventQueue";
34
+ * ...
35
+ * <EventQueueProvider>
36
+ * <App />
37
+ * </EventQueueProvider>
38
+ *
39
+ * // In any other component
40
+ * import { useEventQueue } from "./eventQueue";
41
+ * ...
42
+ * const eventQueue = useEventQueue();
43
+ * eventQueue.set(operationId, onSuccess, onFailure);
44
+ */
45
+ export declare function createEventQueue<T>(): {
46
+ EventQueueProvider: FC<{
47
+ children: ReactNode;
48
+ }>;
49
+ useEventQueue: () => EventQueue<T>;
50
+ EventQueueContext: React.Context<EventQueue<T>>;
51
+ };
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createEventQueue = createEventQueue;
7
+ var _react = _interopRequireWildcard(require("react"));
8
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
9
+ 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 && Object.prototype.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; }
10
+ /**
11
+ * This provides an event queue system for managing callbacks associated with
12
+ * asynchronous operations (e.g., API calls) in an application.
13
+ *
14
+ * It allows components to register success, failure,
15
+ * and optional finish handlers for a given operation ID, and later retrieve or remove them.
16
+ *
17
+ * This is useful for handling side effects when dealing
18
+ * with multiple operations that need to be tracked and updated independently.
19
+ *
20
+ * The `createEventQueue` function should be used to create a single provider and context that are shared throughout your application.
21
+ * The returned `EventQueueProvider` and `useEventQueue` hook should be exported
22
+ * from a shared module and reused across the app to ensure a single
23
+ * context instance is used.
24
+ *
25
+ * Usage pattern:
26
+ * // eventQueue.ts
27
+ * export const { EventQueueProvider, useEventQueue } = createEventQueue<EventType>();
28
+ *
29
+ * // App.tsx
30
+ * import { EventQueueProvider } from "./eventQueue";
31
+ * ...
32
+ * <EventQueueProvider>
33
+ * <App />
34
+ * </EventQueueProvider>
35
+ *
36
+ * // In any other component
37
+ * import { useEventQueue } from "./eventQueue";
38
+ * ...
39
+ * const eventQueue = useEventQueue();
40
+ * eventQueue.set(operationId, onSuccess, onFailure);
41
+ */
42
+
43
+ function createEventQueue() {
44
+ const EventQueueContext = /*#__PURE__*/(0, _react.createContext)(undefined);
45
+ const eventQueue = new Map();
46
+ const EventQueueProvider = _ref => {
47
+ let {
48
+ children
49
+ } = _ref;
50
+ return /*#__PURE__*/_react.default.createElement(EventQueueContext.Provider, {
51
+ value: {
52
+ get: operationId => eventQueue.get(operationId),
53
+ set: (operationId, onSuccess, onFailure, onFinish) => eventQueue.set(operationId, {
54
+ onSuccess,
55
+ onFailure,
56
+ onFinish
57
+ }),
58
+ remove: operationId => eventQueue.delete(operationId)
59
+ }
60
+ }, children);
61
+ };
62
+ const useEventQueue = () => {
63
+ const context = (0, _react.useContext)(EventQueueContext);
64
+ if (!context) {
65
+ throw new Error("useEventQueue must be used within an EventQueueProvider");
66
+ }
67
+ return context;
68
+ };
69
+ return {
70
+ EventQueueProvider,
71
+ useEventQueue,
72
+ EventQueueContext
73
+ };
74
+ }
@@ -0,0 +1,2 @@
1
+ export { createEventQueue } from "./EventQueue";
2
+ export type { EventCallback, EventQueue } from "./EventQueue";
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "createEventQueue", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _EventQueue.createEventQueue;
10
+ }
11
+ });
12
+ var _EventQueue = require("./EventQueue");
@@ -0,0 +1,51 @@
1
+ import { FC, ReactNode } from "react";
2
+ import React from "react";
3
+ export interface EventCallback<T> {
4
+ onSuccess: (event: T) => void;
5
+ onFailure: (msg: string) => void;
6
+ onFinish?: () => void;
7
+ }
8
+ export interface EventQueue<T> {
9
+ get: (operationId: string) => EventCallback<T> | undefined;
10
+ set: (operationId: string, onSuccess: (event: T) => void, onFailure: (msg: string) => void, onFinish?: () => void) => void;
11
+ remove: (operationId: string) => void;
12
+ }
13
+ /**
14
+ * This provides an event queue system for managing callbacks associated with
15
+ * asynchronous operations (e.g., API calls) in an application.
16
+ *
17
+ * It allows components to register success, failure,
18
+ * and optional finish handlers for a given operation ID, and later retrieve or remove them.
19
+ *
20
+ * This is useful for handling side effects when dealing
21
+ * with multiple operations that need to be tracked and updated independently.
22
+ *
23
+ * The `createEventQueue` function should be used to create a single provider and context that are shared throughout your application.
24
+ * The returned `EventQueueProvider` and `useEventQueue` hook should be exported
25
+ * from a shared module and reused across the app to ensure a single
26
+ * context instance is used.
27
+ *
28
+ * Usage pattern:
29
+ * // eventQueue.ts
30
+ * export const { EventQueueProvider, useEventQueue } = createEventQueue<EventType>();
31
+ *
32
+ * // App.tsx
33
+ * import { EventQueueProvider } from "./eventQueue";
34
+ * ...
35
+ * <EventQueueProvider>
36
+ * <App />
37
+ * </EventQueueProvider>
38
+ *
39
+ * // In any other component
40
+ * import { useEventQueue } from "./eventQueue";
41
+ * ...
42
+ * const eventQueue = useEventQueue();
43
+ * eventQueue.set(operationId, onSuccess, onFailure);
44
+ */
45
+ export declare function createEventQueue<T>(): {
46
+ EventQueueProvider: FC<{
47
+ children: ReactNode;
48
+ }>;
49
+ useEventQueue: () => EventQueue<T>;
50
+ EventQueueContext: React.Context<EventQueue<T>>;
51
+ };
@@ -0,0 +1,67 @@
1
+ import { createContext, useContext } from "react";
2
+ import React from "react";
3
+ /**
4
+ * This provides an event queue system for managing callbacks associated with
5
+ * asynchronous operations (e.g., API calls) in an application.
6
+ *
7
+ * It allows components to register success, failure,
8
+ * and optional finish handlers for a given operation ID, and later retrieve or remove them.
9
+ *
10
+ * This is useful for handling side effects when dealing
11
+ * with multiple operations that need to be tracked and updated independently.
12
+ *
13
+ * The `createEventQueue` function should be used to create a single provider and context that are shared throughout your application.
14
+ * The returned `EventQueueProvider` and `useEventQueue` hook should be exported
15
+ * from a shared module and reused across the app to ensure a single
16
+ * context instance is used.
17
+ *
18
+ * Usage pattern:
19
+ * // eventQueue.ts
20
+ * export const { EventQueueProvider, useEventQueue } = createEventQueue<EventType>();
21
+ *
22
+ * // App.tsx
23
+ * import { EventQueueProvider } from "./eventQueue";
24
+ * ...
25
+ * <EventQueueProvider>
26
+ * <App />
27
+ * </EventQueueProvider>
28
+ *
29
+ * // In any other component
30
+ * import { useEventQueue } from "./eventQueue";
31
+ * ...
32
+ * const eventQueue = useEventQueue();
33
+ * eventQueue.set(operationId, onSuccess, onFailure);
34
+ */
35
+
36
+ export function createEventQueue() {
37
+ var EventQueueContext = /*#__PURE__*/createContext(undefined);
38
+ var eventQueue = new Map();
39
+ var EventQueueProvider = _ref => {
40
+ var {
41
+ children
42
+ } = _ref;
43
+ return /*#__PURE__*/React.createElement(EventQueueContext.Provider, {
44
+ value: {
45
+ get: operationId => eventQueue.get(operationId),
46
+ set: (operationId, onSuccess, onFailure, onFinish) => eventQueue.set(operationId, {
47
+ onSuccess,
48
+ onFailure,
49
+ onFinish
50
+ }),
51
+ remove: operationId => eventQueue.delete(operationId)
52
+ }
53
+ }, children);
54
+ };
55
+ var useEventQueue = () => {
56
+ var context = useContext(EventQueueContext);
57
+ if (!context) {
58
+ throw new Error("useEventQueue must be used within an EventQueueProvider");
59
+ }
60
+ return context;
61
+ };
62
+ return {
63
+ EventQueueProvider,
64
+ useEventQueue,
65
+ EventQueueContext
66
+ };
67
+ }
@@ -0,0 +1,2 @@
1
+ export { createEventQueue } from "./EventQueue";
2
+ export type { EventCallback, EventQueue } from "./EventQueue";
@@ -0,0 +1 @@
1
+ export { createEventQueue } from "./EventQueue";
@@ -21,6 +21,7 @@ export { default as ConfirmationModal } from "./components/ConfirmationModal";
21
21
  export { default as ContextualMenu } from "./components/ContextualMenu";
22
22
  export { default as DoughnutChart } from "./components/DoughnutChart";
23
23
  export { default as EmptyState } from "./components/EmptyState";
24
+ export { createEventQueue } from "./components/EventQueue";
24
25
  export { default as Field } from "./components/Field";
25
26
  export { default as Form } from "./components/Form";
26
27
  export { default as FormikField } from "./components/FormikField";
@@ -92,6 +93,7 @@ export type { ConfirmationModalProps } from "./components/ConfirmationModal";
92
93
  export type { ContextualMenuProps, ContextualMenuDropdownProps, MenuLink, Position, } from "./components/ContextualMenu";
93
94
  export type { DoughnutChartProps, Segment } from "./components/DoughnutChart";
94
95
  export type { EmptyStateProps } from "./components/EmptyState";
96
+ export type { EventCallback, EventQueue } from "./components/EventQueue";
95
97
  export type { FieldProps } from "./components/Field";
96
98
  export type { FormProps } from "./components/Form";
97
99
  export type { FormikFieldProps } from "./components/FormikField";
package/dist/esm/index.js CHANGED
@@ -21,6 +21,7 @@ export { default as ConfirmationModal } from "./components/ConfirmationModal";
21
21
  export { default as ContextualMenu } from "./components/ContextualMenu";
22
22
  export { default as DoughnutChart } from "./components/DoughnutChart";
23
23
  export { default as EmptyState } from "./components/EmptyState";
24
+ export { createEventQueue } from "./components/EventQueue";
24
25
  export { default as Field } from "./components/Field";
25
26
  export { default as Form } from "./components/Form";
26
27
  export { default as FormikField } from "./components/FormikField";
package/dist/index.d.ts CHANGED
@@ -21,6 +21,7 @@ export { default as ConfirmationModal } from "./components/ConfirmationModal";
21
21
  export { default as ContextualMenu } from "./components/ContextualMenu";
22
22
  export { default as DoughnutChart } from "./components/DoughnutChart";
23
23
  export { default as EmptyState } from "./components/EmptyState";
24
+ export { createEventQueue } from "./components/EventQueue";
24
25
  export { default as Field } from "./components/Field";
25
26
  export { default as Form } from "./components/Form";
26
27
  export { default as FormikField } from "./components/FormikField";
@@ -92,6 +93,7 @@ export type { ConfirmationModalProps } from "./components/ConfirmationModal";
92
93
  export type { ContextualMenuProps, ContextualMenuDropdownProps, MenuLink, Position, } from "./components/ContextualMenu";
93
94
  export type { DoughnutChartProps, Segment } from "./components/DoughnutChart";
94
95
  export type { EmptyStateProps } from "./components/EmptyState";
96
+ export type { EventCallback, EventQueue } from "./components/EventQueue";
95
97
  export type { FieldProps } from "./components/Field";
96
98
  export type { FormProps } from "./components/Form";
97
99
  export type { FormikFieldProps } from "./components/FormikField";
package/dist/index.js CHANGED
@@ -29,6 +29,7 @@ var _exportNames = {
29
29
  ContextualMenu: true,
30
30
  DoughnutChart: true,
31
31
  EmptyState: true,
32
+ createEventQueue: true,
32
33
  Field: true,
33
34
  Form: true,
34
35
  FormikField: true,
@@ -584,6 +585,12 @@ Object.defineProperty(exports, "Tooltip", {
584
585
  return _Tooltip.default;
585
586
  }
586
587
  });
588
+ Object.defineProperty(exports, "createEventQueue", {
589
+ enumerable: true,
590
+ get: function () {
591
+ return _EventQueue.createEventQueue;
592
+ }
593
+ });
587
594
  Object.defineProperty(exports, "failure", {
588
595
  enumerable: true,
589
596
  get: function () {
@@ -721,6 +728,7 @@ var _ConfirmationModal = _interopRequireDefault(require("./components/Confirmati
721
728
  var _ContextualMenu = _interopRequireDefault(require("./components/ContextualMenu"));
722
729
  var _DoughnutChart = _interopRequireDefault(require("./components/DoughnutChart"));
723
730
  var _EmptyState = _interopRequireDefault(require("./components/EmptyState"));
731
+ var _EventQueue = require("./components/EventQueue");
724
732
  var _Field = _interopRequireDefault(require("./components/Field"));
725
733
  var _Form = _interopRequireDefault(require("./components/Form"));
726
734
  var _FormikField = _interopRequireDefault(require("./components/FormikField"));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canonical/react-components",
3
- "version": "2.3.1",
3
+ "version": "2.4.0",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "author": {