Skip to content

Commit

Permalink
refact: Simplify and make Scene more flexible
Browse files Browse the repository at this point in the history
The `FamousEngine` is supposed to expose an API for adding and removing
scenes. While the corresponding methods have been outlined, the Scene
class itself doesn't reflect the ability to re-mount the scene to a
different selector.

* The Scene constructor accepts an updater and a selector
* Scene#_globalUpdater is inconsistent with the inherited,
  Node-specific (theoretically) Node#_updater
* The Scene updater is being set as via the constructor (instead of
  Node#_setUpdater)
* The Scene constructor requests the context size

* The scene constructor no longer accepts any arguments
* The scene itself is not bound to a specific selector, but instead
  can be mounted to an arbitrary path (just like nodes)
* Scene constructor logic has been moved to Scene#mount
* Less redundancy

* More flexibility: Scene can request size at a later point in time
  instead of upon instantiation
* Scene constructor does not have side effects
* Scene updater (Scene#_globalUpdater) can be changed via Dispatch

* => FamousEngine#removeScene and FamousEngine#addScene can be
     implemented
  • Loading branch information
alexanderGugel committed Jul 16, 2015
1 parent 970b4e2 commit 20a6744
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 33 deletions.
7 changes: 5 additions & 2 deletions core/FamousEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,11 @@ FamousEngine.prototype.createScene = function createScene (selector) {
selector = selector || 'body';

if (this._scenes[selector]) this._scenes[selector].dismount();
this._scenes[selector] = new Scene(selector, this);
return this._scenes[selector];

var scene = new Scene();
this._scenes[selector] = scene;
scene.mount(selector);
return scene;
};

/**
Expand Down
43 changes: 12 additions & 31 deletions core/Scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,9 @@ var SizeSystem = require('./SizeSystem');
* @class Scene
* @constructor
* @extends Node
*
* @param {String} selector a string which is a dom selector
* signifying which dom element the context
* should be set upon
* @param {Famous} updater a class which conforms to Famous' interface
* it needs to be able to send methods to
* the renderers and update nodes in the scene graph
*/
function Scene (selector, updater) {
if (!selector) throw new Error('Scene needs to be created with a DOM selector');
if (!updater) throw new Error('Scene needs to be created with a class like Famous');

Node.call(this); // Scene inherits from node

this._globalUpdater = updater; // The updater that will both
// send messages to the renderers
// and update dirty nodes

this._selector = selector; // reference to the DOM selector
// that represents the element
// in the dom that this context
// inhabits

this.mount(selector); // Mount the context to itself
// (it is its own parent)

this.show(); // the context begins shown (it's already present in the dom)
function Scene () {
Node.call(this);
}

// Scene inherits from node
Expand All @@ -79,7 +55,8 @@ Scene.NO_DEFAULT_COMPONENTS = true;
* @return {String} dom selector
*/
Scene.prototype.getSelector = function getSelector () {
return this._selector;
console.warn('Scene#getSelector is deprecated, use Scene#getLocation or Scene#getId instead');
return this._id;
};

/**
Expand Down Expand Up @@ -121,7 +98,7 @@ Scene.prototype.onReceive = function onReceive (event, payload) {
payload[1],
payload[2] ? payload[2] : 0);

this._updater.message(Commands.WITH).message(this._selector).message(Commands.READY);
this._updater.message(Commands.WITH).message(this._id).message(Commands.READY);
}
};

Expand All @@ -130,16 +107,20 @@ Scene.prototype.mount = function mount (path) {
if (this.isMounted())
throw new Error('Scene is already mounted at: ' + this.getLocation());

this._globalUpdater // message a request for the dom
Dispatch.mount(path, this);

this._updater // message a request for the dom
.message(Commands.NEED_SIZE_FOR) // size of the context so that
.message(this._selector); // the scene graph has a total size
.message(path); // the scene graph has a total size

Dispatch.mount(path, this);
this._id = path;
this._mounted = true;
this._parent = this;
TransformSystem.registerTransformAtPath(path);
SizeSystem.registerSizeAtPath(path);

// the context begins shown (it's already present in the dom)
this.show();
};

module.exports = Scene;

0 comments on commit 20a6744

Please sign in to comment.