Skip to content

Commit

Permalink
Intro 'either',
Browse files Browse the repository at this point in the history
refactor to use it to boot the app w/o
blowing up in the browser.
Populate window.initialData in a
browser context.
One more util fn. :)
  • Loading branch information
wayneseymour committed Feb 6, 2020
1 parent e3e76bd commit ecf0724
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 30 deletions.
23 changes: 3 additions & 20 deletions src/dev/code_coverage/cc_app/public/initial_data_raw.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,3 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

const initialData = {
testRunnerTypes: [
{
Expand All @@ -41,12 +22,14 @@ https://build-stats.elastic.co/app/kibana#/dashboard/02b9d310-9513-11e8-a9c0-db5
historicalItems: [],
};


if (!isInBrowser()) {
module.exports.default = {}
module.exports.default = initialData
} else {
window.initialData = initialData;
}

function isInBrowser() {
return !!(typeof window !== 'undefined');
}

29 changes: 19 additions & 10 deletions src/dev/code_coverage/cc_app/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,31 @@ import ReactDOM from 'react-dom';
import './index.css';
import App from './app';
import * as serviceWorker from './service_worker';
import { pretty as p } from './utils/pretty';
import { tryCatch as tc } from './utils/either';

const initialData = window.initialData;
const { testRunnerTypes, buildStats, historicalItems } = initialData;
const tryInit = () => tc(() => initialData);
const tryOneProp = () => tc(() => initialData.testRunnerTypes);

initialPrint();
tryInit()
.chain(tryOneProp)
.map(boot);

ReactDOM.render(
<App testRunnerTypes={testRunnerTypes} buildStats={buildStats} historicalItems={historicalItems} />,
document.getElementById('root')
);
function boot(testRunnerTypes) {
const { buildStats, historicalItems } = initialData;
initPrint(initialData);
ReactDOM.render(
<App testRunnerTypes={testRunnerTypes} buildStats={buildStats} historicalItems={historicalItems}/>,
document.getElementById('root'),
);
}

function initPrint(x) {
console.log(`\n### initial data: \n\n${p(x)}`);
}

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister();

function initialPrint() {
console.log(`\n### initial data: \n\n${JSON.stringify(window.initialData, null, 2)}`);
}
49 changes: 49 additions & 0 deletions src/dev/code_coverage/cc_app/src/utils/either.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
export const Right = x =>
({
chain: f => f(x),
map: f => Right(f(x)),
fold: (f, g) => g(x),
inspect: () => `Right(${x})`
});

Right.of = function of(x) {
return new Right(x);
};

export function right(x) {
return Right.of(x);
}

export const Left = x =>
({
chain: f => Left(x),
map: f => Left(x),
fold: (f, g) => f(x),
inspect: () => `Left(${x})`
});

Left.of = function of(x) {
return new Left(x);
};

export function left(x) {
return Left.of(x);
}

export const fromNullable = x =>
(
x !== null &&
x !== undefined &&
x !== false &&
x !== 'undefined'
)
? Right(x)
: Left(null);

export const tryCatch = f => {
try {
return Right(f());
} catch (e) {
return Left(e);
}
};
1 change: 1 addition & 0 deletions src/dev/code_coverage/cc_app/src/utils/pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const pretty = x => JSON.stringify(x, null, 2);

0 comments on commit ecf0724

Please sign in to comment.