2.48.0 • Published 4 months ago

@memberjunction/global v2.48.0

Weekly downloads
-
License
ISC
Repository
-
Last release
4 months ago

@memberjunction/global

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.

Installation

npm install @memberjunction/global

Overview

The @memberjunction/global library serves as the foundation for cross-component communication and coordination in MemberJunction applications. It provides:

  • Global Singleton Management - Ensures true singleton instances across module boundaries
  • Class Factory System - Dynamic class registration and instantiation with inheritance support
  • Event System - RxJS-based event bus for component communication
  • Object Caching - In-memory object cache for application lifetime
  • Utility Functions - Common string manipulation, JSON parsing, and formatting utilities

Core Components

MJGlobal Class

The central singleton class that coordinates events and manages components across your application.

import { MJGlobal } from '@memberjunction/global';

// Get the singleton instance
const mjGlobal = MJGlobal.Instance;

// Register a component
mjGlobal.RegisterComponent(myComponent);

// Raise an event
mjGlobal.RaiseEvent({
  component: myComponent,
  event: MJEventType.ComponentEvent,
  eventCode: 'CUSTOM_EVENT',
  args: { data: 'example' }
});

// Listen for events
const subscription = mjGlobal.GetEventListener().subscribe(event => {
  console.log('Event received:', event);
});

// Listen with replay (gets past events too)
const replaySubscription = mjGlobal.GetEventListener(true).subscribe(event => {
  console.log('Event with replay:', event);
});

Class Factory System

Register and instantiate classes dynamically with support for inheritance and key-based selection.

import { RegisterClass, MJGlobal } from '@memberjunction/global';

// Define a base class
class BaseProcessor {
  process(data: any): void {
    console.log('Base processing');
  }
}

// Register a subclass using the decorator
@RegisterClass(BaseProcessor, 'custom', 100)
class CustomProcessor extends BaseProcessor {
  process(data: any): void {
    console.log('Custom processing');
  }
}

// Create instances via the factory
const factory = MJGlobal.Instance.ClassFactory;
const processor = factory.CreateInstance<BaseProcessor>(BaseProcessor, 'custom');
processor.process(data); // Uses CustomProcessor

Object Cache

In-memory caching system for application-lifetime object storage.

const cache = MJGlobal.Instance.ObjectCache;

// Add an object to cache
cache.Add('user:123', { id: 123, name: 'John Doe' });

// Find an object
const user = cache.Find<User>('user:123');

// Replace an existing object
cache.Replace('user:123', { id: 123, name: 'Jane Doe' });

// Remove from cache
cache.Remove('user:123');

// Clear all cached objects
cache.Clear();

BaseSingleton Class

Abstract base class for creating global singleton instances that persist across module boundaries.

import { BaseSingleton } from '@memberjunction/global';

export class MyService extends BaseSingleton<MyService> {
  private data: string[] = [];

  public static get Instance(): MyService {
    return super.getInstance<MyService>();
  }

  public addData(item: string): void {
    this.data.push(item);
  }
}

// Usage anywhere in your app
const service = MyService.Instance;
service.addData('example');

Event Types

The library provides predefined event types for common scenarios:

export const MJEventType = {
  ComponentRegistered: 'ComponentRegistered',
  ComponentUnregistered: 'ComponentUnregistered',
  ComponentEvent: 'ComponentEvent',
  LoggedIn: 'LoggedIn',
  LoggedOut: 'LoggedOut',
  LoginFailed: 'LoginFailed',
  LogoutFailed: 'LogoutFailed',
  ManualResizeRequest: 'ManualResizeRequest',
  DisplaySimpleNotificationRequest: 'DisplaySimpleNotificationRequest',
} as const;

Utility Functions

String Manipulation

import { 
  convertCamelCaseToHaveSpaces,
  stripWhitespace,
  generatePluralName,
  adjustCasing,
  stripTrailingChars,
  replaceAllSpaces
} from '@memberjunction/global';

// Convert camel case to spaces
convertCamelCaseToHaveSpaces('AIAgentLearningCycle'); // "AI Agent Learning Cycle"

// Remove all whitespace
stripWhitespace('  Hello   World  '); // "HelloWorld"

// Generate plural forms
generatePluralName('child'); // "children"
generatePluralName('box'); // "boxes"
generatePluralName('party'); // "parties"

// Adjust casing
adjustCasing('hello', { capitalizeFirstLetterOnly: true }); // "Hello"
adjustCasing('world', { capitalizeEntireWord: true }); // "WORLD"

// Strip trailing characters
stripTrailingChars('example.txt', '.txt', false); // "example"

