name:
dist/Parser/DataStore.js
-rw-r--r--
2430
1"use strict";
2exports.__esModule = true;
3/**
4 * Holds cell values, and allows for the updating and manipulation of those cells.
5 */
6var Cell_1 = require("../Cell");
7/**
8 * Cell DataStore that stores cells in memory.
9 */
10var DataStore = /** @class */ (function () {
11 function DataStore() {
12 /**
13 * Holds cells inside an object for quick access.
14 */
15 this.data = {};
16 }
17 DataStore.prototype.getCell = function (key) {
18 if (key in this.data) {
19 return this.data[key];
20 }
21 return new Cell_1.Cell(key);
22 };
23 DataStore.prototype.addCell = function (cell) {
24 var cellId = cell.getId();
25 if (!(cellId in this.data)) {
26 this.data[cellId] = cell;
27 }
28 else {
29 this.getCell(cellId).updateDependencies(cell.getDependencies());
30 this.getCell(cellId).setValue(cell.getValue());
31 this.getCell(cellId).setError(cell.getError());
32 }
33 return this.getCell(cellId);
34 };
35 DataStore.prototype.getDependencies = function (id) {
36 var getDependencies = function (id) {
37 var filtered = [];
38 for (var key in this.data) {
39 var cell = this.data[key];
40 if (cell.dependencies) {
41 if (cell.dependencies.indexOf(id) > -1) {
42 filtered.push(cell);
43 }
44 }
45 }
46 var deps = [];
47 filtered.forEach(function (cell) {
48 if (deps.indexOf(cell.id) === -1) {
49 deps.push(cell.id);
50 }
51 });
52 return deps;
53 }.bind(this);
54 var allDependencies = [];
55 var getTotalDependencies = function (id) {
56 var deps = getDependencies(id);
57 if (deps.length) {
58 deps.forEach(function (refId) {
59 if (allDependencies.indexOf(refId) === -1) {
60 allDependencies.push(refId);
61 var cell = this.getCell(refId);
62 if (cell.getDependencies().length) {
63 getTotalDependencies(refId);
64 }
65 }
66 }.bind(this));
67 }
68 }.bind(this);
69 getTotalDependencies(id);
70 return allDependencies;
71 };
72 return DataStore;
73}());
74exports.DataStore = DataStore;