Skip to content

Commit

Permalink
Pop errored handles, add keys to cartridge.
Browse files Browse the repository at this point in the history
  • Loading branch information
flatheadmill committed Dec 28, 2020
1 parent 9f1ac63 commit c274f4f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
37 changes: 27 additions & 10 deletions magazine.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,15 @@ class Magazine {
this._options = options
}

openClose () {
const child = new this.constructor(this.magazine.magazine(), this._options)
child._map = this._map
return child
subordinate () {
return this._subordinate((magazine, options) => new Magazine.OpenClose(magazine, options))
}

_subordinate (constructor) {
const openClose = constructor(this.magazine.magazine(), this._options)
assert(openClose instanceof Magazine.OpenClose)
openClose._map = this._map
return openClose
}

// If your open function raises an exception no entry will be added to
Expand Down Expand Up @@ -190,7 +195,7 @@ class Magazine {
let capture
meta.promise = new Promise(resolve => capture = { resolve })
try {
await this.close(cartridge.value)
await this.close(cartridge.value, cartridge.key)
cartridge.remove()
} catch (error) {
cartridge.release()
Expand All @@ -202,12 +207,24 @@ class Magazine {
}
}

errored () {
const cartridge = this.magazine.least()
if (cartridge == null) {
return null
}
if (cartridge.valid) {
cartridge.release()
return null
}
return cartridge
}

async open (...vargs) {
return this._options.open.apply(null, vargs)
}

async close (handle) {
return this._options.close.call(null, handle)
async close (handle, key) {
return this._options.close.call(null, handle, key)
}
}

Expand Down Expand Up @@ -250,7 +267,7 @@ class Magazine {
if (vargs.length == 0) {
return null
}
this._cache[keyified] = cartridge = new Cartridge(this, keyified, qualified, vargs[0])
this._cache[keyified] = cartridge = new Cartridge(this, keyified, key, vargs[0])
cartridge._link()
if (vargs.length == 2) {
cartridge.heft = vargs[1]
Expand Down Expand Up @@ -328,10 +345,10 @@ class Magazine {
}

class Cartridge {
constructor (magazine, keyified, qualified, value) {
constructor (magazine, keyified, key, value) {
this.magazine = magazine
this._keyified = keyified
this.qualified = qualified
this.key = key
this.value = value
this._references = 0
this._links = []
Expand Down
14 changes: 8 additions & 6 deletions test/readme.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
// we make about Magazine.

//
require('proof')(86, async okay => {
require('proof')(87, async okay => {
//

// First we'll talk about the basics of Magazine. Some of the functionality
Expand Down Expand Up @@ -676,7 +676,7 @@ require('proof')(86, async okay => {
expect: { key: 1, vargs: [ 2 ] }, response: 2, message: 'get evict race'
}, new Error('open') ]
const closings = [{
expect: { handle: 1 }, message: 'close evict race'
expect: { handle: 1, key: 1 }, message: 'close evict race'
}, new Error('close') ]
const openClose = new Magazine.OpenClose(magazine, {
open: async (key, ...vargs) => {
Expand All @@ -687,12 +687,12 @@ require('proof')(86, async okay => {
okay({ key, vargs }, opening.expect, opening.message)
return opening.response
},
close: async handle => {
close: async (handle, key) => {
const closing = closings.shift()
if (closing instanceof Error) {
throw closing
}
okay({ handle }, closing.expect, closing.message)
okay({ handle, key }, closing.expect, closing.message)
}
})
// Open race.
Expand Down Expand Up @@ -741,7 +741,9 @@ require('proof')(86, async okay => {
okay(error.message, 'invalid handle', 'cache is corrupted')
}

magazine.shrink(0)
const errored = openClose.errored()
okay(!! errored, 'errored')
errored.remove()

okay(magazine.size, 0, 'override')
}
Expand Down Expand Up @@ -769,7 +771,7 @@ require('proof')(86, async okay => {
}
})

const subOpenClose = openClose.openClose()
const subOpenClose = openClose.subordinate()
{
const got = await openClose.get(1, 1)
okay(got.value, 1, 'parent got')
Expand Down

0 comments on commit c274f4f

Please sign in to comment.