// Remove all spaces
replaceAllSpaces('Hello World'); // "HelloWorld"

JSON Utilities

import { CleanJSON, SafeJSONParse } from '@memberjunction/global';

// Clean and extract JSON from markdown or mixed content
const cleaned = CleanJSON('```json\n{"key": "value"}\n```');

// Safe JSON parsing with error handling
const parsed = SafeJSONParse<MyType>('{"key": "value"}', true);

HTML Conversion

import { ConvertMarkdownStringToHtmlList } from '@memberjunction/global';

// Convert markdown to HTML list
const html = ConvertMarkdownStringToHtmlList('Unordered', '- Item 1\n- Item 2\n- Item 3');
// Returns: <ul><li>Item 1</li><li>Item 2</li><li>Item 3</li></ul>

Global Object Store

Access the global object store for cross-module state sharing:

import { GetGlobalObjectStore } from '@memberjunction/global';

const globalStore = GetGlobalObjectStore();
// Returns window object in browser, global in Node.js

Manual Resize Request

Trigger a manual resize event across components:

import { InvokeManualResize } from '@memberjunction/global';

// Request resize after 50ms delay
InvokeManualResize(50, myComponent);

Advanced Usage

Class Factory with Parameters

// Register a class that requires constructor parameters
@RegisterClass(BaseService, 'api')
class ApiService extends BaseService {
  constructor(private apiUrl: string) {
    super();
  }
}

// Create with parameters
const service = factory.CreateInstance<BaseService>(
  BaseService, 
  'api', 
  'https://api.example.com'
);

Priority-based Registration

// Lower priority (registered first)
@RegisterClass(BaseHandler, 'data', 10)
class BasicDataHandler extends BaseHandler {}

// Higher priority (overrides BasicDataHandler)
@RegisterClass(BaseHandler, 'data', 20)
class AdvancedDataHandler extends BaseHandler {}

// Will create AdvancedDataHandler instance
const handler = factory.CreateInstance<BaseHandler>(BaseHandler, 'data');

Global Properties

Store and retrieve global properties:

const properties = MJGlobal.Instance.Properties;
properties.push({
  key: 'apiEndpoint',
  value: 'https://api.example.com'
});

Integration with MemberJunction

This package is a core dependency for most MemberJunction packages. It provides the foundation for:

  • Entity registration and instantiation
  • Cross-component event communication
  • Singleton service management
  • Global state coordination

When building MemberJunction applications or extensions, use this package to ensure proper integration with the framework's architecture.

TypeScript Support

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.

Dependencies

  • rxjs (^7.8.1) - For reactive event handling

Development

# Build the package
npm run build

# Start in development mode with hot reload
npm run start

# Run tests (when implemented)
npm test

License

ISC

Author

MemberJunction.com

@everything-registry/sub-chunk-594mj_generatedentities@memberjunction/ai-vectors-entity-sync@memberjunction/graphql-dataprovider@memberjunction/ng-ask-skip@memberjunction/ng-base-forms@memberjunction/ng-base-types@memberjunction/ng-code-editor@memberjunction/ng-treelist@memberjunction/ng-user-view-grid@memberjunction/ng-user-view-properties@memberjunction/queue@memberjunction/scheduled-actions@memberjunction/server@memberjunction/sqlserver-dataprovider@memberjunction/storage@memberjunction/templates@memberjunction/ng-container-directives@memberjunction/ng-data-context@memberjunction/ng-entity-communications@memberjunction/ng-entity-form-dialog@memberjunction/ng-entity-permissions@memberjunction/ng-explorer-core@memberjunction/ng-explorer-settings@memberjunction/ng-file-storage@memberjunction/ng-find-record@memberjunction/ng-form-toolbar@memberjunction/ng-join-grid@memberjunction/ng-list-detail-grid@memberjunction/ng-notifications@memberjunction/ng-query-grid@memberjunction/ng-record-changes@memberjunction/ng-record-selector@memberjunction/ng-resource-permissions@memberjunction/ng-simple-record-list@memberjunction/ng-skip-chat@memberjunction/ng-timeline@memberjunction/ai-recommendations-rex@memberjunction/ai-vector-dupe@memberjunction/ai-vector-sync@memberjunction/ai-vectordb@memberjunction/ai-vectors@memberjunction/ai-vectors-pinecone@memberjunction/ai-vertex@memberjunction/aiengine@memberjunction/codegen-lib@memberjunction/communication-engine@memberjunction/communication-gmail@memberjunction/communication-ms-graph@memberjunction/communication-sendgrid@memberjunction/communication-twilio@memberjunction/communication-types@memberjunction/content-autotagging@memberjunction/core@memberjunction/core-actions@memberjunction/core-entities@memberjunction/core-entities-server@memberjunction/data-context@memberjunction/data-context-server@memberjunction/doc-utils@memberjunction/entity-communications-base@memberjunction/entity-communications-client@memberjunction/entity-communications-server@memberjunction/external-change-detection@memberjunction/templates-base-types@memberjunction/ai-azure@memberjunction/ai-bedrock@memberjunction/ai-betty-bot@memberjunction/ai-cerebras@memberjunction/ai-elevenlabs@memberjunction/ai-engine-base@memberjunction/ai-gemini@memberjunction/ai-groq@memberjunction/ai-heygen@memberjunction/ai-mcp-server@memberjunction/ai-mistral@memberjunction/ai-openai@memberjunction/ai-prompts@memberjunction/ai-recommendations@memberjunction/a2aserver@memberjunction/actions@memberjunction/actions-apollo@memberjunction/actions-base@memberjunction/actions-content-autotag@memberjunction/ai@memberjunction/ai-agents@memberjunction/ai-anthropic
2.23.2

