Skip to content

Commit

Permalink
feat(lib): enable overriding of the elements registry, fixes #9
Browse files Browse the repository at this point in the history
- use LAZY_ELEMENTS_REGISTRY token (with LazyElementsRegistry interface)
- can be used to share registry between multiple apps / elements
- can prevent multiple downloads of the same javascript bundles
  • Loading branch information
tomastrajan committed Nov 17, 2019
1 parent a186d20 commit e11a112
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Injectable, Type, Optional, Inject } from '@angular/core';

import { LazyElementRootOptions } from './lazy-elements.module';
import { LAZY_ELEMENT_ROOT_OPTIONS } from './lazy-elements.tokens';
import {
LAZY_ELEMENT_ROOT_OPTIONS,
LAZY_ELEMENTS_REGISTRY,
LazyElementsRegistry
} from './lazy-elements.tokens';

const LOG_PREFIX = '@angular-extensions/elements';

Expand All @@ -18,10 +22,10 @@ export interface ElementConfig {
providedIn: 'root'
})
export class LazyElementsLoaderService {
registry: Map<string, Promise<void>> = new Map<string, Promise<void>>();
configs: ElementConfig[] = [];

constructor(
@Inject(LAZY_ELEMENTS_REGISTRY) private registry: LazyElementsRegistry,
@Optional()
@Inject(LAZY_ELEMENT_ROOT_OPTIONS)
public options: LazyElementRootOptions
Expand Down
14 changes: 14 additions & 0 deletions projects/elements/src/lib/lazy-elements/lazy-elements.tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,17 @@ export const LAZY_ELEMENT_ROOT_OPTIONS = new InjectionToken<
export const LAZY_ELEMENT_ROOT_GUARD = new InjectionToken<void>(
'LAZY_ELEMENT_ROOT_GUARD'
);

export const LAZY_ELEMENTS_REGISTRY = new InjectionToken<LazyElementsRegistry>(
'Lazu elements registry',
{
providedIn: 'root',
factory: () => new Map<string, Promise<void>>()
}
);

export interface LazyElementsRegistry {
get: (url: string) => Promise<void>;
set: (url: string, notifier: Promise<void>) => void;
has: (url: string) => boolean;
}

0 comments on commit e11a112

Please sign in to comment.