Commit b9cba6385a3e8c9e1550b4519e5afc5043652077
- Diff rendering mode:
- inline
- side by side
core/scripting/propertyscriptextension.cpp
(12 / 4)
|   | |||
| 37 | 37 | m_NextListenerID(1) | |
| 38 | 38 | { } // ctor | |
| 39 | 39 | ||
| 40 | PropertyScriptExtension::~PropertyScriptExtension() { | ||
| 41 | scrubVector(m_Listeners); | ||
| 42 | } // dtor | ||
| 43 | |||
| 40 | 44 | JSBool global_prop_setProperty(JSContext* cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { | |
| 41 | 45 | if(argc < 2) { | |
| 42 | 46 | Logger::getInstance()->log("JS: global_prop_setProperty: need two arguments: property-path & value", lsError); | |
| … | … | ||
| 251 | 251 | m_Listeners.push_back(_pListener); | |
| 252 | 252 | } // addListener | |
| 253 | 253 | ||
| 254 | void PropertyScriptExtension::removeListener(const std::string& _identifier) { | ||
| 254 | void PropertyScriptExtension::removeListener(const std::string& _identifier, bool _destroy) { | ||
| 255 | 255 | for(std::vector<PropertyScriptListener*>::iterator it = m_Listeners.begin(), e = m_Listeners.end(); | |
| 256 | 256 | it != e; ++it) { | |
| 257 | 257 | if((*it)->getIdentifier() == _identifier) { | |
| 258 | (*it)->unsubscribe(); | ||
| 258 | PropertyScriptListener* pListener = *it; | ||
| 259 | pListener->unsubscribe(); | ||
| 259 | 260 | m_Listeners.erase(it); | |
| 261 | if(_destroy) { | ||
| 262 | delete pListener; | ||
| 263 | } | ||
| 260 | 264 | return; | |
| 261 | 265 | } | |
| 262 | 266 | } | |
| … | … | ||
| 284 | 284 | } // ctor | |
| 285 | 285 | ||
| 286 | 286 | PropertyScriptListener::~PropertyScriptListener() { | |
| 287 | m_pExtension->removeListener(m_Identifier); | ||
| 288 | } // dtor | ||
| 287 | m_pExtension->removeListener(m_Identifier, false); | ||
| 288 | } | ||
| 289 | 289 | ||
| 290 | 290 | void PropertyScriptListener::createScriptObject() { | |
| 291 | 291 | if(m_pScriptObject == NULL) { |
core/scripting/propertyscriptextension.h
(3 / 3)
|   | |||
| 48 | 48 | PropertyScriptExtension* m_pExtension; | |
| 49 | 49 | JSObject* m_pFunctionObject; | |
| 50 | 50 | jsval m_Function; | |
| 51 | std::string m_Identifier; | ||
| 51 | const std::string m_Identifier; | ||
| 52 | 52 | boost::scoped_ptr<ScriptObject> m_pScriptObject; | |
| 53 | 53 | ScriptFunctionRooter m_FunctionRoot; | |
| 54 | 54 | }; // PropertyScriptListener | |
| … | … | ||
| 56 | 56 | class PropertyScriptExtension : public ScriptExtension { | |
| 57 | 57 | public: | |
| 58 | 58 | PropertyScriptExtension(PropertySystem& _propertySystem); | |
| 59 | virtual ~PropertyScriptExtension() {} | ||
| 59 | virtual ~PropertyScriptExtension(); | ||
| 60 | 60 | ||
| 61 | 61 | virtual void extendContext(ScriptContext& _context); | |
| 62 | 62 | ||
| … | … | ||
| 65 | 65 | JSObject* createJSProperty(ScriptContext& _ctx, boost::shared_ptr<PropertyNode> _node); | |
| 66 | 66 | std::string produceListenerID(); | |
| 67 | 67 | void addListener(PropertyScriptListener* _pListener); | |
| 68 | void removeListener(const std::string& _identifier); | ||
| 68 | void removeListener(const std::string& _identifier, bool _destroy = true); | ||
| 69 | 69 | private: | |
| 70 | 70 | PropertySystem& m_PropertySystem; | |
| 71 | 71 | std::vector<PropertyScriptListener*> m_Listeners; |
tests/modeljstests.cpp
(3 / 5)
|   | |||
| 362 | 362 | ||
| 363 | 363 | boost::scoped_ptr<ScriptContext> ctx(env->getContext()); | |
| 364 | 364 | ctx->evaluate<void>("setProperty('/testing', 1); setProperty('/triggered', false); " | |
| 365 | "listener_ident = setListener('/testing', function(changedNode) { setProperty('/triggered', true); }); " | ||
| 365 | "var listener_ident = setListener('/testing', function(changedNode) { setProperty('/triggered', true); }); " | ||
| 366 | 366 | ); | |
| 367 | 367 | ||
| 368 | 368 | BOOST_CHECK_EQUAL(propSys.getBoolValue("/triggered"), false); | |
| … | … | ||
| 414 | 414 | ||
| 415 | 415 | boost::scoped_ptr<ScriptContext> ctx(env->getContext()); | |
| 416 | 416 | ctx->evaluate<void>("setProperty('/testing', 1); setProperty('/triggered', false); " | |
| 417 | "other_ident = setListener('/triggered', function() { setProperty('/itWorks', true); } ); " | ||
| 418 | "listener_ident = setListener('/testing', function(changedNode) { setProperty('/triggered', true); }); " | ||
| 417 | "var other_ident = setListener('/triggered', function() { setProperty('/itWorks', true); } ); " | ||
| 418 | "var listener_ident = setListener('/testing', function(changedNode) { setProperty('/triggered', true); }); " | ||
| 419 | 419 | ); | |
| 420 | 420 | ||
| 421 | 421 | propSys.setBoolValue("/testing", true); | |
| 422 | 422 | ||
| 423 | 423 | BOOST_CHECK_EQUAL(propSys.getBoolValue("/itWorks"), true); | |
| 424 | 424 | ||
| 425 | // TODO: find out why it crashes w/o those lines | ||
| 426 | 425 | ctx->evaluate<void>("removeListener(other_ident); " | |
| 427 | 426 | "removeListener(listener_ident); " | |
| 428 | 427 | ); | |
| … | … | ||
| 494 | 494 | sleepMS(100); | |
| 495 | 495 | } | |
| 496 | 496 | ||
| 497 | // TODO: find out why it crashes w/o those lines | ||
| 498 | 497 | ctx->evaluate<void>("removeListener(l1); removeListener(l2);"); | |
| 499 | 498 | } // testThreading | |
| 500 | 499 |

