-
Notifications
You must be signed in to change notification settings - Fork 0
/
defaults.js
150 lines (119 loc) · 4.12 KB
/
defaults.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
146
147
148
149
150
// Command Line Defaults Fetched from Server
// Copyright (C) MMXVIII Arthur A. Gleckler.
// GNU LGPL v3. See "LICENSE.txt" and "COPYING.LESSER".
let DefaultsMixin = Base => class extends Base {
constructor(grammar, makeURL, timeout = 1000 * 60 * 5) {
super();
this.grammar = grammar;
this.defaultsCache = {};
this.cacheTimeoutMilliseconds = timeout;
this.makeURL = makeURL;
}
default(id, parameterName) {
let info = this.isIdCached(id) && this.defaultsCache[id].value;
return info && info[parameterName];
}
isKeyParameterValue(keyParameter) {
return function(annotation) {
return annotation.label.tag === "parameter-value"
&& annotation.label.name === keyParameter;
};
}
keyParameter(commandName) {
let commandGrammarEntry = findCommand(commandName, this.grammar);
return commandGrammarEntry && commandGrammarEntry.keyParameter;
}
parseDefaults(commandName, parameterName, presentationType) {
let context = this;
return function(input, success) {
let keyParameter = context.keyParameter(commandName);
if (keyParameter) {
let keyValue = success.annotations.find(
context.isKeyParameterValue(keyParameter));
if (keyValue) {
let defaults = [];
for (let id of context.keyValueToIDs(keyValue.label.witness)) {
let defaultValues = context.default(id, parameterName);
if (defaultValues) {
for (let d of defaultValues) {
defaults.push(d);
}
}
}
let parser = parseChoice(
presentationType.parse,
...defaults.map(d => parseConstant(presentationType.unparse(d), d)));
return parser(input, success);
}
}
return presentationType.parse(input, success);
};
}
isIdCached(id) {
if (id in this.defaultsCache) {
let entry = this.defaultsCache[id];
return "received" in entry
&& (new Date().getTime() - entry.received
< this.cacheTimeoutMilliseconds);
}
return false;
}
isIdPending(id) {
return (id in this.defaultsCache
&& "pending" in this.defaultsCache[id]);
}
markIdPending(id) {
this.defaultsCache[id] = { pending: new Date().getTime() };
}
rememberId(id, value) {
this.defaultsCache[id] = { received: new Date().getTime(), value: value };
}
// Convert <keyValue> witness into a list of IDs. Normally, the witness is
// just one ID, but this method exists to support multiple IDs and therefore
// multiple defaults.
keyValueToIDs(witness) {
return witness ? [witness] : [];
}
onReceivingValuesForEdit(request) {
let context = this;
return function() {
if (request.readyState != 4) {
return;
}
if (request.status < 400) {
let defaults = JSON.parse(request.responseText);
for (let id of Object.keys(defaults)) {
context.rememberId(id, defaults[id]);
}
} else {
console.log("Failed to retrieve existing values for edit operation.");
}
};
}
maybeFetchDefaultValues(annotations, end) {
let commandAnnotation = annotations.find(
a => a.label.tag == "command-name");
if (commandAnnotation) {
let keyParameter = this.keyParameter(commandAnnotation.label.name);
if (keyParameter) {
let keyValue = annotations.find(
this.isKeyParameterValue(keyParameter, end));
if (keyValue && (end < keyValue.start || end > keyValue.end)) {
let toFetch = this.keyValueToIDs(keyValue.label.witness).filter(
id => id && !this.isIdPending(id) && !this.isIdCached(id));
if (toFetch.length > 0) {
let request = new XMLHttpRequest();
request.open("GET", this.makeURL(toFetch.join(",")));
request.onreadystatechange =
this.onReceivingValuesForEdit(request);
request.setRequestHeader("Content-type", "application/json");
request.send(null);
for (let id of toFetch) {
this.markIdPending(id);
}
}
}
}
}
}
};