Skip to content

Commit

Permalink
chore(script): init http script lib
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelramos committed May 2, 2019
1 parent 075e892 commit 69264d9
Show file tree
Hide file tree
Showing 17 changed files with 254 additions and 1 deletion.
38 changes: 38 additions & 0 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,44 @@
"styleext": "scss"
}
}
},
"http-script": {
"root": "libs/http/script",
"sourceRoot": "libs/http/script/src",
"projectType": "library",
"prefix": "ng-lab",
"architect": {
"build": {
"builder": "@angular-devkit/build-ng-packagr:build",
"options": {
"tsConfig": "libs/http/script/tsconfig.lib.json",
"project": "libs/http/script/ng-package.json"
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"libs/http/script/tsconfig.lib.json",
"libs/http/script/tsconfig.spec.json"
],
"exclude": ["**/node_modules/**"]
}
},
"test": {
"builder": "@nrwl/builders:jest",
"options": {
"jestConfig": "libs/http/script/jest.config.js",
"tsConfig": "libs/http/script/tsconfig.spec.json",
"setupFile": "libs/http/script/src/test-setup.ts"
}
}
},
"schematics": {
"@nrwl/schematics:component": {
"styleext": "scss"
}
}
}
},
"cli": {
Expand Down
9 changes: 9 additions & 0 deletions libs/http/script/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
name: 'http-script',
preset: '../../../jest.config.js',
coverageDirectory: '../../../coverage/libs/http/script',
snapshotSerializers: [
'jest-preset-angular/AngularSnapshotSerializer.js',
'jest-preset-angular/HTMLCommentSerializer.js'
]
};
7 changes: 7 additions & 0 deletions libs/http/script/ng-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "../../node_modules/ng-packagr/ng-package.schema.json",
"dest": "../../../dist/libs/http/script",
"lib": {
"entryFile": "src/index.ts"
}
}
8 changes: 8 additions & 0 deletions libs/http/script/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@ng-lab/http-script",
"version": "0.0.1",
"peerDependencies": {
"@angular/common": "^7.2.0",
"@angular/core": "^7.2.0"
}
}
4 changes: 4 additions & 0 deletions libs/http/script/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './lib/http-script.module';

export { SCRIPTS_LOADER } from './lib/http-script.token';
export { HttpScriptLoaderService } from './lib/http-script.service';
14 changes: 14 additions & 0 deletions libs/http/script/src/lib/http-script.module.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { async, TestBed } from '@angular/core/testing';
import { HttpScriptModule } from './http-script.module';

describe('HttpScriptModule', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [HttpScriptModule]
}).compileComponents();
}));

it('should create', () => {
expect(HttpScriptModule).toBeDefined();
});
});
21 changes: 21 additions & 0 deletions libs/http/script/src/lib/http-script.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { NgModule } from '@angular/core';
import { CommonModule, DOCUMENT } from '@angular/common';

import { SCRIPTS_LOADER } from './http-script.token';
import { HttpScriptLoaderService } from './http-script.service';

@NgModule({
imports: [CommonModule],
providers: [
{
provide: SCRIPTS_LOADER,
useValue: []
},
{
provide: HttpScriptLoaderService,
useClass: HttpScriptLoaderService,
deps: [DOCUMENT, SCRIPTS_LOADER]
}
]
})
export class HttpScriptModule {}
78 changes: 78 additions & 0 deletions libs/http/script/src/lib/http-script.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* @license
* Copyright NgLab All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://www.ng-lab.com/license
*/
import { DOCUMENT } from '@angular/common';
import { Injectable, Inject } from '@angular/core';

import { Observable, Observer } from 'rxjs';

import { SCRIPTS_LOADER } from './http-script.token';
import { ScriptInterface } from './http-script.typing';

