@expressive/mvc 0.27.0 → 0.28.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.esm.js CHANGED
@@ -42,160 +42,7 @@ function issues(register) {
42
42
 
43
43
  const { assign, create, defineProperties, defineProperty, entries, getPrototypeOf, getOwnPropertyDescriptor, getOwnPropertySymbols, values } = Object;
44
44
 
45
- const Oops$8 = issues({
46
- NotReady: (model, key) => `Value ${model}.${key} value is not yet available.`,
47
- Destoryed: () => "Model is destroyed."
48
- });
49
- function suspense(source, key) {
50
- const error = Oops$8.NotReady(source.subject, key);
51
- const watch = source.observers.get(key);
52
- const promise = new Promise((resolve, reject) => {
53
- function onUpdate(key) {
54
- if (key)
55
- return () => {
56
- if (source.state[key] !== undefined) {
57
- watch.delete(onUpdate);
58
- resolve();
59
- }
60
- };
61
- if (key === null)
62
- reject(Oops$8.Destoryed());
63
- }
64
- watch.add(onUpdate);
65
- });
66
- return assign(promise, {
67
- toString: () => String(error),
68
- name: "Suspense",
69
- message: error.message,
70
- stack: error.stack
71
- });
72
- }
73
- function mayRetry(fn) {
74
- const retry = (err) => {
75
- if (err instanceof Promise)
76
- return err.then(compute);
77
- else
78
- throw err;
79
- };
80
- const compute = () => {
81
- try {
82
- const output = fn();
83
- return output instanceof Promise
84
- ? output.catch(retry)
85
- : output;
86
- }
87
- catch (err) {
88
- return retry(err);
89
- }
90
- };
91
- return compute();
92
- }
93
-
94
45
  const Oops$7 = issues({
95
- BadCallback: () => `Callback for effect-callback may only return a function.`
96
- });
97
- function createEffect(source, callback) {
98
- return control(source, self => {
99
- let { subject } = self;
100
- let unSet;
101
- let busy = false;
102
- function invoke() {
103
- if (busy)
104
- return;
105
- const output = mayRetry(() => {
106
- if (typeof unSet == "function")
107
- unSet();
108
- unSet = callback.call(subject, subject);
109
- if (unSet instanceof Promise)
110
- unSet = undefined;
111
- if (unSet && typeof unSet !== "function")
112
- throw Oops$7.BadCallback();
113
- });
114
- if (output instanceof Promise) {
115
- output.finally(() => busy = false);
116
- busy = true;
117
- }
118
- }
119
- let refresh;
120
- subject = watch(subject, () => refresh);
121
- self.followers.add(key => (key === null || refresh === null ? refresh : undefined));
122
- invoke();
123
- refresh = invoke;
124
- return () => refresh = null;
125
- });
126
- }
127
- function createValueEffect(callback) {
128
- let unSet;
129
- return function (value) {
130
- if (typeof unSet == "function")
131
- unSet(value);
132
- const out = callback.call(this, value, this);
133
- if (typeof out == "boolean")
134
- return out;
135
- if (!out || out instanceof Promise)
136
- return;
137
- if (typeof out == "function")
138
- unSet = out;
139
- else
140
- throw Oops$7.BadCallback();
141
- };
142
- }
143
-
144
- const Oops$6 = issues({
145
- KeysExpected: (key) => `Key "${key}" was expected in current update.`
146
- });
147
- function addEventListener(source, select, callback, once) {
148
- return control(source, self => {
149
- const { subject } = self;
150
- if (typeof select == "string")
151
- select = [select];
152
- if (select)
153
- for (const key of select)
154
- try {
155
- void subject[key];
156
- }
157
- catch (e) {
158
- // TODO: should this be caught?
159
- }
160
- const removeListener = self.addListener(key => {
161
- if (key === null)
162
- callback.call(subject, null);
163
- else if (!select || select.includes(key)) {
164
- if (once)
165
- removeListener();
166
- return () => callback.call(subject, self.latest);
167
- }
168
- });
169
- return removeListener;
170
- });
171
- }
172
- function awaitUpdate(source, select, timeout) {
173
- return new Promise((resolve, reject) => {
174
- if (timeout === 0) {
175
- const self = control(source, true);
176
- if (!self.frame.size)
177
- resolve(false);
178
- else if (typeof select == "string" && !self.frame.has(select))
179
- reject(Oops$6.KeysExpected(select));
180
- else {
181
- const remove = self.addListener(() => {
182
- remove();
183
- return () => resolve(self.latest);
184
- });
185
- }
186
- }
187
- else {
188
- const remove = addEventListener(source, select, resolve, true);
189
- if (timeout > 0)
190
- setTimeout(() => {
191
- remove();
192
- resolve(false);
193
- }, timeout);
194
- }
195
- });
196
- }
197
-
198
- const Oops$5 = issues({
199
46
  NotFound: (name) => `Could not find ${name} in context.`
200
47
  });
201
48
  function get$1(argument) {
@@ -210,7 +57,7 @@ function get$1(argument) {
210
57
  ? Control.watch(got, k => k ? onUpdate : undefined)
211
58
  : got;
212
59
  else if (argument !== false)
213
- throw Oops$5.NotFound(this);
60
+ throw Oops$7.NotFound(this);
214
61
  return {
215
62
  mount() {
216
63
  onUpdate = refresh;
@@ -226,7 +73,7 @@ function get$1(argument) {
226
73
  let proxy;
227
74
  const found = context.get(this);
228
75
  if (!found)
229
- throw Oops$5.NotFound(this);
76
+ throw Oops$7.NotFound(this);
230
77
  function forceUpdate(action) {
231
78
  if (typeof action == "function")
232
79
  action = action();
@@ -316,7 +163,7 @@ function use$1(apply, repeat) {
316
163
  instance[key] = props[key];
317
164
  if (!repeat)
318
165
  shouldApply = false;
319
- instance.on(0).then(() => onUpdate = refresh);
166
+ instance.set(0).then(() => onUpdate = refresh);
320
167
  }
321
168
  return local;
322
169
  }
@@ -325,59 +172,182 @@ function use$1(apply, repeat) {
325
172
  return render(apply);
326
173
  }
327
174
 
328
- class Model {
329
- constructor(id) {
330
- new Control(this, id);
331
- }
332
- /**
333
- * Reference to `this` without a subscription.
334
- * Use to obtain full reference from a destructure.
335
- */
336
- get is() {
337
- return this;
338
- }
339
- on(arg1, arg2, arg3) {
340
- if (typeof arg1 == "function")
341
- return createEffect(this, arg1);
342
- if (typeof arg1 == "number") {
343
- arg2 = arg1;
344
- arg1 = undefined;
175
+ const Oops$6 = issues({
176
+ NotReady: (model, key) => `Value ${model}.${key} value is not yet available.`,
177
+ Destoryed: () => "Model is destroyed."
178
+ });
179
+ function suspense(source, key) {
180
+ const error = Oops$6.NotReady(source.subject, key);
181
+ const watch = source.observers.get(key);
182
+ const promise = new Promise((resolve, reject) => {
183
+ function onUpdate(key) {
184
+ if (key)
185
+ return () => {
186
+ if (source.state[key] !== undefined) {
187
+ watch.delete(onUpdate);
188
+ resolve();
189
+ }
190
+ };
191
+ if (key === null)
192
+ reject(Oops$6.Destoryed());
345
193
  }
346
- return typeof arg2 == "function"
347
- ? addEventListener(this, arg1, arg2, arg3)
348
- : awaitUpdate(this, arg1, arg2);
349
- }
350
- get(arg1, arg2) {
351
- const { state } = control(this, true);
352
- function values() {
353
- if (typeof arg1 == "string")
354
- return state[arg1];
355
- if (!arg1)
356
- return Object.assign({}, state);
357
- const output = {};
358
- for (const key of arg1)
359
- output[key] = state[key];
360
- return output;
194
+ watch.add(onUpdate);
195
+ });
196
+ return assign(promise, {
197
+ toString: () => String(error),
198
+ name: "Suspense",
199
+ message: error.message,
200
+ stack: error.stack
201
+ });
202
+ }
203
+ function mayRetry(fn) {
204
+ const retry = (err) => {
205
+ if (err instanceof Promise)
206
+ return err.then(compute);
207
+ else
208
+ throw err;
209
+ };
210
+ const compute = () => {
211
+ try {
212
+ const output = fn();
213
+ return output instanceof Promise
214
+ ? output.catch(retry)
215
+ : output;
216
+ }
217
+ catch (err) {
218
+ return retry(err);
361
219
  }
362
- return typeof arg2 == "function"
363
- ? this.on(arg1, () => arg2(values()))
364
- : values();
220
+ };
221
+ return compute();
222
+ }
223
+
224
+ const Oops$5 = issues({
225
+ BadCallback: () => `Callback for effect-callback may only return a function.`
226
+ });
227
+ function createEffect(source, callback) {
228
+ return control(source, self => {
229
+ let { subject } = self;
230
+ let unSet;
231
+ let busy = false;
232
+ function invoke() {
233
+ if (busy)
234
+ return;
235
+ const output = mayRetry(() => {
236
+ if (typeof unSet == "function")
237
+ unSet();
238
+ unSet = callback.call(subject, subject);
239
+ if (unSet instanceof Promise)
240
+ unSet = undefined;
241
+ if (unSet && typeof unSet !== "function")
242
+ throw Oops$5.BadCallback();
243
+ });
244
+ if (output instanceof Promise) {
245
+ output.finally(() => busy = false);
246
+ busy = true;
247
+ }
248
+ }
249
+ let refresh;
250
+ subject = watch(subject, () => refresh);
251
+ self.followers.add(key => (key === null || refresh === null ? refresh : undefined));
252
+ invoke();
253
+ refresh = invoke;
254
+ return () => refresh = null;
255
+ });
256
+ }
257
+ function createValueEffect(callback) {
258
+ let unSet;
259
+ return function (value) {
260
+ if (typeof unSet == "function")
261
+ unSet(value);
262
+ const out = callback.call(this, value, this);
263
+ if (typeof out == "boolean")
264
+ return out;
265
+ if (!out || out instanceof Promise)
266
+ return;
267
+ if (typeof out == "function")
268
+ unSet = out;
269
+ else
270
+ throw Oops$5.BadCallback();
271
+ };
272
+ }
273
+
274
+ function getMethod(argument, callback, once) {
275
+ if (typeof argument == "function")
276
+ return createEffect(this, argument);
277
+ const self = control(this, true);
278
+ function get() {
279
+ if (typeof argument == "string")
280
+ return self.state[argument];
281
+ if (!argument)
282
+ return Object.assign({}, self.state);
283
+ const output = {};
284
+ for (const key of argument)
285
+ output[key] = self.state[key];
286
+ return output;
365
287
  }
366
- set(arg1, arg2) {
367
- const controller = control(this, true);
368
- const { state } = controller;
369
- if (typeof arg1 == "string") {
370
- controller.update(arg1);
371
- if (1 in arguments && arg1 in state)
372
- state[arg1] = arg2;
288
+ if (typeof callback != "function")
289
+ return get();
290
+ const select = typeof argument == "string" ? [argument] : argument;
291
+ if (select)
292
+ for (const key of select)
293
+ try {
294
+ const value = self.subject[key];
295
+ if (!(key in self.state))
296
+ self.watch(key, { value });
297
+ }
298
+ catch (e) {
299
+ // TODO: should this be caught?
300
+ }
301
+ const remove = self.addListener(key => {
302
+ if (!select || select.includes(key)) {
303
+ if (once)
304
+ remove();
305
+ return () => callback(get(), self.latest);
373
306
  }
374
- else if (typeof arg1 == "object")
307
+ });
308
+ return remove;
309
+ }
310
+ function setMethod(arg1, arg2) {
311
+ const self = control(this, true);
312
+ const { state } = self;
313
+ let timeout = 0;
314
+ switch (typeof arg1) {
315
+ case "string":
316
+ if (1 in arguments)
317
+ if (arg1 in state)
318
+ state[arg1] = arg2;
319
+ else
320
+ self.watch(arg1, { value: arg2 });
321
+ self.update(arg1);
322
+ break;
323
+ case "object":
375
324
  for (const key in state)
376
325
  if (key in arg1 && (!arg2 || arg2.includes(key))) {
377
326
  state[key] = arg1[key];
378
- controller.update(key);
327
+ self.update(key);
379
328
  }
380
- return this.on(0);
329
+ break;
330
+ default:
331
+ timeout = arg1;
332
+ }
333
+ return new Promise((resolve) => {
334
+ if (!self.frame.size && timeout === 0)
335
+ resolve(false);
336
+ const remove = self.addListener(() => {
337
+ remove();
338
+ return () => resolve(self.latest);
339
+ });
340
+ if (timeout > 0)
341
+ setTimeout(() => {
342
+ remove();
343
+ resolve(false);
344
+ }, timeout);
345
+ });
346
+ }
347
+
348
+ class Model {
349
+ constructor(id) {
350
+ new Control(this, id);
381
351
  }
382
352
  /** Mark this instance for garbage collection. */
383
353
  null() {
@@ -407,10 +377,29 @@ class Model {
407
377
  }
408
378
  Model.get = get$1;
409
379
  Model.use = use$1;
410
- defineProperty(Model.prototype, "toString", {
411
- configurable: true,
412
- value() {
413
- return `${this.constructor.name}-${control(this).id}`;
380
+ defineProperties(Model.prototype, {
381
+ get: { value: getMethod },
382
+ set: { value: setMethod },
383
+ is: {
384
+ configurable: true,
385
+ get() {
386
+ // TODO: Remove this method on 1.0
387
+ throw new Error("Model.is property is now is only available from a hook.");
388
+ }
389
+ },
390
+ on: {
391
+ configurable: true,
392
+ writable: true,
393
+ value() {
394
+ // TODO: Remove this method on 1.0
395
+ throw new Error("Model.on() is deprecated. Use Model.get or Model.set instead.");
396
+ }
397
+ },
398
+ toString: {
399
+ configurable: true,
400
+ value() {
401
+ return `${this.constructor.name}-${control(this).id}`;
402
+ }
414
403
  }
415
404
  });
416
405
  defineProperty(Model, "toString", {
@@ -433,10 +422,12 @@ class Control {
433
422
  this.frame = new Set();
434
423
  this.followers = new Set();
435
424
  this.observers = new Map([["", this.followers]]);
436
- this.id = typeof id == "undefined" ? uid() : id;
437
425
  this.subject = subject;
438
426
  REGISTER.set(subject, this);
439
- PENDING$1.set(this, new Set(Control.ready));
427
+ if (id !== false) {
428
+ this.id = id === undefined ? uid() : id;
429
+ PENDING$1.set(this, new Set(Control.ready));
430
+ }
440
431
  }
441
432
  add(key) {
442
433
  const { value } = getOwnPropertyDescriptor(this.subject, key);
@@ -536,7 +527,7 @@ Control.for = control;
536
527
  Control.apply = apply;
537
528
  Control.watch = watch;
538
529
  function control(subject, ready) {
539
- const control = REGISTER.get(subject.is);
530
+ const control = REGISTER.get(subject);
540
531
  const pending = PENDING$1.get(control);
541
532
  if (typeof ready == "function") {
542
533
  if (!pending)
@@ -589,11 +580,14 @@ function parent(child, assign) {
589
580
  return PARENTS.get(child);
590
581
  PARENTS.set(child, assign);
591
582
  }
592
- function watch(on, cb) {
593
- if (!on.hasOwnProperty("is"))
594
- on = defineProperty(create(on), "is", { value: on });
595
- OBSERVER.set(on, cb);
596
- return on;
583
+ function watch(value, cb) {
584
+ if (!OBSERVER.has(value)) {
585
+ const control = REGISTER.get(value);
586
+ value = defineProperty(create(value), "is", { value });
587
+ REGISTER.set(value, control);
588
+ }
589
+ OBSERVER.set(value, cb);
590
+ return value;
597
591
  }
598
592
  function apply(instruction) {
599
593
  const placeholder = Symbol("instruction");
@@ -901,28 +895,11 @@ function use(input, argument) {
901
895
  });
902
896
  }
903
897
  function manage(next) {
904
- const methods = {
905
- on(key, event) {
906
- const callback = () => event(next[key], key);
907
- if (!(key in next))
908
- control.watch(key, { value: subject[key] });
909
- callback();
910
- return control.addListener(k => {
911
- if (k === key)
912
- return callback;
913
- if (typeof k !== "string")
914
- return k;
915
- });
916
- },
917
- set(key, value) {
918
- if (key in next)
919
- subject[key] = value;
920
- else
921
- control.watch(key, { value });
922
- }
923
- };
924
- const subject = assign(create(next), methods);
925
- const control = new Control(subject);
898
+ const subject = assign(create(next), {
899
+ get: getMethod,
900
+ set: setMethod
901
+ });
902
+ const control = new Control(subject, false);
926
903
  for (const key in control.state = next)
927
904
  control.watch(key, {});
928
905
  return subject;
@@ -987,7 +964,7 @@ class Context {
987
964
  I = new input();
988
965
  }
989
966
  else {
990
- I = input.is;
967
+ I = control(input).subject;
991
968
  T = I.constructor;
992
969
  writable = false;
993
970
  }