@mdfriday/foundry
Version:
The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.
116 lines • 3.27 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SimpleTree = void 0;
exports.newSimpleTree = newSimpleTree;
const radix_1 = require("../radix");
const support_1 = require("./support");
// NewSimpleTree creates a new SimpleTree.
function newSimpleTree() {
return new SimpleTree();
}
// SimpleTree is a thread safe radix tree that holds T.
class SimpleTree {
constructor() {
this.tree = new radix_1.Tree();
this.zero = undefined;
// Simple mutex implementation for TypeScript
let locked = false;
let readLocks = 0;
this.mu = {
lock: () => {
// In a real implementation, this would use proper async locking
// For now, we'll use a simple synchronous approach
while (locked || readLocks > 0) {
// Busy wait - in production, use proper async locks
}
locked = true;
},
unlock: () => {
locked = false;
},
rLock: () => {
while (locked) {
// Busy wait
}
readLocks++;
},
rUnlock: () => {
readLocks--;
}
};
}
get(s) {
this.mu.rLock();
try {
const [v, ok] = this.tree.get(s);
if (ok) {
return v;
}
return this.zero;
}
finally {
this.mu.rUnlock();
}
}
longestPrefix(s) {
this.mu.rLock();
try {
const [key, v, ok] = this.tree.longestPrefix(s);
if (ok) {
return [key, v];
}
return ['', this.zero];
}
finally {
this.mu.rUnlock();
}
}
insert(s, v) {
this.mu.lock();
try {
this.tree.insert(s, v);
return v;
}
finally {
this.mu.unlock();
}
}
async walkPrefix(lockType, s, f) {
switch (lockType) {
case support_1.LockType.LockTypeNone:
break;
case support_1.LockType.LockTypeRead:
this.mu.rLock();
break;
case support_1.LockType.LockTypeWrite:
this.mu.lock();
break;
}
try {
let err = null;
await this.tree.walkPrefix(s, async (key, v) => {
const [shouldStop, error] = f(key, v);
if (error) {
err = error;
return Promise.resolve(true);
}
return shouldStop;
});
return err;
}
finally {
switch (lockType) {
case support_1.LockType.LockTypeNone:
break;
case support_1.LockType.LockTypeRead:
this.mu.rUnlock();
break;
case support_1.LockType.LockTypeWrite:
this.mu.unlock();
break;
}
}
}
}
exports.SimpleTree = SimpleTree;
//# sourceMappingURL=simpletree.js.map