Skip to content

Commit

Permalink
Merge 762ebe9 into 81f2baf
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath committed Apr 6, 2016
2 parents 81f2baf + 762ebe9 commit 1b2e1f2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 40 deletions.
66 changes: 35 additions & 31 deletions index.js
Expand Up @@ -3,67 +3,71 @@
*/

'use strict'

const BehaviorSubject = require('rx').BehaviorSubject
const histable = require('histable')

exports.create = exports.createStoreStream = (value, limit) => {
limit = limit > 0 ? limit : 0
exports.create = exports.createStoreStream = (value, opt) => {
const options = Object.assign({}, opt)
const limit = options.limit > 0 ? opt.limit : 0
const isHistoryEnabled = limit > 0
const ignoredValues = {}
var history = null
const initHistory = () => {
if (isHistoryEnabled) {
history = histable.create(limit)
history.push(value)
}
}
initHistory()
if (value === undefined) {
value = ignoredValues
}
var UNDO_HISTORY = []
var REDO_HISTORY = []

const subject = new BehaviorSubject(value)
const dispatchValue = (_value, push) => {
var isDefined = _value !== undefined
var isDiff = value !== _value
var isPushable = push === true
var isNotIgnored = value !== ignoredValues
var isHistoryEnabled = limit > 0

if ([isHistoryEnabled, isDefined, !isPushable].every(Boolean)) {
REDO_HISTORY.push(value)
}

if ([isHistoryEnabled, isDefined, isDiff, isPushable, isNotIgnored].every(Boolean)) {
UNDO_HISTORY.push(value)
}

if ([isHistoryEnabled, isDefined, isDiff, isPushable, isNotIgnored, UNDO_HISTORY.length > limit].every(Boolean)) {
UNDO_HISTORY.shift()
}

if ([isDefined, isDiff].every(Boolean)) {
value = _value
subject.onNext(value)
}
}
const reset = () => {
REDO_HISTORY = []
UNDO_HISTORY = []
}

return {
stream: subject.filter(x => x !== ignoredValues),
set: cb => {
REDO_HISTORY = []
dispatchValue(typeof cb === 'function' ? cb(value) : cb, true)
const val = typeof cb === 'function' ? cb(value) : cb
dispatchValue(val)
if (isHistoryEnabled) {
history.push(val)
}
return subject
},
end: () => {
reset()
initHistory()
subject.onCompleted()
},
get: () => value,
undo: () => dispatchValue(UNDO_HISTORY.pop(), false),
redo: () => dispatchValue(REDO_HISTORY.pop(), true),
reset,
undo: () => {
if (isHistoryEnabled) {
dispatchValue(history.undo())
}
},
redo () {
if (isHistoryEnabled) {
dispatchValue(history.redo())
}
},
reset () {
initHistory()
},
get canUndo () {
return UNDO_HISTORY.length > 0
return history.canUndo
},
get canRedo () {
return REDO_HISTORY.length > 0
return history.canRedo
}
}
}
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -38,5 +38,8 @@
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
},
"dependencies": {
"histable": "^1.1.0"
}
}
18 changes: 9 additions & 9 deletions test.js
Expand Up @@ -45,7 +45,7 @@ test('update(value):distinct', t => {

test('undo():single', t => {
const out = []
const store = createStoreStream(100, 100)
const store = createStoreStream(100, {limit: 100})
store.stream.subscribe(x => out.push(x))
store.set(200)
store.set(300)
Expand All @@ -56,7 +56,7 @@ test('undo():single', t => {

test('undo()', t => {
const out = []
const store = createStoreStream(100, 100)
const store = createStoreStream(100, {limit: 100})
store.stream.subscribe(x => out.push(x))
store.set(200)
store.set(300)
Expand All @@ -71,7 +71,7 @@ test('undo()', t => {

test('redo+undo', t => {
const out = []
const store = createStoreStream(void 0, 100)
const store = createStoreStream(void 0, {limit: 100})
store.stream.subscribe(x => out.push(x))
store.set(100) // 100
store.set(200) // 200
Expand All @@ -93,7 +93,7 @@ test('redo+undo', t => {

test('redo+undo+update', t => {
const out = []
const store = createStoreStream(void 0, 100)
const store = createStoreStream(void 0, {limit: 100})
store.stream.subscribe(x => out.push(x))
store.set(100)
store.set(200)
Expand All @@ -109,7 +109,7 @@ test('redo+undo+update', t => {

test('history-limit:2', t => {
const out = []
const store = createStoreStream(0, 2)
const store = createStoreStream(0, {limit: 2})
store.stream.subscribe(x => out.push(x))
store.set(100)
store.set(200)
Expand Down Expand Up @@ -139,7 +139,7 @@ test('history-limit:default', t => {

test('canUndo(),canRedo()', t => {
const out = []
const store = createStoreStream(0, 2)
const store = createStoreStream(0, {limit: 2})
store.stream.subscribe(x => out.push(x))
store.set(100)
t.true(store.canUndo)
Expand All @@ -151,7 +151,7 @@ test('canUndo(),canRedo()', t => {

test('reset()', t => {
const out = []
const store = createStoreStream(0, 2)
const store = createStoreStream(0, {limit: 2})
store.stream.subscribe(x => out.push(x))
store.set(100)
store.set(100)
Expand All @@ -163,7 +163,7 @@ test('reset()', t => {

test('end()', t => {
const out = []
const store = createStoreStream(0, 2)
const store = createStoreStream(0, {limit: 2})
store.stream.subscribe(
x => out.push(x),
null,
Expand All @@ -175,7 +175,7 @@ test('end()', t => {

test('end():history', t => {
const out = []
const store = createStoreStream(0, 2)
const store = createStoreStream(0, {limit: 2})
store.stream.subscribe(
x => out.push(x),
null,
Expand Down

0 comments on commit 1b2e1f2

Please sign in to comment.