@mapples/action 0.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.
- package/README.md +196 -0
- package/dist/ActionProvider.d.ts +35 -0
- package/dist/ActionProvider.d.ts.map +1 -0
- package/dist/ActionProvider.js +47 -0
- package/dist/ActionProvider.js.map +1 -0
- package/dist/context.d.ts +18 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +21 -0
- package/dist/context.js.map +1 -0
- package/dist/hooks/index.d.ts +3 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.js +3 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/useActionStore.d.ts +52 -0
- package/dist/hooks/useActionStore.d.ts.map +1 -0
- package/dist/hooks/useActionStore.js +57 -0
- package/dist/hooks/useActionStore.js.map +1 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +35 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +22 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/dist/types.js.map +1 -0
- package/package.json +38 -0
package/README.md
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
# @mapples/action
|
|
2
|
+
|
|
3
|
+
A React Native action management package that provides a centralized way to handle and track user actions throughout your application using RxJS observables.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🎯 **Action Tracking**: Centralized action store using RxJS Subject for reactive action handling
|
|
8
|
+
- 🔒 **Type Safety**: Full TypeScript support with comprehensive type definitions
|
|
9
|
+
- ⚛️ **React Integration**: Seamless integration with React hooks and context
|
|
10
|
+
- 🎁 **Flexible Payload**: Support for custom action payloads and event data
|
|
11
|
+
- 🏗️ **Provider Pattern**: Easy setup with ActionProvider component
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @mapples/action
|
|
17
|
+
# or
|
|
18
|
+
yarn add @mapples/action
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Peer Dependencies
|
|
22
|
+
|
|
23
|
+
This package requires the following peer dependencies:
|
|
24
|
+
|
|
25
|
+
- `react`: ~19.0.0
|
|
26
|
+
- `react-native`: ~0.79.0
|
|
27
|
+
- `rxjs`: For observable functionality
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
### Basic Setup
|
|
32
|
+
|
|
33
|
+
Wrap your app with the `ActionProvider` to enable action tracking:
|
|
34
|
+
|
|
35
|
+
```tsx
|
|
36
|
+
import React from 'react';
|
|
37
|
+
import ActionProvider from '@mapples/action';
|
|
38
|
+
|
|
39
|
+
function App() {
|
|
40
|
+
const handleAction = (action) => {
|
|
41
|
+
console.log('Action triggered:', action);
|
|
42
|
+
// Handle the action (e.g., analytics, logging, state updates)
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
return (
|
|
46
|
+
<ActionProvider onAction={handleAction}>
|
|
47
|
+
{/* Your app components */}
|
|
48
|
+
</ActionProvider>
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Using the Action Store
|
|
54
|
+
|
|
55
|
+
Access the action store in your components using the `useActionStore` hook:
|
|
56
|
+
|
|
57
|
+
```tsx
|
|
58
|
+
import React from 'react';
|
|
59
|
+
import { useActionStore } from '@mapples/action';
|
|
60
|
+
|
|
61
|
+
function MyComponent() {
|
|
62
|
+
const actionStore = useActionStore();
|
|
63
|
+
|
|
64
|
+
const handleButtonPress = () => {
|
|
65
|
+
// Emit an action
|
|
66
|
+
actionStore.next({
|
|
67
|
+
time: Date.now(),
|
|
68
|
+
type: 'BUTTON_PRESSED',
|
|
69
|
+
payload: {
|
|
70
|
+
data: { buttonId: 'submit' },
|
|
71
|
+
originalEventData: ['button', 'press'],
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
return <button onPress={handleButtonPress}>Submit</button>;
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Action Types
|
|
81
|
+
|
|
82
|
+
The package provides the following TypeScript interfaces:
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
interface Action {
|
|
86
|
+
time: number; // Timestamp when action occurred
|
|
87
|
+
type?: string; // Action type identifier
|
|
88
|
+
payload?: ActionPayload; // Optional payload data
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
interface ActionPayload {
|
|
92
|
+
originalEventData?: any[]; // Original event data
|
|
93
|
+
data?: any; // Custom data payload
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## API Reference
|
|
98
|
+
|
|
99
|
+
### ActionProvider
|
|
100
|
+
|
|
101
|
+
A React context provider that manages the action store and handles action subscriptions.
|
|
102
|
+
|
|
103
|
+
**Props:**
|
|
104
|
+
|
|
105
|
+
- `onAction: (action: Action) => void` - Callback function called when an action is emitted
|
|
106
|
+
- `children: ReactNode` - Child components
|
|
107
|
+
|
|
108
|
+
### useActionStore
|
|
109
|
+
|
|
110
|
+
A React hook that provides access to the action store.
|
|
111
|
+
|
|
112
|
+
**Returns:**
|
|
113
|
+
|
|
114
|
+
- `Subject<Action>` - RxJS Subject for emitting and subscribing to actions
|
|
115
|
+
|
|
116
|
+
**Example:**
|
|
117
|
+
|
|
118
|
+
```tsx
|
|
119
|
+
const actionStore = useActionStore();
|
|
120
|
+
|
|
121
|
+
// Emit an action
|
|
122
|
+
actionStore.next({
|
|
123
|
+
time: Date.now(),
|
|
124
|
+
type: 'USER_LOGIN',
|
|
125
|
+
payload: { data: { userId: '123' } },
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
// Subscribe to actions (if needed)
|
|
129
|
+
useEffect(() => {
|
|
130
|
+
const subscription = actionStore.subscribe((action) => {
|
|
131
|
+
console.log('New action:', action);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
return () => subscription.unsubscribe();
|
|
135
|
+
}, [actionStore]);
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## Advanced Usage
|
|
139
|
+
|
|
140
|
+
### Custom Action Types
|
|
141
|
+
|
|
142
|
+
Define custom action types for better type safety:
|
|
143
|
+
|
|
144
|
+
```tsx
|
|
145
|
+
type CustomActionType =
|
|
146
|
+
| 'USER_LOGIN'
|
|
147
|
+
| 'USER_LOGOUT'
|
|
148
|
+
| 'BUTTON_PRESSED'
|
|
149
|
+
| 'FORM_SUBMITTED';
|
|
150
|
+
|
|
151
|
+
interface CustomAction extends Action {
|
|
152
|
+
type: CustomActionType;
|
|
153
|
+
payload: {
|
|
154
|
+
data: any;
|
|
155
|
+
originalEventData?: any[];
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Action Middleware
|
|
161
|
+
|
|
162
|
+
You can create middleware to process actions before they reach the main handler:
|
|
163
|
+
|
|
164
|
+
```tsx
|
|
165
|
+
function createActionMiddleware(store: Subject<Action>) {
|
|
166
|
+
return store.pipe(
|
|
167
|
+
// Add timestamp if missing
|
|
168
|
+
map((action) => ({
|
|
169
|
+
...action,
|
|
170
|
+
time: action.time || Date.now(),
|
|
171
|
+
})),
|
|
172
|
+
// Filter out invalid actions
|
|
173
|
+
filter((action) => action.type && action.type.length > 0),
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## Best Practices
|
|
179
|
+
|
|
180
|
+
1. **Consistent Action Types**: Use consistent naming conventions for action types (e.g., `COMPONENT_ACTION` format)
|
|
181
|
+
|
|
182
|
+
2. **Meaningful Payloads**: Include relevant data in action payloads for better debugging and analytics
|
|
183
|
+
|
|
184
|
+
3. **Error Handling**: Always handle potential errors in your action handlers
|
|
185
|
+
|
|
186
|
+
4. **Performance**: Be mindful of action frequency to avoid performance issues
|
|
187
|
+
|
|
188
|
+
5. **Testing**: Test your action flows to ensure they work as expected
|
|
189
|
+
|
|
190
|
+
## License
|
|
191
|
+
|
|
192
|
+
MIT
|
|
193
|
+
|
|
194
|
+
## Author
|
|
195
|
+
|
|
196
|
+
Oleksandr Soloviov - [mapples.org](https://mapples.org/people/olek_soloviov)
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { FC, PropsWithChildren } from 'react';
|
|
2
|
+
import { ActionProviderProps } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* ActionProvider component that manages the action store and provides it to child components.
|
|
5
|
+
*
|
|
6
|
+
* This component:
|
|
7
|
+
* - Creates and manages an RxJS Subject for action emissions
|
|
8
|
+
* - Subscribes to actions and calls the provided onAction callback
|
|
9
|
+
* - Provides the action store to child components via React context
|
|
10
|
+
* - Automatically cleans up subscriptions when the component unmounts
|
|
11
|
+
*
|
|
12
|
+
* @component
|
|
13
|
+
* @param props - The component props
|
|
14
|
+
* @param props.children - Child components that will have access to the action store
|
|
15
|
+
* @param props.onAction - Callback function called whenever an action is emitted
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```tsx
|
|
19
|
+
* function App() {
|
|
20
|
+
* const handleAction = (action: Action) => {
|
|
21
|
+
* console.log('Action received:', action);
|
|
22
|
+
* // Handle analytics, logging, etc.
|
|
23
|
+
* };
|
|
24
|
+
*
|
|
25
|
+
* return (
|
|
26
|
+
* <ActionProvider onAction={handleAction}>
|
|
27
|
+
* <MyApp />
|
|
28
|
+
* </ActionProvider>
|
|
29
|
+
* );
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
declare const ActionProvider: FC<PropsWithChildren<ActionProviderProps>>;
|
|
34
|
+
export default ActionProvider;
|
|
35
|
+
//# sourceMappingURL=ActionProvider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ActionProvider.d.ts","sourceRoot":"","sources":["../src/ActionProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,iBAAiB,EAAqB,MAAM,OAAO,CAAC;AAMjE,OAAO,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAE9C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,QAAA,MAAM,cAAc,EAAE,EAAE,CAAC,iBAAiB,CAAC,mBAAmB,CAAC,CAkB9D,CAAC;AAEF,eAAe,cAAc,CAAC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { useEffect, useRef } from 'react';
|
|
2
|
+
import { Subject } from 'rxjs';
|
|
3
|
+
import { ActionContextProvider } from './context';
|
|
4
|
+
/**
|
|
5
|
+
* ActionProvider component that manages the action store and provides it to child components.
|
|
6
|
+
*
|
|
7
|
+
* This component:
|
|
8
|
+
* - Creates and manages an RxJS Subject for action emissions
|
|
9
|
+
* - Subscribes to actions and calls the provided onAction callback
|
|
10
|
+
* - Provides the action store to child components via React context
|
|
11
|
+
* - Automatically cleans up subscriptions when the component unmounts
|
|
12
|
+
*
|
|
13
|
+
* @component
|
|
14
|
+
* @param props - The component props
|
|
15
|
+
* @param props.children - Child components that will have access to the action store
|
|
16
|
+
* @param props.onAction - Callback function called whenever an action is emitted
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```tsx
|
|
20
|
+
* function App() {
|
|
21
|
+
* const handleAction = (action: Action) => {
|
|
22
|
+
* console.log('Action received:', action);
|
|
23
|
+
* // Handle analytics, logging, etc.
|
|
24
|
+
* };
|
|
25
|
+
*
|
|
26
|
+
* return (
|
|
27
|
+
* <ActionProvider onAction={handleAction}>
|
|
28
|
+
* <MyApp />
|
|
29
|
+
* </ActionProvider>
|
|
30
|
+
* );
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
const ActionProvider = ({ children, onAction, }) => {
|
|
35
|
+
const ActionStore = useRef(new Subject());
|
|
36
|
+
useEffect(() => {
|
|
37
|
+
const subscription = ActionStore.current.subscribe(onAction);
|
|
38
|
+
return () => {
|
|
39
|
+
return subscription.unsubscribe();
|
|
40
|
+
};
|
|
41
|
+
}, [onAction]);
|
|
42
|
+
return (<ActionContextProvider value={{ actionStore: ActionStore.current }}>
|
|
43
|
+
{children}
|
|
44
|
+
</ActionContextProvider>);
|
|
45
|
+
};
|
|
46
|
+
export default ActionProvider;
|
|
47
|
+
//# sourceMappingURL=ActionProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ActionProvider.js","sourceRoot":"","sources":["../src/ActionProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAyB,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAGjE,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAE/B,OAAO,EAAE,qBAAqB,EAAE,MAAM,WAAW,CAAC;AAGlD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,MAAM,cAAc,GAA+C,CAAC,EAClE,QAAQ,EACR,QAAQ,GACT,EAAE,EAAE;IACH,MAAM,WAAW,GAAG,MAAM,CAAC,IAAI,OAAO,EAAU,CAAC,CAAC;IAElD,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,YAAY,GAAG,WAAW,CAAC,OAAO,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;QAC7D,OAAO,GAAG,EAAE;YACV,OAAO,YAAY,CAAC,WAAW,EAAE,CAAC;QACpC,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC;IAEf,OAAO,CACL,CAAC,qBAAqB,CAAC,KAAK,CAAC,CAAC,EAAE,WAAW,EAAE,WAAW,CAAC,OAAO,EAAE,CAAC,CACjE;MAAA,CAAC,QAAQ,CACX;IAAA,EAAE,qBAAqB,CAAC,CACzB,CAAC;AACJ,CAAC,CAAC;AAEF,eAAe,cAAc,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ActionContextType } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* React context for managing action store throughout the application.
|
|
4
|
+
* This context provides access to the RxJS Subject that handles action emissions and subscriptions.
|
|
5
|
+
*
|
|
6
|
+
* @constant ActionContext
|
|
7
|
+
*/
|
|
8
|
+
declare const ActionContext: import("react").Context<ActionContextType>;
|
|
9
|
+
/**
|
|
10
|
+
* Provider component for the Action context.
|
|
11
|
+
* This is the actual React context provider that should be used to wrap components
|
|
12
|
+
* that need access to the action store.
|
|
13
|
+
*
|
|
14
|
+
* @constant ActionContextProvider
|
|
15
|
+
*/
|
|
16
|
+
export declare const ActionContextProvider: import("react").Provider<ActionContextType>;
|
|
17
|
+
export default ActionContext;
|
|
18
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAE5C;;;;;GAKG;AACH,QAAA,MAAM,aAAa,4CAEjB,CAAC;AAEH;;;;;;GAMG;AACH,eAAO,MAAM,qBAAqB,6CAAyB,CAAC;AAE5D,eAAe,aAAa,CAAC"}
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { createContext } from 'react';
|
|
2
|
+
import { Subject } from 'rxjs';
|
|
3
|
+
/**
|
|
4
|
+
* React context for managing action store throughout the application.
|
|
5
|
+
* This context provides access to the RxJS Subject that handles action emissions and subscriptions.
|
|
6
|
+
*
|
|
7
|
+
* @constant ActionContext
|
|
8
|
+
*/
|
|
9
|
+
const ActionContext = createContext({
|
|
10
|
+
actionStore: new Subject(),
|
|
11
|
+
});
|
|
12
|
+
/**
|
|
13
|
+
* Provider component for the Action context.
|
|
14
|
+
* This is the actual React context provider that should be used to wrap components
|
|
15
|
+
* that need access to the action store.
|
|
16
|
+
*
|
|
17
|
+
* @constant ActionContextProvider
|
|
18
|
+
*/
|
|
19
|
+
export const ActionContextProvider = ActionContext.Provider;
|
|
20
|
+
export default ActionContext;
|
|
21
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AAEtC,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAI/B;;;;;GAKG;AACH,MAAM,aAAa,GAAG,aAAa,CAAoB;IACrD,WAAW,EAAE,IAAI,OAAO,EAAE;CAC3B,CAAC,CAAC;AAEH;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,aAAa,CAAC,QAAQ,CAAC;AAE5D,eAAe,aAAa,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAE9C,OAAO,EAAE,cAAc,EAAE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAE9C,OAAO,EAAE,cAAc,EAAE,CAAC"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook that provides access to the action store from the Action context.
|
|
3
|
+
*
|
|
4
|
+
* This hook allows components to:
|
|
5
|
+
* - Emit new actions using `actionStore.next(action)`
|
|
6
|
+
* - Subscribe to actions using `actionStore.subscribe(callback)`
|
|
7
|
+
* - Access the RxJS Subject for advanced reactive operations
|
|
8
|
+
*
|
|
9
|
+
* @returns {Subject<Action>} The RxJS Subject that manages action emissions and subscriptions
|
|
10
|
+
*
|
|
11
|
+
* @throws {Error} Throws an error if used outside of an ActionProvider
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```tsx
|
|
15
|
+
* function MyComponent() {
|
|
16
|
+
* const actionStore = useActionStore();
|
|
17
|
+
*
|
|
18
|
+
* const handleButtonPress = () => {
|
|
19
|
+
* // Emit an action
|
|
20
|
+
* actionStore.next({
|
|
21
|
+
* time: Date.now(),
|
|
22
|
+
* type: 'BUTTON_PRESSED',
|
|
23
|
+
* payload: {
|
|
24
|
+
* data: { buttonId: 'submit' }
|
|
25
|
+
* }
|
|
26
|
+
* });
|
|
27
|
+
* };
|
|
28
|
+
*
|
|
29
|
+
* return <button onPress={handleButtonPress}>Submit</button>;
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```tsx
|
|
35
|
+
* function ActionLogger() {
|
|
36
|
+
* const actionStore = useActionStore();
|
|
37
|
+
*
|
|
38
|
+
* useEffect(() => {
|
|
39
|
+
* const subscription = actionStore.subscribe(action => {
|
|
40
|
+
* console.log('Action logged:', action);
|
|
41
|
+
* });
|
|
42
|
+
*
|
|
43
|
+
* return () => subscription.unsubscribe();
|
|
44
|
+
* }, [actionStore]);
|
|
45
|
+
*
|
|
46
|
+
* return null;
|
|
47
|
+
* }
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
declare const useActionStore: () => import("rxjs").Subject<import("@mapples/types").Action>;
|
|
51
|
+
export default useActionStore;
|
|
52
|
+
//# sourceMappingURL=useActionStore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useActionStore.d.ts","sourceRoot":"","sources":["../../src/hooks/useActionStore.tsx"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgDG;AACH,QAAA,MAAM,cAAc,+DAInB,CAAC;AAEF,eAAe,cAAc,CAAC"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { useContext } from 'react';
|
|
2
|
+
import ActionContext from '../context';
|
|
3
|
+
/**
|
|
4
|
+
* Hook that provides access to the action store from the Action context.
|
|
5
|
+
*
|
|
6
|
+
* This hook allows components to:
|
|
7
|
+
* - Emit new actions using `actionStore.next(action)`
|
|
8
|
+
* - Subscribe to actions using `actionStore.subscribe(callback)`
|
|
9
|
+
* - Access the RxJS Subject for advanced reactive operations
|
|
10
|
+
*
|
|
11
|
+
* @returns {Subject<Action>} The RxJS Subject that manages action emissions and subscriptions
|
|
12
|
+
*
|
|
13
|
+
* @throws {Error} Throws an error if used outside of an ActionProvider
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```tsx
|
|
17
|
+
* function MyComponent() {
|
|
18
|
+
* const actionStore = useActionStore();
|
|
19
|
+
*
|
|
20
|
+
* const handleButtonPress = () => {
|
|
21
|
+
* // Emit an action
|
|
22
|
+
* actionStore.next({
|
|
23
|
+
* time: Date.now(),
|
|
24
|
+
* type: 'BUTTON_PRESSED',
|
|
25
|
+
* payload: {
|
|
26
|
+
* data: { buttonId: 'submit' }
|
|
27
|
+
* }
|
|
28
|
+
* });
|
|
29
|
+
* };
|
|
30
|
+
*
|
|
31
|
+
* return <button onPress={handleButtonPress}>Submit</button>;
|
|
32
|
+
* }
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```tsx
|
|
37
|
+
* function ActionLogger() {
|
|
38
|
+
* const actionStore = useActionStore();
|
|
39
|
+
*
|
|
40
|
+
* useEffect(() => {
|
|
41
|
+
* const subscription = actionStore.subscribe(action => {
|
|
42
|
+
* console.log('Action logged:', action);
|
|
43
|
+
* });
|
|
44
|
+
*
|
|
45
|
+
* return () => subscription.unsubscribe();
|
|
46
|
+
* }, [actionStore]);
|
|
47
|
+
*
|
|
48
|
+
* return null;
|
|
49
|
+
* }
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
const useActionStore = () => {
|
|
53
|
+
const { actionStore } = useContext(ActionContext);
|
|
54
|
+
return actionStore;
|
|
55
|
+
};
|
|
56
|
+
export default useActionStore;
|
|
57
|
+
//# sourceMappingURL=useActionStore.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useActionStore.js","sourceRoot":"","sources":["../../src/hooks/useActionStore.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AAEnC,OAAO,aAAa,MAAM,YAAY,CAAC;AAEvC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgDG;AACH,MAAM,cAAc,GAAG,GAAG,EAAE;IAC1B,MAAM,EAAE,WAAW,EAAE,GAAG,UAAU,CAAC,aAAa,CAAC,CAAC;IAElD,OAAO,WAAW,CAAC;AACrB,CAAC,CAAC;AAEF,eAAe,cAAc,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import ActionProvider from './ActionProvider';
|
|
2
|
+
/**
|
|
3
|
+
* @fileoverview Main entry point for the @mapples/action package.
|
|
4
|
+
*
|
|
5
|
+
* This package provides a centralized action management system for React Native applications
|
|
6
|
+
* using RxJS observables. It allows you to track user actions, handle analytics, and manage
|
|
7
|
+
* application state through a reactive action store.
|
|
8
|
+
*
|
|
9
|
+
* @packageDocumentation
|
|
10
|
+
*/
|
|
11
|
+
export * from './types';
|
|
12
|
+
export * from './hooks';
|
|
13
|
+
/**
|
|
14
|
+
* Default export: ActionProvider component
|
|
15
|
+
*
|
|
16
|
+
* The main component that should be used to wrap your application or specific parts
|
|
17
|
+
* that need action tracking capabilities.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```tsx
|
|
21
|
+
* import ActionProvider from '@mapples/action';
|
|
22
|
+
*
|
|
23
|
+
* function App() {
|
|
24
|
+
* return (
|
|
25
|
+
* <ActionProvider onAction={(action) => console.log(action)}>
|
|
26
|
+
* <YourApp />
|
|
27
|
+
* </ActionProvider>
|
|
28
|
+
* );
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export default ActionProvider;
|
|
33
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAE9C;;;;;;;;GAQG;AAGH,cAAc,SAAS,CAAC;AAGxB,cAAc,SAAS,CAAC;AAExB;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAe,cAAc,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import ActionProvider from './ActionProvider';
|
|
2
|
+
/**
|
|
3
|
+
* @fileoverview Main entry point for the @mapples/action package.
|
|
4
|
+
*
|
|
5
|
+
* This package provides a centralized action management system for React Native applications
|
|
6
|
+
* using RxJS observables. It allows you to track user actions, handle analytics, and manage
|
|
7
|
+
* application state through a reactive action store.
|
|
8
|
+
*
|
|
9
|
+
* @packageDocumentation
|
|
10
|
+
*/
|
|
11
|
+
// Export all types for external use
|
|
12
|
+
export * from './types';
|
|
13
|
+
// Export all hooks for external use
|
|
14
|
+
export * from './hooks';
|
|
15
|
+
/**
|
|
16
|
+
* Default export: ActionProvider component
|
|
17
|
+
*
|
|
18
|
+
* The main component that should be used to wrap your application or specific parts
|
|
19
|
+
* that need action tracking capabilities.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```tsx
|
|
23
|
+
* import ActionProvider from '@mapples/action';
|
|
24
|
+
*
|
|
25
|
+
* function App() {
|
|
26
|
+
* return (
|
|
27
|
+
* <ActionProvider onAction={(action) => console.log(action)}>
|
|
28
|
+
* <YourApp />
|
|
29
|
+
* </ActionProvider>
|
|
30
|
+
* );
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export default ActionProvider;
|
|
35
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAE9C;;;;;;;;GAQG;AAEH,oCAAoC;AACpC,cAAc,SAAS,CAAC;AAExB,oCAAoC;AACpC,cAAc,SAAS,CAAC;AAExB;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAe,cAAc,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Action } from '@mapples/types';
|
|
2
|
+
import { Subject } from 'rxjs';
|
|
3
|
+
/**
|
|
4
|
+
* Context type for the Action context provider.
|
|
5
|
+
* Contains the RxJS Subject that manages action emissions and subscriptions.
|
|
6
|
+
*
|
|
7
|
+
* @interface ActionContextType
|
|
8
|
+
*/
|
|
9
|
+
export interface ActionContextType {
|
|
10
|
+
/** RxJS Subject for emitting and subscribing to actions */
|
|
11
|
+
actionStore: Subject<Action>;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Props for the ActionProvider component.
|
|
15
|
+
*
|
|
16
|
+
* @interface ActionProviderProps
|
|
17
|
+
*/
|
|
18
|
+
export interface ActionProviderProps {
|
|
19
|
+
/** Callback function that will be called whenever an action is emitted */
|
|
20
|
+
onAction?: (action: Action) => void;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACxC,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAE/B;;;;;GAKG;AACH,MAAM,WAAW,iBAAiB;IAChC,2DAA2D;IAC3D,WAAW,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;CAC9B;AAED;;;;GAIG;AACH,MAAM,WAAW,mBAAmB;IAClC,0EAA0E;IAC1E,QAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;CACrC"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mapples/action",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"types": "dist/index.d.ts",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "tsc -b tsconfig.build.json",
|
|
8
|
+
"lint": "eslint .",
|
|
9
|
+
"type-check": "tsc --noEmit",
|
|
10
|
+
"clean": "rm -rf dist"
|
|
11
|
+
},
|
|
12
|
+
"author": {
|
|
13
|
+
"name": "Oleksandr Soloviov",
|
|
14
|
+
"url": "https://mapples.org/people/olek_soloviov"
|
|
15
|
+
},
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "https://github.com/mapplesorg/mapples-open-kit.git",
|
|
20
|
+
"directory": "packages/@mapples/action"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"rxjs": "^7.8.2"
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"react": ">=18.0.0 <20.0.0",
|
|
27
|
+
"react-native": ">=0.71.0"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/react": "~19.0.10",
|
|
31
|
+
"typescript": "~5.8.3"
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"dist",
|
|
35
|
+
"README.md",
|
|
36
|
+
"package.json"
|
|
37
|
+
]
|
|
38
|
+
}
|