-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ios): add KrollPromise to expose ES6 promises natively
Fixes TIMOB-24549
- Loading branch information
1 parent
f804d8a
commit 23541b3
Showing
3 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
iphone/TitaniumKit/TitaniumKit/Sources/Kroll/KrollPromise.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Appcelerator Titanium Mobile | ||
* Copyright (c) 2020-Present by Appcelerator, Inc. All Rights Reserved. | ||
* Licensed under the terms of the Apache Public License | ||
* Please see the LICENSE included with this distribution for details. | ||
*/ | ||
#ifndef KrollPromise_h | ||
#define KrollPromise_h | ||
|
||
#import <JavaScriptCore/JavaScriptCore.h> | ||
|
||
@interface KrollPromise : NSObject { | ||
@private | ||
JSValue *resolveFunc; | ||
JSValue *rejectFunc; | ||
} | ||
|
||
@property (readonly) JSValue *JSValue; | ||
|
||
- (void)resolve:(NSArray *)arguments; | ||
- (void)reject:(NSArray *)arguments; | ||
|
||
- (KrollPromise *)initInContext:(JSContext *)context; | ||
|
||
+ (JSValue *)resolved:(NSArray *)arguments inContext:(JSContext *)context; | ||
+ (JSValue *)rejected:(NSArray *)arguments inContext:(JSContext *)context; | ||
|
||
@end | ||
|
||
#endif /* KrollPromise_h */ |
73 changes: 73 additions & 0 deletions
73
iphone/TitaniumKit/TitaniumKit/Sources/Kroll/KrollPromise.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#import "KrollPromise.h" | ||
#import "TiExceptionHandler.h" | ||
|
||
@implementation KrollPromise | ||
|
||
- (KrollPromise *)initInContext:(JSContext *)context | ||
{ | ||
if (self = [super init]) { | ||
if (@available(iOS 13, *)) { | ||
// Use iOS 13 APIs. | ||
JSObjectRef resolve; | ||
JSObjectRef reject; | ||
JSValueRef exception = NULL; | ||
_JSValue = [JSValue valueWithJSValueRef:JSObjectMakeDeferredPromise(context.JSGlobalContextRef, &resolve, &reject, &exception) inContext:context]; | ||
if (exception) { | ||
// report exception | ||
[TiExceptionHandler.defaultExceptionHandler reportScriptError:[JSValue valueWithJSValueRef:exception inContext:context] inJSContext: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; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
if (exception != nil) { | ||
[TiExceptionHandler.defaultExceptionHandler reportScriptError:exception inJSContext:context]; | ||
} | ||
JSValue *createPromise = [context evaluateScript:@"function createPromise(executor) { return new Promise(executor); }\ncreatePromise;"]; | ||
exception = context.exception; | ||
if (exception != nil) { | ||
[TiExceptionHandler.defaultExceptionHandler reportScriptError:exception inJSContext:context]; | ||
} | ||
_JSValue = [createPromise callWithArguments:@[ executor ]]; | ||
resolveFunc = [executor[@"resolve"] retain]; | ||
rejectFunc = [executor[@"reject"] retain]; | ||
} | ||
} | ||
return self; | ||
} | ||
|
||
+ (JSValue *)resolved:(NSArray *)arguments inContext:(JSContext *)context | ||
{ | ||
KrollPromise *promise = [[[KrollPromise alloc] initInContext:context] autorelease]; | ||
[promise resolve:arguments]; | ||
return promise.JSValue; | ||
} | ||
|
||
+ (JSValue *)rejected:(NSArray *)arguments inContext:(JSContext *)context | ||
{ | ||
KrollPromise *promise = [[[KrollPromise alloc] initInContext:context] autorelease]; | ||
[promise reject:arguments]; | ||
return promise.JSValue; | ||
} | ||
|
||
- (void)resolve:(NSArray *)arguments | ||
{ | ||
[resolveFunc callWithArguments:arguments]; | ||
} | ||
|
||
- (void)reject:(NSArray *)arguments | ||
{ | ||
[rejectFunc callWithArguments:arguments]; | ||
} | ||
|
||
- (void)dealloc | ||
{ | ||
[resolveFunc release]; | ||
[rejectFunc release]; | ||
[super dealloc]; | ||
} | ||
|
||
@end |
@sgtcoolguy @vijaysingh-axway Has this ever been validated / reviewed? We need to opt out of this..it forces all code around it to wait for this sync-main-thread code. This is horrible.