-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool.js
More file actions
101 lines (84 loc) · 2.73 KB
/
pool.js
File metadata and controls
101 lines (84 loc) · 2.73 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
(function(global){
'use strict';
const utils = global.HAMLEARN_UTILS || {};
if (!utils || typeof utils.parsePlainTextPool !== 'function') {
throw new Error('HAMLEARN_UTILS.parsePlainTextPool must be loaded before pool.js.');
}
const parsePlainTextPool = utils.parsePlainTextPool;
const fetchRef = typeof global.fetch === 'function' ? global.fetch.bind(global) : null;
if (!fetchRef) {
throw new Error('Fetch API is unavailable. A modern browser environment is required.');
}
const registry = new Map();
const ensurePromise = (value) => (value && typeof value.then === 'function' ? value : Promise.resolve(value));
const fetchText = (url) => fetchRef(url).then((response) => {
if (!response.ok) {
throw new Error(`Failed to load question pool from "${url}".`);
}
return response.text();
});
const createTextLoader = (url, options = {}) => {
const { parserOptions = {}, expectedMinimum = 1 } = options;
return () => fetchText(url).then((text) => {
const questions = parsePlainTextPool(text, parserOptions);
if (expectedMinimum && Array.isArray(questions) && questions.length < expectedMinimum) {
throw new Error(`Question pool "${url}" appears incomplete.`);
}
return questions;
});
};
const register = (name, loader) => {
if (!name) {
throw new Error('A pool name is required for registration.');
}
if (typeof loader !== 'function') {
throw new Error(`Pool loader for "${name}" must be a function.`);
}
registry.set(name, { loader, cache: null });
};
const ensureEntry = (name) => {
if (!registry.has(name)) {
throw new Error(`Unknown question pool "${name}".`);
}
return registry.get(name);
};
const load = (name) => {
const entry = ensureEntry(name);
if (entry.cache) {
return Promise.resolve(entry.cache);
}
return ensurePromise(entry.loader()).then((questions) => {
entry.cache = questions;
return questions;
});
};
const preload = (name) => load(name).then(() => true);
const clear = (name) => {
if (!name) {
registry.forEach((entry) => {
entry.cache = null;
});
return;
}
if (registry.has(name)) {
registry.get(name).cache = null;
}
};
const api = {
register,
load,
preload,
clear,
createTextLoader,
};
api.register('technician', api.createTextLoader('Technician Pool.txt', {
expectedMinimum: 400,
}));
api.register('general', api.createTextLoader('General pool.txt', {
expectedMinimum: 400,
}));
api.register('extra', api.createTextLoader('Extra pool.txt', {
expectedMinimum: 500,
}));
global.HAMLEARN_POOL = api;
})(typeof window !== 'undefined' ? window : globalThis);