@memberjunction/global 2.43.0 → 2.44.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/package.json +1 -1
- package/readme.md +282 -29
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
```markdown
|
|
2
1
|
# @memberjunction/global
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
Core global utilities and coordination library for MemberJunction applications. This package provides essential singleton management, class factory patterns, event coordination, and utility functions that are used throughout the MemberJunction ecosystem.
|
|
5
4
|
|
|
6
5
|
## Installation
|
|
7
6
|
|
|
@@ -9,54 +8,308 @@ The `@memberjunction/global` library is designed to coordinate events and compon
|
|
|
9
8
|
npm install @memberjunction/global
|
|
10
9
|
```
|
|
11
10
|
|
|
12
|
-
##
|
|
11
|
+
## Overview
|
|
13
12
|
|
|
14
|
-
|
|
13
|
+
The `@memberjunction/global` library serves as the foundation for cross-component communication and coordination in MemberJunction applications. It provides:
|
|
15
14
|
|
|
16
|
-
|
|
15
|
+
- **Global Singleton Management** - Ensures true singleton instances across module boundaries
|
|
16
|
+
- **Class Factory System** - Dynamic class registration and instantiation with inheritance support
|
|
17
|
+
- **Event System** - RxJS-based event bus for component communication
|
|
18
|
+
- **Object Caching** - In-memory object cache for application lifetime
|
|
19
|
+
- **Utility Functions** - Common string manipulation, JSON parsing, and formatting utilities
|
|
17
20
|
|
|
18
|
-
|
|
19
|
-
|
|
21
|
+
## Core Components
|
|
22
|
+
|
|
23
|
+
### MJGlobal Class
|
|
24
|
+
|
|
25
|
+
The central singleton class that coordinates events and manages components across your application.
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { MJGlobal } from '@memberjunction/global';
|
|
29
|
+
|
|
30
|
+
// Get the singleton instance
|
|
31
|
+
const mjGlobal = MJGlobal.Instance;
|
|
32
|
+
|
|
33
|
+
// Register a component
|
|
34
|
+
mjGlobal.RegisterComponent(myComponent);
|
|
35
|
+
|
|
36
|
+
// Raise an event
|
|
37
|
+
mjGlobal.RaiseEvent({
|
|
38
|
+
component: myComponent,
|
|
39
|
+
event: MJEventType.ComponentEvent,
|
|
40
|
+
eventCode: 'CUSTOM_EVENT',
|
|
41
|
+
args: { data: 'example' }
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// Listen for events
|
|
45
|
+
const subscription = mjGlobal.GetEventListener().subscribe(event => {
|
|
46
|
+
console.log('Event received:', event);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// Listen with replay (gets past events too)
|
|
50
|
+
const replaySubscription = mjGlobal.GetEventListener(true).subscribe(event => {
|
|
51
|
+
console.log('Event with replay:', event);
|
|
52
|
+
});
|
|
20
53
|
```
|
|
21
54
|
|
|
22
|
-
###
|
|
55
|
+
### Class Factory System
|
|
23
56
|
|
|
24
|
-
|
|
25
|
-
// Getting the singleton instance of MJGlobal
|
|
26
|
-
const globalInstance = MJ.MJGlobal.Instance;
|
|
57
|
+
Register and instantiate classes dynamically with support for inheritance and key-based selection.
|
|
27
58
|
|
|
28
|
-
|
|
29
|
-
|
|
59
|
+
```typescript
|
|
60
|
+
import { RegisterClass, MJGlobal } from '@memberjunction/global';
|
|
30
61
|
|
|
31
|
-
//
|
|
32
|
-
|
|
62
|
+
// Define a base class
|
|
63
|
+
class BaseProcessor {
|
|
64
|
+
process(data: any): void {
|
|
65
|
+
console.log('Base processing');
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Register a subclass using the decorator
|
|
70
|
+
@RegisterClass(BaseProcessor, 'custom', 100)
|
|
71
|
+
class CustomProcessor extends BaseProcessor {
|
|
72
|
+
process(data: any): void {
|
|
73
|
+
console.log('Custom processing');
|
|
74
|
+
}
|
|
75
|
+
}
|
|
33
76
|
|
|
34
|
-
//
|
|
35
|
-
const
|
|
77
|
+
// Create instances via the factory
|
|
78
|
+
const factory = MJGlobal.Instance.ClassFactory;
|
|
79
|
+
const processor = factory.CreateInstance<BaseProcessor>(BaseProcessor, 'custom');
|
|
80
|
+
processor.process(data); // Uses CustomProcessor
|
|
36
81
|
```
|
|
37
82
|
|
|
38
|
-
###
|
|
83
|
+
### Object Cache
|
|
84
|
+
|
|
85
|
+
In-memory caching system for application-lifetime object storage.
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
const cache = MJGlobal.Instance.ObjectCache;
|
|
89
|
+
|
|
90
|
+
// Add an object to cache
|
|
91
|
+
cache.Add('user:123', { id: 123, name: 'John Doe' });
|
|
92
|
+
|
|
93
|
+
// Find an object
|
|
94
|
+
const user = cache.Find<User>('user:123');
|
|
95
|
+
|
|
96
|
+
// Replace an existing object
|
|
97
|
+
cache.Replace('user:123', { id: 123, name: 'Jane Doe' });
|
|
98
|
+
|
|
99
|
+
// Remove from cache
|
|
100
|
+
cache.Remove('user:123');
|
|
101
|
+
|
|
102
|
+
// Clear all cached objects
|
|
103
|
+
cache.Clear();
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### BaseSingleton Class
|
|
107
|
+
|
|
108
|
+
Abstract base class for creating global singleton instances that persist across module boundaries.
|
|
39
109
|
|
|
40
|
-
```
|
|
41
|
-
@
|
|
42
|
-
|
|
43
|
-
|
|
110
|
+
```typescript
|
|
111
|
+
import { BaseSingleton } from '@memberjunction/global';
|
|
112
|
+
|
|
113
|
+
export class MyService extends BaseSingleton<MyService> {
|
|
114
|
+
private data: string[] = [];
|
|
115
|
+
|
|
116
|
+
public static get Instance(): MyService {
|
|
117
|
+
return super.getInstance<MyService>();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
public addData(item: string): void {
|
|
121
|
+
this.data.push(item);
|
|
122
|
+
}
|
|
44
123
|
}
|
|
124
|
+
|
|
125
|
+
// Usage anywhere in your app
|
|
126
|
+
const service = MyService.Instance;
|
|
127
|
+
service.addData('example');
|
|
45
128
|
```
|
|
46
129
|
|
|
47
|
-
##
|
|
130
|
+
## Event Types
|
|
48
131
|
|
|
49
|
-
|
|
132
|
+
The library provides predefined event types for common scenarios:
|
|
50
133
|
|
|
51
|
-
|
|
134
|
+
```typescript
|
|
135
|
+
export const MJEventType = {
|
|
136
|
+
ComponentRegistered: 'ComponentRegistered',
|
|
137
|
+
ComponentUnregistered: 'ComponentUnregistered',
|
|
138
|
+
ComponentEvent: 'ComponentEvent',
|
|
139
|
+
LoggedIn: 'LoggedIn',
|
|
140
|
+
LoggedOut: 'LoggedOut',
|
|
141
|
+
LoginFailed: 'LoginFailed',
|
|
142
|
+
LogoutFailed: 'LogoutFailed',
|
|
143
|
+
ManualResizeRequest: 'ManualResizeRequest',
|
|
144
|
+
DisplaySimpleNotificationRequest: 'DisplaySimpleNotificationRequest',
|
|
145
|
+
} as const;
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## Utility Functions
|
|
149
|
+
|
|
150
|
+
### String Manipulation
|
|
151
|
+
|
|
152
|
+
```typescript
|
|
153
|
+
import {
|
|
154
|
+
convertCamelCaseToHaveSpaces,
|
|
155
|
+
stripWhitespace,
|
|
156
|
+
generatePluralName,
|
|
157
|
+
adjustCasing,
|
|
158
|
+
stripTrailingChars,
|
|
159
|
+
replaceAllSpaces
|
|
160
|
+
} from '@memberjunction/global';
|
|
161
|
+
|
|
162
|
+
// Convert camel case to spaces
|
|
163
|
+
convertCamelCaseToHaveSpaces('AIAgentLearningCycle'); // "AI Agent Learning Cycle"
|
|
164
|
+
|
|
165
|
+
// Remove all whitespace
|
|
166
|
+
stripWhitespace(' Hello World '); // "HelloWorld"
|
|
167
|
+
|
|
168
|
+
// Generate plural forms
|
|
169
|
+
generatePluralName('child'); // "children"
|
|
170
|
+
generatePluralName('box'); // "boxes"
|
|
171
|
+
generatePluralName('party'); // "parties"
|
|
172
|
+
|
|
173
|
+
// Adjust casing
|
|
174
|
+
adjustCasing('hello', { capitalizeFirstLetterOnly: true }); // "Hello"
|
|
175
|
+
adjustCasing('world', { capitalizeEntireWord: true }); // "WORLD"
|
|
176
|
+
|
|
177
|
+
// Strip trailing characters
|
|
178
|
+
stripTrailingChars('example.txt', '.txt', false); // "example"
|
|
179
|
+
|
|
180
|
+
// Remove all spaces
|
|
181
|
+
replaceAllSpaces('Hello World'); // "HelloWorld"
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### JSON Utilities
|
|
185
|
+
|
|
186
|
+
```typescript
|
|
187
|
+
import { CleanJSON, SafeJSONParse } from '@memberjunction/global';
|
|
188
|
+
|
|
189
|
+
// Clean and extract JSON from markdown or mixed content
|
|
190
|
+
const cleaned = CleanJSON('```json\n{"key": "value"}\n```');
|
|
191
|
+
|
|
192
|
+
// Safe JSON parsing with error handling
|
|
193
|
+
const parsed = SafeJSONParse<MyType>('{"key": "value"}', true);
|
|
194
|
+
```
|
|
52
195
|
|
|
53
|
-
|
|
196
|
+
### HTML Conversion
|
|
54
197
|
|
|
55
|
-
|
|
198
|
+
```typescript
|
|
199
|
+
import { ConvertMarkdownStringToHtmlList } from '@memberjunction/global';
|
|
56
200
|
|
|
57
|
-
|
|
201
|
+
// Convert markdown to HTML list
|
|
202
|
+
const html = ConvertMarkdownStringToHtmlList('Unordered', '- Item 1\n- Item 2\n- Item 3');
|
|
203
|
+
// Returns: <ul><li>Item 1</li><li>Item 2</li><li>Item 3</li></ul>
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### Global Object Store
|
|
207
|
+
|
|
208
|
+
Access the global object store for cross-module state sharing:
|
|
209
|
+
|
|
210
|
+
```typescript
|
|
211
|
+
import { GetGlobalObjectStore } from '@memberjunction/global';
|
|
212
|
+
|
|
213
|
+
const globalStore = GetGlobalObjectStore();
|
|
214
|
+
// Returns window object in browser, global in Node.js
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
### Manual Resize Request
|
|
218
|
+
|
|
219
|
+
Trigger a manual resize event across components:
|
|
220
|
+
|
|
221
|
+
```typescript
|
|
222
|
+
import { InvokeManualResize } from '@memberjunction/global';
|
|
223
|
+
|
|
224
|
+
// Request resize after 50ms delay
|
|
225
|
+
InvokeManualResize(50, myComponent);
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
## Advanced Usage
|
|
229
|
+
|
|
230
|
+
### Class Factory with Parameters
|
|
231
|
+
|
|
232
|
+
```typescript
|
|
233
|
+
// Register a class that requires constructor parameters
|
|
234
|
+
@RegisterClass(BaseService, 'api')
|
|
235
|
+
class ApiService extends BaseService {
|
|
236
|
+
constructor(private apiUrl: string) {
|
|
237
|
+
super();
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// Create with parameters
|
|
242
|
+
const service = factory.CreateInstance<BaseService>(
|
|
243
|
+
BaseService,
|
|
244
|
+
'api',
|
|
245
|
+
'https://api.example.com'
|
|
246
|
+
);
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
### Priority-based Registration
|
|
250
|
+
|
|
251
|
+
```typescript
|
|
252
|
+
// Lower priority (registered first)
|
|
253
|
+
@RegisterClass(BaseHandler, 'data', 10)
|
|
254
|
+
class BasicDataHandler extends BaseHandler {}
|
|
255
|
+
|
|
256
|
+
// Higher priority (overrides BasicDataHandler)
|
|
257
|
+
@RegisterClass(BaseHandler, 'data', 20)
|
|
258
|
+
class AdvancedDataHandler extends BaseHandler {}
|
|
259
|
+
|
|
260
|
+
// Will create AdvancedDataHandler instance
|
|
261
|
+
const handler = factory.CreateInstance<BaseHandler>(BaseHandler, 'data');
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
### Global Properties
|
|
265
|
+
|
|
266
|
+
Store and retrieve global properties:
|
|
58
267
|
|
|
268
|
+
```typescript
|
|
269
|
+
const properties = MJGlobal.Instance.Properties;
|
|
270
|
+
properties.push({
|
|
271
|
+
key: 'apiEndpoint',
|
|
272
|
+
value: 'https://api.example.com'
|
|
273
|
+
});
|
|
59
274
|
```
|
|
60
275
|
|
|
61
|
-
|
|
276
|
+
## Integration with MemberJunction
|
|
277
|
+
|
|
278
|
+
This package is a core dependency for most MemberJunction packages. It provides the foundation for:
|
|
279
|
+
|
|
280
|
+
- Entity registration and instantiation
|
|
281
|
+
- Cross-component event communication
|
|
282
|
+
- Singleton service management
|
|
283
|
+
- Global state coordination
|
|
284
|
+
|
|
285
|
+
When building MemberJunction applications or extensions, use this package to ensure proper integration with the framework's architecture.
|
|
286
|
+
|
|
287
|
+
## TypeScript Support
|
|
288
|
+
|
|
289
|
+
This package is written in TypeScript and includes full type definitions. All exports are properly typed for excellent IDE support and compile-time type checking.
|
|
290
|
+
|
|
291
|
+
## Dependencies
|
|
292
|
+
|
|
293
|
+
- **rxjs** (^7.8.1) - For reactive event handling
|
|
294
|
+
|
|
295
|
+
## Development
|
|
296
|
+
|
|
297
|
+
```bash
|
|
298
|
+
# Build the package
|
|
299
|
+
npm run build
|
|
300
|
+
|
|
301
|
+
# Start in development mode with hot reload
|
|
302
|
+
npm run start
|
|
303
|
+
|
|
304
|
+
# Run tests (when implemented)
|
|
305
|
+
npm test
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
## License
|
|
309
|
+
|
|
310
|
+
ISC
|
|
311
|
+
|
|
312
|
+
## Author
|
|
313
|
+
|
|
314
|
+
MemberJunction.com
|
|
62
315
|
|