aspen-core
Options
All
  • Public
  • Public/Protected
  • All
Menu

Aspen Core

The most performant solution for syncing dynamic nested objects/trees with their flattened representation. Aspen trees are powered by superfast TypedArrays that are ~5x faster than regular Arrays in all operations like lookups (indexOf) and alterations (splice), which means best of the best.

Aspen was built to serve react-aspen, a library for displaying nested trees in React apps (like file trees, indented todo lists etc.) while ensuring everything is super-efficient.

It is recommended that you read documentation for react-aspen to get a better idea on what this is all about.

Usage

You shouldn't have to use this library "as is" unless you're porting react-aspen to another rendering library and you need a very efficient backend

Install

npm i aspen-core

Light it up

import * as fse from 'fs-extra'
import * as Path from 'path'
import { IBasicFileSystemHost, Root, IFileEntryItem, WatchTerminator, FileType } from 'aspen-core'

const host: IBasicFileSystemHost = {
    pathStyle: 'unix', // or 'win32'
    getItems: async (path: string): Promise<IFileEntryItem[]> => Promise.all(
        (await fse.readdir(path))
            .map(async (filename) => {
                const stat = await fse.stat(Path.join(path, filename))
                return {
                    name: filename,
                    type: stat.isDirectory() ? FileType.Directory : FileType.File
                }
            })),

    /**
     * [optional] sort function
     */
    // sortComparator: (a: FileOrDir, b: FileOrDir): number => { by default Directories come before FileEntries }

    /**
     * [optional] use this to know when a Directory is interested in file watching
     * Note that this is used just to let you "know", `Root` can receive events through `Root#inotify` regardless of this method's presence
     */
    // watch: (path: string): WatchTerminator => { return a function that will be called when a Directory is no longer interested in watching changes }
}

const root: Root = new Root(host, '/absolute/path/that/will/act/as/root')

// refer to the full API on what is possible next

API

This library is written in TypeScript. Type definitions are included when you do npm i aspen-core. Documentaion is available here.

License

This project is licensed under MIT license. You are free to use, modify, distribute the code as you like (credits although not required, are highly appreciated)

Index

Type aliases

FileOrDir

FileOrDir: FileEntry | Directory

IWatcherEvent

TopDownIteratorCallback

TopDownIteratorCallback: function

Iterator callback as expected by Root#iterateTopDown

Type declaration

    • (item: FileOrDir, stepOver: function, stepIn: function, stepOut: function, exit: function): void
    • Parameters

      • item: FileOrDir

        Current FileEntry or Directory item

      • stepOver: function

        Jump to next item at same level (meaning if current item is Directory, it won't go "inside")

          • (): void
          • Jump to next item at same level (meaning if current item is Directory, it won't go "inside")

            Returns void

      • stepIn: function

        If current item is Directory, jump "inside" and current item becomes it's first child

          • (): void
          • If current item is Directory, jump "inside" and current item becomes it's first child

            Returns void

      • stepOut: function

        Back out of current Directory i.e one level out

          • (): void
          • Back out of current Directory i.e one level out

            Returns void

      • exit: function

        Exit the iterator

          • (): void
          • Exit the iterator

            Returns void

      Returns void

WatchTerminator

WatchTerminator: function

Function that when called should terminate a watch session associated with a directory

Type declaration

    • (path?: string): void
    • Parameters

      • Optional path: string

      Returns void

WatcherCallback

WatcherCallback: function

Type declaration

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc