git » hoplite.git » commit 4ee02a0

add record(), a set of named inputs

author Alan Dipert
2021-06-13 06:04:06 UTC
committer Alan Dipert
2021-06-13 06:04:06 UTC
parent 41bffbb1d3bb806dfd0e55333fbb2b2840a3ca5f

add record(), a set of named inputs

record.mjs +27 -0

diff --git a/record.mjs b/record.mjs
new file mode 100644
index 0000000..6443b40
--- /dev/null
+++ b/record.mjs
@@ -0,0 +1,27 @@
+import { input, transaction } from './reactives.mjs';
+
+class Record {
+  constructor(fields) {
+    this.fieldNames = fields;
+    this.fields = Object.fromEntries(fields.map(f => [f, input()]))
+    this.get = new Proxy({}, {
+      get: (obj, name) => this.fields[name]
+    });
+  }
+  clear() {
+    transaction(() => {
+      Object.values(this.fields).forEach(c => c.set());
+    });
+  }
+  asTuples(eid) {
+    return Object.entries(this.fields)
+      .filter(([f, c]) => c.value)
+      .map(([f, c]) => [eid, f, c.value]);
+  }
+}
+
+function record(...fields) {
+  return new Record(fields);
+} 
+
+export { record };