Skip to content

Commit

Permalink
refactor: change code to be more clearer
Browse files Browse the repository at this point in the history
Signed-off-by: Eries Trisnadi <[email protected]>
  • Loading branch information
eriestrisnadi committed Feb 7, 2021
1 parent 142298c commit 45b76b0
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 42 deletions.
59 changes: 17 additions & 42 deletions src/DB.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,9 @@ import { struct } from 'superstruct';
import Immutable from 'immutable';
import { File } from './adapters/File';
import { LocalStorage } from './adapters/LocalStorage';
import { IMMUTABLE_READ, IMMUTABLE_TYPE, IMMUTABLE_WRITE, JAYSN_PATH } from './constants';

export const JAYSN_PATH = '.jaysn';

const IMMUTABLE_TYPE = [
'List',
'Map',
'OrderedMap',
'Set',
'OrderedSet',
'Stack',
'Range',
'Repeat',
'Record',
'Seq',
'Collection',
];

const IMMUTABLE_READ = ['get', 'getIn'];

const IMMUTABLE_WRITE = [
'delete',
'deleteIn',
'merge',
'mergeDeep',
'mergeDeepIn',
'mergeIn',
'remove',
'removeIn',
'set',
'setIn',
'update',
'updateIn',
];

export { JAYSN_PATH } from './constants';
export class DB {
constructor(schema, options = {}) {
const defaultOpts = {
Expand All @@ -54,24 +23,23 @@ export class DB {

// Merge options with defaultOptions
const opts = Immutable.fromJS(defaultOpts).merge(Immutable.fromJS(options)).toJS();
let adapter;

// Let's pick the specific adapter
switch (opts.use) {
case 'LocalStorage':
if (typeof localStorage === 'undefined' || localStorage === null) {
throw new TypeError('LocalStorage adapter only available on browser!');
}
adapter = new LocalStorage(opts.source);
this.adapter = new LocalStorage(opts.source);
if (!localStorage.getItem(opts.source)) {
adapter.write(defaultData);
this.adapter.write(defaultData);
}
break;

default:
adapter = new File(opts.source);
this.adapter = new File(opts.source);
if (!existsSync(opts.source)) {
adapter.write(defaultData);
this.adapter.write(defaultData);
}
break;
}
Expand All @@ -94,7 +62,7 @@ export class DB {
Immutable[K].prototype.write = () => {
const data = this.getState().toJS();

this._state.push(Immutable.fromJS(adapter.write(data)));
this._state.push(Immutable.fromJS(this.adapter.write(data)));
this._stateId += 1;
return this.getState();
};
Expand All @@ -103,22 +71,29 @@ export class DB {
}, {});

// Init state from the db
const initState = Immutable.fromJS(adapter.read());
const initState = Immutable.fromJS(this.adapter.read());

this._state = [initState];
this._stateId = 0;

this.initiateImmutableRead();
this.initiateImmutableWrite();
}

initiateImmutableRead() {
// Return Latest State when using an Immutable Read Functions
IMMUTABLE_READ.forEach((method) => {
this[method] = (...args) => {
const state = this.getState();
this._state.push(Immutable.fromJS(adapter.read()));
this._state.push(Immutable.fromJS(this.adapter.read()));
this._stateId += 1;

return state[method](...args);
};
});
}

initiateImmutableWrite() {
// Return and push newState, then increase stateId when using an Immutable Write Functions
IMMUTABLE_WRITE.forEach((method) => {
this[method] = (...args) => {
Expand All @@ -140,7 +115,7 @@ export class DB {
typeof L === 'number' &&
L > -1 &&
L % 1 === 0 &&
L <= 9007199254740991
L <= Number.MAX_SAFE_INTEGER + 1
) {
V.forEach((NV) => {
struct(this.schema[O])(NV);
Expand Down
39 changes: 39 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
export const JAYSN_PATH = '.jaysn';

export const IMMUTABLE_TYPE = [
'List',
'Map',
'OrderedMap',
'Set',
'OrderedSet',
'Stack',
'Range',
'Repeat',
'Record',
'Seq',
'Collection',
];

export const IMMUTABLE_READ = ['get', 'getIn'];

export const IMMUTABLE_WRITE = [
'delete',
'deleteIn',
'merge',
'mergeDeep',
'mergeDeepIn',
'mergeIn',
'remove',
'removeIn',
'set',
'setIn',
'update',
'updateIn',
];

export default {
JAYSN_PATH,
IMMUTABLE_TYPE,
IMMUTABLE_READ,
IMMUTABLE_WRITE,
};

0 comments on commit 45b76b0

Please sign in to comment.