Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make backends pluggable via FileSystem ctor #36

Closed
humphd opened this issue Nov 22, 2013 · 1 comment
Closed

Make backends pluggable via FileSystem ctor #36

humphd opened this issue Nov 22, 2013 · 1 comment
Milestone

Comments

@humphd
Copy link
Contributor

humphd commented Nov 22, 2013

The rewrite for different backends landed, and I think we can take it a bit further. Where we currently have FileSystem, IndexedDBFileSystem, and WebSQLFileSystem, I think we only need/want FileSystem. To get there, I propose an API change like so:

// Create a FileSystem using whatever backed the current browser supports
// (one of IDBFS.StorageProviders.IndexedDB or IDBFS.StorageProviders.WebSQL)
var fs = new IDBFS.FileSystem({ name: "local" });

// Create an IndexedDB FileSystem explicitly
var idbfs = new IDBFS.FileSystem({
  name: "local2",
  db: IDBFS.StorageProviders.IndexedDB
});

// Create a WebSQL FileSystem explicitly
var websqlfs = new IDBFS.FileSystem({
  name: "local3",
  db: IDBFS.StorageProviders.WebSQL
});

// Create a "RAM Disk" FileSystem explicitly
var ramfs = new IDBFS.FileSystem({
  name: "local4",
  db: IDBFS.StorageProviders.Memory
});

In the case that no db option is provided to the FileSystem constructor, it will figure out what it supports (e.g., IndexedDB or WebSQL). However, if an explicit constructor function is provided for a Storage Provider (one we provide, or a custom one we don't--someone could add localStorage if they wanted without changing our code) the FileSystem constructor will attempt to use that instead. This will also make it possible to test different providers easily in tests, provide mocks if we want, etc.

The FileSystem creates and holds a reference to a db object using the appropriate Storage Provider constructor. All storage operations are delegated to this object, essentially what is happening now with IndexedDBContext. This also means that we can flatten the code paths for IndexedDBFileFystem, WebSQLFileSytem, and FileSystem into FileSystem. The advantage of this is smaller size, but also a much simpler model for extending with new types of storage providers, since you only need to send a new db into the FileSystem constructor vs. having to also implement a FooFilesystem, deal with scope issues on internal function calls in fs.js, etc.

The db object will provide ways to open the database, deal with transactions (real or simulated, depending on backend), do operations like get/put/delete, obtain a context to pass around to fs operations.etc. It won't be a large object, but will encapsulate all the backend specific bits of working with data. The FileSystem will do everything in terms of db operations when needing to work with a backend. There won't be any need for more specific types of file systems.

@modeswitch
Copy link
Member

This looks good. As part of this, it might make sense to start splitting things into separate files as well. I'll review + merge this ASAP as soon as you're ready.

@ghost ghost assigned humphd Nov 22, 2013
modeswitch pushed a commit that referenced this issue Nov 27, 2013
Make backends pluggable via FileSystem ctor, fixes #36.
humphd added a commit to humphd/filer that referenced this issue Jan 14, 2014
@humphd humphd removed their assignment Sep 18, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants