Skip to content

Commit

Permalink
feat(ios): have Ti.Database.DB.executeAsync return a Promise
Browse files Browse the repository at this point in the history
  • Loading branch information
sgtcoolguy committed Jan 26, 2021
1 parent 4b03ac6 commit fbbbe98
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
2 changes: 1 addition & 1 deletion iphone/Classes/TiDatabaseProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ READONLY_PROPERTY(NSUInteger, rowsAffected, RowsAffected);
- (void)close;
// This supports varargs, but we hack it in the impl to check currentArgs
- (TiDatabaseResultSetProxy *)execute:(NSString *)sql;
- (void)executeAsync:(NSString *)sql;
- (JSValue *)executeAsync:(NSString *)sql;
- (NSArray<TiDatabaseResultSetProxy *> *)executeAll:(NSArray<NSString *> *)queries;
JSExportAs(executeAllAsync,
-(void)executeAllAsync
Expand Down
58 changes: 40 additions & 18 deletions iphone/Classes/TiDatabaseProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

#import "TiDatabaseProxy.h"
#import "TiDatabaseResultSetProxy.h"
@import TitaniumKit.KrollPromise;
@import TitaniumKit.TiFilesystemFileProxy;
@import TitaniumKit.TiUtils;
@import TitaniumKit.JSValue_Addons;

@implementation TiDatabaseProxy

Expand Down Expand Up @@ -272,22 +274,34 @@ - (TiDatabaseResultSetProxy *)execute:(NSString *)sql
return result;
}

- (void)executeAsync:(NSString *)sql
- (JSValue *)executeAsync:(NSString *)sql
{
NSArray *currentArgs = [JSContext currentArguments];
if ([currentArgs count] < 2) {
[self throwException:@"callback function must be supplied" subreason:@"" location:CODELOCATION];
return;
}
JSValue *callback = [currentArgs objectAtIndex:[currentArgs count] - 1];

NSArray *currentArgs = JSContext.currentArguments;
NSArray *params = @[];
if ([currentArgs count] > 2) {
JSValue *possibleParams = [currentArgs objectAtIndex:1];
if ([possibleParams isArray]) {
params = [possibleParams toArray];
} else {
params = [self sqlParams:[currentArgs subarrayWithRange:NSMakeRange(1, [currentArgs count] - 2)]];
JSContext *context = [self currentContext];
KrollPromise *promise = [[[KrollPromise alloc] initInContext:context] autorelease];

// Callback is optional, so are the parameter args, so this gets a bit ugly!
JSValue *callback = nil;
if (currentArgs.count > 1) {
JSValue *possibleCallback = currentArgs[currentArgs.count - 1];
if ([possibleCallback isFunction]) {
callback = possibleCallback;
if (currentArgs.count > 2) {
JSValue *possibleParams = currentArgs[1];
if ([possibleParams isArray]) {
params = [possibleParams toArray];
} else {
params = [self sqlParams:[currentArgs subarrayWithRange:NSMakeRange(1, (currentArgs.count - 2))]];
}
}
} else { // last arg is not a callback, treat as param
JSValue *possibleParams = currentArgs[1];
if ([possibleParams isArray]) {
params = [possibleParams toArray];
} else {
params = [self sqlParams:[currentArgs subarrayWithRange:NSMakeRange(1, (currentArgs.count - 1))]];
}
}
}

Expand All @@ -297,17 +311,25 @@ - (void)executeAsync:(NSString *)sql
TiDatabaseResultSetProxy *proxy = [self executeSQL:sql withParams:params withError:&error];
if (error != nil) {
dispatch_async(dispatch_get_main_queue(), ^{
JSValue *jsError = [JSValue valueWithNewErrorFromMessage:[NSString stringWithFormat:@"failed to execute SQL statement: %@", [error description]] inContext:[callback context]];
[callback callWithArguments:@[ jsError ]];
NSString *message = [NSString stringWithFormat:@"failed to execute SQL statement: %@", error.description];
JSValue *jsError = [JSValue valueWithNewErrorFromMessage:message inContext:context];
if (callback != nil) {
[callback callWithArguments:@[ jsError ]];
}
[promise rejectWithErrorMessage:message];
});
return;
}

dispatch_async(dispatch_get_main_queue(), ^{
JSContext *context = [callback context];
[callback callWithArguments:@[ [JSValue valueWithUndefinedInContext:context], proxy == nil ? [JSValue valueWithNullInContext:context] : proxy ]];
id jsProxy = (proxy == nil) ? [JSValue valueWithNullInContext:context] : proxy;
if (callback != nil) {
[callback callWithArguments:@[ [JSValue valueWithUndefinedInContext:context], jsProxy ]];
}
[promise resolve:@[ jsProxy ]];
});
});
return promise.JSValue;
}

- (NSArray<TiDatabaseResultSetProxy *> *)executeAll:(NSArray<NSString *> *)queries withContext:(JSContext *)context withError:(NSError *__nullable *__nullable)error
Expand Down

0 comments on commit fbbbe98

Please sign in to comment.