Skip to content

Commit

Permalink
Fix Scheduler API inconsistency
Browse files Browse the repository at this point in the history
  • Loading branch information
pandamicro committed Oct 21, 2015
1 parent 558ac3c commit 9eeda12
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 24 deletions.
53 changes: 33 additions & 20 deletions cocos/scripting/js-bindings/manual/cocos2d_specifics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2124,30 +2124,39 @@ bool js_CCScheduler_schedule(JSContext *cx, uint32_t argc, jsval *vp)
js_proxy_t *proxy = jsb_get_js_proxy(obj);
cocos2d::Scheduler *sched = (cocos2d::Scheduler *)(proxy ? proxy->ptr : NULL);

JS::RootedObject tmpObj(cx, args.get(1).toObjectOrNull());

std::function<void (float)> callback;
JS::RootedObject targetObj(cx);
do {
if(JS_TypeOfValue(cx, args.get(0)) == JSTYPE_FUNCTION)
JS::RootedValue callbackVal(cx);
if (JS_TypeOfValue(cx, args.get(0)) == JSTYPE_FUNCTION)
{
std::shared_ptr<JSFunctionWrapper> func(new JSFunctionWrapper(cx, tmpObj, args.get(0)));
auto lambda = [=](float larg0) -> void {
JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET
jsval largv[1];
largv[0] = DOUBLE_TO_JSVAL(larg0);
JS::RootedValue rval(cx);
bool invokeOk = func->invoke(1, &largv[0], &rval);
if (!invokeOk && JS_IsExceptionPending(cx)) {
JS_ReportPendingException(cx);
}
};
callback = lambda;
callbackVal.set(args.get(0));
targetObj.set(args.get(1).toObjectOrNull());
}
else if (JS_TypeOfValue(cx, args.get(1)) == JSTYPE_FUNCTION)
{
targetObj.set(args.get(0).toObjectOrNull());
callbackVal.set(args.get(1));
}
else
{
ok = false;
callback = nullptr;
break;
}

std::shared_ptr<JSFunctionWrapper> func(new JSFunctionWrapper(cx, targetObj, callbackVal));
auto lambda = [=](float larg0) -> void {
JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET
jsval largv[1];
largv[0] = DOUBLE_TO_JSVAL(larg0);
JS::RootedValue rval(cx);
bool invokeOk = func->invoke(1, &largv[0], &rval);
if (!invokeOk && JS_IsExceptionPending(cx)) {
JS_ReportPendingException(cx);
}
};
callback = lambda;
} while(0);

double interval = 0;
Expand All @@ -2171,21 +2180,25 @@ bool js_CCScheduler_schedule(JSContext *cx, uint32_t argc, jsval *vp)
ok &= JS::ToNumber(cx, JS::RootedValue(cx, args.get(4)), &delay );
}

//
// paused
//
bool paused = false;

if( argc >= 6 ) {
paused = JS::ToBoolean(JS::RootedValue(cx, args.get(5)));
}

//
// key
//
std::string key;

if( argc >= 7 ) {
ok &= jsval_to_std_string(cx, args.get(6), &key);
if ( argc >= 7 ) {
jsval_to_std_string(cx, args.get(6), &key);
}

JSB_PRECONDITION2(ok, cx, false, "Error processing arguments");

sched->schedule(callback, tmpObj, interval, repeat, delay, paused, key);
sched->schedule(callback, targetObj, interval, repeat, delay, paused, key);

args.rval().setUndefined();
return true;
Expand Down
34 changes: 33 additions & 1 deletion cocos/scripting/js-bindings/script/jsb_cocos2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -2655,7 +2655,39 @@ _p.setBoundingHeight = _p.setHeight;
//
_p = cc.Scheduler.prototype;
_p.unscheduleUpdateForTarget = _p.unscheduleUpdate;
_p.unscheduleAllCallbacksForTarget = _p.unscheduleAllForTarget;
_p.unscheduleAllCallbacksForTarget = function (target) {
this.unschedule(target.__instanceId + "", target);
};
_p._schedule = _p.schedule;
_p.schedule = function (callback, target, interval, repeat, delay, paused, key) {
var isSelector = false;
if(typeof callback !== "function"){
var selector = callback;
isSelector = true;
}
if(isSelector === false){
//callback, target, interval, repeat, delay, paused, key
//callback, target, interval, paused, key
if(arguments.length === 4 || arguments.length === 5) {
key = delay;
paused = repeat;
delay = 0;
repeat = cc.REPEAT_FOREVER;
}
}else{
//selector, target, interval, repeat, delay, paused
//selector, target, interval, paused
if(arguments.length === 4){
paused = repeat;
repeat = cc.REPEAT_FOREVER;
delay = 0;
}
}
if (key === undefined) {
key = target.__instanceId + "";
}
this._schedule(callback, target, interval, repeat, delay, paused, key);
}


cc._NodeGrid = cc.NodeGrid;
Expand Down
2 changes: 1 addition & 1 deletion cocos/scripting/js-bindings/script/jsb_prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ cc.Class.extend = function (prop) {

// The dummy class constructor
function Class() {
// All construction is actually done in the init method
if (!initializing) {
this.__instanceId = ClassManager.getNewInstanceId();
if (!this.ctor) {
if (this.__nativeObj)
cc.log("No ctor function found! Please check whether `classes_need_extend` section in `ini` file like which in `tools/tojs/cocos2dx.ini`");
Expand Down
3 changes: 2 additions & 1 deletion tests/js-tests/src/SchedulerTest/SchedulerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ var ScheduleUsingSchedulerTest = SchedulerTestLayer.extend({
var repeat = cc.REPEAT_FOREVER; // how many repeats. cc.REPEAT_FOREVER means forever
var delay = 2; // start after 2 seconds;
paused = false; // not paused. queue it now.
scheduler.scheduleCallbackForTarget(this, this.onSchedUpdate, interval, repeat, delay, paused);
scheduler.schedule(this.onSchedUpdate, this, interval, repeat, delay, paused);
//----end9----
},
title:function () {
Expand All @@ -522,6 +522,7 @@ var ScheduleUsingSchedulerTest = SchedulerTestLayer.extend({
this._accum += dt;
if( this._accum > 3 ) {
var scheduler = director.getScheduler();
scheduler.unscheduleUpdate(this);
scheduler.unscheduleAllCallbacksForTarget(this);
}
cc.log("onSchedUpdate accum: " + this._accum);
Expand Down
2 changes: 1 addition & 1 deletion web

0 comments on commit 9eeda12

Please sign in to comment.