Skip to content

Commit

Permalink
Getting through better loading structure
Browse files Browse the repository at this point in the history
  • Loading branch information
ghemingway committed Dec 11, 2015
1 parent 8aa6afb commit 05ada37
Show file tree
Hide file tree
Showing 21 changed files with 263 additions and 405 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,4 +1,5 @@
node_modules/*
/public/js/*
.idea/*
.DS_Store
data/*
11 changes: 8 additions & 3 deletions package.json
Expand Up @@ -19,7 +19,6 @@
"connect-redis": "latest",
"cookie-parser": "latest",
"errorhandler": "latest",
"eventemitter2": "latest",
"express": "latest",
"express-session": "latest",
"jade": "latest",
Expand All @@ -29,10 +28,13 @@
"method-override": "latest",
"morgan": "latest",
"react": "latest",
"react-dom": "^0.14.2",
"react-dom": "latest",
"redis": "latest",
"serve-favicon": "latest",
"serve-static": "latest",
"socket.io": "^1.3.7",
"socket.io-client": "^1.3.7",
"socket.io-express-session": "^0.1.3",
"three": "latest",
"winston": "latest"
},
Expand All @@ -45,11 +47,14 @@
"babel-preset-react": "latest",
"bootstrap-webpack": "latest",
"css-loader": "latest",
"exports-loader": "^0.6.2",
"extract-text-webpack-plugin": "latest",
"file-loader": "latest",
"imports-loader": "^0.6.5",
"less": "^2.5.3",
"less-loader": "^2.2.2",
"mocha": "latest",
"node-sass": "latest",
"sass": "latest",
"sass-loader": "latest",
"style-loader": "latest",
"url-loader": "latest",
Expand Down
57 changes: 17 additions & 40 deletions src/client/main.js
Expand Up @@ -3,9 +3,11 @@


// Necessary modules
var Session = require('./models/session'),
Router = require('./routes'),
CADjs = require('./views/cad');
require('./stylesheets/base.scss');
require('bootstrap-webpack');
var io = require('socket.io-client'),
Router = require('./routes');
import CADManager from './models/cad_manager';

/*************************************************************************/

