nitro-codegen
Version:
The code-generator for react-native-nitro-modules.
62 lines (61 loc) • 1.9 kB
JavaScript
import {} from '../SourceFile.js';
import { ErrorType } from './ErrorType.js';
import { FunctionType } from './FunctionType.js';
import { NamedWrappingType } from './NamedWrappingType.js';
import { VoidType } from './VoidType.js';
export class PromiseType {
resultingType;
errorType;
constructor(resultingType) {
this.resultingType = resultingType;
this.errorType = new ErrorType();
}
get canBePassedByReference() {
// It's a future<..>, it cannot be copied.
return true;
}
get kind() {
return 'promise';
}
get resolverFunction() {
if (this.resultingType.kind === 'void') {
return new FunctionType(new VoidType(), []);
}
else {
return new FunctionType(new VoidType(), [
new NamedWrappingType('value', this.resultingType),
]);
}
}
get rejecterFunction() {
return new FunctionType(new VoidType(), [
new NamedWrappingType('error', this.errorType),
]);
}
getCode(language) {
const resultingCode = this.resultingType.getCode(language);
switch (language) {
case 'c++':
return `std::shared_ptr<Promise<${resultingCode}>>`;
case 'swift':
return `Promise<${resultingCode}>`;
case 'kotlin':
return `Promise<${resultingCode}>`;
default:
throw new Error(`Language ${language} is not yet supported for PromiseType!`);
}
}
getExtraFiles() {
return this.resultingType.getExtraFiles();
}
getRequiredImports() {
return [
{
language: 'c++',
name: 'NitroModules/Promise.hpp',
space: 'system',
},
...this.resultingType.getRequiredImports(),
];
}
}