added deps support

This commit is contained in:
Peter Schojer 2008-10-02 07:38:11 +00:00
parent a01e9c78b8
commit a0392bbd64
4 changed files with 123 additions and 6 deletions

View File

@ -65,6 +65,9 @@ public:
void renderBody(const Renderable* pRenderable, const RenderContext& context, std::ostream& ostr);
/// Emits code for the page body to the given output stream.
void renderVariables(const DynamicCodeLoader* pLoader, const RenderContext& context, std::ostream& ostr);
// You must first call renderVariables then renderHead
private:
static void onCodeGen(DynamicCodeLoader* &pLoader);

View File

@ -72,7 +72,7 @@ void DynamicCodeLoaderRenderer::renderHead(const Renderable* pRenderable, const
View::Ptr pView = pLoader->view();
Panel::Ptr pPanel = pView.cast<Panel>();
poco_assert_dbg (pLoader != 0);
str << "var " << pLoader->loaderFunctionName() << "Loaded = false;" << std::endl;
str << "function " << pLoader->loaderFunctionName() << "(){" << std::endl;
str << "if ("<< pLoader->loaderFunctionName() << "Loaded) return;" << std::endl;
str << pLoader->loaderFunctionName() << "Loaded = true;" << std::endl;
@ -87,6 +87,14 @@ void DynamicCodeLoaderRenderer::renderHead(const Renderable* pRenderable, const
str << "var parent = Ext.getCmp('" << pLoader->parent()->id() << "');" << std::endl;
str << "var child = " << pLoader->functionName() << "();" << std::endl;
str << "parent.add(child);" << std::endl;
// reduce the missing child cnt for all parents
const std::vector<const DynamicCodeLoader*>& parents = pLoader->dependentParents();
std::vector<const DynamicCodeLoader*>::const_iterator itP = parents.begin();
for (; itP != parents.end() ; ++itP)
{
if (*itP)
str << pLoader->loaderFunctionName() << "MissingChildCnt--;" << std::endl;
}
if (!pLoader->getSuccessCall().empty())
str << pLoader->getSuccessCall() << ";" << std::endl;
str << "}";
@ -94,6 +102,42 @@ void DynamicCodeLoaderRenderer::renderHead(const Renderable* pRenderable, const
str << ",failure:" << pLoader->getErrorCall() << std::endl;
str << "});" << std::endl;
str << "}" << std::endl;
const std::vector<const DynamicCodeLoader*>& deps = pLoader->dependencies();
if (!deps.empty())
{
// write the Check function
str << "function " << pLoader->loadAllFunctionName() << "Check(){" << std::endl;
str << "if (" << pLoader->loaderFunctionName() << "MissingChildCnt <= 0)" << std::endl;
str << str << pLoader->loaderFunctionName() << "();" << std::endl;
str << pLoader->loaderFunctionName() << "RetryCnt--;" << std::endl;
str << "if (" << pLoader->loaderFunctionName() << "RetryCnt < 0) {" << std::endl;
str << "Ext.Msg.alert ('Load Failed', 'Not all children could be loaded for the page');" << std::endl;
str << "window.clearInterval(" << pLoader->loaderFunctionName() << "PeriodicCheck);" << std::endl;
str << "}" << std::endl;
str << "}" << std::endl;
}
// write loadAll
str << "function " << pLoader->loadAllFunctionName() << "(){" << std::endl;
std::vector<const DynamicCodeLoader*>::const_iterator it = deps.begin();
str << "function " << pLoader->loadAllFunctionName() << "(){" << std::endl;
for (; it != deps.end(); ++it)
{
if (*it)
str << (*it)->loadAllFunctionName() << "();" << std::endl;
}
if (deps.empty())
str << pLoader->loaderFunctionName() << "();";
else
{
str << "if (!" << pLoader->loaderFunctionName() << "PeriodicCheck){" << std::endl;
str << pLoader->loaderFunctionName() << "PeriodicCheck = window.setInterval(" << std::endl;
str << pLoader->loaderFunctionName() << "Check, 1000);" << std::endl;
str << "}" << std::endl;
}
str << "}" << std::endl;
DynamicCodeLoader* pL = const_cast<DynamicCodeLoader*>(pLoader);
pL->generateCode += Poco::delegate(&DynamicCodeLoaderRenderer::onCodeGen);
@ -102,6 +146,18 @@ void DynamicCodeLoaderRenderer::renderHead(const Renderable* pRenderable, const
}
void DynamicCodeLoaderRenderer::renderVariables(const DynamicCodeLoader* pLoader, const RenderContext& context, std::ostream& str)
{
str << "var " << pLoader->loaderFunctionName() << "Loaded = false;" << std::endl;
if (!pLoader->dependencies().empty())
{
str << "var " << pLoader->loaderFunctionName() << "PeriodicCheck;" << std::endl;
str << "var " << pLoader->loaderFunctionName() << "RetryCnt = 30;" << std::endl;
str << "var " << pLoader->loaderFunctionName() << "MissingChildCnt = " << pLoader->dependencies().size() << ";" << std::endl;
}
}
void DynamicCodeLoaderRenderer::renderBody(const Renderable* pRenderable, const RenderContext& context, std::ostream& ostr)
{
}

View File

@ -45,6 +45,7 @@
#include "Poco/WebWidgets/View.h"
#include "Poco/URI.h"
#include "Poco/FIFOEvent.h"
#include <vector>
namespace Poco {
@ -77,7 +78,11 @@ public:
/// Returns the function name
const std::string& loaderFunctionName() const;
/// Returns the function name used to load the js
/// Returns the function name used to load the js. Loads only the parent,
/// none of the dependent Children
const std::string& loadAllFunctionName() const;
/// Returns the function name used to load the js. Loads all the dependent children
View::Ptr view() const;
/// Returns the view
@ -113,21 +118,38 @@ public:
/// The JS fucntion which should be called on success
const std::string& getErrorCall() const;
void addDependency(DynamicCodeLoader* pLoader);
/// Tells this loader that it should first invoke pLoader
/// before loading itself
const std::vector<const DynamicCodeLoader*>& dependentParents() const;
/// Gets all parents that depend on the success of the child
const std::vector<const DynamicCodeLoader*>& dependencies() const;
/// Gets all children that must be loaded before this code
protected:
~DynamicCodeLoader();
/// Destroys the DynamicCodeLoader.
void addDependentParent(const DynamicCodeLoader* pLoader);
/// Adds another DynamicCodeLoader that depends on the success of this loader
/// For example, if loader1 uses a panel from loader2,
/// you add loader1 to loader2:
/// loader2->addDependendParent(loader1);
private:
View* _pParent;
Poco::URI _uri;
std::string _fctName;
std::string _loaderFct;
std::string _loadAllFct;
View::Ptr _pView;
std::string _code;
std::string _success;
std::string _error;
std::string _success;
std::string _error;
std::vector<const DynamicCodeLoader*> _dependencies;
std::vector<const DynamicCodeLoader*> _dependentParents;
};
@ -167,6 +189,12 @@ inline const std::string& DynamicCodeLoader::loaderFunctionName() const
}
inline const std::string& DynamicCodeLoader::loadAllFunctionName() const
{
return _loadAllFct;
}
inline bool DynamicCodeLoader::operator < (const DynamicCodeLoader& other) const
{
return _fctName < other._fctName;
@ -202,6 +230,34 @@ inline const std::string& DynamicCodeLoader::getErrorCall() const
return _error;
}
inline void DynamicCodeLoader::addDependency(DynamicCodeLoader* pLoader)
{
if (pLoader)
{
pLoader->addDependentParent(this);
_dependencies.push_back(pLoader);
}
}
inline void DynamicCodeLoader::addDependentParent(const DynamicCodeLoader* pLoader)
{
if (pLoader) _dependentParents.push_back(pLoader);
}
inline const std::vector<const DynamicCodeLoader*>& DynamicCodeLoader::dependentParents() const
{
return _dependentParents;
}
inline const std::vector<const DynamicCodeLoader*>& DynamicCodeLoader::dependencies() const
{
return _dependencies;
}
} } // namespace Poco::WebWidgets

View File

@ -53,11 +53,13 @@ DynamicCodeLoader::DynamicCodeLoader(View* pParent, const Poco::URI& uri, const
_uri(uri),
_fctName(fctName),
_loaderFct("load"),
_loadAllFct("loadAll"),
_pView(pView)
{
poco_check_ptr (_pView);
poco_assert (!_fctName.empty());
_loaderFct.append(_fctName);
_loadAllFct.append(_fctName);
}