2022-03-25 07:49:03 +00:00
|
|
|
/* Core of picomath: generates an instance */
|
|
|
|
import poortf from './poortf.js'
|
|
|
|
|
2022-03-25 20:43:37 +00:00
|
|
|
export default function picomathInstance (instName, config) {
|
2022-03-25 21:40:57 +00:00
|
|
|
/* Since we have to do it all the time, when we call a picomath instance
|
|
|
|
* as a function, it takes a name and 0 or more implementations add adds
|
|
|
|
* them to its poortf property named name, returning that property value.
|
|
|
|
*/
|
|
|
|
function fn (name, imps, author, data) {
|
|
|
|
if (name in fn) {
|
|
|
|
fn[name].addImps(imps, author, data)
|
|
|
|
} else {
|
|
|
|
fn[name] = poortf(name, imps, author, data)
|
|
|
|
}
|
|
|
|
/* For PoC, just assume only reason to provide author info
|
|
|
|
* is to be invalidated when config changes
|
|
|
|
*/
|
|
|
|
if (author) {
|
|
|
|
if (!fn.configUsers.has(author)) {
|
|
|
|
fn.configUsers.set(author, new Set())
|
|
|
|
}
|
|
|
|
fn.configUsers.get(author).add(name)
|
|
|
|
}
|
|
|
|
return fn[name]
|
|
|
|
}
|
2022-03-25 07:49:03 +00:00
|
|
|
|
2022-03-25 21:40:57 +00:00
|
|
|
Object.defineProperty(fn, 'name', {value: instName})
|
|
|
|
// There is an issue below of possible collision between the following
|
|
|
|
// property names and operation names. Since it would not be too hard to
|
|
|
|
// solve, we won't worry about it in this PoC.
|
|
|
|
fn.config = config || { resolveComplex: false } // primitive default for POC
|
|
|
|
fn.configUsers = new Map()
|
|
|
|
fn.reconfigure = config => {
|
|
|
|
for (const [author, names] of fn.configUsers.entries()) {
|
|
|
|
for (const name of names) {
|
|
|
|
fn[name].invalidate(author)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn.config = config
|
|
|
|
}
|
|
|
|
return fn
|
2022-03-25 07:49:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|