Skip to content

Commit

Permalink
replace console.log with debug
Browse files Browse the repository at this point in the history
  • Loading branch information
krisl committed Jul 9, 2023
1 parent c6d982e commit 5d68e68
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/graph.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import makeDebug from 'debug'
const debug = makeDebug('graferse')

declare global {
interface Set<T> {
addAll(s: Set<T> | undefined): void
Expand Down Expand Up @@ -26,7 +29,7 @@ class Lock {
return true
}

console.log(`Resource ${what} is locked, ${byWhom} will wait`)
debug(`Resource ${what} is locked, ${byWhom} will wait`)
this.waiting.add(byWhom)
return false
}
Expand Down Expand Up @@ -152,7 +155,7 @@ class Graferse
}

clearAllLocks(byWhom: string) {
console.log(`── clearAllLocks | ${byWhom} ──`);
debug(`── clearAllLocks | ${byWhom} ──`);
const whoCanMoveNow = new Set<string>()
for (const lock of this.locks) {
whoCanMoveNow.addAll(lock.unlock(byWhom))
Expand Down Expand Up @@ -231,7 +234,7 @@ function makeMakeLocker<T,U=string> (
}

const clearAllPathLocks = () => {
console.log(`── clearAllPathLocks | ${byWhom} ──`);
debug(`── clearAllPathLocks | ${byWhom} ──`);
const whoCanMoveNow = new Set<string>()
for (let i = 0; i < path.length; i++) {
whoCanMoveNow.addAll(getLock(path[i]).unlock(byWhom))
Expand All @@ -254,7 +257,7 @@ function makeMakeLocker<T,U=string> (
}

const arrivedAt = (currentIdx: number) => {
console.log(`┌─ Lock | ${byWhom} ${currentIdx} ${identity(path[currentIdx])} ──`);
debug(`┌─ Lock | ${byWhom} ${currentIdx} ${identity(path[currentIdx])} ──`);
creator.lastCallCache.set(byWhom, () => arrivedAt(currentIdx))


Expand All @@ -276,21 +279,21 @@ function makeMakeLocker<T,U=string> (
// if its behind the firstToLock, unlock it
if (i < firstToLock) {
whoCanMoveNow.addAll(getLock(path[i]).unlock(byWhom))
console.log(`unlocked ${identity(path[i])} for ${byWhom}`)
debug(`unlocked ${identity(path[i])} for ${byWhom}`)
continue
}

const lock = getLock(path[i])
if (!creator.isLockGroupAvailable(lock, byWhom)) {
console.warn("Could not obtain lock, group is locked")
debug("Could not obtain lock, group is locked")
break;
}
/* Lock from firstToLock to lastToLock */
// if failed to obtain lock, dont try to get any more
if (!lock.requestLock(byWhom, JSON.stringify(identity(path[i])))) {
break;
}
console.log(" trying to lock bidir edges from node %o", identity(path[i]))
debug(" trying to lock bidir edges from node %o", identity(path[i]))
// TODO consider returning the length of obtained edge locks
// if its > 0, even though further failed, allow the againt to retain the node lock
// so we can enter corridors as far as we can and wait there
Expand All @@ -302,15 +305,15 @@ function makeMakeLocker<T,U=string> (
nextNodes.push({node: identity(path[i]), index: i})
}

console.log({whoCanMoveNow})
debug({whoCanMoveNow})
// TODO consider not calling back with same values as last time or leave it up to clients to handle this
callback(
nextNodes,
path.length - (currentIdx +1)
)

creator.notifyWaiters(whoCanMoveNow)
console.log('└────\n')
debug('└────\n')
}

return {
Expand Down

0 comments on commit 5d68e68

Please sign in to comment.