@expressive/mvc 0.0.4 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -156,9 +156,18 @@ const LOCAL = Symbol("LOCAL");
156
156
  const STATE = Symbol("STATE");
157
157
  class Model {
158
158
  constructor() {
159
- define(this, CONTROL, new Controller(this));
159
+ const control = new Controller(this);
160
+ define(this, CONTROL, control);
160
161
  define(this, "get", this);
161
- define(this, "set", this);
162
+ defineLazy(this, "set", () => {
163
+ const assign = (key, value) => {
164
+ control.update(key, value);
165
+ };
166
+ ready(control);
167
+ for (const key of getOwnPropertyNames(control.proxy))
168
+ define(assign, key, control.ref(key));
169
+ return assign;
170
+ });
162
171
  }
163
172
  get [STATE]() {
164
173
  return this[CONTROL].state;
@@ -295,10 +304,7 @@ class Model {
295
304
  return (typeof maybe == "function" &&
296
305
  maybe.prototype instanceof this);
297
306
  }
298
- }
299
- defineLazy(Model, CONTROL, function () {
300
- return new Controller(this);
301
- });
307
+ }
302
308
 
303
309
  class Subscriber {
304
310
  constructor(target, onUpdate) {
@@ -500,26 +506,28 @@ class Controller {
500
506
  this.proxy = Object.create(subject);
501
507
  }
502
508
  start() {
503
- for (const key in this.subject)
504
- this.manage(key);
509
+ READY.add(this);
510
+ for (const key in this.subject) {
511
+ const { value: entry } = getOwnPropertyDescriptor(this.subject, key);
512
+ if (typeof entry == "function")
513
+ continue;
514
+ if (typeof entry == "symbol") {
515
+ const instruction = PENDING.get(entry);
516
+ if (instruction)
517
+ instruction.call(this, key, this);
518
+ continue;
519
+ }
520
+ this.manage(key, entry);
521
+ }
505
522
  this.emit([]);
506
523
  }
507
524
  stop() {
508
525
  this.followers.clear();
509
526
  this.onDestroy.forEach(x => x());
510
527
  }
511
- manage(key) {
528
+ manage(key, value) {
512
529
  const { proxy, state, subject } = this;
513
- const { value: entry } = getOwnPropertyDescriptor(subject, key);
514
- if (typeof entry == "function")
515
- return;
516
- if (typeof entry == "symbol") {
517
- const instruction = PENDING.get(entry);
518
- if (instruction)
519
- instruction.call(this, key, this);
520
- return;
521
- }
522
- state[key] = entry;
530
+ state[key] = value;
523
531
  const set = this.ref(key);
524
532
  defineProperty(subject, key, {
525
533
  enumerable: false,
@@ -611,26 +619,23 @@ class Controller {
611
619
  }
612
620
  function control(subject, cb) {
613
621
  let control = subject[CONTROL];
614
- const ready = READY.has(control);
615
622
  if (!control) {
616
623
  control = new Controller(subject);
617
624
  define(subject, CONTROL, control);
618
625
  }
619
- if (!cb) {
620
- if (!ready) {
621
- READY.add(control);
622
- control.start();
623
- }
624
- return control;
625
- }
626
+ return ready(control, cb);
627
+ }
628
+ function ready(control, cb) {
629
+ const ready = READY.has(control);
626
630
  if (!ready) {
627
- let done;
628
- control.requestUpdate(() => {
629
- done = cb(control);
630
- });
631
- return () => done && done();
631
+ if (cb) {
632
+ let done;
633
+ control.requestUpdate(() => done = cb(control));
634
+ return () => done && done();
635
+ }
636
+ control.start();
632
637
  }
633
- return cb(control);
638
+ return cb ? cb(control) : control;
634
639
  }
635
640
 
636
641
  function apply(fn, label) {