@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.
@@ -0,0 +1,8 @@
1
+ import { PropsWithChildren, ReactNode } from 'react';
2
+ import { GenericEventReceiver } from './EventReceiver';
3
+ /**
4
+ * Provides general configuration to its descendants using React's context API.
5
+ */
6
+ export default function ConfigProvider<EventReceiverI extends GenericEventReceiver>({ eventReceiver, children, }: PropsWithChildren<{
7
+ eventReceiver: EventReceiverI;
8
+ }>): ReactNode;
@@ -0,0 +1,27 @@
1
+ import { ReactNode } from 'react';
2
+ import { GenericEventReceiver, InferEvent, InferOptions, InferResource } from './EventReceiver';
3
+ /**
4
+ * Enables automatic updating for a Resource component or useResource hook by subscribing to an EventReceiver.
5
+ *
6
+ * Necessary configuration that is not directly specified is taken from the ConfigContext and ResourceContext.
7
+ *
8
+ * onEvent can be used to directly access events allowing you to add custom event logic to your components.
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, }: {
11
+ /** EventReceiver to be used */
12
+ eventReceiver?: EventReceiverI;
13
+ /** ResourceContext to be used */
14
+ resource?: ResourceI;
15
+ /** Disables the event handler */
16
+ disabled?: boolean;
17
+ /** Options for the EventReceiver */
18
+ options?: OptionsI;
19
+ /** Callback to filter events and handle your own event logic - if true is returned, the event does not cause the resource to update */
20
+ onEvent?: (event: EventI) => boolean;
21
+ /** Provides information on when the resource has been requested to update - events contains the events that lead to the update */
22
+ onNotify?: (next: {
23
+ request: string;
24
+ revision: string;
25
+ }, events: EventI[]) => void;
26
+ children?: ReactNode;
27
+ }): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +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;
4
+ readonly _inferEvent: Event;
5
+ readonly _inferOptions: Options;
6
+ subscribe<ResourceI extends Resource, OptionsI extends Options, EventI extends Event>(resource: ResourceI, options: OptionsI | undefined, handler: (events: EventI[]) => void): () => void;
7
+ abstract handleSubscribe(resource: Resource, options: Options | undefined, handler: (events: Event[]) => void): () => void;
8
+ }
9
+ export declare const isEventReceiver: (eventReceiver: unknown) => boolean;
10
+ export type EventReceiverImplementation<EventReceiverI extends GenericEventReceiver, ConstructorArgs extends any[]> = Constructor<ConstructorArgs, EventReceiverI & {
11
+ handleSubscribe(resource: InferResource<EventReceiverI>, options: InferOptions<EventReceiverI> | undefined, handler: (events: InferEvent<EventReceiverI>[]) => void): () => void;
12
+ }>;
13
+ export type GenericEventReceiver = EventReceiver<ResourceContextValue<GenericDataProvider>, unknown, unknown>;
14
+ export type InferResource<EventReceiverI extends GenericEventReceiver> = EventReceiverI['_inferResource'];
15
+ export type InferEvent<EventReceiverI extends GenericEventReceiver> = EventReceiverI['_inferEvent'];
16
+ export type InferOptions<EventReceiverI extends GenericEventReceiver> = EventReceiverI['_inferOptions'];
@@ -0,0 +1 @@
1
+ export default function composeHandlers<Args extends any[]>(...handlers: ((...args: Args) => boolean)[]): (...args: Args) => boolean;
@@ -0,0 +1,10 @@
1
+ import { ConsumerProps, ExoticComponent, ReactNode } from 'react';
2
+ import { GenericEventReceiver } from './EventReceiver';
3
+ export type ConfigContextValue<EventReceiverI extends GenericEventReceiver> = {
4
+ eventReceiver?: EventReceiverI;
5
+ };
6
+ export declare const ConfigContext: import('react').Context<ConfigContextValue<GenericEventReceiver>>;
7
+ export declare const ConfigConsumer: ExoticComponent<GenericEventReceiver> & {
8
+ <DataProviderI extends GenericEventReceiver>(props: ConsumerProps<ConfigContextValue<DataProviderI>>): ReactNode;
9
+ };
10
+ export declare const useConfigContext: <DataProviderI extends GenericEventReceiver>() => ConfigContextValue<DataProviderI>;
package/dist/main.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { ConfigConsumer, useConfigContext } from './context';
2
+ export { default as composeHandlers } from './composeHandlers';
3
+ export { default as ConfigProvider } from './ConfigProvider';
4
+ export { default as EventHandler } from './EventHandler';
5
+ export { default as EventReceiver, isEventReceiver } from './EventReceiver';
6
+ export type { EventReceiverImplementation, GenericEventReceiver, InferEvent, InferOptions, InferResource, } from './EventReceiver';
7
+ export { default as useEventHandler } from './useEventHandler';
8
+ export { ConfigConsumer, useConfigContext };