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

Tidying squirrel.js #16

Merged
merged 1 commit into from
May 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import path from 'path';
import {
find,
get as get_,
has,
keys,
startsWith
} from 'lodash/fp';
import createDebug from 'debug';

const debug = createDebug('squirrel');

const createAPI = store => {
const getBy = (index, key) => {
debug(`getBy: ${index} => ${key}`);
return store('indexes').then(indexes => {
return has(key, indexes[index]) ?
get_(key, indexes[index]).value :
null;
});
};

const getAll = index => {
debug(`getAll => ${index}`);
return store('indexes').then(indexes => {
return keys(indexes && indexes[index]);
});
};

const get = path => {
debug(`get => ${path}`);
return store('node').then(node => {
return _get(path, node);
});
};

const _get = (_path, node) => {
if (!node) return null;
if (path.relative(node.key, _path) === '') return node;

return _get(_path, find(function(child) {
return startsWith(child.key, _path);
}, node.nodes));
};

return {
getBy,
getAll,
get
};
};

export default createAPI;
75 changes: 8 additions & 67 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
import path from 'path';
import {
assign,
find,
get as get_,
has,
identity,
keys,
pick,
startsWith
pick
} from 'lodash/fp';
import Etcd from 'node-etcd';
import {Observable} from 'rxjs';
import createDebug from 'debug';

import {createEtcd$} from './etcd';
import createCombiner$ from './combiner';
import createFallback$ from './fallback';
import createSave from './save';
import createIndexer from './indexer';
import createCombiner$ from './combiner';
import createStore from './store';
import createAPI from './api';

const debug = createDebug('squirrel');

Expand All @@ -38,70 +31,18 @@ const createSquirrel = options => {

const client = new Etcd(options.hosts, pick(['auth', 'ca', 'key', 'cert'], options));
const watcher = client.watcher(options.cwd, null, {recursive: true});
const save = options.save ? createSave(options.fallback) : identity;
const indexer = createIndexer(options.indexes);

const events$ = Observable.concat(
createFallback$(options.fallback),
createEtcd$(client, watcher, options.cwd)
);

const store$ = save(createCombiner$(events$)).map(node => ({
node,
indexes: indexer(node)
}));

let store = null;
const storeReady = store$.take(1).do(_store => {
store = _store;
}).toPromise();
const subscription = store$.subscribe(_store => {
store = _store;
});

subscription.add(() => {
debug('Unsubscribe');
watcher.stop();
});

const ready = key => storeReady.then(() => store[key]);

const getBy = (index, key) => {
debug(`getBy: ${index} => ${key}`);
return ready('indexes').then(indexes => {
return has(key, indexes[index]) ?
get_(key, indexes[index]).value :
null;
});
};

const getAll = index => {
debug(`getAll => ${index}`);
return ready('indexes').then(indexes => {
return keys(indexes && indexes[index]);
});
};

const get = path => {
debug(`get => ${path}`);
return ready('node').then(node => {
return _get(path, node);
});
};

const _get = (_path, node) => {
if (!node) return null;
if (path.relative(node.key, _path) === '') return node;

return _get(_path, find(function(child) {
return startsWith(child.key, _path);
}, node.nodes));
};
const combiner$ = createCombiner$(events$);
const {store, subscription} = createStore(options, combiner$, watcher);
const api = createAPI(store);

return {
getBy,
getAll,
get,
...api,
close: () => subscription.unsubscribe()
};
};
Expand Down
40 changes: 40 additions & 0 deletions src/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import createDebug from 'debug';
import {
identity
} from 'lodash/fp';
import createSave from './save';
import createIndexer from './indexer';
const debug = createDebug('squirrel');

const createStore = (options, combiner$, watcher) => {
const indexer = createIndexer(options.indexes);

const save = options.save ? createSave(options.fallback) : identity;

const store$ = save(combiner$).map(node => ({
node,
indexes: indexer(node)
}));

let store = null;
const storeReady = store$.take(1).do(_store => {
store = _store;
}).toPromise();
const subscription = store$.subscribe(_store => {
store = _store;
});

subscription.add(() => {
debug('Unsubscribe');
watcher.stop();
});

const ready = key => storeReady.then(() => store[key]);

return {
store: ready,
subscription
};
};

export default createStore;