@civet/events 3.0.0 → 4.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.
@@ -7,7 +7,7 @@ import { GenericEventReceiver, InferEvent, InferOptions, InferResource } from '.
7
7
  *
8
8
  * onEvent can be used to directly access events allowing you to add custom event logic to your components.
9
9
  */
10
- export default function EventHandler<EventReceiverI extends GenericEventReceiver, ResourceI extends InferResource<EventReceiverI> = InferResource<EventReceiverI>, OptionsI extends InferOptions<EventReceiverI> = InferOptions<EventReceiverI>, EventI extends InferEvent<EventReceiverI> = InferEvent<EventReceiverI>>({ eventReceiver, resource, disabled, options, onEvent, onNotify, children, }: {
10
+ export default function EventHandler<EventReceiverI extends GenericEventReceiver, EventI extends InferEvent<EventReceiverI> = InferEvent<EventReceiverI>, ResourceI extends InferResource<EventReceiverI> = InferResource<EventReceiverI>, OptionsI extends InferOptions<EventReceiverI> = InferOptions<EventReceiverI>>({ eventReceiver, resource, disabled, options, onEvent, onNotify, children, }: {
11
11
  /** EventReceiver to be used */
12
12
  eventReceiver?: EventReceiverI;
13
13
  /** ResourceContext to be used */
@@ -1,16 +1,16 @@
1
- import { Constructor, GenericDataProvider, ResourceContextValue } from '@civet/core';
2
- export default abstract class EventReceiver<Resource extends ResourceContextValue<GenericDataProvider>, Event, Options> {
3
- readonly _inferResource: Resource;
1
+ import { Constructor, GenericDataProvider, Notifier, ResourceContextValue } from '@civet/core';
2
+ export default abstract class EventReceiver<Event, Resource extends ResourceContextValue<GenericDataProvider>, Options> {
4
3
  readonly _inferEvent: Event;
4
+ readonly _inferResource: Resource;
5
5
  readonly _inferOptions: Options;
6
- subscribe<ResourceI extends Resource = Resource, OptionsI extends Options = Options, EventI extends Event = Event>(resource: ResourceI, options: OptionsI | undefined, handler: (events: EventI[]) => void): () => void;
7
- abstract handleSubscribe(resource: Resource, options: Options | undefined, handler: (events: Event[]) => void): () => void;
6
+ subscribe<EventI extends Event = Event, ResourceI extends Resource = Resource, OptionsI extends Options = Options>(resource: Notifier<[ResourceI | undefined]>, options: OptionsI | undefined, handler: (events: EventI[]) => void): () => void;
7
+ abstract handleSubscribe(resource: Notifier<[Resource | undefined]>, options: Options | undefined, handler: (events: Event[]) => void): () => void;
8
8
  }
9
9
  export declare const isEventReceiver: (eventReceiver: unknown) => boolean;
10
10
  export type EventReceiverImplementation<EventReceiverI extends GenericEventReceiver, ConstructorArgs extends any[]> = Constructor<ConstructorArgs, EventReceiverI & {
11
11
  handleSubscribe(resource: InferResource<EventReceiverI>, options: InferOptions<EventReceiverI> | undefined, handler: (events: InferEvent<EventReceiverI>[]) => void): () => void;
12
12
  }>;
13
- export type GenericEventReceiver = EventReceiver<ResourceContextValue<GenericDataProvider>, unknown, unknown>;
14
- export type InferResource<EventReceiverI extends GenericEventReceiver> = EventReceiverI['_inferResource'];
13
+ export type GenericEventReceiver = EventReceiver<unknown, ResourceContextValue<GenericDataProvider>, unknown>;
15
14
  export type InferEvent<EventReceiverI extends GenericEventReceiver> = EventReceiverI['_inferEvent'];
15
+ export type InferResource<EventReceiverI extends GenericEventReceiver> = EventReceiverI['_inferResource'];
16
16
  export type InferOptions<EventReceiverI extends GenericEventReceiver> = EventReceiverI['_inferOptions'];
@@ -1,6 +1,6 @@
1
1
  import { GenericDataProviderImplementation } from '@civet/core';
2
2
  import { GenericEventReceiver, InferEvent, InferOptions } from './EventReceiver';
3
- type EventProps<EventReceiverI extends GenericEventReceiver, OptionsI extends InferOptions<EventReceiverI> = InferOptions<EventReceiverI>, EventI extends InferEvent<EventReceiverI> = InferEvent<EventReceiverI>> = {
3
+ type EventProps<EventReceiverI extends GenericEventReceiver, EventI extends InferEvent<EventReceiverI> = InferEvent<EventReceiverI>, OptionsI extends InferOptions<EventReceiverI> = InferOptions<EventReceiverI>> = {
4
4
  events?: {
5
5
  /** EventReceiver to be used */
6
6
  eventReceiver?: EventReceiverI;
package/dist/main.d.ts CHANGED
@@ -2,6 +2,7 @@ import { ConfigConsumer, useConfigContext } from './context';
2
2
  export { default as composeHandlers } from './composeHandlers';
3
3
  export { default as ConfigProvider } from './ConfigProvider';
4
4
  export { default as EventHandler } from './EventHandler';
5
+ export { default as eventPlugin } from './eventPlugin';
5
6
  export { default as EventReceiver, isEventReceiver } from './EventReceiver';
6
7
  export type { EventReceiverImplementation, GenericEventReceiver, InferEvent, InferOptions, InferResource, } from './EventReceiver';
7
8
  export { default as useEventHandler } from './useEventHandler';
package/dist/main.js CHANGED
@@ -1,124 +1,159 @@
1
- import { createContext as R, useContext as j, useMemo as q, useState as y, useEffect as D } from "react";
2
- import { jsx as E, Fragment as S } from "react/jsx-runtime";
3
- import { useResourceContext as w } from "@civet/core";
4
- const l = R({});
1
+ import { createContext as H, useContext as P, useMemo as N, useState as y, useEffect as q } from "react";
2
+ import { jsx as x, Fragment as A } from "react/jsx-runtime";
3
+ import { useResourceContext as M, Notifier as B, createPlugin as I } from "@civet/core";
4
+ const l = H({});
5
5
  l.displayName = "Events.ConfigContext";
6
- const J = l.Consumer, _ = () => j(l);
7
- function K(...r) {
8
- return (...e) => r.reduce(
9
- (t, n) => t || (typeof n == "function" ? n(...e) : !1),
6
+ const V = l.Consumer, k = () => P(l);
7
+ function W(...o) {
8
+ return (...e) => o.reduce(
9
+ (t, r) => t || (typeof r == "function" ? r(...e) : !1),
10
10
  !1
11
11
  );
12
12
  }
13
- class F {
14
- _inferResource;
13
+ class z {
15
14
  _inferEvent;
15
+ _inferResource;
16
16
  _inferOptions;
17
- subscribe(e, t, n) {
18
- if (typeof n != "function")
17
+ subscribe(e, t, r) {
18
+ if (typeof r != "function")
19
19
  throw new Error("Handler must be a function");
20
- const o = this.handleSubscribe(
20
+ const n = this.handleSubscribe(
21
21
  e,
22
22
  t,
23
- n
23
+ r
24
24
  );
25
- return typeof o != "function" && console.warn(
25
+ return typeof n != "function" && console.warn(
26
26
  "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."
27
- ), o;
27
+ ), n;
28
28
  }
29
29
  }
30
- const L = (r) => r instanceof F;
31
- function Q({
32
- eventReceiver: r,
30
+ const X = (o) => o instanceof z;
31
+ function Y({
32
+ eventReceiver: o,
33
33
  children: e
34
34
  }) {
35
- const t = q(() => ({ eventReceiver: r }), [r]);
36
- return /* @__PURE__ */ E(l.Provider, { value: t, children: e });
35
+ const t = N(() => ({ eventReceiver: o }), [o]);
36
+ return /* @__PURE__ */ x(l.Provider, { value: t, children: e });
37
37
  }
38
- function H(r) {
39
- return r && r.__esModule && Object.prototype.hasOwnProperty.call(r, "default") ? r.default : r;
38
+ function G(o) {
39
+ return o && o.__esModule && Object.prototype.hasOwnProperty.call(o, "default") ? o.default : o;
40
40
  }
41
- var g, C;
42
- function A() {
43
- return C || (C = 1, g = function r(e, t) {
41
+ var E, h;
42
+ function J() {
43
+ return h || (h = 1, E = function o(e, t) {
44
44
  if (e === t) return !0;
45
45
  if (e && t && typeof e == "object" && typeof t == "object") {
46
46
  if (e.constructor !== t.constructor) return !1;
47
- var n, o, s;
47
+ var r, n, s;
48
48
  if (Array.isArray(e)) {
49
- if (n = e.length, n != t.length) return !1;
50
- for (o = n; o-- !== 0; )
51
- if (!r(e[o], t[o])) return !1;
49
+ if (r = e.length, r != t.length) return !1;
50
+ for (n = r; n-- !== 0; )
51
+ if (!o(e[n], t[n])) return !1;
52
52
  return !0;
53
53
  }
54
54
  if (e.constructor === RegExp) return e.source === t.source && e.flags === t.flags;
55
55
  if (e.valueOf !== Object.prototype.valueOf) return e.valueOf() === t.valueOf();
56
56
  if (e.toString !== Object.prototype.toString) return e.toString() === t.toString();
57
- if (s = Object.keys(e), n = s.length, n !== Object.keys(t).length) return !1;
58
- for (o = n; o-- !== 0; )
59
- if (!Object.prototype.hasOwnProperty.call(t, s[o])) return !1;
60
- for (o = n; o-- !== 0; ) {
61
- var u = s[o];
62
- if (!r(e[u], t[u])) return !1;
57
+ if (s = Object.keys(e), r = s.length, r !== Object.keys(t).length) return !1;
58
+ for (n = r; n-- !== 0; )
59
+ if (!Object.prototype.hasOwnProperty.call(t, s[n])) return !1;
60
+ for (n = r; n-- !== 0; ) {
61
+ var c = s[n];
62
+ if (!o(e[c], t[c])) return !1;
63
63
  }
64
64
  return !0;
65
65
  }
66
66
  return e !== e && t !== t;
67
- }), g;
67
+ }), E;
68
68
  }
69
- var M = A();
70
- const P = /* @__PURE__ */ H(M);
71
- function B({
72
- eventReceiver: r,
69
+ var K = J();
70
+ const L = /* @__PURE__ */ G(K);
71
+ function j({
72
+ eventReceiver: o,
73
73
  resource: e,
74
74
  disabled: t,
75
- options: n,
76
- onEvent: o,
75
+ options: r,
76
+ onEvent: n,
77
77
  onNotify: s
78
78
  }) {
79
- const u = _(), a = r || u.eventReceiver, b = w(), f = e || b, [c, O] = y(f);
80
- f?.request !== c?.request && O(f);
81
- const [p, h] = y(n);
82
- P(p, n) || h(n);
83
- const m = !!(t || f?.isDisabled);
84
- D(() => a == null || m ? void 0 : a.subscribe(
85
- c,
86
- p,
87
- (v) => {
88
- if ((v?.length || 0) === 0) return;
89
- let i;
90
- if (typeof o == "function" ? i = v.filter((d) => !o(d)) : i = v, i.length === 0 || typeof c?.notify != "function")
91
- return;
92
- const x = c.notify();
93
- typeof s == "function" && x.then((d) => s(d, i));
79
+ const c = k(), a = o || c.eventReceiver, D = M(), u = e === null ? void 0 : e || D, [i, w] = y(
80
+ u
81
+ );
82
+ u?.request !== i?.request && w(u);
83
+ const [p, S] = y(r);
84
+ L(p, r) || S(r);
85
+ const C = !!(t || u?.isDisabled), [
86
+ [R, b, v] = [],
87
+ O
88
+ ] = y();
89
+ q(() => {
90
+ if (a == null || C) {
91
+ O(void 0);
92
+ return;
94
93
  }
95
- ), [a, m, c, p, o, s]);
94
+ const d = new B(), _ = a.subscribe(
95
+ d,
96
+ p,
97
+ (g) => {
98
+ if ((g?.length || 0) === 0) return;
99
+ let f;
100
+ if (typeof n == "function" ? f = g.filter((m) => !n(m)) : f = g, f.length === 0 || typeof i?.notify != "function")
101
+ return;
102
+ const F = i.notify();
103
+ typeof s == "function" && F.then((m) => s(m, f));
104
+ }
105
+ );
106
+ return d.trigger(i), O([
107
+ i?.request,
108
+ i?.revision,
109
+ d
110
+ ]), _;
111
+ }, [a, C, i, p, n, s]), q(() => {
112
+ v == null || u?.request !== R || u?.revision === b || v.trigger(u);
113
+ }, [
114
+ v,
115
+ u,
116
+ R,
117
+ b
118
+ ]);
96
119
  }
97
- function T({
98
- eventReceiver: r,
120
+ function Z({
121
+ eventReceiver: o,
99
122
  resource: e,
100
123
  disabled: t,
101
- options: n,
102
- onEvent: o,
124
+ options: r,
125
+ onEvent: n,
103
126
  onNotify: s,
104
- children: u
127
+ children: c
105
128
  }) {
106
- return B({
107
- eventReceiver: r,
129
+ return j({
130
+ eventReceiver: o,
108
131
  resource: e,
109
132
  disabled: t,
110
- options: n,
111
- onEvent: o,
133
+ options: r,
134
+ onEvent: n,
112
135
  onNotify: s
113
- }), /* @__PURE__ */ E(S, { children: u });
136
+ }), /* @__PURE__ */ x(A, { children: c });
114
137
  }
138
+ const $ = I(
139
+ (o) => class extends o {
140
+ extend(t) {
141
+ t.context(
142
+ (r, { events: n = !1 }) => (j(
143
+ typeof n == "boolean" ? { disabled: !n, resource: r } : { ...n, resource: r }
144
+ ), r)
145
+ );
146
+ }
147
+ }
148
+ );
115
149
  export {
116
- J as ConfigConsumer,
117
- Q as ConfigProvider,
118
- T as EventHandler,
119
- F as EventReceiver,
120
- K as composeHandlers,
121
- L as isEventReceiver,
122
- _ as useConfigContext,
123
- B as useEventHandler
150
+ V as ConfigConsumer,
151
+ Y as ConfigProvider,
152
+ Z as EventHandler,
153
+ z as EventReceiver,
154
+ W as composeHandlers,
155
+ $ as eventPlugin,
156
+ X as isEventReceiver,
157
+ k as useConfigContext,
158
+ j as useEventHandler
124
159
  };
@@ -6,11 +6,11 @@ import { GenericEventReceiver, InferEvent, InferOptions, InferResource } from '.
6
6
  *
7
7
  * onEvent can be used to directly access events allowing you to add custom event logic to your components.
8
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, }: {
9
+ export default function useEventHandler<EventReceiverI extends GenericEventReceiver, EventI extends InferEvent<EventReceiverI> = InferEvent<EventReceiverI>, ResourceI extends InferResource<EventReceiverI> = InferResource<EventReceiverI>, OptionsI extends InferOptions<EventReceiverI> = InferOptions<EventReceiverI>>({ eventReceiver: eventReceiverProp, resource: resourceProp, disabled, options: optionsProp, onEvent, onNotify, }: {
10
10
  /** EventReceiver to be used */
11
11
  eventReceiver?: EventReceiverI;
12
12
  /** ResourceContext to be used */
13
- resource?: ResourceI;
13
+ resource?: ResourceI | null;
14
14
  /** Disables the event handler */
15
15
  disabled?: boolean;
16
16
  /** Options for the EventReceiver */
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@civet/events",
3
- "version": "3.0.0",
3
+ "version": "4.0.0",
4
4
  "description": "Civet",
5
5
  "author": {
6
6
  "name": "Aaron Burmeister"
7
7
  },
8
8
  "repository": {
9
9
  "type": "git",
10
- "url": "https://github.com/civet-org/events.git"
10
+ "url": "git+https://github.com/civet-org/events.git"
11
11
  },
12
12
  "bugs": {
13
13
  "url": "https://github.com/civet-org/events/issues"
@@ -55,7 +55,7 @@
55
55
  "react-dom": ">=18.0"
56
56
  },
57
57
  "devDependencies": {
58
- "@civet/core": "^6.0.2",
58
+ "@civet/core": "^6.1.1",
59
59
  "@eslint/js": "^9.35.0",
60
60
  "@types/react": "^19.1.12",
61
61
  "@types/react-dom": "^19.1.9",