Skip to content

Commit

Permalink
progress on DefaultPlugin and DefaultLoader, update of extend and pro…
Browse files Browse the repository at this point in the history
…mote
  • Loading branch information
OJay Robinson committed Nov 3, 2014
1 parent 275e1d8 commit 32c280e
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/createjs/utils/extend.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* core
* extend
* Visit http://createjs.com/ for documentation, updates and examples.
*
* Copyright (c) 2010 gskinner.com, inc.
Expand Down Expand Up @@ -44,7 +44,7 @@ this.createjs = this.createjs||{};
* This should be called right after creating the class constructor.
*
* function MySubClass() {}
* createjs.extend(MySubClass, MySuperClass); // returns the prototype
* createjs.extend(MySubClass, MySuperClass);
* ClassB.prototype.doSomething = function() { }
*
* var foo = new MySubClass();
Expand Down
29 changes: 14 additions & 15 deletions src/createjs/utils/promote.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* core
* promote
* Visit http://createjs.com/ for documentation, updates and examples.
*
* Copyright (c) 2010 gskinner.com, inc.
Expand Down Expand Up @@ -39,13 +39,14 @@ this.createjs = this.createjs||{};
*/

/**
* Promotes any methods on the super class that were overridden, by creating an alias in the format `SuperClassName_methodName`.
* An alias to the super class's constructor is always added in the format `SuperClassName_constructor`.
* Promotes any methods on the super class that were overridden, by creating an alias in the format `prefix_methodName`.
* It is recommended to use the super class's name as the prefix.
* An alias to the super class's constructor is always added in the format `prefix_constructor`.
* This allows the subclass to call super class methods without using `function.call`, providing better performance.
*
* For example, if `MySubClass` extends `MySuperClass`, and both define a `draw` method, then calling `promote(MySubClass)`
* would: add a `MySuperClass_constructor` method to MySubClass and promote the `draw` method on MySuperClass to the
* prototype of MySubClass as `MySuperClass_draw`.
* For example, if `MySubClass` extends `MySuperClass`, and both define a `draw` method, then calling `promote(MySubClass, "MySuperClass")`
* would add a `MySuperClass_constructor` method to MySubClass and promote the `draw` method on `MySuperClass` to the
* prototype of `MySubClass` as `MySuperClass_draw`.
*
* This should be called after the class's prototype is fully defined.
*
Expand All @@ -64,24 +65,22 @@ this.createjs = this.createjs||{};
* ClassB.prototype.greet = function() {
* return this.ClassA_greet()+this.punctuation;
* }
* createjs.promote(ClassB);
* createjs.promote(ClassB, "ClassA");
*
* var foo = new ClassB("World", "!?!");
* console.log(foo.greet()); // Hello World!?!
*
* @method extends
* @param {Function} subclass The subclass to promote super class methods on.
* @param {String} [superclassName] The name of the superclass. This is only necessary if the constructor is an anonymous function (`MyClass = function()` instead of `function MyClass()`).
* @method promote
* @param {Function} subclass The class to promote super class methods on.
* @param {String} prefix The prefix to add to the promoted method names. Usually the name of the superclass.
* @return {Function} Returns the subclass.
*/
createjs.promote = function(subclass, superclassName) {
createjs.promote = function(subclass, prefix) {
var subP = subclass.prototype, supP = (Object.getPrototypeOf&&Object.getPrototypeOf(subP))||subP.__proto__;
if (supP) {
superclassName = superclassName || supP.constructor.name || /^function\s+([^\s\(]+)\s*\(/.exec(String(supP.constructor))[1];

subP[superclassName + "_constructor"] = supP.constructor; // constructor is not always innumerable
subP[(prefix+="_") + "constructor"] = supP.constructor; // constructor is not always innumerable
for (var n in supP) {
if (subP[n] && (typeof supP[n] == "function")) { subP[superclassName + "_" + n] = supP[n]; }
if (subP.hasOwnProperty(n) && (typeof supP[n] == "function")) { subP[prefix + n] = supP[n]; }
}
}
return subclass;
Expand Down
202 changes: 202 additions & 0 deletions src/soundjs/DefaultLoader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
/*
* DefaultLoader
* Visit http://createjs.com/ for documentation, updates and examples.
*
*
* Copyright (c) 2012 gskinner.com, inc.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

// namespace:
this.createjs = this.createjs || {};

(function () {
"use strict";

/**
* An internal helper class that preloads audio. Note that this class and its methods are not documented
* properly to avoid generating HTML documentation.
* #class DefaultLoader
* @param {String} src The source of the sound to load.
* @param {Object} owner A reference to the class that created this instance.
* @constructor
*/
function DefaultLoader(src) {
this.EventDispatcher_constructor();
this._init(src);
}
var p = createjs.extend(DefaultLoader, createjs.EventDispatcher);

/**
* If load results in an object, it is stored as result
* #property result
* @type {Object}
*/
p.result = null;

/**
* Indicates the percentage of loading progress, between 0 and 1.
* #property progress
* @type {number}
*/
p.progress = -1;

/**
* The source of the sound to load. Used by callback functions when we return this class.
* #property src
* @type {String}
*/
p.src = null;

/**
* The result of the loading operation
* #property _result
* @type {Object}
* @protected
*/
p._result = null;

// Calbacks
/**
* The callback that fires when the load completes.
* #property onload
* #event load
* @type {Method}
*/
p.onload = null;

/**
* The callback that fires when data is being fetched at a rate that would allow playback without interruption. This follows HTML tag naming.
* #property oncanplaythrough
* #event canplaythrough
* @type {Method}
*/
p.oncanplaythrough = null;

/**
* The callback that fires as the load progresses. This follows HTML tag naming.
* #property onprogress
* #event progress
* @type {Method}
*/
p.onprogress = null;

/**
* The callback that fires if the load hits an error. This follows HTML tag naming.
* #property onerror
* #event error
* @type {Method}
* @protected
*/
p.onerror = null;

// constructor
p._init = function (src) {
this.src = src;
};

/**
* Begin loading the content.
* #method load
* @param {String} src The path to the sound.
*/
p.load = function (src) {
if (src != null) {this.src = src;}
// plugin specific code
};

/**
* The DefaultLoader has reported progress.
*
* <strong>Note</strong>: this is not a public API, but is used to allow preloaders to subscribe to load
* progress as if this is an HTML audio tag. This reason is why this still uses a callback instead of an event.
* #method handleProgress
* @param {event} event Progress event that gives event.loaded and event.total if server is configured correctly
* @protected
*/
p.handleProgress = function (event) {
this.progress = event.loaded / event.total;
var e = new createjs.Event("progress");
e.loaded = event.loaded;
e.total = event.total;
e.progress = this.progress;

this.dispatchEvent(e);
this.onprogress && this.onprogress(e);
};

/**
* The sound has completed loading.
* #method handleLoad
* @protected
*/
p.handleLoad = function (event) {
// TODO consider params of event
this.progress = 1;
var e = new createjs.Event("load");
e.target = this;
this.dispatchEvent(e)
this.onload && this.onload(e);
};

/**
* The sound has loading enough to play through without interruption at the current download rate.
* #method handleLoad
* @protected
*/
p.handleCanPlayThrough = function (event) {
// TODO double check params of canplaythrough event
var e = new createjs.Event("canplaythrough");
e.target = this;
this.dispatchEvent(e)
this.oncanplaythrough && this.oncanplaythrough(e)
}

/**
* Errors have been caused by the DefaultLoader.
* #method handleError
* @protected
*/
p.handleError = function (event) {
this.dispatchEvent (event);
this.onerror && this.onerror(event);
};

/**
* Remove all external references from loader
* #method cleanUp
*/
p.cleanUp = function () {
this.src = null;
this.onload = null;
this.onprogress = null;
this.onerror = null;
this.removeAllEventListeners();
};

p.toString = function () {
return "[DefaultLoader]";
};

createjs.DefaultLoader = createjs.promote(DefaultLoader, "EventDispatcher");
}());
2 changes: 1 addition & 1 deletion src/soundjs/DefaultPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ this.createjs = this.createjs || {};
* @protected
*/
p._handlePreloadComplete = function (loader) {
createjs.Sound._sendFileLoadEvent(loader.src);
createjs.Sound._sendFileLoadEvent(loader.src); // OJR is this worth changing to events?
loader.cleanUp();
};

Expand Down
7 changes: 4 additions & 3 deletions src/soundjs/DefaultSoundInstance.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ this.createjs = this.createjs || {};
"use strict";

var DefaultSoundInstance = function () {
this.EventDispatcher_constructor();
this.init();
};
var p = DefaultSoundInstance.prototype;
var p = createjs.extend(DefaultSoundInstance, createjs.EventDispatcher);

p.init = function () {

};

createjs.DefaultSoundInstance = DefaultSoundInstance;


createjs.DefaultSoundInstance = createjs.promote(DefaultSoundInstance, "EventDispatcher");
}());

0 comments on commit 32c280e

Please sign in to comment.