8 months ago

2.46.0

4 months ago

2.23.1

8 months ago

2.34.0

6 months ago

2.19.4

9 months ago

2.19.5

9 months ago

2.19.2

9 months ago

2.19.3

9 months ago

2.19.0

9 months ago

2.19.1

9 months ago

2.34.2

6 months ago

2.34.1

6 months ago

2.45.0

5 months ago

2.22.1

8 months ago

2.22.0

8 months ago

2.22.2

8 months ago

2.33.0

6 months ago

2.18.3

9 months ago

2.18.1

9 months ago

2.18.2

9 months ago

2.18.0

9 months ago

2.21.0

9 months ago

2.44.0

5 months ago

2.29.0

7 months ago

2.29.2

7 months ago

2.29.1

7 months ago

2.32.0

7 months ago

2.32.2

7 months ago

2.32.1

7 months ago

2.17.0

9 months ago

2.43.0

5 months ago

2.20.2

9 months ago

2.20.3

9 months ago

2.20.0

9 months ago

2.20.1

9 months ago

2.28.0

8 months ago

2.31.0

7 months ago

2.39.0

5 months ago

2.16.1

9 months ago

2.16.0

9 months ago

2.42.1

5 months ago

2.42.0

5 months ago

2.27.1

8 months ago

2.27.0

8 months ago

2.30.0

7 months ago

2.15.2

9 months ago

2.15.0

9 months ago

2.15.1

9 months ago

2.38.0

5 months ago

2.41.0

5 months ago

2.26.1

8 months ago

2.26.0

8 months ago

2.37.1

5 months ago

2.37.0

5 months ago

2.14.0

10 months ago

2.40.0

5 months ago

2.25.0

8 months ago

2.48.0

4 months ago

2.13.4

10 months ago

2.36.0

6 months ago

2.13.2

11 months ago

2.13.3

10 months ago

2.13.0

11 months ago

2.36.1

6 months ago

2.13.1

11 months ago

2.47.0

4 months ago

2.24.1

8 months ago

2.24.0

8 months ago

2.12.0

12 months ago

2.35.1

6 months ago

2.35.0

6 months ago

2.23.0

8 months ago

2.11.0

12 months ago

2.10.0

12 months ago

2.9.0

12 months ago

2.8.0

1 year ago

2.7.0

1 year ago

2.7.1

1 year ago

2.6.1

1 year ago

2.6.0

1 year ago

2.5.2

1 year ago

1.6.1

1 year ago

1.6.0

1 year ago

2.4.1

1 year ago

2.4.0

1 year ago

1.5.3

1 year ago

1.5.2

1 year ago

1.5.1

1 year ago

1.5.0

1 year ago

2.3.0

1 year ago

2.3.2

1 year ago

2.3.1

1 year ago

2.3.3

1 year ago

1.4.1

1 year ago

1.4.0

1 year ago

2.2.1

1 year ago

2.2.0

1 year ago

1.3.3

1 year ago

1.3.2

1 year ago

1.3.1

1 year ago

1.3.0

1 year ago

2.1.2

1 year ago

2.1.1

1 year ago

2.1.4

1 year ago

2.1.3

1 year ago

2.1.5

1 year ago

2.1.0

1 year ago

2.0.0

1 year ago

1.8.1

1 year ago

1.8.0

1 year ago

1.7.1

1 year ago

1.7.0

1 year ago

