@flexsurfer/reflex 0.1.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,150 @@
1
+ import { Draft } from 'immer';
2
+
3
+ type Db<T = Record<string, any>> = T;
4
+ type Id = string;
5
+ type EventVector = [Id, ...any[]];
6
+ type EventHandler<T = Record<string, any>> = (coeffects: CoEffects<T>, ...params: any[]) => Effects | void;
7
+ type EffectHandler = (value: any) => void;
8
+ type CoEffectHandler<T = Record<string, any>> = (coeffects: CoEffects<T>, value?: any) => CoEffects<T>;
9
+ type ErrorHandler = (originalError: Error, reFrameError: Error & {
10
+ data: any;
11
+ }) => void;
12
+ type SubVector = [Id, ...any[]];
13
+ type Effects = [string, any][];
14
+ interface DispatchLaterEffect {
15
+ ms: number;
16
+ dispatch: EventVector;
17
+ }
18
+ interface CoEffects<T = Record<string, any>> {
19
+ event: EventVector;
20
+ draftDb: Draft<Db<T>>;
21
+ [key: string]: any;
22
+ }
23
+ interface Context<T = Record<string, any>> {
24
+ coeffects: CoEffects<T>;
25
+ effects: Effects;
26
+ newDb: Db<T>;
27
+ patches: any[];
28
+ queue: Interceptor<T>[];
29
+ stack: Interceptor<T>[];
30
+ originalException: boolean;
31
+ }
32
+ interface Interceptor<T = Record<string, any>> {
33
+ id: string;
34
+ before?: (context: Context<T>) => Context<T>;
35
+ after?: (context: Context<T>) => Context<T>;
36
+ comment?: string;
37
+ }
38
+
39
+ declare function initAppDb<T = Record<string, any>>(value: Db<T>): void;
40
+
41
+ /** Register an event handler with only a handler function (db event) */
42
+ declare function regEvent<T = Record<string, any>>(id: Id, handler: EventHandler<T>): void;
43
+ /** Register an event handler with interceptors and handler function (backward compatibility) */
44
+ declare function regEvent<T = Record<string, any>>(id: Id, handler: EventHandler<T>, interceptors: Interceptor<T>[]): void;
45
+ /** Register an event handler with cofx and handler function */
46
+ declare function regEvent<T = Record<string, any>>(id: Id, handler: EventHandler<T>, cofx: [Id, ...any[]][]): void;
47
+ /** Register an event handler with cofx, interceptors and handler function */
48
+ declare function regEvent<T = Record<string, any>>(id: Id, handler: EventHandler<T>, cofx: [Id, ...any[]][], interceptors: Interceptor<T>[]): void;
49
+ /**
50
+ * Register the given event error handler function that will catch unhandled exceptions
51
+ * thrown in the interceptors/handler chain.
52
+ *
53
+ * Only one handler can be registered. Registering a new handler clears the existing handler.
54
+ *
55
+ * This handler function has the signature:
56
+ * `(originalError: Error, reFrameError: Error & { data: any }) => void`
57
+ *
58
+ * - `originalError`: A platform-native Error object.
59
+ * Represents the original error thrown by user code.
60
+ * This is the error you see when no handler is registered.
61
+ *
62
+ * - `reFrameError`: An Error object with additional data.
63
+ * Includes the stacktrace of reflex's internal functions,
64
+ * and extra data about the interceptor process.
65
+ * Access `reFrameError.data` to get this info.
66
+ *
67
+ * The data includes:
68
+ * - `interceptor`: the `id` of the throwing interceptor.
69
+ * - `direction`: `'before'` or `'after'`.
70
+ * - `eventV`: the reflex event which invoked this interceptor.
71
+ */
72
+ declare function regEventErrorHandler(handler: ErrorHandler): void;
73
+ /**
74
+ * Default error handler that logs errors to console
75
+ */
76
+ declare function defaultErrorHandler(originalError: Error, reFrameError: Error & {
77
+ data: any;
78
+ }): void;
79
+
80
+ declare function regSub<R>(id: Id, computeFn?: (...values: any[]) => R, depsFn?: (...params: any[]) => SubVector[]): void;
81
+
82
+ declare function regEffect(id: string, handler: EffectHandler): void;
83
+
84
+ declare function regCoeffect(id: string, handler: CoEffectHandler): void;
85
+
86
+ /**
87
+ * Register a global interceptor
88
+ */
89
+ declare function regGlobalInterceptor(interceptor: Interceptor): void;
90
+ /**
91
+ * Get all global interceptors
92
+ */
93
+ declare function getGlobalInterceptors(): Interceptor[];
94
+ /**
95
+ * Clear global interceptors - either all or by specific ID
96
+ */
97
+ declare function clearGlobalInterceptors(): void;
98
+ declare function clearGlobalInterceptors(id: string): void;
99
+ /**
100
+ * Enable or disable debug mode
101
+ */
102
+ declare function setDebugEnabled(enabled: boolean): void;
103
+ /**
104
+ * Check if debug mode is enabled
105
+ */
106
+ declare function isDebugEnabled(): boolean;
107
+
108
+ /**
109
+ * Port of re-frame.router from ClojureScript to TypeScript.
110
+ * Implements an event queue with a finite-state-machine to schedule and process events.
111
+ */
112
+
113
+ /**
114
+ * Dispatch an event asynchronously
115
+ */
116
+ declare function dispatch(event: EventVector): void;
117
+
118
+ /**
119
+ * Dispatches `event` iff it was not dispatched for the duration of `durationMs`.
120
+ * Cancels any existing timeout for the same event and sets a new one.
121
+ */
122
+ declare function debounceAndDispatch(event: EventVector, durationMs: number): void;
123
+ /**
124
+ * Dispatches event and ignores subsequent calls for the duration of `durationMs`.
125
+ * Unlike debouncing, this dispatches immediately on the first call.
126
+ */
127
+ declare function throttleAndDispatch(event: EventVector, durationMs: number): void;
128
+
129
+ declare function useSubscription<T>(subVector: SubVector, componentName?: string): T;
130
+
131
+ type TraceID = number;
132
+ interface TraceOpts {
133
+ operation?: string;
134
+ opType?: string;
135
+ tags?: Record<string, any>;
136
+ childOf?: TraceID;
137
+ }
138
+ interface Trace extends TraceOpts {
139
+ id: TraceID;
140
+ start: number;
141
+ end?: number;
142
+ duration?: number;
143
+ }
144
+ type TraceCallback = (traces: Trace[]) => void;
145
+ declare function enableTracing(): void;
146
+ declare function disableTracing(): void;
147
+ declare function registerTraceCb(key: string, cb: TraceCallback): void;
148
+ declare function enableTracePrint(): void;
149
+
150
+ export { CoEffectHandler, CoEffects, Context, Db, DispatchLaterEffect, EffectHandler, Effects, ErrorHandler, EventHandler, EventVector, Id, Interceptor, SubVector, clearGlobalInterceptors, debounceAndDispatch, defaultErrorHandler, disableTracing, dispatch, enableTracePrint, enableTracing, getGlobalInterceptors, initAppDb, isDebugEnabled, regCoeffect, regEffect, regEvent, regEventErrorHandler, regGlobalInterceptor, regSub, registerTraceCb, setDebugEnabled, throttleAndDispatch, useSubscription };
@@ -0,0 +1,150 @@
1
+ import { Draft } from 'immer';
2
+
3
+ type Db<T = Record<string, any>> = T;
4
+ type Id = string;
5
+ type EventVector = [Id, ...any[]];
6
+ type EventHandler<T = Record<string, any>> = (coeffects: CoEffects<T>, ...params: any[]) => Effects | void;
7
+ type EffectHandler = (value: any) => void;
8
+ type CoEffectHandler<T = Record<string, any>> = (coeffects: CoEffects<T>, value?: any) => CoEffects<T>;
9
+ type ErrorHandler = (originalError: Error, reFrameError: Error & {
10
+ data: any;
11
+ }) => void;
12
+ type SubVector = [Id, ...any[]];
13
+ type Effects = [string, any][];
14
+ interface DispatchLaterEffect {
15
+ ms: number;
16
+ dispatch: EventVector;
17
+ }
18
+ interface CoEffects<T = Record<string, any>> {
19
+ event: EventVector;
20
+ draftDb: Draft<Db<T>>;
21
+ [key: string]: any;
22
+ }
23
+ interface Context<T = Record<string, any>> {
24
+ coeffects: CoEffects<T>;
25
+ effects: Effects;
26
+ newDb: Db<T>;
27
+ patches: any[];
28
+ queue: Interceptor<T>[];
29
+ stack: Interceptor<T>[];
30
+ originalException: boolean;
31
+ }
32
+ interface Interceptor<T = Record<string, any>> {
33
+ id: string;
34
+ before?: (context: Context<T>) => Context<T>;
35
+ after?: (context: Context<T>) => Context<T>;
36
+ comment?: string;
37
+ }
38
+
39
+ declare function initAppDb<T = Record<string, any>>(value: Db<T>): void;
40
+
41
+ /** Register an event handler with only a handler function (db event) */
42
+ declare function regEvent<T = Record<string, any>>(id: Id, handler: EventHandler<T>): void;
43
+ /** Register an event handler with interceptors and handler function (backward compatibility) */
44
+ declare function regEvent<T = Record<string, any>>(id: Id, handler: EventHandler<T>, interceptors: Interceptor<T>[]): void;
45
+ /** Register an event handler with cofx and handler function */
46
+ declare function regEvent<T = Record<string, any>>(id: Id, handler: EventHandler<T>, cofx: [Id, ...any[]][]): void;
47
+ /** Register an event handler with cofx, interceptors and handler function */
48
+ declare function regEvent<T = Record<string, any>>(id: Id, handler: EventHandler<T>, cofx: [Id, ...any[]][], interceptors: Interceptor<T>[]): void;
49
+ /**
50
+ * Register the given event error handler function that will catch unhandled exceptions
51
+ * thrown in the interceptors/handler chain.
52
+ *
53
+ * Only one handler can be registered. Registering a new handler clears the existing handler.
54
+ *
55
+ * This handler function has the signature:
56
+ * `(originalError: Error, reFrameError: Error & { data: any }) => void`
57
+ *
58
+ * - `originalError`: A platform-native Error object.
59
+ * Represents the original error thrown by user code.
60
+ * This is the error you see when no handler is registered.
61
+ *
62
+ * - `reFrameError`: An Error object with additional data.
63
+ * Includes the stacktrace of reflex's internal functions,
64
+ * and extra data about the interceptor process.
65
+ * Access `reFrameError.data` to get this info.
66
+ *
67
+ * The data includes:
68
+ * - `interceptor`: the `id` of the throwing interceptor.
69
+ * - `direction`: `'before'` or `'after'`.
70
+ * - `eventV`: the reflex event which invoked this interceptor.
71
+ */
72
+ declare function regEventErrorHandler(handler: ErrorHandler): void;
73
+ /**
74
+ * Default error handler that logs errors to console
75
+ */
76
+ declare function defaultErrorHandler(originalError: Error, reFrameError: Error & {
77
+ data: any;
78
+ }): void;
79
+
80
+ declare function regSub<R>(id: Id, computeFn?: (...values: any[]) => R, depsFn?: (...params: any[]) => SubVector[]): void;
81
+
82
+ declare function regEffect(id: string, handler: EffectHandler): void;
83
+
84
+ declare function regCoeffect(id: string, handler: CoEffectHandler): void;
85
+
86
+ /**
87
+ * Register a global interceptor
88
+ */
89
+ declare function regGlobalInterceptor(interceptor: Interceptor): void;
90
+ /**
91
+ * Get all global interceptors
92
+ */
93
+ declare function getGlobalInterceptors(): Interceptor[];
94
+ /**
95
+ * Clear global interceptors - either all or by specific ID
96
+ */
97
+ declare function clearGlobalInterceptors(): void;
98
+ declare function clearGlobalInterceptors(id: string): void;
99
+ /**
100
+ * Enable or disable debug mode
101
+ */
102
+ declare function setDebugEnabled(enabled: boolean): void;
103
+ /**
104
+ * Check if debug mode is enabled
105
+ */
106
+ declare function isDebugEnabled(): boolean;
107
+
108
+ /**
109
+ * Port of re-frame.router from ClojureScript to TypeScript.
110
+ * Implements an event queue with a finite-state-machine to schedule and process events.
111
+ */
112
+
113
+ /**
114
+ * Dispatch an event asynchronously
115
+ */
116
+ declare function dispatch(event: EventVector): void;
117
+
118
+ /**
119
+ * Dispatches `event` iff it was not dispatched for the duration of `durationMs`.
120
+ * Cancels any existing timeout for the same event and sets a new one.
121
+ */
122
+ declare function debounceAndDispatch(event: EventVector, durationMs: number): void;
123
+ /**
124
+ * Dispatches event and ignores subsequent calls for the duration of `durationMs`.
125
+ * Unlike debouncing, this dispatches immediately on the first call.
126
+ */
127
+ declare function throttleAndDispatch(event: EventVector, durationMs: number): void;
128
+
129
+ declare function useSubscription<T>(subVector: SubVector, componentName?: string): T;
130
+
131
+ type TraceID = number;
132
+ interface TraceOpts {
133
+ operation?: string;
134
+ opType?: string;
135
+ tags?: Record<string, any>;
136
+ childOf?: TraceID;
137
+ }
138
+ interface Trace extends TraceOpts {
139
+ id: TraceID;
140
+ start: number;
141
+ end?: number;
142
+ duration?: number;
143
+ }
144
+ type TraceCallback = (traces: Trace[]) => void;
145
+ declare function enableTracing(): void;
146
+ declare function disableTracing(): void;
147
+ declare function registerTraceCb(key: string, cb: TraceCallback): void;
148
+ declare function enableTracePrint(): void;
149
+
150
+ export { CoEffectHandler, CoEffects, Context, Db, DispatchLaterEffect, EffectHandler, Effects, ErrorHandler, EventHandler, EventVector, Id, Interceptor, SubVector, clearGlobalInterceptors, debounceAndDispatch, defaultErrorHandler, disableTracing, dispatch, enableTracePrint, enableTracing, getGlobalInterceptors, initAppDb, isDebugEnabled, regCoeffect, regEffect, regEvent, regEventErrorHandler, regGlobalInterceptor, regSub, registerTraceCb, setDebugEnabled, throttleAndDispatch, useSubscription };