Skip to content

Commit

Permalink
refactor(ios): optimize promise creation on iOS 12
Browse files Browse the repository at this point in the history
Fixes TIMOB-28412
  • Loading branch information
jquick-axway authored and ewanharris committed Jun 17, 2021
1 parent 9d0c668 commit a07c56c
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions iphone/TitaniumKit/TitaniumKit/Sources/Kroll/KrollPromise.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,28 @@ - (KrollPromise *)initInContext:(JSContext *)context
resolveFunc = [[JSValue valueWithJSValueRef:resolve inContext:context] retain];
rejectFunc = [[JSValue valueWithJSValueRef:reject inContext:context] retain];
} else {
// Alternative code for earlier versions of iOS. We hack it by evaluating JS
// TODO: I assume this is pretty slow. Can we re-use eval'd values here?
JSValue *executor = [context evaluateScript:@"function executor(resolve, reject) { executor.resolve = resolve; executor.reject = reject; }\nexecutor;"];
JSValue *exception = context.exception;
if (exception != nil) {
[TiExceptionHandler.defaultExceptionHandler reportScriptError:exception inJSContext:context];
// For older iOS versions, create promise via below JS since there is no native API.
const NSString *JS_FUNCTION_NAME = @"_createKrollPromiseHandler";
JSValue *createPromiseHandler = context[JS_FUNCTION_NAME];
if (createPromiseHandler.isUndefined) {
[context evaluateScript:@"function _createKrollPromiseHandler() {"
" const handler = {};"
" handler.promise = new Promise((resolve, reject) => {"
" handler.resolve = resolve;"
" handler.reject = reject;"
" });"
" return handler;"
"}"];
createPromiseHandler = context[JS_FUNCTION_NAME];
}
JSValue *createPromise = [context evaluateScript:@"function createPromise(executor) { return new Promise(executor); }\ncreatePromise;"];
exception = context.exception;
JSValue *exception = context.exception;
if (exception != nil) {
[TiExceptionHandler.defaultExceptionHandler reportScriptError:exception inJSContext:context];
}
_JSValue = [[createPromise callWithArguments:@[ executor ]] retain];
resolveFunc = [executor[@"resolve"] retain];
rejectFunc = [executor[@"reject"] retain];
JSValue *handler = [createPromiseHandler callWithArguments:nil];
_JSValue = [handler[@"promise"] retain];
resolveFunc = [handler[@"resolve"] retain];
rejectFunc = [handler[@"reject"] retain];
}
}
return self;
Expand Down

0 comments on commit a07c56c

Please sign in to comment.