feat: Invalidation and lazy reloading of implementations

The interface is a bit clunky, the "author" and the callback "data"
  have to be specified when adding (an) implementation(s) in order
  for this to work. But it does, and this is just a PoC, the interface
  could be cleaned up.
This commit is contained in:
Glen Whitney 2022-03-25 13:09:50 -07:00
parent 00a7f79552
commit 1c1ba91e48
4 changed files with 70 additions and 18 deletions

View file

@ -21,13 +21,22 @@ describe('poortf', () => {
assert.throws(() => add('kilroy'), TypeError)
})
const defNumber = what => {
slate.addImps([args => typeof args[0] === 'number',
n => 'I am not number ' + (what+n)],
defNumber, what+1)
}
it('extends an empty tf', () => {
defNumber(2)
assert.strictEqual(slate(0, 'was here'), 'I am not number 2')
assert.throws(() => slate('Kilroy', 'was here'), TypeError)
slate.addImps([
[args => typeof args[0] === 'string', s => s + ' wuz here'],
[args => typeof args[0] === 'number', () => 'I am not a number']
[args => typeof args[0] === 'boolean', () => 'Maybe I am a number']
])
assert.strictEqual(slate('Kilroy', 'was here'), 'Kilroy wuz here')
assert.strictEqual(slate(2, 'was here'), 'I am not a number')
assert.strictEqual(slate(true), 'Maybe I am a number')
assert.throws(() => slate(['Ha!']), TypeError)
})
@ -36,6 +45,15 @@ describe('poortf', () => {
assert.strictEqual(add('Kilroy', 23), 'Kilroy23')
assert.throws(() => add(['Ha!'], 'gotcha'), TypeError)
})
it('can invalidate and lazily reload implementations', () => {
slate.invalidate(defNumber)
assert.strictEqual(slate.imps.length, 3) // reloader plus two non-number
assert.strictEqual(slate(1), 'I am not number 4') // 'what' is now 3!!
assert.strictEqual(slate('Yorlik', 7), 'Yorlik wuz here')
assert.strictEqual(slate(false), 'Maybe I am a number')
assert.strictEqual(slate.imps.length, 3) // reloader gone
})
})