-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Expand file tree
/
Copy pathtemplate.ts
More file actions
143 lines (119 loc) · 3.75 KB
/
Copy pathtemplate.ts
File metadata and controls
143 lines (119 loc) · 3.75 KB
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
import type {
CompilableProgram,
Initializable,
LayoutWithContext,
Nullable,
Owner,
SerializedTemplateBlock,
SerializedTemplateWithLazyBlock,
Template,
TemplateFactory,
TemplateOk,
} from '@glimmer/interfaces';
import { assign } from '@glimmer/util/lib/object-utils';
import { compilable } from './compilable-template';
import { WrappedBuilder } from './wrapped-component';
let clientId = 0;
export let templateCacheCounters = {
cacheHit: 0,
cacheMiss: 0,
};
// These interfaces are for backwards compatibility, some addons use these intimate APIs
export interface TemplateFactoryWithIdAndMeta extends TemplateFactory {
__id?: string;
__meta?: { moduleName: string };
}
export interface TemplateWithIdAndReferrer extends TemplateOk {
id: string;
referrer: {
moduleName: string;
owner: Owner | null;
};
}
/**
* Wraps a template js in a template module to change it into a factory
* that handles lazy parsing the template and to create per env singletons
* of the template.
*/
/* @__NO_SIDE_EFFECTS__ */
export default function templateFactory({
id: templateId,
moduleName,
block,
scope,
isStrictMode,
}: SerializedTemplateWithLazyBlock): TemplateFactory {
// TODO(template-refactors): This should be removed in the near future, as it
// appears that id is unused. It is currently kept for backwards compat reasons.
let id = templateId || `client-${clientId++}`;
// TODO: This caches JSON serialized output once in case a template is
// compiled by multiple owners, but we haven't verified if this is actually
// helpful. We should benchmark this in the future.
let parsedBlock: Initializable<SerializedTemplateBlock>;
let ownerlessTemplate: Template | null = null;
let templateCache = new WeakMap<object, Template>();
let factory: TemplateFactoryWithIdAndMeta = (owner?: Owner) => {
if (parsedBlock === undefined) {
parsedBlock = JSON.parse(block) as SerializedTemplateBlock;
}
if (owner === undefined) {
if (ownerlessTemplate === null) {
templateCacheCounters.cacheMiss++;
ownerlessTemplate = new TemplateImpl({
id,
block: parsedBlock,
moduleName,
owner: null,
scope,
isStrictMode,
});
} else {
templateCacheCounters.cacheHit++;
}
return ownerlessTemplate;
}
let result = templateCache.get(owner);
if (result === undefined) {
templateCacheCounters.cacheMiss++;
result = new TemplateImpl({ id, block: parsedBlock, moduleName, owner, scope, isStrictMode });
templateCache.set(owner, result);
} else {
templateCacheCounters.cacheHit++;
}
return result;
};
factory.__id = id;
factory.__meta = { moduleName };
return factory;
}
class TemplateImpl implements TemplateWithIdAndReferrer {
readonly result = 'ok';
private layout: Nullable<CompilableProgram> = null;
private wrappedLayout: Nullable<CompilableProgram> = null;
constructor(private parsedLayout: LayoutWithContext) {}
get moduleName() {
return this.parsedLayout.moduleName;
}
get id() {
return this.parsedLayout.id;
}
// TODO(template-refactors): This should be removed in the near future, it is
// only being exposed for backwards compatibility
get referrer() {
return {
moduleName: this.parsedLayout.moduleName,
owner: this.parsedLayout.owner,
};
}
asLayout(): CompilableProgram {
if (this.layout) return this.layout;
return (this.layout = compilable(assign({}, this.parsedLayout), this.moduleName));
}
asWrappedLayout(): CompilableProgram {
if (this.wrappedLayout) return this.wrappedLayout;
return (this.wrappedLayout = new WrappedBuilder(
assign({}, this.parsedLayout),
this.moduleName
));
}
}