diff --git a/android/src/main/java/to/holepunch/bare/kit/MessagingService.java b/android/src/main/java/to/holepunch/bare/kit/MessagingService.java index b5f104d..7bd8f4f 100644 --- a/android/src/main/java/to/holepunch/bare/kit/MessagingService.java +++ b/android/src/main/java/to/holepunch/bare/kit/MessagingService.java @@ -27,10 +27,50 @@ protected MessagingService(ReplyCallback callback) { this.callback = callback; } + protected MessagingService(String filename, ByteBuffer source, ReplyCallback callback) { + this(callback); + start(filename, source); + } + + protected MessagingService(String filename, String source, Charset charset, ReplyCallback callback) { + this(callback); + start(filename, source, charset); + } + + protected MessagingService(String filename, String source, String charset, ReplyCallback callback) { + this(callback); + start(filename, source, charset); + } + + protected MessagingService(String filename, InputStream source, ReplyCallback callback) throws IOException { + this(callback); + start(filename, source); + } + protected MessagingService() { this(null); } + protected MessagingService(String filename, ByteBuffer source) { + this(); + start(filename, source); + } + + protected MessagingService(String filename, String source, Charset charset) { + this(); + start(filename, source, charset); + } + + protected MessagingService(String filename, String source, String charset) { + this(); + start(filename, source, charset); + } + + protected MessagingService(String filename, InputStream source) throws IOException { + this(); + start(filename, source); + } + protected void start (String filename, ByteBuffer source) { this.worklet.start(filename, source); diff --git a/apple/BareKit/BareKit.h b/apple/BareKit/BareKit.h index 4b07ddd..57d0916 100644 --- a/apple/BareKit/BareKit.h +++ b/apple/BareKit/BareKit.h @@ -19,6 +19,13 @@ - (void)start:(NSString *_Nonnull)filename source:(NSString *_Nonnull)source encoding:(NSStringEncoding)encoding; +- (void)start:(NSString *_Nonnull)name + ofType:(NSString *_Nonnull)type + inBundle:(NSBundle *_Nonnull)bundle; +- (void)start:(NSString *_Nonnull)name + ofType:(NSString *_Nonnull)type + inDirectory:(NSString *_Nonnull)subpath + inBundle:(NSBundle *_Nonnull)bundle; - (void)suspend; - (void)suspendWithLinger:(int)linger; - (void)resume; @@ -83,7 +90,6 @@ @end typedef void (^BareRPCRequestHandler)(BareRPCIncomingRequest *_Nullable request, NSError *_Nullable error); -typedef void (^BareRPCResponseHandler)(NSData *_Nullable data, NSError *_Nullable error); @interface BareRPC : NSObject @@ -103,6 +109,8 @@ typedef void (^BareRPCResponseHandler)(NSData *_Nullable data, NSError *_Nullabl @property(nonatomic, assign, nonnull) id delegate; +- (_Nullable instancetype)init; +- (_Nullable instancetype)initWithFilename:(NSString *_Nonnull)filename; - (_Nullable instancetype)initWithFilename:(NSString *_Nonnull)filename source:(NSData *_Nonnull)source; - (_Nullable instancetype)initWithFilename:(NSString *_Nonnull)filename @@ -115,6 +123,19 @@ typedef void (^BareRPCResponseHandler)(NSData *_Nullable data, NSError *_Nullabl ofType:(NSString *_Nonnull)type inDirectory:(NSString *_Nonnull)subpath inBundle:(NSBundle *_Nonnull)bundle; +- (void)start:(NSString *_Nonnull)filename; +- (void)start:(NSString *_Nonnull)filename + source:(NSData *_Nonnull)source; +- (void)start:(NSString *_Nonnull)filename + source:(NSString *_Nonnull)source + encoding:(NSStringEncoding)encoding; +- (void)start:(NSString *_Nonnull)name + ofType:(NSString *_Nonnull)type + inBundle:(NSBundle *_Nonnull)bundle; +- (void)start:(NSString *_Nonnull)name + ofType:(NSString *_Nonnull)type + inDirectory:(NSString *_Nonnull)subpath + inBundle:(NSBundle *_Nonnull)bundle; @end diff --git a/apple/BareKit/BareKit.m b/apple/BareKit/BareKit.m index 710a268..85e28f2 100644 --- a/apple/BareKit/BareKit.m +++ b/apple/BareKit/BareKit.m @@ -116,7 +116,8 @@ - (void)start:(NSString *_Nonnull)filename { _outgoing = _worklet.outgoing; } -- (void)start:(NSString *_Nonnull)filename source:(NSData *_Nonnull)source { +- (void)start:(NSString *_Nonnull)filename + source:(NSData *_Nonnull)source { int err; const char *_filename = [filename cStringUsingEncoding:NSUTF8StringEncoding]; @@ -130,10 +131,29 @@ - (void)start:(NSString *_Nonnull)filename source:(NSData *_Nonnull)source { _outgoing = _worklet.outgoing; } -- (void)start:(NSString *_Nonnull)filename source:(NSString *_Nonnull)source encoding:(NSStringEncoding)encoding { +- (void)start:(NSString *_Nonnull)filename + source:(NSString *_Nonnull)source + encoding:(NSStringEncoding)encoding { [self start:filename source:[source dataUsingEncoding:encoding]]; } +- (void)start:(NSString *_Nonnull)name + ofType:(NSString *_Nonnull)type + inBundle:(NSBundle *_Nonnull)bundle { + NSString *path = [bundle pathForResource:name ofType:type]; + + [self start:path source:[NSData dataWithContentsOfFile:path]]; +} + +- (void)start:(NSString *_Nonnull)name + ofType:(NSString *_Nonnull)type + inDirectory:(NSString *_Nonnull)subpath + inBundle:(NSBundle *_Nonnull)bundle { + NSString *path = [bundle pathForResource:name ofType:type inDirectory:subpath]; + + [self start:path source:[NSData dataWithContentsOfFile:path]]; +} + - (void)suspend { int err; err = bare_worklet_suspend(&_worklet, 0); @@ -311,7 +331,8 @@ @implementation BareRPCIncomingRequest { BareRPC *_rpc; } -- (_Nullable instancetype)initWithRPC:(BareRPC *_Nonnull)rpc request:(rpc_message_t *)request { +- (_Nullable instancetype)initWithRPC:(BareRPC *_Nonnull)rpc + request:(rpc_message_t *)request { self = [super init]; if (self) { @@ -339,6 +360,8 @@ - (void)reply:(NSString *_Nonnull)data @end +typedef void (^BareRPCResponseHandler)(NSData *_Nullable data, NSError *_Nullable error); + @implementation BareRPCOutgoingRequest { BareRPC *_rpc; BareRPCResponseHandler _responseHandler; @@ -558,8 +581,7 @@ @implementation BareNotificationService { BareWorklet *_worklet; } -- (_Nullable instancetype)initWithFilename:(NSString *_Nonnull)filename - source:(NSData *_Nonnull)source { +- (_Nullable instancetype)init { self = [super init]; if (self) { @@ -568,8 +590,27 @@ - (_Nullable instancetype)initWithFilename:(NSString *_Nonnull)filename [BareWorklet optimizeForMemory:YES]; _worklet = [[BareWorklet alloc] init]; + } + + return self; +} + +- (_Nullable instancetype)initWithFilename:(NSString *_Nonnull)filename { + self = [self init]; + + if (self) { + [self start:filename]; + } + + return self; +} + +- (_Nullable instancetype)initWithFilename:(NSString *_Nonnull)filename + source:(NSData *_Nonnull)source { + self = [self init]; - [_worklet start:filename source:source]; + if (self) { + [self start:filename source:source]; } return self; @@ -578,27 +619,66 @@ - (_Nullable instancetype)initWithFilename:(NSString *_Nonnull)filename - (_Nullable instancetype)initWithFilename:(NSString *_Nonnull)filename source:(NSString *_Nonnull)source encoding:(NSStringEncoding)encoding { - return [self initWithFilename:filename - source:[source dataUsingEncoding:encoding]]; + self = [self init]; + + if (self) { + [self start:filename source:source encoding:encoding]; + } + + return self; } - (_Nullable instancetype)initWithResource:(NSString *_Nonnull)name ofType:(NSString *_Nonnull)type inBundle:(NSBundle *_Nonnull)bundle { - NSString *path = [bundle pathForResource:name ofType:type]; + self = [self init]; - return [self initWithFilename:path - source:[NSData dataWithContentsOfFile:path]]; + if (self) { + [self start:name ofType:type inBundle:bundle]; + } + + return self; } - (_Nullable instancetype)initWithResource:(NSString *_Nonnull)name ofType:(NSString *_Nonnull)type inDirectory:(NSString *_Nonnull)subpath inBundle:(NSBundle *_Nonnull)bundle { - NSString *path = [bundle pathForResource:name ofType:type inDirectory:subpath]; + self = [self init]; + + if (self) { + [self start:name ofType:type inDirectory:subpath inBundle:bundle]; + } + + return self; +} + +- (void)start:(NSString *_Nonnull)filename { + [_worklet start:filename]; +} + +- (void)start:(NSString *_Nonnull)filename + source:(NSData *_Nonnull)source { + [_worklet start:filename source:source]; +} + +- (void)start:(NSString *_Nonnull)filename + source:(NSString *_Nonnull)source + encoding:(NSStringEncoding)encoding { + [_worklet start:filename source:source encoding:encoding]; +} + +- (void)start:(NSString *_Nonnull)name + ofType:(NSString *_Nonnull)type + inBundle:(NSBundle *_Nonnull)bundle { + [_worklet start:name ofType:type inBundle:bundle]; +} - return [self initWithFilename:path - source:[NSData dataWithContentsOfFile:path]]; +- (void)start:(NSString *_Nonnull)name + ofType:(NSString *_Nonnull)type + inDirectory:(NSString *_Nonnull)subpath + inBundle:(NSBundle *_Nonnull)bundle { + [_worklet start:name ofType:type inDirectory:subpath inBundle:bundle]; } - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request