1 | import {
|
2 | isFunction,
|
3 | hasOwnProperty,
|
4 | HINT_DISPOSE, HINT_DEPENDS, defineHint,
|
5 | MESSAGE_NOT_FUNCTION, throwError
|
6 | } from "./util.js";
|
7 |
|
8 |
|
9 | var MAX_QUEUE = 2000;
|
10 |
|
11 |
|
12 | export var computedLock = false;
|
13 |
|
14 | export var computedQueue = [];
|
15 |
|
16 | export var computedI = 0;
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | function computedOverflow() {
|
22 | var message = "Computed queue overflow! Last 10 functions in the queue:";
|
23 |
|
24 | var length = computedQueue.length;
|
25 | for (var i = length - 11; i < length; i++) {
|
26 | var func = computedQueue[i];
|
27 | message +=
|
28 | "\n"
|
29 | + (i + 1)
|
30 | + ": "
|
31 | + (func.name || "anonymous");
|
32 | }
|
33 |
|
34 | throwError(message, true);
|
35 | }
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | export function computedNotify(func) {
|
42 | if (hasOwnProperty(func, HINT_DISPOSE)) return;
|
43 |
|
44 |
|
45 | if (computedQueue.lastIndexOf(func) >= computedI) return;
|
46 | computedQueue.push(func);
|
47 |
|
48 |
|
49 | if (!hasOwnProperty(func, HINT_DEPENDS)) {
|
50 | defineHint(func, HINT_DEPENDS, []);
|
51 | }
|
52 |
|
53 |
|
54 | if (!computedLock) {
|
55 | computedLock = true;
|
56 |
|
57 | try {
|
58 | for (; computedI < computedQueue.length; computedI++) {
|
59 |
|
60 | (0, computedQueue[computedI])();
|
61 | if (computedI > MAX_QUEUE) computedOverflow();
|
62 | }
|
63 | } finally {
|
64 | computedLock = false;
|
65 | computedQueue = [];
|
66 | computedI = 0;
|
67 | }
|
68 | }
|
69 | }
|
70 |
|
71 |
|
72 | export function computed(func) {
|
73 | if (!isFunction(func)) {
|
74 | throwError(MESSAGE_NOT_FUNCTION);
|
75 | }
|
76 |
|
77 | computedNotify(func);
|
78 | return func;
|
79 | }
|