Added FileRequest abstraction on top of HTTPRequest to allow synchronous loading in CommonJS.

Reviewed by me.
This commit is contained in:
Francisco Ryan Tolmasky I committed 2010-01-27 07:50:37 -08:00
1 parent 2bc31de69d
commit 42712166e9
3 files changed
+52 -15

No files matched your search

+8 -8
View File
@@ -130,14 +130,15 @@ Bundle.prototype.load = function(/*BOOL*/ shouldEvaluate)
var self = this;
self._loadStatus = Bundle.Loading | Bundle.LoadingInfoPlist;
/* print(FILE.cwd());
print(self.path());
print(rootNode);
print("will resolve: " + FILE.dirname(self.path()));*/
rootNode.resolveSubPath(FILE.dirname(self.path()), StaticResourceNode.DirectoryType, function(aStaticResourceNode)
{
{//print("found: " + FILE.dirname(self.path()));
self._staticResourceNode = new StaticResourceNode(FILE.basename(self.path()), aStaticResourceNode, StaticResourceNode.DirectoryType, NO);
var request = new HTTPRequest();
request.onsuccess = function(/*Event*/ anEvent)
function onsuccess(/*Event*/ anEvent)
{
self._loadStatus &= ~Bundle.LoadingInfoPlist;
self._infoDictionary = anEvent.request.responsePropertyList();
@@ -156,7 +157,7 @@ Bundle.prototype.load = function(/*BOOL*/ shouldEvaluate)
loadExecutableAndResources(self);
}
request.onfailure = function()
function onfailure()
{
self._loadStatus = Bundle.Unloaded;
self._eventDispatcher.dispatchEvent(
@@ -166,8 +167,7 @@ Bundle.prototype.load = function(/*BOOL*/ shouldEvaluate)
});
}
request.open("GET", FILE.join(self.path(), "Info.plist"), YES);
request.send("");
new FileRequest(FILE.join(self.path(), "Info.plist"), onsuccess, onfailure);
});
}
+35
View File
@@ -167,3 +167,38 @@ function determineAndDispatchHTTPRequestEvents(/*HTTPRequest*/ aRequest)
eventDispatcher.dispatchEvent({ type: result, request: aRequest});
}
}
function FileRequest(/*String*/ aFilePath, onsuccess, onfailure)
{
#if BROWSER
var request = new HTTPRequest();
request.onsuccess = onsuccess;
request.onfailure = onfailure;
request.open("GET", aFilePath, YES);
request.send("");
#else
if (!require("file").exists(aFilePath))
return onfailure();
onsuccess({ type:"success", request:this });
#endif
}
#if COMMONJS
FileRequest.prototype.responseText = function()
{
return this._responseText;
}
FileRequest.prototype.responseXML = function()
{
return new DOMParser().parseFromString(anXMLString, "text/xml");
}
FileRequest.prototype.responsePropertyList = function()
{
return PropertyList.createFromString(responseText);
}
#endif
+9 -7
View File
@@ -20,7 +20,11 @@ var FILE =
cwd: function()
{
#if BROWSER
return FILE.dirname(window.location.pathname);
#else
return require("file").cwd();
#endif
},
normal: function(/*String*/ aPath)
@@ -179,26 +183,24 @@ StaticResourceNode.prototype.write = function(/*String*/ aString)
StaticResourceNode.prototype.resolveAsFile = function()
{console.log("NOW LOOKING FOR FILE!");
var self = this,
request = new HTTPRequest();
var self = this;
request.onsuccess = function()
function onsuccess(/*anEvent*/ anEvent)
{
self._type = StaticResourceNode.FileType;
self._contents = request.responseText();
self._contents = anEvent.request.responseText();
resolveStaticResourceNode(self, YES);
}
request.onfailure = function()
function onfailure()
{console.log("FIALE");
self._type = StaticResourceNode.NotFoundType;
resolveStaticResourceNode(self, YES);
}
request.open("GET", this.path(), true);
request.send("");
new FileRequest(this.path(), onsuccess, onfailure);
}
StaticResourceNode.prototype.resolveSubPath = function(/*String*/ aPath, /*Type*/ aType, /*Function*/ aCallback)