GithubHelp home page GithubHelp logo

gioragutt / ngx-plugin-modules Goto Github PK

View Code? Open in Web Editor NEW
5.0 3.0 0.0 1.8 MB

An angular library for creating the perfect plugin-based architectures ๐Ÿš€

Home Page: https://ngx-plugin-modules-demo.netlify.app

TypeScript 74.13% JavaScript 16.12% HTML 5.66% SCSS 2.22% CSS 1.86%
angular plugin-architecture plugins architecture angular-library angular-lazy-loading

ngx-plugin-modules's Introduction

ngx-plugin-modules

Netlify Status

Docs: https://ngx-plugin-modules.netlify.app/

TODO - REWRITE README

Purpose

The purpose of this repository is to present a POC of a plugin system, and provide a way for these modules to expose API in a unified way. PluginModules should be lazy-loadable. When lazy-loaded, they should be conditionally loaded (like canLoad with lazy loaded routes).

Why not use the routing mechanism?

This is a POC which will hopefully, after refactoring and improving, be used in a certain app.
This app does not rely on routing, and basically has two routes: /login and /app.
The /app contains a big application, with a map, and a lot of map elements and floating forms.
Under certain circumstances, we don't want to load all feature modules -

  • Different environments
  • A/B Testing
  • Etc

And so, I want to provide a way for modules to expose some api, that will be available to the app, if and once loaded.

Structure

The main library is in /projects/plugin-modules.
It contains the entire plugin modules API, and exposes services which are needed to interact with, an plug into, the plugin module system.

The example plugin system is in /projects/forms-registry.
The purpose of the library is to save and expose a registry of components with certain attributes (e.g: each component has a category, code, etc.).

Each feature module can expose such components by using the forms-registry library.

The Example Application

The app component shows a simple menu with all the components from the registry. Clicking on a component name loads the component below the menu.

Plugin System

Each plugin system can expose instances of the PluginProcessor interface.

// From library
export interface PluginProcessor {
  process(moduleRef: NgModuleRef<any>): void | Promise<void> | Observable<void>;
}

// Userland
export class MyProcessor implements PluginProcessor { ... }

NgModule({
  providers: [
    providePluginProcessor(MyProcessor),
  ],
})
class MyPluginSystemModule { ... }

Plugin systems can save whatever data they want on modules via providers.

When a feature module is loaded and boostrapped, it is ran through the PluginProcessorsService which saves all PluginProcessors and activates them.

When the plugin system processor is activated, this is where you can collect the data saved on the module, and act accordingly.

The Example Plugin System: Forms Registry

The library exposes the FormsRegistryService which is the main public API for the plugin system:

@Injectable()
export class FormsRegistry {
  formEntries$(): Observable<FormEntry[]>;

  add<T = any>(formEntry: FormEntry<T>, componentFactory: ComponentFactory<T>): void;

  resolveComponentFactory<T>(component: Type<T>): ComponentFactory<T> | null;
}

The plugin system registers the PluginProcessor via a forRoot import:

@NgModule({
  imports: [
    PluginsModule.forRoot([
      {
        loadChildren: () => import('./feature1/feature1.module').then(m => m.Feature1Module),
        name: 'feature1', // name provided for diagnostics
      },
    ]),
    FormsRegistryModule.forRoot(),
  ],
})
export class AppModule {}

Feature Modules expose forms to the registry via a forFeature import:

@NgModule({
  declarations: [Feature1FormComponent],
  imports: [
    CommonModule,
    FormsRegistryModule.forFeature([
      {
        category: 'Category 1',
        component: Feature1FormComponent,
        name: 'Feature1FormComponent',
      },
    ]), // expose api
    PluginsModule.forFeature(), // register as module that exposes api via plugins
  ],
})
export class Feature1Module {}

When the module is loaded, the FormsRegistryProcessor collects the forms provided in the feature module, and loads them into the registry with FormRegistryService#add.

ngx-plugin-modules's People

Contributors

gioragutt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.