Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

React 16 compatibility and new tag generator #3

Open
wants to merge 2 commits 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
4 changes: 2 additions & 2 deletions Node-DC-SSR-electrode/src/client/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { Router, browserHistory } from 'react-router';
import { createStore } from 'redux';
import { Provider } from 'react-redux';
import rootReducer from './reducers';
import injectTapEventPlugin from 'react-tap-event-plugin';
// import injectTapEventPlugin from 'react-tap-event-plugin';

injectTapEventPlugin();
// injectTapEventPlugin();

window.webappStart = () => {
const initialState = window.__PRELOADED_STATE__;
Expand Down
94 changes: 27 additions & 67 deletions Node-DC-SSR-electrode/src/client/components/generators/TagCount.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,12 @@
import React from 'react';
import PropTypes from 'prop-types';

export default class TagCount extends React.Component {
static propTypes = {
count: PropTypes.number,
depth: PropTypes.number,
node: PropTypes.oneOfType([
PropTypes.element,
PropTypes.string
]),
leaf: PropTypes.node,
useCache: PropTypes.bool
};

static defaultProps = {
count: 3000,
depth: 7,
node: 'div',
leaf: '.',
useCache: false
};

static cache = {};

createTag(children) {
const {node, leaf} = this.props;
if(children.length > 0) {
return React.createElement(node, null, ...children);
} else {
return React.createElement(node, null, leaf);
}
}

generateTagsHelper(curdepth, children, genState) {
const {count, depth} = this.props;

if(curdepth >= depth) {
return this.createTag([]);
}

const newChildren = Math.max(0, Math.min(children, count - genState.count));
genState.count += newChildren;
const numberOfChildren = (depth, count) => {

const elems = new Array(newChildren);
for(let ii = 0; ii < newChildren; ++ii) {
elems[ii] = this.generateTagsHelper(curdepth + 1, children, genState);
if(depth == 1 || depth > count) {
return count;
}

return this.createTag(elems);
}

generateTags() {
const {depth, node, count} = this.props;

let children = 2;

for(; ; ++children) {
Expand All @@ -63,30 +17,36 @@ export default class TagCount extends React.Component {
}
}

const genState = {count: 1};

const tags = React.createElement(node, null,
this.generateTagsHelper(0, children, genState));
return children;
};

return tags;
}
export default class TagCount extends React.Component {

render() {
const {useCache, count, depth, node, leaf} = this.props;
let key;
if(useCache) {
key = [count, depth, node, leaf].join('<=>');
if(TagCount.cache[key]) {
return TagCount.cache[key];
}
let {depth, count: tagCount} = this.props;

if(depth <= 0) {
return <div>{'This is a leaf'}</div>;
}

const tags = this.generateTags();
tagCount -= 1;

const childCount = numberOfChildren(depth, tagCount);
const childTagCount = Math.ceil(tagCount / childCount);
let tagsRemaining = tagCount;

const children = [];

for(let ii = 0; ii < childCount; ++ii) {
children.push(<TagCount
key={ii}
depth={depth - 1}
count={Math.min(tagsRemaining, childTagCount)}
/>);

if(useCache) {
TagCount.cache[key] = tags;
tagsRemaining -= childTagCount;
}

return tags;
return <div>{children}</div>;
}
};
}
4 changes: 2 additions & 2 deletions Node-DC-SSR-electrode/src/server/views/direct-mode.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { renderToString } from 'react-dom/server';
import { match, RouterContext } from 'react-router'
import { createStore } from 'redux';
import { Provider } from 'react-redux';
import injectTapEventPlugin from 'react-tap-event-plugin';
// import injectTapEventPlugin from 'react-tap-event-plugin';
import { ArgumentParser } from 'argparse';
import format from 'python-format';
import { routes } from '../../client/routes';
Expand Down Expand Up @@ -70,7 +70,7 @@ user-agent: ${args.userAgent}`);
} else if (redirectLocation) {
console.log('Error: 302');
} else if (renderProps) {
injectTapEventPlugin();
// injectTapEventPlugin();
global.navigator = global.navigator || {};
global.navigator.userAgent = args.userAgent;

Expand Down
4 changes: 2 additions & 2 deletions Node-DC-SSR-electrode/src/server/views/index-view.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import ReduxRouterEngine from 'electrode-redux-router-engine';
import {routes} from '../../client/routes';
import {createStore} from 'redux';
import rootReducer from '../../client/reducers';
import injectTapEventPlugin from 'react-tap-event-plugin';
// import injectTapEventPlugin from 'react-tap-event-plugin';
import {generateProduct} from '../data/generator';
import {config} from 'electrode-confippet';

Expand Down Expand Up @@ -42,7 +42,7 @@ module.exports = (req) => {

const app = req.server && req.server.app || req.app;
if (!app.routesEngine) {
injectTapEventPlugin();
// injectTapEventPlugin();
app.routesEngine = new ReduxRouterEngine({routes, createReduxStore});
}

Expand Down