Docs: Context - rollthecloudinc/quell GitHub Wiki

Documentation for Angular Context Library


Table of Contents

  1. Introduction
  2. Installation
  3. Overview
  4. Key Concepts
    • Context Plugins
    • Inline Contexts
    • Context Resolvers
  5. Core Components
    • ContextModule
    • Components (ContextFormComponent, ContextDatasourceComponent, etc.)
  6. Key Services
    • ContextPluginManager
    • InlineContextResolverService
    • ResolvedContextPluginManager
  7. Usage
    • Define and Use Contexts
    • Registering and Resolving Context Plugins
  8. API Reference
  9. Examples
  10. Testing

1. Introduction

The Angular Context Library provides a structured approach to managing and resolving application contexts, allowing dynamic resolution, contextual data management, and plugin-based extensibility. Contexts can represent application states, runtime parameters, or forms of data that need to be resolved dynamically.


2. Installation

Install the library using npm:

npm install @your-org/context

Dependencies such as @rollthecloudinc/attributes, @rollthecloudinc/plugin, and @rollthecloudinc/datasource might also be required as they provide support for plugins, attributes, and data sources.


3. Overview

The ContextModule is at the core of this library and integrates its key concepts:

  • Context Plugins: Define and register plugins to manage specific types of contexts.
  • Dynamic Resolution: Resolve context data dynamically at runtime using resolvers.
  • Inline Contexts: Inline definitions of contexts that can be resolved dynamically or stored for later use.
  • Data Binding: Bind contexts to data sources, creating reusable content across the app.

The library extends the realm of dynamic data handling by providing tools for rendering, resolving, and managing contexts through plugins, components, and services.


4. Key Concepts

4.1 Context Plugins

A ContextPlugin represents an entity used for resolving context-specific data. It is defined with:

  • Name: Plugin identifier.
  • Resolver: A class or service that resolves the plugin's data.
  • Base Object: Parameters or base settings for the plugin.

Example:

export const routeContextFactory = (resolver: RouteResolver) => {
  const baseObject = {
    path: '',
  };
  return new ContextPlugin<string>({
    id: 'route',
    name: 'route',
    title: 'Route',
    global: true,
    baseObject,
    resolver,
  });
};

4.2 Inline Contexts

An InlineContext defines context configurations inline, including plugins and adaptors. Inline contexts can be used for dynamic resolution and runtime data management.

Adaptors supported:

  • Rest: API integrations.
  • Datasource: Integration with data sources.
  • Token: Tokenized data inputs.
  • Snippet: Script or JSON snippets for dynamic configurations.

Example:

const inlineContext = new InlineContext({
  name: 'user',
  adaptor: 'rest',
  rest: {
    method: 'GET',
    url: '/api/users',
  },
});

4.3 Context Resolvers

Resolvers are services responsible for resolving the data associated with a context. A resolver implements the ContextResolver interface, handling the process of transforming context plugins or inline contexts into resolved data.


5. Core Components

5.1 ContextModule

The ContextModule encapsulates all the components, services, and factories required for managing contexts. It integrates with Angular features such as forms, material design, and flex layout.

Key Features:

  • Provides core services like ContextPluginManager and InlineContextResolverService.
  • Dynamically registers resolvers, components, and plugins.
@NgModule({
  imports: [
    CommonModule,
    ReactiveFormsModule,
    MaterialModule,
    TokenModule,
    DatasourceModule,
  ],
  providers: [
    { provide: CONTEXT_PLUGIN, useFactory: routeContextFactory, multi: true },
  ],
  declarations: [
    ContextFormComponent,
    ContextDatasourceComponent,
    DatasourceContextEditorComponent,
  ],
  exports: [
    ContextFormComponent,
    ContextDatasourceComponent,
    DatasourceContextEditorComponent,
  ],
})
export class ContextModule {}

5.2 Components

  • ContextFormComponent: A dynamic form for creating and managing contexts.
  • ContextDatasourceComponent: Bridges data source-based contexts with forms.
  • DatasourceContextEditorComponent: Provides an editing UI for context datasource configurations.

