Fix Scheduler API inconsistency

This commit is contained in:
pandamicro 2015-10-21 18:03:46 +08:00
parent 558ac3cfa0
commit 9eeda122e9
5 changed files with 70 additions and 24 deletions

View File

@ -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;
@ -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;

View File

@ -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;

View File

@ -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`");

View File

@ -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 () {
@ -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);

2
web

@ -1 +1 @@
Subproject commit 133441be8873e63685da04cb7c8f19ad082b9992
Subproject commit bcf78ece2a2743d0f1bfb37ffced23a0caba14a5