Skip to content

Commit 490ba4a

Browse files
authored
add RAM.reusable (#16)
* add RAM.reusable * doc reusable
1 parent 9e504ea commit 490ba4a

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ You can also initialize a `RAM` instance with a `Buffer`:
2525
const file = new RAM(Buffer.from('hello world'))
2626
```
2727

28+
If you want to mimick a folder on disk, you can use `const createRAM = RAM.resuable()`.
29+
30+
This stores the created ram instances, ie `ram = createRAM(name)` in a map so they can be reopened
31+
with the same state, similar to working with files, but still backed by ram.
32+
2833
## License
2934

3035
MIT

index.js

+17
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@ module.exports = class RAM extends RandomAccess {
2323
if (opts.buffer) this.buffers.push(opts.buffer)
2424
}
2525

26+
static reusable () {
27+
const all = new Map()
28+
const RAM = this
29+
30+
return function createStorage (name) {
31+
const existing = all.get(name)
32+
const ram = existing ? existing.clone() : new RAM()
33+
34+
all.set(name, ram)
35+
ram.on('unlink', function () {
36+
if (all.get(name) === ram) all.delete(name)
37+
})
38+
39+
return ram
40+
}
41+
}
42+
2643
_stat (req) {
2744
const st = {
2845
size: this.length,

0 commit comments

Comments
 (0)