feat: Allow reconfiguration of an existing picomath instance

Keeps track of which operations depend on the configuration and
  invalidates them for lazy reconfiguration when the config changes.
This commit is contained in:
Glen Whitney 2022-03-25 14:40:57 -07:00
parent 4213ade4ba
commit d95e5ad930
3 changed files with 44 additions and 17 deletions

View File

@ -13,6 +13,7 @@ export default function create(pmath) {
} }
if (resolve && Math.abs(sum.im/sum.re) < 1e-10) return sum.re if (resolve && Math.abs(sum.im/sum.re) < 1e-10) return sum.re
return sum return sum
}]) }],
create, pmath) // register ourselves for invalidation and reconfiguration
} }

View File

@ -12,12 +12,32 @@ export default function picomathInstance (instName, config) {
} else { } else {
fn[name] = poortf(name, imps, author, data) 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] return fn[name]
} }
Object.defineProperty(fn, 'name', {value: instName}) 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.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 return fn
} }

View File

@ -46,4 +46,10 @@ describe('Custom instances', () => {
assert.deepStrictEqual(math.add(math.complex(2,3), math.complex(2,-3)), assert.deepStrictEqual(math.add(math.complex(2,3), math.complex(2,-3)),
math.complex(4,0)) math.complex(4,0))
}) })
it("can be reconfigured", () => {
res.reconfigure({resolveComplex: false})
assert.deepStrictEqual(math.add(math.complex(2,3), math.complex(2,-3)),
math.complex(4,0))
})
}) })