MacOS react 返回数组任务的本机 macos 函数

MacOS react native macos function that returns an array tasks

提问人:Paul 提问时间:10/6/2023 最后编辑:PiepantsPaul 更新时间:10/6/2023 访问量:24

问:

用:

import { getTasks } from 'react-native-childprocess';

...

      getTasks()
      .then(tasks => console.log(taks))
      .catch(console.error)

react-native-child进程

export async function getTasks() {
    const tasks = await Childprocess.getTasks();
    return tasks;
}

Childprocess.h

#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>

@interface Childprocess : RCTEventEmitter <RCTBridgeModule>

@property (nonatomic, assign) int ID_INC;
@property (nonatomic, strong) NSMutableDictionary *tasks;

@end

儿童进程.m

#import "Childprocess.h"
#import <React/RCTUtils.h>

@implementation Childprocess

- (instancetype)init {
    if (self = [super init]) {
        _tasks = [NSMutableDictionary dictionary];
    }
    return self;
}

+ (BOOL)requiresMainQueueSetup {
    return YES;
}

RCT_EXPORT_MODULE()

RCT_REMAP_METHOD(spawn,
        spawnWithCmd:
        (nonnull NSString*)cmd
    withArguments:(nonnull NSArray*)arguments
    withOptions:(nonnull NSDictionary*)options
    withResolver:(RCTPromiseResolveBlock)resolve
    withRejecter:(RCTPromiseRejectBlock)reject
) {
    NSNumber *cmdID = [self executeCommand:cmd arguments:arguments options:options];
    if ([cmdID intValue] < 0) {
        reject(@"failed", @"execute command failed", RCTErrorWithMessage(@"execute command failed"));
    } else {
        resolve(cmdID);
    }
}

RCT_REMAP_METHOD(kill,
        killWithCmdID:
        (nonnull NSNumber*)cmdID
    withResolver:(RCTPromiseResolveBlock)resolve
    withRejecter:(RCTPromiseRejectBlock)reject
) {
    NSTask *task = self.tasks[cmdID];
    if (task == nil) {
        reject(@"invalid cmdID", @"invalid cmdID", RCTErrorWithMessage(@"invalid cmdID"));
    } else {
        if (task.running) {
            [task terminate];
        }
        [self.tasks removeObjectForKey:cmdID];
        resolve(cmdID);
    }
}

- (NSArray<NSString *> *)supportedEvents {
    return @[@"stdout", @"stderr", @"terminate"];
}

- (void)processDataFromHandle:(NSFileHandle *)handle withCmdID:(NSString *)cmdID andType:(NSString *)type {
    NSData *data = [handle availableData];
    if (data.length > 0) {
        NSString *output = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
        NSLog(@"cmd[%@][%@]> %@", cmdID, type, output);
        [self sendEventWithName:type body:@{@"event": type, @"id": cmdID, @"output": output}];
    }
}

- (NSNumber *)executeCommand:(NSString *)cmd arguments:(NSArray *)arguments options:(NSDictionary *)options {
    NSNumber *cmdID = @(++self.ID_INC);

    NSTask *task = [[NSTask alloc] init];
    [task setExecutableURL:[NSURL fileURLWithPath:cmd]];
    [task setArguments:arguments];

    NSString *pwd = options[@"pwd"];
    if(pwd != nil){
        [task setCurrentDirectoryURL:[NSURL fileURLWithPath:pwd]];
    }

    NSPipe *stdoutPipe = [NSPipe pipe];
    NSPipe *stderrPipe = [NSPipe pipe];
    [task setStandardOutput:stdoutPipe];
    [task setStandardError:stderrPipe];
    NSFileHandle *stdoutHandle = [stdoutPipe fileHandleForReading];
    NSFileHandle *stderrHandle = [stderrPipe fileHandleForReading];

    __weak typeof(self) weakSelf = self;
    stdoutHandle.readabilityHandler = ^(NSFileHandle *handle) {
        NSString *type = @"stdout";
        [self processDataFromHandle:handle withCmdID:cmdID andType:type];
    };

    stderrHandle.readabilityHandler = ^(NSFileHandle *handle) {
        NSString *type = @"stderr";
        [self processDataFromHandle:handle withCmdID:cmdID andType:type];
    };

    task.terminationHandler = ^(NSTask *task) {
        NSString *type = @"terminate";
        NSLog(@"cmd[%@][%@]", cmdID, type);
        [weakSelf sendEventWithName:type body:@{@"event": type}];
    };

    self.tasks[cmdID] = task;

    NSError *error;
    [task launchAndReturnError:&error];

    return error == nil ? cmdID : @(-1);
}

@end

我写的函数,但它不能正常工作,我希望它返回过去传递的命令。

RCT_REMAP_METHOD(getTasks,
                 withResolver:(RCTPromiseResolveBlock)resolve
                 withRejecter:(RCTPromiseRejectBlock)reject
){
  //if (tasks) {
    resolve(@"events", @"There were tasks");
 /*} else {
    reject(@"no_events", @"There were no tasks");
  }*/
}

但是该功能目前不起作用,你能帮我吗?

Objective-C React-Native macOS 反应原生、macOS

评论


答: 暂无答案