2
0
mirror of https://github.com/esiur/esiur-js.git synced 2025-05-06 04:22:58 +00:00
This commit is contained in:
Ahmed Zamil 2022-09-16 14:03:13 +03:00
parent dad9461cd7
commit 8128a32c4d
7 changed files with 98 additions and 45 deletions

View File

@ -37,16 +37,7 @@ var server;
class MyChat extends IResource { class MyChat extends IResource {
// void (string, string)->void
static get templateOld() {
return {
namespace: "Chat",
properties: [["title", String], ["messages", Array], ["users", Array]],
events: [["message", Map], ["voice", 0, {listenable: true }], ["login"], ["logout"]],
functions: [[ "send", [["msg", String, {optional: true}] ]]]
};
}
static get template() { static get template() {
return new TemplateDescriber("Chat",[ return new TemplateDescriber("Chat",[
new Prop("title", String), new Prop("messages", Array), new Prop("users", Array), new Prop("title", String), new Prop("messages", Array), new Prop("users", Array),

View File

@ -15,7 +15,7 @@ import { Prop, TemplateDescriber } from "../../src/Resource/Template/TemplateDes
class User extends IResource { class User extends IResource {
static get template() { static get template() {
return new TemplateDescriber("Esiur", [new Prop("username", String), new Prop("password", String)]); return new TemplateDescriber("Demo", [new Prop("username", String), new Prop("password", String)]);
} }
} }

View File

@ -135,16 +135,16 @@ export default class DataSerializer {
return new DataSerializerComposeResults( return new DataSerializerComposeResults(
TransmissionTypeIdentifier.Null, new DC(0)); TransmissionTypeIdentifier.Null, new DC(0));
var cts = template.constants.where((x) => x.value == value); var cts = template.constants.filter((x) => x.value == value);
if (cts.isEmpty) if (cts.length == 0)
return new DataSerializerComposeResults( return new DataSerializerComposeResults(
TransmissionTypeIdentifier.Null, new DC(0)); TransmissionTypeIdentifier.Null, new DC(0));
var rt = BinaryList(); var rt = BinaryList();
rt.addGuid(template.classId); rt.addGuid(template.classId);
rt.addUint8(cts.first.index); rt.addUint8(cts[0].index);
return new DataSerializerComposeResults( return new DataSerializerComposeResults(
TransmissionTypeIdentifier.Enum, rt.toDC()); TransmissionTypeIdentifier.Enum, rt.toDC());

View File

@ -132,6 +132,13 @@ export default class KeyList
this.removeAt(0); this.removeAt(0);
} }
first(selector) {
for(let v of this.values)
if (selector(v))
return v;
return null;
}
filter(selector) filter(selector)
{ {
if (selector instanceof Function){ if (selector instanceof Function){

View File

@ -3,38 +3,85 @@ import Warehouse from "../Resource/Warehouse.js";
export default class ResourceProxy { export default class ResourceProxy {
static cache = {}; static cache = {};
static getProxy(type) { static getBase(type) {
let template = Warehouse.getTemplateByType(type); if (type.baseType != null)
let className = type.prototype.constructor.name; return type.baseType;
let classUrl = "esiur://" + template.className.replace('.', '/');
return type;
}
if (template.namespace != null) { static getProxy(type) {
className = template.namespace + "_" + className;
} if (type.baseType != null)
return type;
let template = Warehouse.getTemplateByType(type);
let className = template.className;
if (ResourceProxy.cache[className]) if (ResourceProxy.cache[className])
return ResourceProxy.cache[className]; return ResourceProxy.cache[className];
//let classUrl = "esiur://" + className.replace('.', '/');
var code = `return ( class E_${className} extends b { constructor() {super();} `; // var code = `return ( class E_${className.replace('.', '/')} extends b { constructor() {super();} `;
// generate class // // generate class
for (var i = 0; i < template.properties.length; i++) { // for (var i = 0; i < template.properties.length; i++) {
// let pt = template.properties[i];
// let desc = Object.getOwnPropertyDescriptor(type.prototype, pt.name);
// if (desc) {
// code += `\r\n\tset ${pt.name}(v) {\r\n\t\tsuper.${pt.name} = v; \r\n\t\t if (this.instance) this.instance.emitModification(this.instance.template.properties[${i}], v); } \r\n\tget ${pt.name}() { \r\n\t\treturn super.${pt.name};}`;
// }
// else {
// code += `\r\n\tset ${pt.name}(v) {\r\n\t\tsuper._${pt.name} = v; \r\n\t\t if (this.instance) this.instance.emitModification(this.instance.template.properties[${i}], v); } \r\n\tget ${pt.name}() { \r\n\t\treturn this._${pt.name};}`;
// }
// }
// var func = new Function("b", `//# sourceURL=${classUrl} \r\n ${code}});`);
// let proxyType = func.call(type /* this */, type);
const makeClass = (name) => ({[name] : class extends type {}})[name];
let proxyType = makeClass(className.replace('.', '_'));
for (let i = 0; i < template.properties.length; i++) {
let pt = template.properties[i]; let pt = template.properties[i];
let desc = Object.getOwnPropertyDescriptor(type.prototype, pt.name); let desc = Object.getOwnPropertyDescriptor(type.prototype, pt.name);
if (desc) { if (desc) {
code += `\r\n set ${pt.name}(v) { \r\n super.${pt.name} = v; \r\n if (this.instance) this.instance.emitModification(this.instance.template.properties[${i}], v); } \r\n get ${pt.name}() { \r\n return super.${pt.name};}`; Object.defineProperty(proxyType.prototype, pt.name, {
get() {
// call parent getter
return desc.get?.apply(this);
},
set(value) {
// call parent setter
desc.set?.call(this, value);
this.instance?.emitModification(pt, value);
}
});
} }
else { else {
code += `\r\n set ${pt.name}(v) { \r\n super._${pt.name} = v; \r\n if (this.instance) this.instance.emitModification(this.instance.template.properties[${i}], v); } \r\n get ${pt.name}() { \r\n return this._${pt.name};}`; Object.defineProperty(proxyType.prototype, pt.name, {
get() {
// get the backing field
return this["_" + pt.name];
},
set(value) {
// set the backing field
this["_" + pt.name] = value;
this.instance?.emitModification(pt, value);
}
});
} }
} }
var func = new Function("b", `//# sourceURL=${classUrl} \r\n ${code}});`);
var proxyType = func.call(type /* this */, type);
ResourceProxy.cache[className] = proxyType; ResourceProxy.cache[className] = proxyType;
Object.defineProperty(proxyType, "baseType", {value: type});
//Object.defineProperty(proxyType, "name", {value: className.replace('.', '_')});
return proxyType; return proxyType;
} }

View File

@ -333,11 +333,13 @@ export default class TemplateGenerator {
rt += `export default class ${className} extends Esiur.Data.IEnum {\r\n`; rt += `export default class ${className} extends Esiur.Data.IEnum {\r\n`;
let options = [];
template.constants.forEach((c) => { template.constants.forEach((c) => {
rt += `\tstatic ${c.name} = new ${className}(${c.index}, ${c.value}, '${c.name}');\r\n`; rt += `\tstatic ${c.name} = new ${className}(${c.index}, ${c.value}, '${c.name}');\r\n`;
options.push(`this.${c.name}`);
}); });
rt += "\r\n"; rt += `\r\n\tstatic options = [${options.join(', ')}];\r\n`;
// add template // add template
var descConsts = template.constants.map((p) => { var descConsts = template.constants.map((p) => {
@ -396,14 +398,14 @@ export default class TemplateGenerator {
// rt += "}\r\n"; // rt += "}\r\n";
template.constants.forEach((c) => { template.constants.forEach((c) => {
var ctTypeName = this.getTypeName(template, c.valueType, templates, dependencies); let ctTypeName = this.getTypeName(template, c.valueType, templates, dependencies);
rt += `\tstatic ${c.name} = new ${ctTypeName}(${c.value});\r\n`; rt += `\tstatic ${c.name} = new ${ctTypeName}(${c.value});\r\n`;
}); });
template.functions.filter((f) => !f.inherited).forEach((f) => { template.functions.filter((f) => !f.inherited).forEach((f) => {
var rtTypeName = this.getDecoratedTypeName(template, f.returnType, templates); let rtTypeName = this.getDecoratedTypeName(template, f.returnType, templates);
var positionalArgs = f.args.filter((x) => !x.optional); let positionalArgs = f.args.filter((x) => !x.optional);
var optionalArgs = f.args.filter((x) => x.optional); let optionalArgs = f.args.filter((x) => x.optional);
if (f.isStatic) { if (f.isStatic) {
//rt += `static AsyncReply<${rtTypeName}> ${f.name}(DistributedConnection connection`; //rt += `static AsyncReply<${rtTypeName}> ${f.name}(DistributedConnection connection`;

View File

@ -315,6 +315,12 @@ export class WH extends IEventHandler
if (type == null) if (type == null)
return null; return null;
// search our records
let template = this.templates.first(x=> x.defineType == type);
if (template != null)
return template;
let templateType; let templateType;
if (type.prototype instanceof IResource) if (type.prototype instanceof IResource)
@ -334,22 +340,22 @@ export class WH extends IEventHandler
|| type.prototype instanceof IRecord)) || type.prototype instanceof IRecord))
return false; return false;
let className = type.prototype.constructor.name; // let className = type.prototype.constructor.name;
if (className.startsWith("E_")) // if (className.startsWith("E_"))
className = className.substr(2); // className = className.substr(2);
className = type.template.namespace + "." + (type.template.className ?? className); // className = type.template.namespace + "." + (type.template.className ?? className);
var templates = this.templates.get(templateType); // var templates = this.templates.get(templateType);
// loaded ? // // loaded ?
for(var i = 0; i < templates.length; i++) // for(var i = 0; i < templates.length; i++)
if (templates.at(i).className == className) // if (templates.at(i).className == className)
return templates.at(i); // return templates.at(i);
var template = new TypeTemplate(type, true); template = new TypeTemplate(type, true);
return template; return template;
} }