Skip to content

Commit 32c280e

Browse files
author
OJay Robinson
committed
progress on DefaultPlugin and DefaultLoader, update of extend and promote
1 parent 275e1d8 commit 32c280e

File tree

5 files changed

+223
-21
lines changed

5 files changed

+223
-21
lines changed

src/createjs/utils/extend.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* core
2+
* extend
33
* Visit http://createjs.com/ for documentation, updates and examples.
44
*
55
* Copyright (c) 2010 gskinner.com, inc.
@@ -44,7 +44,7 @@ this.createjs = this.createjs||{};
4444
* This should be called right after creating the class constructor.
4545
*
4646
* function MySubClass() {}
47-
* createjs.extend(MySubClass, MySuperClass); // returns the prototype
47+
* createjs.extend(MySubClass, MySuperClass);
4848
* ClassB.prototype.doSomething = function() { }
4949
*
5050
* var foo = new MySubClass();

src/createjs/utils/promote.js

+14-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* core
2+
* promote
33
* Visit http://createjs.com/ for documentation, updates and examples.
44
*
55
* Copyright (c) 2010 gskinner.com, inc.
@@ -39,13 +39,14 @@ this.createjs = this.createjs||{};
3939
*/
4040

4141
/**
42-
* Promotes any methods on the super class that were overridden, by creating an alias in the format `SuperClassName_methodName`.
43-
* An alias to the super class's constructor is always added in the format `SuperClassName_constructor`.
42+
* Promotes any methods on the super class that were overridden, by creating an alias in the format `prefix_methodName`.
43+
* It is recommended to use the super class's name as the prefix.
44+
* An alias to the super class's constructor is always added in the format `prefix_constructor`.
4445
* This allows the subclass to call super class methods without using `function.call`, providing better performance.
4546
*
46-
* For example, if `MySubClass` extends `MySuperClass`, and both define a `draw` method, then calling `promote(MySubClass)`
47-
* would: add a `MySuperClass_constructor` method to MySubClass and promote the `draw` method on MySuperClass to the
48-
* prototype of MySubClass as `MySuperClass_draw`.
47+
* For example, if `MySubClass` extends `MySuperClass`, and both define a `draw` method, then calling `promote(MySubClass, "MySuperClass")`
48+
* would add a `MySuperClass_constructor` method to MySubClass and promote the `draw` method on `MySuperClass` to the
49+
* prototype of `MySubClass` as `MySuperClass_draw`.
4950
*
5051
* This should be called after the class's prototype is fully defined.
5152
*
@@ -64,24 +65,22 @@ this.createjs = this.createjs||{};
6465
* ClassB.prototype.greet = function() {
6566
* return this.ClassA_greet()+this.punctuation;
6667
* }
67-
* createjs.promote(ClassB);
68+
* createjs.promote(ClassB, "ClassA");
6869
*
6970
* var foo = new ClassB("World", "!?!");
7071
* console.log(foo.greet()); // Hello World!?!
7172
*
72-
* @method extends
73-
* @param {Function} subclass The subclass to promote super class methods on.
74-
* @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()`).
73+
* @method promote
74+
* @param {Function} subclass The class to promote super class methods on.
75+
* @param {String} prefix The prefix to add to the promoted method names. Usually the name of the superclass.
7576
* @return {Function} Returns the subclass.
7677
*/
77-
createjs.promote = function(subclass, superclassName) {
78+
createjs.promote = function(subclass, prefix) {
7879
var subP = subclass.prototype, supP = (Object.getPrototypeOf&&Object.getPrototypeOf(subP))||subP.__proto__;
7980
if (supP) {
80-
superclassName = superclassName || supP.constructor.name || /^function\s+([^\s\(]+)\s*\(/.exec(String(supP.constructor))[1];
81-
82-
subP[superclassName + "_constructor"] = supP.constructor; // constructor is not always innumerable
81+
subP[(prefix+="_") + "constructor"] = supP.constructor; // constructor is not always innumerable
8382
for (var n in supP) {
84-
if (subP[n] && (typeof supP[n] == "function")) { subP[superclassName + "_" + n] = supP[n]; }
83+
if (subP.hasOwnProperty(n) && (typeof supP[n] == "function")) { subP[prefix + n] = supP[n]; }
8584
}
8685
}
8786
return subclass;

src/soundjs/DefaultLoader.js

+202
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
/*
2+
* DefaultLoader
3+
* Visit http://createjs.com/ for documentation, updates and examples.
4+
*
5+
*
6+
* Copyright (c) 2012 gskinner.com, inc.
7+
*
8+
* Permission is hereby granted, free of charge, to any person
9+
* obtaining a copy of this software and associated documentation
10+
* files (the "Software"), to deal in the Software without
11+
* restriction, including without limitation the rights to use,
12+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the
14+
* Software is furnished to do so, subject to the following
15+
* conditions:
16+
*
17+
* The above copyright notice and this permission notice shall be
18+
* included in all copies or substantial portions of the Software.
19+
*
20+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27+
* OTHER DEALINGS IN THE SOFTWARE.
28+
*/
29+
30+
// namespace:
31+
this.createjs = this.createjs || {};
32+
33+
(function () {
34+
"use strict";
35+
36+
/**
37+
* An internal helper class that preloads audio. Note that this class and its methods are not documented
38+
* properly to avoid generating HTML documentation.
39+
* #class DefaultLoader
40+
* @param {String} src The source of the sound to load.
41+
* @param {Object} owner A reference to the class that created this instance.
42+
* @constructor
43+
*/
44+
function DefaultLoader(src) {
45+
this.EventDispatcher_constructor();
46+
this._init(src);
47+
}
48+
var p = createjs.extend(DefaultLoader, createjs.EventDispatcher);
49+
50+
/**
51+
* If load results in an object, it is stored as result
52+
* #property result
53+
* @type {Object}
54+
*/
55+
p.result = null;
56+
57+
/**
58+
* Indicates the percentage of loading progress, between 0 and 1.
59+
* #property progress
60+
* @type {number}
61+
*/
62+
p.progress = -1;
63+
64+
/**
65+
* The source of the sound to load. Used by callback functions when we return this class.
66+
* #property src
67+
* @type {String}
68+
*/
69+
p.src = null;
70+
71+
/**
72+
* The result of the loading operation
73+
* #property _result
74+
* @type {Object}
75+
* @protected
76+
*/
77+
p._result = null;
78+
79+
// Calbacks
80+
/**
81+
* The callback that fires when the load completes.
82+
* #property onload
83+
* #event load
84+
* @type {Method}
85+
*/
86+
p.onload = null;
87+
88+
/**
89+
* The callback that fires when data is being fetched at a rate that would allow playback without interruption. This follows HTML tag naming.
90+
* #property oncanplaythrough
91+
* #event canplaythrough
92+
* @type {Method}
93+
*/
94+
p.oncanplaythrough = null;
95+
96+
/**
97+
* The callback that fires as the load progresses. This follows HTML tag naming.
98+
* #property onprogress
99+
* #event progress
100+
* @type {Method}
101+
*/
102+
p.onprogress = null;
103+
104+
/**
105+
* The callback that fires if the load hits an error. This follows HTML tag naming.
106+
* #property onerror
107+
* #event error
108+
* @type {Method}
109+
* @protected
110+
*/
111+
p.onerror = null;
112+
113+
// constructor
114+
p._init = function (src) {
115+
this.src = src;
116+
};
117+
118+
/**
119+
* Begin loading the content.
120+
* #method load
121+
* @param {String} src The path to the sound.
122+
*/
123+
p.load = function (src) {
124+
if (src != null) {this.src = src;}
125+
// plugin specific code
126+
};
127+
128+
/**
129+
* The DefaultLoader has reported progress.
130+
*
131+
* <strong>Note</strong>: this is not a public API, but is used to allow preloaders to subscribe to load
132+
* progress as if this is an HTML audio tag. This reason is why this still uses a callback instead of an event.
133+
* #method handleProgress
134+
* @param {event} event Progress event that gives event.loaded and event.total if server is configured correctly
135+
* @protected
136+
*/
137+
p.handleProgress = function (event) {
138+
this.progress = event.loaded / event.total;
139+
var e = new createjs.Event("progress");
140+
e.loaded = event.loaded;
141+
e.total = event.total;
142+
e.progress = this.progress;
143+
144+
this.dispatchEvent(e);
145+
this.onprogress && this.onprogress(e);
146+
};
147+
148+
/**
149+
* The sound has completed loading.
150+
* #method handleLoad
151+
* @protected
152+
*/
153+
p.handleLoad = function (event) {
154+
// TODO consider params of event
155+
this.progress = 1;
156+
var e = new createjs.Event("load");
157+
e.target = this;
158+
this.dispatchEvent(e)
159+
this.onload && this.onload(e);
160+
};
161+
162+
/**
163+
* The sound has loading enough to play through without interruption at the current download rate.
164+
* #method handleLoad
165+
* @protected
166+
*/
167+
p.handleCanPlayThrough = function (event) {
168+
// TODO double check params of canplaythrough event
169+
var e = new createjs.Event("canplaythrough");
170+
e.target = this;
171+
this.dispatchEvent(e)
172+
this.oncanplaythrough && this.oncanplaythrough(e)
173+
}
174+
175+
/**
176+
* Errors have been caused by the DefaultLoader.
177+
* #method handleError
178+
* @protected
179+
*/
180+
p.handleError = function (event) {
181+
this.dispatchEvent (event);
182+
this.onerror && this.onerror(event);
183+
};
184+
185+
/**
186+
* Remove all external references from loader
187+
* #method cleanUp
188+
*/
189+
p.cleanUp = function () {
190+
this.src = null;
191+
this.onload = null;
192+
this.onprogress = null;
193+
this.onerror = null;
194+
this.removeAllEventListeners();
195+
};
196+
197+
p.toString = function () {
198+
return "[DefaultLoader]";
199+
};
200+
201+
createjs.DefaultLoader = createjs.promote(DefaultLoader, "EventDispatcher");
202+
}());

src/soundjs/DefaultPlugin.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ this.createjs = this.createjs || {};
161161
* @protected
162162
*/
163163
p._handlePreloadComplete = function (loader) {
164-
createjs.Sound._sendFileLoadEvent(loader.src);
164+
createjs.Sound._sendFileLoadEvent(loader.src); // OJR is this worth changing to events?
165165
loader.cleanUp();
166166
};
167167

src/soundjs/DefaultSoundInstance.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@ this.createjs = this.createjs || {};
3434
"use strict";
3535

3636
var DefaultSoundInstance = function () {
37+
this.EventDispatcher_constructor();
3738
this.init();
3839
};
39-
var p = DefaultSoundInstance.prototype;
40+
var p = createjs.extend(DefaultSoundInstance, createjs.EventDispatcher);
4041

4142
p.init = function () {
4243

4344
};
4445

45-
createjs.DefaultSoundInstance = DefaultSoundInstance;
46-
46+
47+
createjs.DefaultSoundInstance = createjs.promote(DefaultSoundInstance, "EventDispatcher");
4748
}());

0 commit comments

Comments
 (0)