Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example resolver for render blocking #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"@angular/platform-server": "~5.0.0",
"@angular/router": "~5.0.0",
"@angular/upgrade": "~5.0.0",
"@splitsoftware/splitio": "^10.2.0",
"@splitsoftware/splitio": "^10.3.0",
"core-js": "^2.4.1",
"rxjs": "^5.5.0",
"zone.js": "^0.8.4"
Expand Down
21 changes: 21 additions & 0 deletions src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { FeaturesComponent } from './features/features.component';

// We'll only resolve Split here but we could potentially
// use it for other prerrequisite data.
import { AppResolver } from './app.resolver';

const routes: Routes = [
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{ path: 'home', component: FeaturesComponent, resolve: {
prerrequisitesReady: AppResolver
}}
];

@NgModule({
imports: [ RouterModule.forRoot(routes) ],
exports: [ RouterModule ]
})
export class AppRoutingModule {}
3 changes: 2 additions & 1 deletion src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
<h1>{{title}}</h1>
<app-features></app-features>
<!-- Here's where our actual page will be -->
<router-outlet></router-outlet>
2 changes: 1 addition & 1 deletion src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import { Component } from '@angular/core';
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Example App Angular';
title = 'App component is kind of dumb, used for root <router-outlet>';
}
10 changes: 8 additions & 2 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,26 @@ import { FormsModule } from '@angular/forms';

import { AppComponent } from './app.component';

import { AppRoutingModule } from './app-routing.module';

// splitio sdk
import { SplitioService } from './splitio.service';
import { AppResolver } from './app.resolver';
import { FeaturesComponent } from './features/features.component';

@NgModule({
imports: [
BrowserModule,
FormsModule
FormsModule,
AppRoutingModule
],
declarations: [
AppComponent,
FeaturesComponent
],
providers: [
providers: [
// Import the resolver
AppResolver,
SplitioService
],
bootstrap: [ AppComponent ]
Expand Down
15 changes: 15 additions & 0 deletions src/app/app.resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Injectable } from '@angular/core';
import { Resolve, ActivatedRouteSnapshot } from '@angular/router';
import { SplitioService } from './splitio.service';

@Injectable()
export class AppResolver implements Resolve<any> {
// Inject the service on the resolver.
constructor(private splitService: SplitioService) {}

resolve() {
// Use it on the resolve function and return a promise
// for the ready.
return this.splitService.getReadyPromise();
}
}
2 changes: 1 addition & 1 deletion src/app/features/features.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ export class FeaturesComponent implements OnInit {
constructor(public splitioService: SplitioService) { }

ngOnInit() {
this.splitioService.initSdk();
this.splitioService.getTreatments();
}
}
30 changes: 13 additions & 17 deletions src/app/splitio.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,18 @@ export class SplitioService {
isReady: boolean = false;
treatments: SplitIO.Treatments
features: string[] = [
'feature_1',
'feature_2',
'feature_3'
'feature_1', 'feature_2'
];

constructor() { }
constructor() {
this.initSdk();
}

initSdk(): void {
// Running the SDK in 'off-the-grid' Mode since authorizationKey : 'localhost'
// To bind a non 'off-the-grid' client, inject the real API Key
this.splitio = SplitFactory({
core: {
authorizationKey: 'localhost',
authorizationKey: 'your-api-key',
key: 'customer-key'
},
// In non-localhost mode, this map is ignored.
features: {
feature_1: 'off',
feature_2: 'on',
feature_3: 'v2'
}
});

Expand All @@ -39,16 +31,20 @@ export class SplitioService {
this.verifyReady();
}

getReadyPromise(): Promise<boolean> {
return this.splitClient.ready().then(()=> true).catch(() => false);
}

private verifyReady(): void {
const isReadyEvent = fromEvent(this.splitClient, this.splitClient.Event.SDK_READY);

const subscription = isReadyEvent.subscribe({
next() {
next() {
this.isReady = true;
console.log('Sdk ready: ', this.isReady);
console.log('Sdk ready: ', this.isReady);
},
error(err) {
console.log('Sdk error: ', err);
error(err) {
console.log('Sdk error: ', err);
this.isReady = false;
}
});
Expand Down