forked from bahmutov/redux-vs-rethinkdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrethink-no-boiler.js
98 lines (91 loc) · 2.55 KB
/
rethink-no-boiler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'use strict'
function initDB() {
const r = require('rethinkdb')
const dbOptions = { host: 'localhost', port: 28015 }
return r.connect(dbOptions)
.then(conn => {
console.log('connected')
const db = r.db('test')
return db.tableList().run(conn)
.then(tables => {
if (tables.indexOf('state') !== -1) {
console.log('deleting existing table state')
return db.tableDrop('state').run(conn)
}
})
.then(() => {
return db.tableCreate('state').run(conn)
.then(() => console.log('created state table'))
})
.then(() => {
console.log('returning db objects')
return {
r: r,
conn: conn,
db: db,
table: r.db('test').table('state')
}
})
})
.then(info => {
console.log('got info', Object.keys(info))
return info
})
}
function createStore(reducer) {
var queue // queue for async actions
var rethinkInterface // interface to rethinkDB
// simpler interface for reducer to use
var state = {
set: function (value) {
return rethinkInterface.table.insert({
id: 1,
state: value
}).run(rethinkInterface.conn)
},
increment: function (delta) {
return rethinkInterface.table.get(1).update({
state: rethinkInterface.r.row('state').add(delta)
}).run(rethinkInterface.conn)
}
}
var stateReducer = reducer.bind(null, state)
const store = {
dispatch: function dispatch(action) {
queue = queue.then(() => stateReducer(action))
},
subscribe: function (cb) {
queue = queue.then(() => {
return rethinkInterface.table.get(1).changes().run(rethinkInterface.conn)
.then(cursor => {
cursor.each((err, change) => cb(change.new_val.state))
})
})
}
}
queue = initDB()
.then(rethink => rethinkInterface = rethink)
return store
}
function counter(rethinkState, action) {
if (!action) {
console.log('setting the default state')
return rethinkState.set(0)
}
switch (action.type) {
case 'INCREMENT':
console.log('incrementing value')
return rethinkState.increment(1)
case 'DECREMENT':
console.log('decrementing')
return rethinkState.increment(-1)
default:
return rethinkState
}
}
const store = createStore(counter)
store.subscribe(state => console.log(state))
store.dispatch() // set default state
store.dispatch({ type: 'INCREMENT' })
store.dispatch({ type: 'INCREMENT' })
store.dispatch({ type: 'DECREMENT' })