forked from DeutscheSoft/aux-widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
200 lines (158 loc) · 5.08 KB
/
options.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* This file is part of AUX.
*
* AUX is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* AUX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General
* Public License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
import { warn } from './utils/log.js';
// Map<Node, Map<string, OptionsComponent>>
const optionsChildren = new Map();
function findOptions(parent, name) {
for (; parent; parent = parent.parentNode) {
const tmp = optionsChildren.get(parent);
if (tmp && tmp.has(name)) return tmp.get(name);
}
return null;
}
class OptionsSubscriber {
constructor(parent, name, callback) {
this.parent = parent;
this.name = name;
this.callback = callback;
this.options = void 0;
this.update();
}
update() {
const options = findOptions(this.parent, this.name);
if (options !== this.options) {
this.options = options;
const cb = this.callback;
try {
cb(options);
} catch (err) {
warn('Subscriber of AUX-OPTIONS generated an error %o', err);
}
}
}
}
// Map<String, Set<OptionsSubscriber>>
const optionsSubscribers = new Map();
function triggerUpdate(name) {
const subscribers = optionsSubscribers.get(name);
if (!subscribers) return;
subscribers.forEach((subscriber) => subscriber.update());
}
function normalizeParent(parent) {
if (parent.tagName === 'HEAD' || parent.tagName === 'BODY') {
parent = parent.parentNode;
}
return parent;
}
export function registerOptions(parent, name, options) {
parent = normalizeParent(parent);
let tmp = optionsChildren.get(parent);
if (!tmp) {
optionsChildren.set(parent, (tmp = new Map()));
}
if (tmp.has(name)) {
throw new Error('AUX-OPTIONS with name ' + name + ' defined twice.');
}
tmp.set(name, options);
triggerUpdate(name);
}
export function unregisterOptions(parent, name, options) {
parent = normalizeParent(parent);
let tmp = optionsChildren.get(parent);
if (!tmp) {
throw new Error('Unknown AUX-OPTIONS');
}
if (tmp.get(name) !== options) {
throw new Error('Found wrong AUX-OPTIONS in unregisterOptions');
}
tmp.delete(name);
triggerUpdate(name);
}
/**
* Subscribe to the set of options defined in the nearest AUX-OPTIONS component
* of a given name and parent.
*
* @param {Node} parent - Parent node. This is usually the parent node of the
* component which references a given set of options.
* @param {String} name - Options name.
* @param {Function} callback - Callback to call when a set of options become
* available. Will be called with null as long as no options set can be
* found.
* @returns {Function} - Returns a function which must be called in order to
* unsubscribe from the options.
*/
export function subscribeOptions(parent, name, callback) {
parent = normalizeParent(parent);
let subscribers = optionsSubscribers.get(name);
if (!subscribers) {
optionsSubscribers.set(name, (subscribers = new Set()));
}
const subscriber = new OptionsSubscriber(parent, name, callback);
subscribers.add(subscriber);
return () => {
subscribers.delete(subscriber);
};
}
/**
* Subscribe to the set of attributes defined by the inheritance chain of
* AUX-OPTIONS of a given name and parent.
*
* @param {Node} parent - Parent node. This is usually the parent node of the
* component which references a given set of options.
* @param {String} name - Options name.
* @param {Function} callback - Callback to call when a set of options become
* available. Will be called with a Map of attributes.
* @returns {Function} - Returns a function which must be called in order to
* unsubscribe from the options.
*/
export function subscribeOptionsAttributes(parent, name, callback) {
let current_options = null;
const attributesChangedCallback = () => {
const attr = current_options ? current_options.auxAttributes() : null;
try {
callback(attr);
} catch (err) {
warn('OptionsAttributes subscriber generated an exception %o', err);
}
};
const subs = subscribeOptions(parent, name, (options) => {
if (current_options) {
current_options.removeEventListener(
'auxAttributesChanged',
attributesChangedCallback
);
}
current_options = options;
if (options) {
options.addEventListener(
'auxAttributesChanged',
attributesChangedCallback
);
}
attributesChangedCallback();
});
return () => {
subs();
if (current_options)
current_options.removeEventListener(
'auxAttributesChanged',
attributesChangedCallback
);
};
}