@civet/events 3.0.1 → 4.0.1

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 { Notifier, Constructor, GenericDataProvider, 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]> | 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.js CHANGED
@@ -1,46 +1,48 @@
1
- import { createContext as j, useContext as q, useMemo as D, useState as y, useEffect as S } from "react";
2
- import { jsx as b, Fragment as w } from "react/jsx-runtime";
3
- import { useResourceContext as P, createPlugin as _ } from "@civet/core";
4
- const l = j({});
1
+ import { createContext as P, useContext as N, useMemo as A, useState as y, useEffect as h } from "react";
2
+ import { jsx as x, Fragment as M } from "react/jsx-runtime";
3
+ import { Notifier as j, useResourceContext as B, createPlugin as I } from "@civet/core";
4
+ const l = P({});
5
5
  l.displayName = "Events.ConfigContext";
6
- const K = l.Consumer, F = () => q(l);
7
- function L(...o) {
6
+ const V = l.Consumer, k = () => N(l);
7
+ function W(...o) {
8
8
  return (...e) => o.reduce(
9
9
  (t, r) => t || (typeof r == "function" ? r(...e) : !1),
10
10
  !1
11
11
  );
12
12
  }
13
- class H {
14
- _inferResource;
13
+ class z {
15
14
  _inferEvent;
15
+ _inferResource;
16
16
  _inferOptions;
17
17
  subscribe(e, t, r) {
18
18
  if (typeof r != "function")
19
19
  throw new Error("Handler must be a function");
20
- const n = this.handleSubscribe(
20
+ const n = e != null;
21
+ n || (e = new j());
22
+ const s = this.handleSubscribe(
21
23
  e,
22
24
  t,
23
25
  r
24
26
  );
25
- return typeof n != "function" && console.warn(
27
+ return n || e?.trigger(void 0), typeof s != "function" && console.warn(
26
28
  "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
- ), n;
29
+ ), s;
28
30
  }
29
31
  }
30
- const Q = (o) => o instanceof H;
31
- function T({
32
+ const X = (o) => o instanceof z;
33
+ function Y({
32
34
  eventReceiver: o,
33
35
  children: e
34
36
  }) {
35
- const t = D(() => ({ eventReceiver: o }), [o]);
36
- return /* @__PURE__ */ b(l.Provider, { value: t, children: e });
37
+ const t = A(() => ({ eventReceiver: o }), [o]);
38
+ return /* @__PURE__ */ x(l.Provider, { value: t, children: e });
37
39
  }
38
- function A(o) {
40
+ function G(o) {
39
41
  return o && o.__esModule && Object.prototype.hasOwnProperty.call(o, "default") ? o.default : o;
40
42
  }
41
- var g, E;
42
- function M() {
43
- return E || (E = 1, g = function o(e, t) {
43
+ var E, q;
44
+ function J() {
45
+ return q || (q = 1, E = function o(e, t) {
44
46
  if (e === t) return !0;
45
47
  if (e && t && typeof e == "object" && typeof t == "object") {
46
48
  if (e.constructor !== t.constructor) return !1;
@@ -58,17 +60,17 @@ function M() {
58
60
  for (n = r; n-- !== 0; )
59
61
  if (!Object.prototype.hasOwnProperty.call(t, s[n])) return !1;
60
62
  for (n = r; n-- !== 0; ) {
61
- var u = s[n];
62
- if (!o(e[u], t[u])) return !1;
63
+ var c = s[n];
64
+ if (!o(e[c], t[c])) return !1;
63
65
  }
64
66
  return !0;
65
67
  }
66
68
  return e !== e && t !== t;
67
- }), g;
69
+ }), E;
68
70
  }
69
- var B = M();
70
- const I = /* @__PURE__ */ A(B);
71
- function C({
71
+ var K = J();
72
+ const L = /* @__PURE__ */ G(K);
73
+ function w({
72
74
  eventReceiver: o,
73
75
  resource: e,
74
76
  disabled: t,
@@ -76,47 +78,70 @@ function C({
76
78
  onEvent: n,
77
79
  onNotify: s
78
80
  }) {
79
- const u = F(), a = o || u.eventReceiver, O = P(), f = e || O, [c, h] = y(f);
80
- f?.request !== c?.request && h(f);
81
- const [p, x] = y(r);
82
- I(p, r) || x(r);
83
- const m = !!(t || f?.isDisabled);
84
- S(() => 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 n == "function" ? i = v.filter((d) => !n(d)) : i = v, i.length === 0 || typeof c?.notify != "function")
91
- return;
92
- const R = c.notify();
93
- typeof s == "function" && R.then((d) => s(d, i));
81
+ const c = k(), a = o || c.eventReceiver, D = B(), u = e === null ? void 0 : e || D, [i, S] = y(
82
+ u
83
+ );
84
+ u?.request !== i?.request && S(u);
85
+ const [p, _] = y(r);
86
+ L(p, r) || _(r);
87
+ const C = !!(t || u?.isDisabled), [
88
+ [R, b, v] = [],
89
+ O
90
+ ] = y();
91
+ h(() => {
92
+ if (a == null || C) {
93
+ O(void 0);
94
+ return;
94
95
  }
95
- ), [a, m, c, p, n, s]);
96
+ const d = new j(), F = a.subscribe(
97
+ d,
98
+ p,
99
+ (g) => {
100
+ if ((g?.length || 0) === 0) return;
101
+ let f;
102
+ if (typeof n == "function" ? f = g.filter((m) => !n(m)) : f = g, f.length === 0 || typeof i?.notify != "function")
103
+ return;
104
+ const H = i.notify();
105
+ typeof s == "function" && H.then((m) => s(m, f));
106
+ }
107
+ );
108
+ return d.trigger(i), O([
109
+ i?.request,
110
+ i?.revision,
111
+ d
112
+ ]), F;
113
+ }, [a, C, i, p, n, s]), h(() => {
114
+ v == null || u?.request !== R || u?.revision === b || v.trigger(u);
115
+ }, [
116
+ v,
117
+ u,
118
+ R,
119
+ b
120
+ ]);
96
121
  }
97
- function U({
122
+ function Z({
98
123
  eventReceiver: o,
99
124
  resource: e,
100
125
  disabled: t,
101
126
  options: r,
102
127
  onEvent: n,
103
128
  onNotify: s,
104
- children: u
129
+ children: c
105
130
  }) {
106
- return C({
131
+ return w({
107
132
  eventReceiver: o,
108
133
  resource: e,
109
134
  disabled: t,
110
135
  options: r,
111
136
  onEvent: n,
112
137
  onNotify: s
113
- }), /* @__PURE__ */ b(w, { children: u });
138
+ }), /* @__PURE__ */ x(M, { children: c });
114
139
  }
115
- const V = _(
140
+ const $ = I(
116
141
  (o) => class extends o {
117
142
  extend(t) {
118
143
  t.context(
119
- (r, { events: n = !1 }) => (C(
144
+ (r, { events: n = !1 }) => (w(
120
145
  typeof n == "boolean" ? { disabled: !n, resource: r } : { ...n, resource: r }
121
146
  ), r)
122
147
  );
@@ -124,13 +149,13 @@ const V = _(
124
149
  }
125
150
  );
126
151
  export {
127
- K as ConfigConsumer,
128
- T as ConfigProvider,
129
- U as EventHandler,
130
- H as EventReceiver,
131
- L as composeHandlers,
132
- V as eventPlugin,
133
- Q as isEventReceiver,
134
- F as useConfigContext,
135
- C as useEventHandler
152
+ V as ConfigConsumer,
153
+ Y as ConfigProvider,
154
+ Z as EventHandler,
155
+ z as EventReceiver,
156
+ W as composeHandlers,
157
+ $ as eventPlugin,
158
+ X as isEventReceiver,
159
+ k as useConfigContext,
160
+ w as useEventHandler
136
161
  };
@@ -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.1",
3
+ "version": "4.0.1",
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",