| 6 | import { areDirty } from './reducers/dirtyHandlerIds'; |
| 7 | |
| 8 | export default class DragDropMonitor { |
| 9 | constructor(store) { |
| 10 | this.store = store; |
| 11 | this.registry = new HandlerRegistry(store); |
| 12 | } |
| 13 | |
| 14 | subscribeToStateChange(listener, options = {}) { |
| 15 | const { handlerIds } = options; |
| 16 | invariant( |
| 17 | typeof listener === 'function', |
| 18 | 'listener must be a function.', |
| 19 | ); |
| 20 | invariant( |
| 21 | typeof handlerIds === 'undefined' || isArray(handlerIds), |
| 22 | 'handlerIds, when specified, must be an array of strings.', |
| 23 | ); |
| 24 | |
| 25 | let prevStateId = this.store.getState().stateId; |
| 26 | const handleChange = () => { |
| 27 | const state = this.store.getState(); |
| 28 | const currentStateId = state.stateId; |
| 29 | try { |
| 30 | const canSkipListener = currentStateId === prevStateId || ( |
| 31 | currentStateId === prevStateId + 1 && |
| 32 | !areDirty(state.dirtyHandlerIds, handlerIds) |
| 33 | ); |
| 34 | |
| 35 | if (!canSkipListener) { |
| 36 | listener(); |
| 37 | } |
| 38 | } finally { |
| 39 | prevStateId = currentStateId; |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | return this.store.subscribe(handleChange); |
| 44 | } |
| 45 | |
| 46 | subscribeToOffsetChange(listener) { |
| 47 | invariant( |
| 48 | typeof listener === 'function', |
| 49 | 'listener must be a function.', |
| 50 | ); |
| 51 | |
| 52 | let previousState = this.store.getState().dragOffset; |
| 53 | const handleChange = () => { |
| 54 | const nextState = this.store.getState().dragOffset; |
| 55 | if (nextState === previousState) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | previousState = nextState; |
| 60 | listener(); |
| 61 | }; |
| 62 | |
| 63 | return this.store.subscribe(handleChange); |
| 64 | } |
| 65 |
nothing calls this directly
no outgoing calls
no test coverage detected