@turbox3d/reactivity
Version:
Large-scale reactive state management library
213 lines • 6.3 kB
JavaScript
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import { fail, nextTick } from '@turbox3d/shared';
import { store } from './store';
import { ctx } from '../const/config';
import { ECollectType, EMaterialType } from '../const/enums';
import { TURBOX_PREFIX } from '../const/symbol';
/**
* collect prop diff history record
*/
export var TimeTravel = /*#__PURE__*/function () {
function TimeTravel() {
_classCallCheck(this, TimeTravel);
this.currentHistory = new Map();
this.transactionHistories = [];
this.cursor = -1;
}
return _createClass(TimeTravel, [{
key: "undoable",
get: function get() {
return this.cursor >= 0;
}
}, {
key: "redoable",
get: function get() {
return this.cursor < this.transactionHistories.length - 1;
}
}, {
key: "onChange",
value: function onChange(undoable, redoable, type, action) {
//
}
/**
* @todo support multiple steps
* revert to the previous history status
*/
}, {
key: "undo",
value: function undo() {
var _this = this;
if (!this.undoable) {
return;
}
var currentHistory = this.transactionHistories[this.cursor];
var original = function original() {
TimeTravel.undoHandler(currentHistory.history);
};
if (!store) {
fail('store is not ready, please init first.');
}
var action = currentHistory.actionChain[0] || {
name: '',
displayName: ''
};
store.dispatch({
name: "".concat(TURBOX_PREFIX, "UNDO_").concat(action.name),
displayName: "UNDO_".concat(action.displayName),
payload: [],
original: original,
type: EMaterialType.UNDO,
isInner: true
});
this.cursor -= 1;
nextTick(function () {
_this.onChange(_this.undoable, _this.redoable, 'undo');
});
}
/**
* @todo support multiple steps
* apply the next history status
*/
}, {
key: "redo",
value: function redo() {
var _this2 = this;
if (!this.redoable) {
return;
}
this.cursor += 1;
var currentHistory = this.transactionHistories[this.cursor];
var original = function original() {
TimeTravel.redoHandler(currentHistory.history);
};
if (!store) {
fail('store is not ready, please init first.');
}
var action = currentHistory.actionChain[0] || {
name: '',
displayName: ''
};
store.dispatch({
name: "".concat(TURBOX_PREFIX, "REDO_").concat(action.name),
displayName: "REDO_".concat(action.displayName),
payload: [],
original: original,
type: EMaterialType.REDO,
isInner: true
});
nextTick(function () {
_this2.onChange(_this2.undoable, _this2.redoable, 'redo');
});
}
}, {
key: "clear",
value: function clear() {
var _this3 = this;
this.currentHistory = new Map();
this.transactionHistories.length = 0;
this.cursor = -1;
nextTick(function () {
_this3.onChange(_this3.undoable, _this3.redoable, 'clear');
});
}
}], [{
key: "undoable",
get: function get() {
return TimeTravel.currentTimeTravel && TimeTravel.currentTimeTravel.undoable;
}
}, {
key: "redoable",
get: function get() {
return TimeTravel.currentTimeTravel && TimeTravel.currentTimeTravel.redoable;
}
}, {
key: "undoHandler",
value: function undoHandler(history) {
history.forEach(function (keyToDiffObj, target) {
if (!keyToDiffObj) {
return;
}
keyToDiffObj.forEach(function (value, key) {
if (!value) {
return;
}
if (value.beforeUpdate === value.didUpdate) {
return;
}
if (value.type === ECollectType.MAP_SET || value.type === ECollectType.MAP_DELETE) {
if (value.beforeUpdate === void 0) {
target["delete"](key);
} else {
target.set(key, value.beforeUpdate);
}
} else if (value.type === ECollectType.SET_ADD) {
target["delete"](key);
} else if (value.type === ECollectType.SET_DELETE) {
target.add(key);
} else if (Array.isArray(target) && value.beforeUpdate === void 0) {
delete target[key];
} else {
target[key] = value.beforeUpdate;
}
});
});
}
}, {
key: "redoHandler",
value: function redoHandler(history) {
history.forEach(function (keyToDiffObj, target) {
if (!keyToDiffObj) {
return;
}
keyToDiffObj.forEach(function (value, key) {
if (!value) {
return;
}
if (value.beforeUpdate === value.didUpdate) {
return;
}
if (value.type === ECollectType.MAP_SET) {
target.set(key, value.didUpdate);
} else if (value.type === ECollectType.SET_ADD) {
target.add(key);
} else if (value.type === ECollectType.SET_DELETE) {
target["delete"](key);
} else if (value.type === ECollectType.MAP_DELETE) {
target["delete"](key);
} else {
target[key] = value.didUpdate;
}
});
});
}
}]);
}();
TimeTravel.timeTravels = {};
TimeTravel.processing = false;
TimeTravel["switch"] = function (name) {
TimeTravel.currentTimeTravel = TimeTravel.get(name);
};
TimeTravel.create = function (name) {
var tt = new TimeTravel();
TimeTravel.timeTravels[name] = tt;
return tt;
};
TimeTravel.get = function (name) {
return TimeTravel.timeTravels[name];
};
TimeTravel.pause = function () {
ctx.timeTravel.isActive = false;
};
TimeTravel.resume = function () {
ctx.timeTravel.isActive = true;
};
TimeTravel.undo = function () {
TimeTravel.currentTimeTravel && TimeTravel.currentTimeTravel.undo();
};
TimeTravel.redo = function () {
TimeTravel.currentTimeTravel && TimeTravel.currentTimeTravel.redo();
};
TimeTravel.clear = function () {
TimeTravel.currentTimeTravel && TimeTravel.currentTimeTravel.clear();
};