Only listen to targets update
This reduces the complexity of the reducer, as well as how frequently it's run. The VM will emit a targets update after a step if any target info has changed.
This commit is contained in:
1 parent
685066089b
commit
ae529ea20d
3 files changed
+3
-39
No files matched your search
@@ -38,7 +38,7 @@ class CostumeTab extends React.Component {
|
||||
editingTarget.sprite.costumes = editingTarget.sprite.costumes
|
||||
.slice(0, costumeIndex)
|
||||
.concat(editingTarget.sprite.costumes.slice(costumeIndex + 1));
|
||||
this.props.vm.runtime.spriteInfoReport(editingTarget);
|
||||
this.props.vm.runtime.requestTargetsUpdate(editingTarget);
|
||||
// @todo not sure if this is getting redrawn correctly
|
||||
this.props.vm.runtime.requestRedraw();
|
||||
|
||||
|
||||
@@ -27,7 +27,6 @@ const vmListenerHOC = function (WrappedComponent) {
|
||||
// If the wrapped component uses the vm in componentDidMount, then
|
||||
// we need to start listening before mounting the wrapped component.
|
||||
this.props.vm.on('targetsUpdate', this.props.onTargetsUpdate);
|
||||
this.props.vm.on('SPRITE_INFO_REPORT', this.props.onSpriteInfoReport);
|
||||
}
|
||||
componentDidMount () {
|
||||
if (this.props.attachKeyboardEvents) {
|
||||
@@ -100,9 +99,6 @@ const vmListenerHOC = function (WrappedComponent) {
|
||||
onTargetsUpdate: data => {
|
||||
dispatch(targets.updateEditingTarget(data.editingTarget));
|
||||
dispatch(targets.updateTargets(data.targetList));
|
||||
},
|
||||
onSpriteInfoReport: spriteInfo => {
|
||||
dispatch(targets.updateTarget(spriteInfo));
|
||||
}
|
||||
});
|
||||
return connect(
|
||||
|
||||
+2
-34
@@ -1,8 +1,5 @@
|
||||
const defaultsDeep = require('lodash.defaultsdeep');
|
||||
|
||||
const UPDATE_EDITING_TARGET = 'scratch-gui/targets/UPDATE_EDITING_TARGET';
|
||||
const UPDATE_TARGET_LIST = 'scratch-gui/targets/UPDATE_TARGET_LIST';
|
||||
const UPDATE_TARGET = 'scratch/targets/UPDATE_TARGET';
|
||||
|
||||
const initialState = {
|
||||
sprites: {},
|
||||
@@ -12,39 +9,19 @@ const initialState = {
|
||||
const reducer = function (state, action) {
|
||||
if (typeof state === 'undefined') state = initialState;
|
||||
switch (action.type) {
|
||||
case UPDATE_TARGET:
|
||||
if (action.target.id === state.stage.id) {
|
||||
return Object.assign({}, state, {
|
||||
stage: Object.assign({}, state.stage, action.target)
|
||||
});
|
||||
}
|
||||
return Object.assign({}, state, {
|
||||
sprites: defaultsDeep(
|
||||
{[action.target.id]: action.target},
|
||||
state.sprites
|
||||
)
|
||||
});
|
||||
case UPDATE_TARGET_LIST:
|
||||
return Object.assign({}, state, {
|
||||
sprites: action.targets
|
||||
.filter(target => !target.isStage)
|
||||
.reduce(
|
||||
(targets, target, listId) => defaultsDeep(
|
||||
(targets, target, listId) => Object.assign(
|
||||
{[target.id]: {order: listId, ...target}},
|
||||
{[target.id]: state.sprites[target.id]},
|
||||
targets
|
||||
),
|
||||
{}
|
||||
),
|
||||
stage: action.targets
|
||||
.filter(target => target.isStage)
|
||||
.reduce(
|
||||
(stage, target) => {
|
||||
if (target.id !== stage.id) return target;
|
||||
return defaultsDeep(target, stage);
|
||||
},
|
||||
state.stage
|
||||
)
|
||||
.filter(target => target.isStage)[0] || {}
|
||||
});
|
||||
case UPDATE_EDITING_TARGET:
|
||||
return Object.assign({}, state, {editingTarget: action.target});
|
||||
@@ -52,15 +29,6 @@ const reducer = function (state, action) {
|
||||
return state;
|
||||
}
|
||||
};
|
||||
reducer.updateTarget = function (target) {
|
||||
return {
|
||||
type: UPDATE_TARGET,
|
||||
target: target,
|
||||
meta: {
|
||||
throttle: 30
|
||||
}
|
||||
};
|
||||
};
|
||||
reducer.updateTargets = function (targetList) {
|
||||
return {
|
||||
type: UPDATE_TARGET_LIST,
|
||||
|
||||
Reference in new issue
Block a user