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