1 | "use strict";
|
2 | var nextId_1 = require("../nextId");
|
3 | var DagGraph_1 = require("../DagGraph");
|
4 | var log = require('debug')('redux-dag-history:DagHistory');
|
5 | function insert(state, history, config) {
|
6 | log('inserting new history state');
|
7 | var graph = history.graph;
|
8 | if (!graph) {
|
9 | throw new Error('History graph is not defined');
|
10 | }
|
11 | var reader = new DagGraph_1.default(graph);
|
12 | var lastStateId = reader.lastStateId, lastBranchId = reader.lastBranchId;
|
13 | var parentStateId = reader.currentStateId;
|
14 | var currentBranchId = reader.currentBranch;
|
15 | var newStateId = nextId_1.default(lastStateId);
|
16 | var newStateName = config.actionName(state, newStateId);
|
17 | var cousins = reader.childrenOf(parentStateId);
|
18 | var isBranching = cousins.length > 0 || lastBranchId > currentBranchId || currentBranchId === undefined;
|
19 | return {
|
20 | current: state,
|
21 | graph: graph.withMutations(function (g) {
|
22 | var dg = new DagGraph_1.default(g)
|
23 | .insertState(newStateId, parentStateId, state, newStateName)
|
24 | .setCurrentStateId(newStateId)
|
25 | .setLastStateId(newStateId);
|
26 | if (config.stateKeyGenerator) {
|
27 | var stateHash = config.stateKeyGenerator(state);
|
28 | log('inserting state with key', stateHash);
|
29 | dg.setHashForState(stateHash, newStateId);
|
30 | }
|
31 | if (isBranching) {
|
32 | var newBranchId = nextId_1.default(lastBranchId);
|
33 | var newBranch = config.branchName(currentBranchId, newBranchId, newStateName);
|
34 | dg.setCurrentBranch(newBranchId)
|
35 | .setLastBranchId(newBranchId)
|
36 | .setBranchName(newBranchId, newBranch)
|
37 | .setLatest(newBranchId, newStateId)
|
38 | .setFirst(newBranchId, newStateId)
|
39 | .setCommitted(newBranchId, newStateId)
|
40 | .markStateForBranch(newStateId, newBranchId);
|
41 | }
|
42 | else {
|
43 | dg.setLatest(currentBranchId, newStateId)
|
44 | .setCommitted(currentBranchId, newStateId)
|
45 | .markStateForBranch(newStateId, currentBranchId);
|
46 | }
|
47 | }),
|
48 | };
|
49 | }
|
50 | Object.defineProperty(exports, "__esModule", { value: true });
|
51 | exports.default = insert;
|