Skip to content

Instantly share code, notes, and snippets.

@lpshanley
Created July 2, 2020 02:08
Show Gist options
  • Save lpshanley/db8c85bd1dbe1356475bb73f62edeb1d to your computer and use it in GitHub Desktop.
Save lpshanley/db8c85bd1dbe1356475bb73f62edeb1d to your computer and use it in GitHub Desktop.
Conceptual in memory storage idea
// @class Disk
// A disk should mock a physical
// drive location in memory. Drives
// should not contain other drives.
// export default class Disk extends Storage {}
export default class Disk extends Storage {}
import Storage from './Storage'
// @class File
// Files should contain data about
// an application as well as a reference
// to the application required to
// view or edit them.
export default class File extends Storage {}
import Storage from './Storage'
// @class Folder
// Folders should be used to create
// logical partitioning of infomation
// in a disk. A folder should not be used
// as a base storage component.
export default class Folder extends Storage {}
// @class Storage
// Promise based in memory
// file system.
export default class Storage {
children = new Map()
parent = null
properties = {}
name
// Create a new storage reference
constructor(name = null, properties = {}) {
if(this.constructor.name === 'Storage')
throw new Error('Storage references should always be created from a child class.')
this.type = this.constructor.name
this.name = name
this.properties = {
created_at: new Date(),
updated_at: new Date()
}
}
// Update last the updated_at timestamp
timestamp() {
return new Promise((resolve, reject) => {
const updated_at = new Date()
this.properties = { ...this.properties, updated_at }
resolve(updated_at)
})
}
// Attach a storage object to
// self and return the newly created
// object
attach(child) {
return new Promise( async(resolve, reject) => {
if(child.type === 'Disk')
return reject(new Error('A disk cannot be the child of another disk.'))
if(this.type === 'File')
return reject(new Error('Files cannot contain other storage objects.'))
try {
await this.timestamp() // Update timestamp
await child.timestamp() // Update child
}
catch (err) {
return reject(err)
}
// Set parent for bi-directional
// traversal
child.parent = this
if(this.children.has(child.name))
return reject(new Error("A child already exists with that name."))
this.children.set(child.name, child)
return resolve(child)
})
}
// Rename the storage reference
rename(name) {
return new Promise((resolve, reject) => {
this.name = name
return resolce(this)
})
}
// Decouple a storage reference from its
// parent and return the reference
detach() {
return new Promise( async(resolve, reject) => {
try {
await this.parent.timestamp() // Update parent timestamp
await this.timestamp() // Update own timestamp
}
catch(err) {
return reject(err)
}
this.parent.children.delete(this.name)
this.parent = null
return resolve(this)
})
}
// BFS Search - returns the first storage
// reference by distance from source. A
// failed search should return a null value.
find(name) {
return new Promise((resolve, reject) => {
if(this.children.has(name))
return this.children.get(name)
let queue = [...this.children.values()]
while(queue.length) {
const child = queue.shift()
if(child.children.has(name))
return resolve(child.children.get(name))
queue.push(...child.children.values())
}
return resolve(null)
})
}
// BFS Search - Returns all storage references
// of a given tree, ordered by distance from
// source
findAll(name) {
return new Promise((resolve, reject) => {
let refs = new Set()
if(this.children.has(name))
return refs.add(this.children.get(name))
let queue = [...this.children.values()]
while(queue.length) {
const child = queue.shift()
if(child.children.has(name)) {
refs.add(rechild.children.get(name))
}
queue.push(...child.children.values())
}
return resolve(refs)
})
}
// This will remove all references
// within a storage object
empty() {
return new Promise((resolve, reject) => {
this.children = new Map()
return resolve(this)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment