Skip to content

Commit 71a5b9c

Browse files
committed
Move getDelayedDispatch to its own file.
1 parent a2cdf36 commit 71a5b9c

File tree

2 files changed

+44
-44
lines changed

2 files changed

+44
-44
lines changed

src/dispatcher.js

+1-44
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
import Scuttlebutt, { filter } from 'scuttlebutt-vector'
33
import * as orderedHistory from './orderedHistory'
4+
import getDelayedDispatch from './getDelayedDispatch'
45

56
import {
67
// action constants
@@ -16,50 +17,6 @@ export function isGossipType(type = '') {
1617
return type.substr(0, 1) !== '@'
1718
}
1819

19-
// queue a _reduxDispatch call, debounced by animation frame.
20-
// configurable, but requires use of private methods at the moment
21-
// keep a reference to dispatcher because methods will change over time
22-
function getDelayedDispatch(dispatcher) {
23-
if (typeof window === 'undefined'
24-
|| typeof window.requestAnimationFrame !== 'function') {
25-
return false
26-
}
27-
28-
const queue = []
29-
30-
function drainQueue() {
31-
let state = dispatcher._reduxGetState(),
32-
i
33-
34-
for (i = 0; i < 100 && (i <= queue.length - 1); i++) {
35-
// for-real dispatch the last action, triggering redux's subscribe
36-
// (and thus UI re-renders). This prioritises crunching data over
37-
// feedback, but potentially we should dispatch perodically, even
38-
// with items in the queue
39-
if (i < queue.length - 1) {
40-
state = dispatcher._historyReducer(state, queue[i])
41-
} else {
42-
dispatcher._reduxDispatch(queue[i])
43-
}
44-
}
45-
46-
// reset the queue
47-
queue.splice(0, i + 1)
48-
49-
if (queue.length)
50-
window.requestAnimationFrame(drainQueue)
51-
}
52-
53-
return function delayedDispatch(action) {
54-
queue.push(action)
55-
56-
// on first action, queue dispatching the action queue
57-
if (queue.length === 1) {
58-
window.requestAnimationFrame(drainQueue)
59-
}
60-
}
61-
}
62-
6320
const defaultOptions = {
6421
customDispatch: getDelayedDispatch,
6522
isGossipType: isGossipType,

src/getDelayedDispatch.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// queue a _reduxDispatch call, debounced by animation frame.
2+
// configurable, but requires use of private methods at the moment
3+
// keep a reference to dispatcher because methods will change over time
4+
export default function getDelayedDispatch(dispatcher) {
5+
if (typeof window === 'undefined'
6+
|| typeof window.requestAnimationFrame !== 'function') {
7+
return false
8+
}
9+
10+
const queue = []
11+
12+
function drainQueue() {
13+
let state = dispatcher._reduxGetState(),
14+
i
15+
16+
for (i = 0; i < 100 && (i <= queue.length - 1); i++) {
17+
// for-real dispatch the last action, triggering redux's subscribe
18+
// (and thus UI re-renders). This prioritises crunching data over
19+
// feedback, but potentially we should dispatch perodically, even
20+
// with items in the queue
21+
if (i < queue.length - 1) {
22+
state = dispatcher._historyReducer(state, queue[i])
23+
} else {
24+
dispatcher._reduxDispatch(queue[i])
25+
}
26+
}
27+
28+
// reset the queue
29+
queue.splice(0, i + 1)
30+
31+
if (queue.length)
32+
window.requestAnimationFrame(drainQueue)
33+
}
34+
35+
return function delayedDispatch(action) {
36+
queue.push(action)
37+
38+
// on first action, queue dispatching the action queue
39+
if (queue.length === 1) {
40+
window.requestAnimationFrame(drainQueue)
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)