@expressive/mvc 0.9.0 → 0.9.2

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.d.ts CHANGED
@@ -35,7 +35,6 @@ declare namespace Model {
35
35
  /** Including but not limited to T. */
36
36
  type Extends<T> = T | (string & Record<never, never>);
37
37
  export type Type<T extends Model = Model> = typeof Model & (new () => T);
38
- export type OnUpdate<T, P> = (this: T, value: ValueOf<keyof T, P>, changed: P) => void;
39
38
  export type Effect<T> = (this: T, argument: T) => Callback | Promise<any> | void;
40
39
  /** Exotic value, actual value is contained. */
41
40
  export type Ref<T = any> = {
@@ -88,7 +87,8 @@ declare class Model {
88
87
  is: this;
89
88
  constructor();
90
89
  on<P extends Model.Event<this>>(key: P, timeout?: number): Promise<Model.ValueOf<this, P>>;
91
- on<P extends Model.Event<this>>(key: P, listener: Model.OnUpdate<this, P>, once?: boolean): Callback;
90
+ on<P extends Model.Field<this>>(key: P, listener: (this: this, value: this[P], key: P) => void, once?: boolean): Callback;
91
+ on<P extends Model.Event<this>>(key: P, listener: (this: this, value: undefined, key: P) => void, once?: boolean): Callback;
92
92
  on<P extends Model.Event<this>>(keys: P[], timeout?: number): Promise<P[]>;
93
93
  on<P extends Model.Event<this>>(keys: P[], listener: (keys: P[]) => void, once?: boolean): Callback;
94
94
  on(effect: Model.Effect<this>): Callback;
package/lib/index.esm.js CHANGED
@@ -73,8 +73,11 @@ function computeMode(self, source, setter, key, required) {
73
73
  const { subject, state } = self;
74
74
  let sub;
75
75
  let order = ORDER.get(self);
76
+ let pending = KEYS.get(self);
76
77
  if (!order)
77
- ORDER.set(self, order = []);
78
+ ORDER.set(self, order = new Map());
79
+ if (!pending)
80
+ KEYS.set(self, pending = new Set());
78
81
  const compute = (initial) => {
79
82
  try {
80
83
  return setter.call(sub.proxy, sub.proxy);
@@ -100,18 +103,13 @@ function computeMode(self, source, setter, key, required) {
100
103
  }
101
104
  }
102
105
  };
103
- const update = (_key, source) => {
104
- if (source !== self) {
105
- refresh();
106
- return;
107
- }
108
- let pending = KEYS.get(self);
109
- if (!pending)
110
- KEYS.set(self, pending = []);
111
- pending.push(refresh);
112
- };
113
106
  const create = () => {
114
- sub = source.subscribe(update);
107
+ sub = source.subscribe((_, source) => {
108
+ if (source !== self)
109
+ refresh();
110
+ else
111
+ pending.add(refresh);
112
+ });
115
113
  try {
116
114
  const value = compute(true);
117
115
  state.set(key, value);
@@ -122,11 +120,15 @@ function computeMode(self, source, setter, key, required) {
122
120
  }
123
121
  finally {
124
122
  sub.commit();
125
- order.push(refresh);
123
+ order.set(refresh, order.size);
126
124
  }
127
125
  };
128
126
  INFO.set(refresh, key);
129
127
  return () => {
128
+ if (pending.has(refresh)) {
129
+ pending.delete(refresh);
130
+ refresh();
131
+ }
130
132
  const value = sub ? state.get(key) : create();
131
133
  if (value === undefined && required)
132
134
  throw suspend(self, key);
@@ -135,18 +137,20 @@ function computeMode(self, source, setter, key, required) {
135
137
  }
136
138
  function flush(control) {
137
139
  const pending = KEYS.get(control);
138
- if (!pending)
140
+ if (!pending || !pending.size)
139
141
  return;
140
- const list = ORDER.get(control);
141
- while (pending.length) {
142
- const compute = pending
143
- .sort((a, b) => list.indexOf(b) - list.indexOf(a))
144
- .pop();
142
+ const priority = ORDER.get(control);
143
+ while (pending.size) {
144
+ let compute;
145
+ for (const item of pending)
146
+ if (!compute || priority.get(item) < priority.get(compute))
147
+ compute = item;
148
+ pending.delete(compute);
145
149
  const key = INFO.get(compute);
146
150
  if (!control.frame.has(key))
147
151
  compute();
148
152
  }
149
- KEYS.delete(control);
153
+ pending.clear();
150
154
  }
151
155
 
152
156
  const REGISTER$1 = new WeakMap();