| 8 | } |
| 9 | |
| 10 | const on = (type) => |
| 11 | fx((dispatch, action) => { |
| 12 | if (!listeners[type]) { |
| 13 | listeners[type] = new Map() |
| 14 | addEventListener(type, globalListener) |
| 15 | } |
| 16 | |
| 17 | listeners[type].set( |
| 18 | dispatch, |
| 19 | (listeners[type].get(dispatch) || []).concat(action) |
| 20 | ) |
| 21 | |
| 22 | return () => { |
| 23 | const actions = listeners[type].get(dispatch).filter((a) => a !== action) |
| 24 | |
| 25 | listeners[type].set(dispatch, actions) |
| 26 | |
| 27 | if ( |
| 28 | actions.length === 0 && |
| 29 | listeners[type].delete(dispatch) && |
| 30 | listeners[type].size === 0 |
| 31 | ) { |
| 32 | delete listeners[type] |
| 33 | removeEventListener(type, globalListener) |
| 34 | } |
| 35 | } |
| 36 | }) |
| 37 | |
| 38 | export const onMouseMove = on("mousemove") |
| 39 | export const onMouseDown = on("mousedown") |