Skip to content

Commit

Permalink
Provide constructor overloads as well
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperisager committed Sep 23, 2024
1 parent 7cd05b2 commit 904e699
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 15 deletions.
40 changes: 40 additions & 0 deletions android/src/main/java/to/holepunch/bare/kit/MessagingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
23 changes: 22 additions & 1 deletion apple/BareKit/BareKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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

Expand All @@ -103,6 +109,8 @@ typedef void (^BareRPCResponseHandler)(NSData *_Nullable data, NSError *_Nullabl

@property(nonatomic, assign, nonnull) id<BareNotificationServiceDelegate> 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
Expand All @@ -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

Expand Down
108 changes: 94 additions & 14 deletions apple/BareKit/BareKit.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit 904e699

Please sign in to comment.