Skip to content

Commit

Permalink
feat($ui): Upgraded the UI to pick up changes dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduard Kyvenko committed Jul 12, 2017
1 parent eeee64a commit 3df3246
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import $ from 'jquery';
import {
tests,
testsObservable,
config,
getUserAgentInfo,
getTest,
Expand All @@ -18,7 +19,12 @@ import { uiFactory } from './ui';

initialize();

const ui = uiFactory(tests, reset, getCurrentTestVariation, getUserAgentInfo);
const ui = uiFactory(
testsObservable,
reset,
getCurrentTestVariation,
getUserAgentInfo
);

$(() => {
if (shouldShowUI()) {
Expand Down
5 changes: 5 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import _getUserAgentInfo, { UserAgentInfo } from './useragentinfo';
import userSession, { UserSession } from './usersession';
import { SplitTest, InternalVariation } from './splittest';
import { BehavioralSubject } from './behavioral-subject';
export { SplitTest } from './splittest';
import {
TrackingDataExtender,
Expand All @@ -21,6 +22,9 @@ export interface UserConfig {

const userAgentInfo = _getUserAgentInfo();
export const tests: SplitTest[] = [];
export const testsObservable: BehavioralSubject<
SplitTest[]
> = new BehavioralSubject(tests);

export function config(userConfig: UserConfig = {}) {
if (userConfig.cookieName) {
Expand Down Expand Up @@ -106,6 +110,7 @@ export function create(name: string): SplitTest {
baseTrackingDataExtenderFactory()
);
tests.push(test);
test.changes.subscribe(() => testsObservable.next(tests));
return test;
}

Expand Down
4 changes: 4 additions & 0 deletions src/splittest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { UserAgentInfo } from './useragentinfo';
import userSession from './usersession';
import { BehavioralSubject } from './behavioral-subject';
import {
TrackingDataExtender,
trackingDataExtenderFactory,
Expand Down Expand Up @@ -31,6 +32,7 @@ export interface InternalVariation extends Variation {

export class SplitTest {
isInitialized = false;
changes = new BehavioralSubject(this);

private condition: ConditionFunction;
private readonly _variations: InternalVariation[] = [];
Expand Down Expand Up @@ -85,6 +87,7 @@ export class SplitTest {
weight: typeof variation.weight === 'number' ? variation.weight : 1
});
this.normalizeVariationWeights();
this.changes.next(this);
return this;
}

Expand Down Expand Up @@ -127,6 +130,7 @@ export class SplitTest {
this.trackViewed();
}
this.isInitialized = true;
this.changes.next(this);
return true;
}

Expand Down
17 changes: 14 additions & 3 deletions src/ui.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import $ from 'jquery';
import { InternalVariation, SplitTest } from './splittest';
import { UserAgentInfo } from './useragentinfo';
import { BehavioralSubject } from './behavioral-subject';

declare const require: any;

Expand All @@ -13,7 +14,7 @@ function getVariationPercentage(variation: InternalVariation): string {
}

export const uiFactory = (
tests: SplitTest[],
tests: BehavioralSubject<SplitTest[]>,
reset: () => void,
getCurrentTestVariation: (testName: string) => string,
getUserAgentInfo: () => UserAgentInfo
Expand Down Expand Up @@ -79,8 +80,18 @@ export const uiFactory = (
const style = document.createElement('style');
style.innerHTML = require('./main.css');

const testListEl = document.createElement('div');
testListEl.className = 'test-list';

tests.subscribe(list => {
while (testListEl.hasChildNodes()) {
testListEl.removeChild(<Node>testListEl.lastChild);
}
testListEl.innerHTML = list.map(renderTest).join('');
});

$abTestContainer = $(`<div class="ui-container hideme"></div>`).append(
`<div class="test-list">${tests.map(renderTest).join('')}</div>`
testListEl
);

$(`<button type="button" class="reset">Reset all</button>`)
Expand All @@ -102,7 +113,7 @@ export const uiFactory = (
function show() {
if (isInitialized) {
$abTestContainer.removeClass('hideme');
} else if (tests.length > 0) {
} else {
showSplitTestUi();
}
}
Expand Down

0 comments on commit 3df3246

Please sign in to comment.