Skip to content

Commit

Permalink
feat(ios): add KrollPromise to expose ES6 promises natively
Browse files Browse the repository at this point in the history
Fixes TIMOB-24549
  • Loading branch information
sgtcoolguy committed Dec 9, 2020
1 parent f804d8a commit 23541b3
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
8 changes: 8 additions & 0 deletions iphone/TitaniumKit/TitaniumKit.xcodeproj/project.pbxproj
Expand Up @@ -27,6 +27,8 @@
B106A6FE24535CB800B1305E /* JSValue+Addons.m in Sources */ = {isa = PBXBuildFile; fileRef = B106A6FC24535CB800B1305E /* JSValue+Addons.m */; };
B10E8D502408559300578E8F /* APSHTTPClient.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = B10E8D4F2408559300578E8F /* APSHTTPClient.xcframework */; };
B10E8D5824085E1E00578E8F /* APSAnalytics.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = B10E8D5724085E1E00578E8F /* APSAnalytics.xcframework */; };
B70ABB8424B79C9C0007D07A /* KrollPromise.h in Headers */ = {isa = PBXBuildFile; fileRef = B70ABB8324B79C9C0007D07A /* KrollPromise.h */; settings = {ATTRIBUTES = (Public, ); }; };
B70ABB8624B79DCF0007D07A /* KrollPromise.m in Sources */ = {isa = PBXBuildFile; fileRef = B70ABB8524B79DCF0007D07A /* KrollPromise.m */; };
B7624B6C216663C7000C365A /* ObjcProxy.h in Headers */ = {isa = PBXBuildFile; fileRef = B7624B6A216663C7000C365A /* ObjcProxy.h */; settings = {ATTRIBUTES = (Public, ); }; };
B7624B6D216663C7000C365A /* ObjcProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = B7624B6B216663C7000C365A /* ObjcProxy.m */; };
DB1596592075389000292B19 /* APIModule.m in Sources */ = {isa = PBXBuildFile; fileRef = DB1596572075389000292B19 /* APIModule.m */; };
Expand Down Expand Up @@ -185,6 +187,8 @@
B106A6FC24535CB800B1305E /* JSValue+Addons.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "JSValue+Addons.m"; sourceTree = "<group>"; };
B10E8D4F2408559300578E8F /* APSHTTPClient.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; name = APSHTTPClient.xcframework; path = TitaniumKit/Libraries/APSHTTPClient/APSHTTPClient.xcframework; sourceTree = "<group>"; };
B10E8D5724085E1E00578E8F /* APSAnalytics.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; name = APSAnalytics.xcframework; path = TitaniumKit/Libraries/APSAnalytics/APSAnalytics.xcframework; sourceTree = "<group>"; };
B70ABB8324B79C9C0007D07A /* KrollPromise.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KrollPromise.h; sourceTree = "<group>"; };
B70ABB8524B79DCF0007D07A /* KrollPromise.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KrollPromise.m; sourceTree = "<group>"; };
B7624B6A216663C7000C365A /* ObjcProxy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ObjcProxy.h; sourceTree = "<group>"; };
B7624B6B216663C7000C365A /* ObjcProxy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ObjcProxy.m; sourceTree = "<group>"; };
DB1596572075389000292B19 /* APIModule.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = APIModule.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -417,6 +421,8 @@
DB258CDA1F0975F7000D0D8D /* Kroll */ = {
isa = PBXGroup;
children = (
B70ABB8324B79C9C0007D07A /* KrollPromise.h */,
B70ABB8524B79DCF0007D07A /* KrollPromise.m */,
DB258D1B1F097680000D0D8D /* KrollBridge.h */,
DB258D1C1F097680000D0D8D /* KrollBridge.m */,
DB15FC231F0A833100A82C45 /* KrollCallback.h */,
Expand Down Expand Up @@ -666,6 +672,7 @@
DB15FD0C1F0A894000A82C45 /* TiWindowProxy.h in Headers */,
DB258DB81F09770C000D0D8D /* TiUtils.h in Headers */,
DB15FC351F0A833100A82C45 /* KrollContext.h in Headers */,
B70ABB8424B79C9C0007D07A /* KrollPromise.h in Headers */,
DB258D9D1F097680000D0D8D /* TiUIView.h in Headers */,
DB15FD2C1F0A93EF00A82C45 /* TiStreamProxy.h in Headers */,
DB15FD241F0A8A7B00A82C45 /* TiBindingEvent.h in Headers */,
Expand Down Expand Up @@ -819,6 +826,7 @@
DB15FC9D1F0A862D00A82C45 /* ListenerEntry.m in Sources */,
DB15FC3C1F0A833100A82C45 /* KrollMethodDelegate.m in Sources */,
DB15FD291F0A93A900A82C45 /* TiFilesystemFileStreamProxy.m in Sources */,
B70ABB8624B79DCF0007D07A /* KrollPromise.m in Sources */,
DB15FC5C1F0A84C000A82C45 /* TiHost.m in Sources */,
DB258D621F097680000D0D8D /* Bridge.m in Sources */,
DB15FD071F0A892500A82C45 /* Ti2DMatrix.m in Sources */,
Expand Down
30 changes: 30 additions & 0 deletions iphone/TitaniumKit/TitaniumKit/Sources/Kroll/KrollPromise.h
@@ -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 iphone/TitaniumKit/TitaniumKit/Sources/Kroll/KrollPromise.m
@@ -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.

Copy link
@hansemannn

hansemannn Mar 15, 2021

Collaborator

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

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

0 comments on commit 23541b3

Please sign in to comment.