@Injectable()
export class HttpScriptLoaderService {
private _scripts: ScriptInterface[] = [];

constructor(
@Inject(DOCUMENT) private _doc: HTMLDocument,
@Inject(SCRIPTS_LOADER) private _loadScripts: ScriptInterface[]
) {
this._initLoader();
}

public load(script: ScriptInterface): Observable<ScriptInterface> {
return new Observable<ScriptInterface>(
(observer: Observer<ScriptInterface>) => {
const scriptExist = this._scripts.find(js => js.name === script.name);

if (scriptExist && scriptExist.loaded) {
observer.next(scriptExist);
observer.complete();
} else {
this._scripts.push(script);
this._createHeaderScriptTag(script, observer);
}
}
);
}

public hasScript(name: string): boolean {
return !!this._scripts.find(script => script.name === name);
}

public isLoaded(name: string): boolean {
const script = this._scripts.find(js => js.name === name);

return script ? script.loaded : false;
}

private _initLoader(): void {
this._loadScripts.map(script => this.load(script).subscribe());
}

private _createHeaderScriptTag(
script: ScriptInterface,
observer: Observer<ScriptInterface>
): void {
const scriptTag = this._doc.createElement('script');
scriptTag.type = 'text/javascript';
scriptTag.src = script.src;
scriptTag.async = true;

scriptTag.addEventListener('load', () => {
script.loaded = true;
observer.next(script);
observer.complete();
});

scriptTag.addEventListener('error', () => {
observer.error(`Cannot load script ${script.name}.`);
});

this._doc.getElementsByTagName('body')[0].appendChild(scriptTag);
}
}
11 changes: 11 additions & 0 deletions libs/http/script/src/lib/http-script.token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { InjectionToken } from '@angular/core';

import { ScriptInterface } from './http-script.typing';

export const SCRIPTS_LOADER = new InjectionToken<ScriptInterface>(
'[SCRIPTS] Scripts loader token',
{
providedIn: 'root',
factory: () => [] as any
}
);
5 changes: 5 additions & 0 deletions libs/http/script/src/lib/http-script.typing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface ScriptInterface {
name: string;
src: string;
loaded: boolean;
}
1 change: 1 addition & 0 deletions libs/http/script/src/test-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import 'jest-preset-angular';
7 changes: 7 additions & 0 deletions libs/http/script/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"types": ["node", "jest"]
},
"include": ["**/*.ts"]
}
29 changes: 29 additions & 0 deletions libs/http/script/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc/libs/http/script",
"target": "es2015",
"module": "es2015",
"moduleResolution": "node",
"declaration": true,
"sourceMap": true,
"inlineSources": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"types": [],
"lib": ["dom", "es2018"],
"paths": {
"@ng-lab/*": ["dist/libs/*"]
}
},
"angularCompilerOptions": {
"annotateForClosureCompiler": true,
"skipTemplateCodegen": true,
"strictMetadataEmit": true,
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true,
"enableResourceInlining": true
},
"exclude": ["src/test.ts", "**/*.spec.ts"]
}
10 changes: 10 additions & 0 deletions libs/http/script/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc/libs/http/script",
"module": "commonjs",
"types": ["jest", "node"]
},
"files": ["src/test-setup.ts"],
"include": ["**/*.spec.ts", "**/*.d.ts"]
}
7 changes: 7 additions & 0 deletions libs/http/script/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../../tslint.json",
"rules": {
"directive-selector": [true, "attribute", "ng-lab", "camelCase"],
"component-selector": [true, "element", "ng-lab", "kebab-case"]
}
}
3 changes: 3 additions & 0 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
},
"http-url": {
"tags": ["scope:lib", "type:functional"]
},
"http-script": {
"tags": ["scope:lib", "type:functional"]
}
}
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"@ng-lab/hub": ["libs/hub/src/index.ts"],
"@ng-lab/responsive": ["libs/responsive/src/index.ts"],
"@ng-lab/jwt": ["libs/jwt/src/index.ts"],
"@ng-lab/http/url": ["libs/http/url/src/index.ts"]
"@ng-lab/http/url": ["libs/http/url/src/index.ts"],
"@ng-lab/http/script": ["libs/http/script/src/index.ts"]
}
},
"exclude": ["node_modules", "tmp"]
Expand Down

0 comments on commit 69264d9

Please sign in to comment.