2
0
mirror of https://github.com/esiur/esiur-dart.git synced 2025-05-06 04:02:57 +00:00

Sync/Async setters

This commit is contained in:
Esiur Project 2022-08-30 22:27:25 +03:00
parent 0dba5f277a
commit 8959f52f6e
3 changed files with 31 additions and 28 deletions

View File

@ -24,16 +24,12 @@ void main(List<String> arguments) async {
..addOption('username', abbr: 'u') ..addOption('username', abbr: 'u')
..addOption('password', abbr: 'p') ..addOption('password', abbr: 'p')
..addOption('dir', abbr: 'd') ..addOption('dir', abbr: 'd')
..addFlag('sync', abbr: 's', defaultsTo: false)
..addFlag( ..addFlag(
"getx", "getx",
abbr: 'x', abbr: 'x',
defaultsTo: false, defaultsTo: false,
help: "Generate apropriate getx bindings for resources", help: "Generate apropriate getx bindings for resources.",
)
..addFlag(
"namedargs",
help:
"Use named arguments instead of positional arguments for resource methods",
); );
var results = parser.parse(arguments.skip(2)); var results = parser.parse(arguments.skip(2));
@ -45,14 +41,12 @@ void main(List<String> arguments) async {
//print("Username ${username} password ${password} dir ${dir}"); //print("Username ${username} password ${password} dir ${dir}");
// make template // make template
var destDir = await TemplateGenerator.getTemplate( var destDir = await TemplateGenerator.getTemplate(link,
link, dir: dir,
dir: dir, username: username,
username: username, password: password,
password: password, getx: results['getx'],
getx: results['getx'], asyncSetters: !results['sync']);
namedArgs: results["namedargs"],
);
print("Generated directory `${destDir}`"); print("Generated directory `${destDir}`");
@ -72,12 +66,13 @@ void printUsage() {
print(""); print("");
print("Available commands:"); print("Available commands:");
print("\tget-template\tGet a template from an IIP link."); print("\tget-template\tGet a template from an IIP link.");
print("\tversion: print esiur version."); print("\tversion: Print Esiur version.");
print(""); print("");
print("Global options:"); print("Global options:");
print("\t-u, --username\tAuthentication username"); print("\t-u, --username\tAuthentication username");
print("\t-p, --password\tAuthentication password"); print("\t-p, --password\tAuthentication password");
print("\t-d, --dir\tName of the directory to generate model inside."); print("\t-d, --dir\tName of the directory to generate model inside.");
print("\t-s, --async\tSynchronous property setters.");
} }
void printVersion() async { void printVersion() async {

View File

@ -443,13 +443,17 @@ class DistributedConnection extends NetworkConnection with IStore {
if (dataType.identifier == if (dataType.identifier ==
TransmissionTypeIdentifier.ResourceList) { TransmissionTypeIdentifier.ResourceList) {
// remove from suspended.
_suspendedResources.remove(r.distributedResourceInstanceId);
// parse them as int // parse them as int
var id = data.getUint32(8); var id = data.getUint32(8);
// id changed ?
if (id != r.distributedResourceInstanceId) if (id != r.distributedResourceInstanceId)
r.distributedResourceInstanceId = id; r.distributedResourceInstanceId = id;
_neededResources[id] = r; _neededResources[id] = r;
_suspendedResources.remove(id);
await fetch(id, null); await fetch(id, null);
} }

View File

@ -266,14 +266,12 @@ class TemplateGenerator {
return v == null || v == ""; return v == null || v == "";
} }
static Future<String> getTemplate( static Future<String> getTemplate(String url,
String url, { {String? dir,
String? dir, String? username,
String? username, String? password,
String? password, bool getx = false,
bool getx = false, bool asyncSetters = true}) async {
bool namedArgs = false,
}) async {
try { try {
if (!_urlRegex.hasMatch(url)) throw Exception("Invalid IIP URL"); if (!_urlRegex.hasMatch(url)) throw Exception("Invalid IIP URL");
@ -328,7 +326,8 @@ class TemplateGenerator {
var source = ""; var source = "";
if (tmp.type == TemplateType.Resource) { if (tmp.type == TemplateType.Resource) {
source = makeImports(tmp) + source = makeImports(tmp) +
generateClass(tmp, templates, getx: getx, namedArgs: namedArgs); generateClass(tmp, templates,
getx: getx, asyncSetters: asyncSetters);
} else if (tmp.type == TemplateType.Record) { } else if (tmp.type == TemplateType.Record) {
source = makeImports(tmp) + generateRecord(tmp, templates); source = makeImports(tmp) + generateRecord(tmp, templates);
} else if (tmp.type == TemplateType.Enum) { } else if (tmp.type == TemplateType.Enum) {
@ -418,7 +417,7 @@ class TemplateGenerator {
TypeTemplate template, TypeTemplate template,
List<TypeTemplate> templates, { List<TypeTemplate> templates, {
bool getx = false, bool getx = false,
bool namedArgs = false, bool asyncSetters = true,
}) { }) {
var className = template.className.split('.').last; var className = template.className.split('.').last;
@ -520,8 +519,13 @@ class TemplateGenerator {
template.properties.where((p) => !p.inherited).forEach((p) { template.properties.where((p) => !p.inherited).forEach((p) {
var ptTypeName = getTypeName(template, p.valueType, templates); var ptTypeName = getTypeName(template, p.valueType, templates);
rt.writeln("${ptTypeName} get ${p.name} { return get(${p.index}); }"); rt.writeln("${ptTypeName} get ${p.name} { return get(${p.index}); }");
rt.writeln(
"set ${p.name}(${ptTypeName} value) { setSync(${p.index}, value); }"); if (asyncSetters)
rt.writeln(
"set ${p.name}(${ptTypeName} value) { set(${p.index}, value); }");
else
rt.writeln(
"set ${p.name}(${ptTypeName} value) { setSync(${p.index}, value); }");
}); });
template.events.where((e) => !e.inherited).forEach((e) { template.events.where((e) => !e.inherited).forEach((e) {