UNPKG

2.52 kBJavaScriptView Raw
1"use strict";
2
3var _class, _temp;
4
5const {
6 UIPlugin
7} = require('@uppy/core');
8/* eslint-disable max-len */
9
10/**
11 * Add Redux DevTools support to Uppy
12 *
13 * See https://medium.com/@zalmoxis/redux-devtools-without-redux-or-how-to-have-a-predictable-state-with-any-architecture-61c5f5a7716f
14 * and https://github.com/zalmoxisus/mobx-remotedev/blob/master/src/monitorActions.js
15 */
16
17/* eslint-enable max-len */
18
19
20module.exports = (_temp = _class = class ReduxDevTools extends UIPlugin {
21 constructor(uppy, opts) {
22 super(uppy, opts);
23 this.type = 'debugger';
24 this.id = this.opts.id || 'ReduxDevTools';
25 this.title = 'Redux DevTools'; // set default options
26
27 const defaultOptions = {}; // merge default options with the ones set by user
28
29 this.opts = { ...defaultOptions,
30 ...opts
31 };
32 this.handleStateChange = this.handleStateChange.bind(this);
33 this.initDevTools = this.initDevTools.bind(this);
34 }
35
36 handleStateChange(prevState, nextState) {
37 this.devTools.send('UPPY_STATE_UPDATE', nextState);
38 }
39
40 initDevTools() {
41 this.devTools = window.devToolsExtension.connect();
42 this.devToolsUnsubscribe = this.devTools.subscribe(message => {
43 if (message.type === 'DISPATCH') {
44 // Implement monitors actions
45 switch (message.payload.type) {
46 case 'RESET':
47 this.uppy.reset();
48 return;
49
50 case 'IMPORT_STATE':
51 {
52 const {
53 computedStates
54 } = message.payload.nextLiftedState;
55 this.uppy.store.state = { ...this.uppy.getState(),
56 ...computedStates[computedStates.length - 1].state
57 };
58 this.uppy.updateAll(this.uppy.getState());
59 return;
60 }
61
62 case 'JUMP_TO_STATE':
63 case 'JUMP_TO_ACTION':
64 this.uppy.store.state = { ...this.uppy.getState(),
65 ...JSON.parse(message.state)
66 };
67 this.uppy.updateAll(this.uppy.getState());
68 }
69 }
70 });
71 }
72
73 install() {
74 // eslint-disable-next-line no-underscore-dangle
75 this.withDevTools = typeof window !== 'undefined' && window.__REDUX_DEVTOOLS_EXTENSION__;
76
77 if (this.withDevTools) {
78 this.initDevTools();
79 this.uppy.on('state-update', this.handleStateChange);
80 }
81 }
82
83 uninstall() {
84 if (this.withDevTools) {
85 this.devToolsUnsubscribe();
86 this.uppy.off('state-update', this.handleStateUpdate);
87 }
88 }
89
90}, _class.VERSION = "2.0.3", _temp);
\No newline at end of file