-
Notifications
You must be signed in to change notification settings - Fork 638
/
index-browser.js
145 lines (122 loc) · 3.53 KB
/
index-browser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
var complain = "MARKO_DEBUG" && require("complain");
var queueMicrotask = require("../../queueMicrotask");
var defineComponent = require("../defineComponent");
var initComponents = require("../init-components");
require("../entry");
var registered = {};
var loaded = {};
var componentTypes = {};
var pendingDefs;
function register(type, def) {
var pendingForType;
if (pendingDefs) {
pendingForType = pendingDefs[type];
}
registered[type] = def;
delete loaded[type];
delete componentTypes[type];
if (pendingForType) {
delete pendingDefs[type];
queueMicrotask(function () {
pendingForType.forEach(function (args) {
initComponents.___tryHydrateComponent(
args[0],
args[1],
args[2],
args[3]
)();
});
});
}
return type;
}
function addPendingDef(def, type, meta, doc, runtimeId) {
if (!pendingDefs) {
pendingDefs = {};
// eslint-disable-next-line no-constant-condition
if ("MARKO_DEBUG") {
doc.addEventListener("load", function () {
var pendingComponentIds = Object.keys(pendingDefs);
if (pendingComponentIds.length) {
complain(
"Marko templates were never loaded for: " + pendingComponentIds
);
}
});
}
}
(pendingDefs[type] = pendingDefs[type] || []).push([
def,
meta,
doc,
runtimeId
]);
}
function isRegistered(type) {
return Boolean(registered[type]);
}
function load(typeName) {
var target = loaded[typeName];
if (!target) {
target = registered[typeName];
if (target) {
target = target();
}
if (!target) {
throw Error("Component not found: " + typeName);
}
loaded[typeName] = target;
}
return target;
}
function getComponentClass(typeName) {
var ComponentClass = componentTypes[typeName];
if (ComponentClass) {
return ComponentClass;
}
ComponentClass = load(typeName);
ComponentClass = ComponentClass.Component || ComponentClass;
if (!ComponentClass.___isComponent) {
ComponentClass = defineComponent(ComponentClass, ComponentClass.renderer);
}
// Make the component "type" accessible on each component instance
ComponentClass.prototype.___type = typeName;
// eslint-disable-next-line no-constant-condition
if ("MARKO_DEBUG") {
var classNameMatch = /\/([^/]+?)(?:\/index|\/template|)(?:\.marko|\.component(?:-browser)?|)$/.exec(
typeName
);
var className = classNameMatch ? classNameMatch[1] : "AnonymousComponent";
className = className.replace(/-(.)/g, function (g) {
return g[1].toUpperCase();
});
className = className
.replace(/\$\d+\.\d+\.\d+$/, "")
.replace(/^[^a-z$_]/i, "_$&")
.replace(/[^0-9a-z$_]+/gi, "_");
className = className[0].toUpperCase() + className.slice(1);
// eslint-disable-next-line no-unused-vars
try {
var OldComponentClass = ComponentClass;
eval(
"ComponentClass = function " +
className +
"(id, doc) { OldComponentClass.call(this, id, doc); }"
);
ComponentClass.prototype = OldComponentClass.prototype;
} catch (e) {
/** ignore error */
}
}
componentTypes[typeName] = ComponentClass;
return ComponentClass;
}
function createComponent(typeName, id) {
var ComponentClass = getComponentClass(typeName);
return new ComponentClass(id);
}
exports.r = register;
exports.___getComponentClass = getComponentClass;
exports.___createComponent = createComponent;
exports.___isRegistered = isRegistered;
exports.___addPendingDef = addPendingDef;