Expand All @@ -14,53 +16,28 @@ var App = function() {
var self = this;
// Get the supported services
var $body = $('body');
this._services = $body.data("services");
this._config = $body.data("config");
this._services = $body.data('services');
this._config = $body.data('config');
// Establish the global URL router
this._router = new Router({ app: this });
this.__defineGetter__('router', function() {
return this._router;
});
// Establish the primary view object
this._cad = new CADjs({
viewContainerId: 'cadjs-view'
});

/*************************************************************************/

// Establish the HTTP session
this._session = new Session({});
this.__defineGetter__('session', function () {
return this._session;
// Create data manager
this.cadManager = new CADManager();
// Establish socket connection
this.socket = io();
// Connect to the socket server
this.socket.on('connect', function() {
console.log('Socket client connected');
});

// Begin routing the application
var startRouting = function() {
Backbone.history.start({pushState: true});
Backbone.history.start({ pushState: true });
self._router.navigate(window.location.pathname, {trigger: true});
};

this.on('setModel', function(req) {
var basePath = this._services.api_endpoint + this._services.model + '/';
self._cad.load(req.path, basePath);
});

// Handle the session management
this.on('setSession', function (session) {
self.user = _.pick(session, 'username');
console.log('Session for user: ' + self.user.username);
}, this);
if (this._config.auth) {
this._session.fetch().then(
function (session) {
self.trigger('setSession', session);
},
function () {
self.trigger('logout');
}
).always(startRouting);
} else startRouting();

/*************************************************************************/
startRouting();
};

_.extend(App.prototype, Backbone.Events);
Expand Down
9 changes: 3 additions & 6 deletions src/client/models/annotation.js
@@ -1,15 +1,12 @@
/* G. Hemingway Copyright @2015
* Annotation object
*/

/* Copyright G. Hemingway, 2015 - All rights reserved */
"use strict";

var THREE = require('three');
//var THREE = require('three');


/********************************* Annotation Class ********************************/

module.exports = class Annotation extends THREE.EventDispatcher {
export default class Annotation extends THREE.EventDispatcher {
constructor(id, assembly) {
super();
var ret = assembly.makeChild(id, this);
Expand Down
4 changes: 1 addition & 3 deletions src/client/models/assembly.js
@@ -1,15 +1,13 @@
/* G. Hemingway Copyright @2014
* Context for the overall CAD assembly
*/

"use strict";

//var THREE = require('three');

/*************************************************************************/


module.exports = class Assembly extends THREE.EventDispatcher {
export default class Assembly extends THREE.EventDispatcher {
constructor(rootID, defaultColor, loader) {
super();
this._rootID = rootID;
Expand Down
60 changes: 25 additions & 35 deletions src/client/views/cad/index.js → src/client/models/cad_manager.js
Expand Up @@ -4,56 +4,47 @@
"use strict";


var _ = require('lodash'),
DataLoader = require('./data_loader'),
React = require('react'),
ReactDOM = require('react-dom'),
CADView = React.createFactory(require('./viewer'));
var _ = require('lodash');
import DataLoader from './data_loader';

/*************************************************************************/

class CADjs extends THREE.EventDispatcher {
constructor(config) {
export default class CADManager extends THREE.EventDispatcher {
constructor() {
super();
var self = this;
this._viewContainerId = config.viewContainerId;
// TODO: Set this to default empty assembly
this._models = {};
this.addEventListener('setModel', this.load);
// Set this to default empty assembly
this._root3DObject = new THREE.Object3D();
this.addEventListener("cadViewer::mounted", function() {
//console.log('I see the CADView got mounted');
// Once view is in place, bind events
self.bindEvents();
});
this._viewer = CADView({
app: this,
viewContainerId: this._viewContainerId
});
this._loader = new DataLoader(this._viewer, {
// Setup data loader
this._loader = new DataLoader({
autorun: false,
workerPath: "/js/webworker.js"
});
ReactDOM.render(this._viewer, document.getElementById('cadjs-view'));
}

load(reqPath, basePath) {
// Load a new assembly request
load(req) {
var self = this;
req.type = req.modelType;
delete req.modelType;
// Initialize the assembly
this._loader.load(reqPath, basePath, "assembly", function(err, part) {
this._loader.load(req, function(err, model) {
if (err) {
console.log('CAD.index Load error: ' + path);
console.log('CADManager.load error: ' + err);
} else {
// Add the part to the list
self._rootAssembly = part;
// calculate the scene's radius for draw distance calculations
self._viewer.updateSceneBoundingBox(part.getBoundingBox());
// center the view
self._viewer.zoomToFit(part);
// Update the tree
self.renderTree();
// Get the rest of the files
self._loader.runLoadQueue();
// Add the model to the list of loaded models
self._models[req.path] = model;
//// calculate the scene's radius for draw distance calculations
//self._viewer.updateSceneBoundingBox(part.getBoundingBox());
//// center the view
//self._viewer.zoomToFit(part);
//// Update the tree
//self.renderTree();
}
});
// Get the rest of the files
this._loader.runLoadQueue();
}

bindEvents() {
Expand Down Expand Up @@ -377,4 +368,3 @@ CADjs.prototype.renderTree = function() {
});
};
*/
module.exports = CADjs;
@@ -1,33 +1,30 @@
/* G. Hemingway Copyright @2015
* Data loader - Specialized for each type of data (e.g. JSON, TYSON, etc.)
*/

"use strict";


var Assembly = require('../../models/assembly'),
Product = require('../../models/product'),
Shape = require('../../models/shape'),
Shell = require('../../models/shell'),
Annotation = require('../../models/annotation');
import Assembly from './assembly';
import Product from './product';
import Shape from './shape';
import Shell from './shell';
import Annotation from './annotation';

/********************************* Helper Functions ********************************/

module.exports = class DataLoader extends THREE.EventDispatcher {
constructor(viewer, config) {
export default class DataLoader extends THREE.EventDispatcher {
constructor(config) {
super();
this._viewer = viewer;
this._queue = []; // The queue of requests to load
this._loading = []; // List of active loading jobs
this._maxWorkers = config.maxWorkers ? config.maxWorkers : 4;
this._freeWorkers = [];
this._shells = {};

var self = this;
this._workers = []; // List of workers
while (this._workers.length < this._maxWorkers) {
var worker = new Worker(config.workerPath);
worker.addEventListener("message", function (event) {
worker.addEventListener('message', function (event) {
self.workerMessage(event);
});
this._freeWorkers.push(this._workers.length);
Expand Down Expand Up @@ -109,60 +106,19 @@ module.exports = class DataLoader extends THREE.EventDispatcher {

/************** DataLoader Class Functions ****************************/

load(reqPath, basePath, validateType, callback) {
var loadErrorCheck = function (error, assembly) {
if (!error) {
callback(undefined, assembly);
}
else {
callback(error);
}
};
var req = {
url: reqPath,
validateType: validateType,
callback: loadErrorCheck
};
// Need to try to get index.json then index.xml then pop error message
var self = this;
this.resolveUrl(req, basePath, function(err, req) {
if (err) {
console.log('DataLoader.Load error: ' + JSON.stringify(err));
} else {
self.addRequest(req);
self.runLoadQueue();
}
load(req, callback) {
this.addRequest(req, function(model) {
console.log('DataLoader.load callback');
console.log(model);
callback(model);
});
}

resolveUrl(req, basePath, callback) {
if (!req.base) {
req.base = basePath;
}
var parts = req.url.split('/');
// Direct request
if (parts.length === 1) {
req.url = req.base + req.url;
callback(undefined, req);
// Need to get actual ModelId
} else {
$.ajax({
url: req.base + 'resolve/' + req.url,
type: 'GET'
}).done(function(data) {
req.url = req.base + data.modelId;
callback(undefined, req);
}).fail(function(err) {
callback(err);
});
}
}

addRequest(req) {
addRequest(req, callback) {
req.callback = callback;
// Push onto the queue and send out a message
this._queue.push(req);
var parts = req.url.split("/");
this.dispatchEvent({type: "addRequest", file: parts[parts.length - 1]});
this.dispatchEvent({type: 'addRequest', path: req.path });
}

sortQueue() {
Expand Down Expand Up @@ -247,13 +203,14 @@ module.exports = class DataLoader extends THREE.EventDispatcher {
}

initRequest(req) {
console.log('InitRequest: ' + req.path);
// Fetch the worker to use
var worker = this._workers[req.workerID];
// Send the request to the worker
var data = {
url: req.url,
url: req.baseURL + '/' + req.path,
workerID: req.workerID,
type: req.validateType
type: req.type
};
if (data.type === "shell") data.shellSize = req.shellSize;
worker.postMessage(data);
Expand Down
4 changes: 1 addition & 3 deletions src/client/models/product.js
@@ -1,14 +1,12 @@
/* G. Hemingway Copyright @2014
* Product class for the CAD models
*/

"use strict";


/********************************* Product Class ********************************/


module.exports = class Product extends THREE.EventDispatcher {
export default class Product extends THREE.EventDispatcher {
constructor(id, assembly, name, stepFile, isRoot) {
super();
console.log('Product: ' + id);
Expand Down

0 comments on commit 05ada37

Please sign in to comment.