Example:

<classifieds-ui-context-form [context]="inlineContext"></classifieds-ui-context-form>

6. Key Services

6.1 ContextPluginManager

The ContextPluginManager extends the BasePluginManager class and manages the lifecycle of context plugins. It handles the registration and retrieval of plugins, enabling contexts to be loaded dynamically.

@Injectable({
  providedIn: 'root',
})
export class ContextPluginManager extends BasePluginManager<ContextPlugin<string>, string> {
  pluginDef() {
    return of(new PluginDef({ name: 'context' }));
  }
}

6.2 InlineContextResolverService

This service resolves inline contexts by invoking the appropriate plugin resolver or directly processing adaptors.

@Injectable({
  providedIn: 'root',
})
export class InlineContextResolverService extends BaseInlineContextResolverService {
  resolveMerged(contexts: Array<InlineContext>): Observable<any> {
    return super.resolveMerged(contexts);
  }
}

6.3 ResolvedContextPluginManager

Manages plugins specific to resolved contexts by providing dynamic resolution logic.

@Injectable({
  providedIn: 'root',
})
export class ResolvedContextPluginManager extends BasePluginManager<ResolvedContextPlugin<string>, string> {
  register(plugin: ResolvedContextPlugin<string>) {
    super.register(plugin);
  }
}

7. Usage

7.1 Define and Use Contexts

Create a dynamic form using InlineContext and the ContextFormComponent.

Template:

<classifieds-ui-context-form [context]="inlineContext"></classifieds-ui-context-form>

Script:

const inlineContext = new InlineContext({
  name: 'example',
  adaptor: 'rest',
  rest: {
    method: 'GET',
    url: '/api/data',
  },
});

7.2 Registering Context Plugins

Define and register a new context plugin in the ContextModule or via ContextPluginManager.

contextPluginManager.register(
  new ContextPlugin({
    id: 'custom',
    name: 'Custom Context',
    resolver: new CustomResolver(),
  })
);

8. API Reference

Classes

  • ContextPlugin: Represents a plugin for resolving contexts.
  • InlineContext: Inline configuration for context resolution.
  • ContextDatasource: Manages context's integration with data sources.

Services

  • ContextPluginManager: Manages the lifecycle of context plugins.
  • InlineContextResolverService: Resolves inline contexts dynamically.
  • ParamContextExtractorService: Extracts context references from parameter mappings.

Components

  • ContextFormComponent: A dynamic form builder for contexts.
  • ContextDatasourceComponent: Bridges contexts and data sources.

9. Examples

Example 1: Define and Resolve Contexts

Use the ContextFormComponent to define a REST-based inline context.

<classifieds-ui-context-form [context]="restContext"></classifieds-ui-context-form>
const restContext = new InlineContext({
  name: 'rest-context',
  adaptor: 'rest',
  rest: {
    method: 'GET',
    url: '/api/example',
  },
});

Example 2: Register Plugins Dynamically

Register a custom context plugin at runtime.

contextPluginManager.register(
  new ContextPlugin<string>({
    id: 'custom-plugin',
    name: 'Custom Plugin',
    resolver: new CustomContextResolver(),
  })
);

10. Testing

Unit testing can be performed using Angular's testing utilities.

To run tests, use:

npm run test

Example test configuration:

describe('ContextManagerService', () => {
  let service: ContextManagerService;

  beforeEach(() => {
    TestBed.configureTestingModule({});
    service = TestBed.inject(ContextManagerService);
  });

  it('should register and retrieve plugins', () => {
    const plugin = new ContextPlugin({ id: 'test', name: 'Test Plugin' });
    service.register(plugin);
    expect(service.lookupContext('test')).toEqual(plugin);
  });
});

Conclusion

The Angular Context Library provides an extensible framework for managing dynamic context data within Angular applications. Using its plugin system, resolvers, and inline configuration, it offers flexibility and modularity for managing contextual data at runtime.

For any enhancements or feature requests, feel free to reach out!

⚠️ **GitHub.com Fallback** ⚠️