2.5.0

1 year ago

2.5.1

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

1.2.0

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.0.11

1 year ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.8-next.6

2 years ago

1.0.8-next.5

2 years ago

1.0.8-next.4

2 years ago

1.0.8-next.3

2 years ago

1.0.8-next.2

2 years ago

1.0.8-next.1

2 years ago

1.0.8-next.0

2 years ago

1.0.7-next.0

2 years ago

1.0.8-beta.0

2 years ago

1.0.2

2 years ago

1.0.6

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.9.168

2 years ago

0.9.167

2 years ago

0.9.166

2 years ago

0.9.165

2 years ago

0.9.164

2 years ago

0.9.161

2 years ago

0.9.160

2 years ago

0.9.163

2 years ago

0.9.162

2 years ago

0.9.157

2 years ago

0.9.159

2 years ago

0.9.156

2 years ago

0.9.155

2 years ago

0.9.158

2 years ago

0.9.153

2 years ago

0.9.152

2 years ago

0.9.148

2 years ago

0.9.147

2 years ago

0.9.146

2 years ago

0.9.143

2 years ago

0.9.142

2 years ago

0.9.141

2 years ago

0.9.140

2 years ago

0.9.139

2 years ago

0.9.138

2 years ago

0.9.136

2 years ago

0.9.137

2 years ago

0.9.135

2 years ago

0.9.134

2 years ago

0.9.133

2 years ago

0.9.132

2 years ago

0.9.123

2 years ago

0.9.117

2 years ago

0.9.108

2 years ago

0.9.107

2 years ago

0.9.106

2 years ago

0.9.105

2 years ago

0.9.104

2 years ago

0.9.102

2 years ago

0.9.101

2 years ago

0.9.100

2 years ago

0.9.98

2 years ago

0.9.99

2 years ago

0.9.96

2 years ago

0.9.97

2 years ago

0.9.92

2 years ago

0.9.93

2 years ago

0.9.94

2 years ago

0.9.95

2 years ago

0.9.91

2 years ago

0.9.90

2 years ago

0.9.89

2 years ago

0.9.88

2 years ago

0.9.85

2 years ago

0.9.86

2 years ago

0.9.87

2 years ago

0.9.82

2 years ago

0.9.83

2 years ago

0.9.84

2 years ago

0.9.81

2 years ago

0.9.80

2 years ago

0.9.78

2 years ago

0.9.79

2 years ago

0.9.76

2 years ago

0.9.77

2 years ago

0.9.75

2 years ago

0.9.74

2 years ago

0.9.73

2 years ago

0.9.72

2 years ago

0.9.71

2 years ago

0.9.70

2 years ago

0.9.68

2 years ago

0.9.67

2 years ago

0.9.66

2 years ago

0.9.65

2 years ago

0.9.64

2 years ago

0.9.63

2 years ago

0.9.62

2 years ago

0.9.61

2 years ago

0.9.60

2 years ago

0.9.59

2 years ago

0.9.58

2 years ago

0.9.57

2 years ago

0.9.56

2 years ago

0.9.55

2 years ago

0.9.54

2 years ago

0.9.52

2 years ago

0.9.51

2 years ago

0.9.50

2 years ago

0.9.49

2 years ago

0.9.48

2 years ago

0.9.47

2 years ago

0.9.46

2 years ago

0.9.45

2 years ago

0.9.44

2 years ago

0.9.43

2 years ago

0.9.42

2 years ago

0.9.40

2 years ago

0.9.39

2 years ago

0.9.38

2 years ago

0.9.37

2 years ago

0.9.36

2 years ago

0.9.35

2 years ago

0.9.34

2 years ago

0.9.33

2 years ago

0.9.32

2 years ago

0.9.31

2 years ago

0.9.30

2 years ago

0.9.29

2 years ago

0.9.28

2 years ago

0.9.27

2 years ago

0.9.26

2 years ago

0.9.25

2 years ago

0.9.24

2 years ago

0.9.23

2 years ago

0.9.22

2 years ago

0.9.21

2 years ago

0.9.20

2 years ago

0.9.19

2 years ago

0.9.16

2 years ago

0.9.15

2 years ago

0.9.14

2 years ago

0.9.13

2 years ago

0.9.12

2 years ago

0.9.11

2 years ago

0.9.9

2 years ago

0.9.8

2 years ago

0.9.7

2 years ago

0.9.6

2 years ago

0.9.5

2 years ago

0.9.4

2 years ago

0.9.3

2 years ago

0.9.2

2 years ago

0.9.1

2 years ago

0.9.0

2 years ago