@vuu-ui/vuu-utils 0.0.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/package.json +22 -0
- package/src/DataWindow.ts +111 -0
- package/src/array-utils.ts +22 -0
- package/src/column-utils.ts +133 -0
- package/src/constants.ts +8 -0
- package/src/data-utils.ts +60 -0
- package/src/date-utils.ts +4 -0
- package/src/event-emitter.ts +139 -0
- package/src/getUniqueId.ts +1 -0
- package/src/index.ts +15 -0
- package/src/input-utils.ts +50 -0
- package/src/invariant.ts +10 -0
- package/src/nanoid/index.ts +30 -0
- package/src/range-utils.ts +72 -0
- package/src/row-utils.ts +44 -0
- package/src/simple-store.ts +33 -0
- package/src/text-utils.ts +9 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2015 UBS
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vuu-ui/vuu-utils",
|
|
3
|
+
"version": "0.0.26",
|
|
4
|
+
"author": "heswell",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "node ../../scripts/run-build.mjs",
|
|
8
|
+
"test": "vitest run",
|
|
9
|
+
"type-defs": "node ../../scripts/build-type-defs.mjs"
|
|
10
|
+
},
|
|
11
|
+
"devDependencies": {},
|
|
12
|
+
"peerDependencies": {
|
|
13
|
+
"react": "^17.0.2",
|
|
14
|
+
"react-dom": "^17.0.2"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"index.js",
|
|
18
|
+
"index.js.map",
|
|
19
|
+
"/src"
|
|
20
|
+
],
|
|
21
|
+
"module": "esm/index.js"
|
|
22
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { WindowRange } from "./range-utils";
|
|
2
|
+
import { metadataKeys } from "./column-utils";
|
|
3
|
+
|
|
4
|
+
export type DataItem = string | number | boolean;
|
|
5
|
+
export type DataRow = [
|
|
6
|
+
/** index */
|
|
7
|
+
number,
|
|
8
|
+
/** render index */
|
|
9
|
+
number,
|
|
10
|
+
/** isLeaf */
|
|
11
|
+
boolean,
|
|
12
|
+
/** isExpanded */
|
|
13
|
+
boolean,
|
|
14
|
+
/** depth */
|
|
15
|
+
number,
|
|
16
|
+
/** child count */
|
|
17
|
+
number,
|
|
18
|
+
/** key */
|
|
19
|
+
string,
|
|
20
|
+
/** selected */
|
|
21
|
+
number,
|
|
22
|
+
/** data values */
|
|
23
|
+
...DataItem[]
|
|
24
|
+
];
|
|
25
|
+
export type RangeLike = { from: number; to: number };
|
|
26
|
+
|
|
27
|
+
const { KEY } = metadataKeys;
|
|
28
|
+
|
|
29
|
+
const log = (message: string) =>
|
|
30
|
+
console.log(`%c[DataWindow] ${message}`, "color: purple;font-weight: bold;");
|
|
31
|
+
export class DataWindow {
|
|
32
|
+
private range: WindowRange;
|
|
33
|
+
public data: DataRow[];
|
|
34
|
+
public rowCount = 0;
|
|
35
|
+
constructor({ from, to }: RangeLike) {
|
|
36
|
+
log(`constructor ${from} - ${to}`);
|
|
37
|
+
this.range = new WindowRange(from, to);
|
|
38
|
+
//internal data is always 0 based, we add range.from to determine an offset
|
|
39
|
+
this.data = new Array(to - from);
|
|
40
|
+
// window.dataWindow = this.data;
|
|
41
|
+
// log(`constructor initial range ${from} - ${to}`);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
setRowCount = (rowCount: number) => {
|
|
45
|
+
// log(`rowCount => ${rowCount}`);
|
|
46
|
+
if (rowCount < this.data.length) {
|
|
47
|
+
this.data.length = rowCount;
|
|
48
|
+
}
|
|
49
|
+
this.rowCount = rowCount;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
// return true if existing row was updated
|
|
53
|
+
add(data: DataRow) {
|
|
54
|
+
const [index] = data;
|
|
55
|
+
if (this.isWithinRange(index)) {
|
|
56
|
+
const internalIndex = index - this.range.from;
|
|
57
|
+
const isUpdate = this.data[internalIndex] !== undefined;
|
|
58
|
+
this.data[internalIndex] = data;
|
|
59
|
+
return isUpdate;
|
|
60
|
+
} else {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
getAtIndex(index: number) {
|
|
66
|
+
return this.range.isWithin(index) &&
|
|
67
|
+
this.data[index - this.range.from] != null
|
|
68
|
+
? this.data[index - this.range.from]
|
|
69
|
+
: undefined;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
getByKey(key: string) {
|
|
73
|
+
return this.data.find((row) => row[KEY] === key);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
isWithinRange(index: number) {
|
|
77
|
+
return this.range.isWithin(index) && index <= this.rowCount;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
setRange(from: number, to: number) {
|
|
81
|
+
log(`setRange ${from} ${to}`);
|
|
82
|
+
if (from !== this.range.from || to !== this.range.to) {
|
|
83
|
+
const [overlapFrom, overlapTo] = this.range.overlap(from, to);
|
|
84
|
+
const newData = new Array(to - from);
|
|
85
|
+
for (let i = overlapFrom; i < overlapTo; i++) {
|
|
86
|
+
const data = this.getAtIndex(i);
|
|
87
|
+
if (data) {
|
|
88
|
+
const index = i - from;
|
|
89
|
+
newData[index] = data;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
this.data = newData;
|
|
93
|
+
this.range.from = from;
|
|
94
|
+
this.range.to = to;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
hasData(from: number, to: number) {
|
|
99
|
+
const offset = this.range.from;
|
|
100
|
+
const start = from - offset;
|
|
101
|
+
const end = Math.min(to - offset - 1, this.rowCount - 1);
|
|
102
|
+
return this.data[start] !== undefined && this.data[end] !== undefined;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
getData(from: number, to: number): any[] {
|
|
106
|
+
const { from: clientFrom, to: clientTo } = this.range;
|
|
107
|
+
const startOffset = Math.max(0, from - clientFrom);
|
|
108
|
+
const endOffset = Math.min(to - clientFrom, this.rowCount ?? to);
|
|
109
|
+
return this.data.slice(startOffset, endOffset);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export function arrayOfIndices(length: number): number[] {
|
|
2
|
+
// not the neatest, but far and away the fastest way to do this ...
|
|
3
|
+
const result = Array(length);
|
|
4
|
+
for (let i = 0; i < length; i++) {
|
|
5
|
+
result[i] = i;
|
|
6
|
+
}
|
|
7
|
+
return result;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export type PartitionTest<T> = (value: T, index: number) => boolean;
|
|
11
|
+
|
|
12
|
+
export function partition<T>(
|
|
13
|
+
array: T[],
|
|
14
|
+
test: PartitionTest<T>,
|
|
15
|
+
pass: T[] = [],
|
|
16
|
+
fail: T[] = []
|
|
17
|
+
): [T[], T[]] {
|
|
18
|
+
for (let i = 0, len = array.length; i < len; i++) {
|
|
19
|
+
(test(array[i], i) ? pass : fail).push(array[i]);
|
|
20
|
+
}
|
|
21
|
+
return [pass, fail];
|
|
22
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ColumnDescriptor,
|
|
3
|
+
KeyedColumnDescriptor,
|
|
4
|
+
} from "@finos/vuu-datagrid/src/grid-model/gridModelTypes";
|
|
5
|
+
import { Row } from "./row-utils";
|
|
6
|
+
|
|
7
|
+
export interface ColumnMap {
|
|
8
|
+
[columnName: string]: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const SORT_ASC = "asc";
|
|
12
|
+
|
|
13
|
+
export type SortCriteriaItem = string | [string, "asc"]; // TODO where is 'desc'?
|
|
14
|
+
|
|
15
|
+
export function mapSortCriteria(
|
|
16
|
+
sortCriteria: SortCriteriaItem[],
|
|
17
|
+
columnMap: ColumnMap,
|
|
18
|
+
metadataOffset = 0
|
|
19
|
+
): [number, "asc"][] {
|
|
20
|
+
return sortCriteria.map((s) => {
|
|
21
|
+
if (typeof s === "string") {
|
|
22
|
+
return [columnMap[s] + metadataOffset, "asc"];
|
|
23
|
+
} else if (Array.isArray(s)) {
|
|
24
|
+
const [columnName, sortDir] = s;
|
|
25
|
+
return [columnMap[columnName] + metadataOffset, sortDir || SORT_ASC];
|
|
26
|
+
} else {
|
|
27
|
+
throw Error("columnUtils.mapSortCriteria invalid input");
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function isKeyedColumn(
|
|
33
|
+
column: ColumnDescriptor
|
|
34
|
+
): column is KeyedColumnDescriptor {
|
|
35
|
+
return typeof (column as KeyedColumnDescriptor).key === "number";
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const toColumnDescriptor = (name: string): ColumnDescriptor => ({
|
|
39
|
+
name,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
export const toKeyedColumn = (
|
|
43
|
+
column: string | ColumnDescriptor,
|
|
44
|
+
key: number
|
|
45
|
+
): KeyedColumnDescriptor => {
|
|
46
|
+
if (typeof column === "string") {
|
|
47
|
+
return { key, name: column };
|
|
48
|
+
}
|
|
49
|
+
if (isKeyedColumn(column)) {
|
|
50
|
+
return column;
|
|
51
|
+
}
|
|
52
|
+
return { ...column, key };
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const EMPTY_COLUMN_MAP = {} as const;
|
|
56
|
+
|
|
57
|
+
export function buildColumnMap(columns?: (Column | string)[]): ColumnMap {
|
|
58
|
+
const start = metadataKeys.count;
|
|
59
|
+
if (columns) {
|
|
60
|
+
return columns.reduce((map, column, i) => {
|
|
61
|
+
if (typeof column === "string") {
|
|
62
|
+
map[column] = start + i;
|
|
63
|
+
} else if (typeof column.key === "number") {
|
|
64
|
+
map[column.name] = column.key;
|
|
65
|
+
} else {
|
|
66
|
+
map[column.name] = start + i;
|
|
67
|
+
}
|
|
68
|
+
return map;
|
|
69
|
+
}, {} as ColumnMap);
|
|
70
|
+
} else {
|
|
71
|
+
return EMPTY_COLUMN_MAP;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function projectUpdates(updates: number[]): number[] {
|
|
76
|
+
const results: number[] = [];
|
|
77
|
+
const metadataOffset = metadataKeys.count - 2;
|
|
78
|
+
for (let i = 0; i < updates.length; i += 3) {
|
|
79
|
+
results[i] = updates[i] + metadataOffset;
|
|
80
|
+
results[i + 1] = updates[i + 1];
|
|
81
|
+
results[i + 2] = updates[i + 2];
|
|
82
|
+
}
|
|
83
|
+
return results;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function projectColumns(
|
|
87
|
+
tableRowColumnMap: ColumnMap,
|
|
88
|
+
columns: ColumnDescriptor[]
|
|
89
|
+
) {
|
|
90
|
+
const columnCount = columns.length;
|
|
91
|
+
const { IDX, RENDER_IDX, DEPTH, COUNT, KEY, SELECTED, count } = metadataKeys;
|
|
92
|
+
return (startIdx: number, offset: number, selectedRows: Row[] = []) =>
|
|
93
|
+
(row: Row, i: number) => {
|
|
94
|
+
// selectedRows are indices of rows within underlying dataset (not sorted or filtered)
|
|
95
|
+
// row is the original row from this set, with original index in IDX pos, which might
|
|
96
|
+
// be overwritten with a different value below if rows are sorted/filtered
|
|
97
|
+
const baseRowIdx: any = row[IDX]; // TODO
|
|
98
|
+
const out = [];
|
|
99
|
+
for (let i = 0; i < columnCount; i++) {
|
|
100
|
+
const colIdx = tableRowColumnMap[columns[i].name];
|
|
101
|
+
out[count + i] = row[colIdx];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
out[IDX] = startIdx + i + offset;
|
|
105
|
+
out[RENDER_IDX] = 0;
|
|
106
|
+
out[DEPTH] = 0;
|
|
107
|
+
out[COUNT] = 0;
|
|
108
|
+
out[KEY] = row[tableRowColumnMap.KEY];
|
|
109
|
+
out[SELECTED] = selectedRows.includes(baseRowIdx) ? 1 : 0;
|
|
110
|
+
return out;
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export type Meta = {
|
|
115
|
+
[key: string]: any;
|
|
116
|
+
} & any[];
|
|
117
|
+
|
|
118
|
+
export const metadataKeys = {
|
|
119
|
+
IDX: 0,
|
|
120
|
+
RENDER_IDX: 1,
|
|
121
|
+
IS_LEAF: 2,
|
|
122
|
+
IS_EXPANDED: 3,
|
|
123
|
+
DEPTH: 4,
|
|
124
|
+
COUNT: 5,
|
|
125
|
+
KEY: 6,
|
|
126
|
+
SELECTED: 7,
|
|
127
|
+
count: 8,
|
|
128
|
+
// TODO following only used in datamodel
|
|
129
|
+
PARENT_IDX: "parent_idx",
|
|
130
|
+
IDX_POINTER: "idx_pointer",
|
|
131
|
+
FILTER_COUNT: "filter_count",
|
|
132
|
+
NEXT_FILTER_IDX: "next_filter_idx",
|
|
133
|
+
} as const;
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
export type valueChangeDirection = "up1" | "up2" | "down1" | "down2" | "";
|
|
2
|
+
|
|
3
|
+
export const UP1 = "up1";
|
|
4
|
+
export const UP2 = "up2";
|
|
5
|
+
export const DOWN1 = "down1";
|
|
6
|
+
export const DOWN2 = "down2";
|
|
7
|
+
|
|
8
|
+
export const isValidNumber = (n: unknown): n is number =>
|
|
9
|
+
typeof n === "number" && isFinite(n);
|
|
10
|
+
|
|
11
|
+
export function getMovingValueDirection(
|
|
12
|
+
newValue: number,
|
|
13
|
+
direction?: valueChangeDirection,
|
|
14
|
+
prevValue?: number,
|
|
15
|
+
/** the number of decimal places to take into account when highlighting a change */
|
|
16
|
+
decimalPlaces?: number
|
|
17
|
+
): valueChangeDirection {
|
|
18
|
+
if (
|
|
19
|
+
!isFinite(newValue) ||
|
|
20
|
+
prevValue === undefined ||
|
|
21
|
+
direction === undefined
|
|
22
|
+
) {
|
|
23
|
+
return "";
|
|
24
|
+
} else {
|
|
25
|
+
let diff = newValue - prevValue;
|
|
26
|
+
if (diff) {
|
|
27
|
+
// make sure there is still a diff when reduced to number of decimals to be displayed
|
|
28
|
+
if (typeof decimalPlaces === "number") {
|
|
29
|
+
diff =
|
|
30
|
+
+newValue.toFixed(decimalPlaces) - +prevValue.toFixed(decimalPlaces);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (diff) {
|
|
35
|
+
if (direction === "") {
|
|
36
|
+
if (diff < 0) {
|
|
37
|
+
return DOWN1;
|
|
38
|
+
} else {
|
|
39
|
+
return UP1;
|
|
40
|
+
}
|
|
41
|
+
} else if (diff > 0) {
|
|
42
|
+
if (direction === DOWN1 || direction === DOWN2 || direction === UP2) {
|
|
43
|
+
return UP1;
|
|
44
|
+
} else {
|
|
45
|
+
return UP2;
|
|
46
|
+
}
|
|
47
|
+
} else if (
|
|
48
|
+
direction === UP1 ||
|
|
49
|
+
direction === UP2 ||
|
|
50
|
+
direction === DOWN2
|
|
51
|
+
) {
|
|
52
|
+
return DOWN1;
|
|
53
|
+
} else {
|
|
54
|
+
return DOWN2;
|
|
55
|
+
}
|
|
56
|
+
} else {
|
|
57
|
+
return "";
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
export interface Event {}
|
|
2
|
+
|
|
3
|
+
export type EventListener = (evtName: string, ...args: any[]) => void;
|
|
4
|
+
|
|
5
|
+
export type EventListenerMap = {
|
|
6
|
+
[eventName: string]: EventListener[] | EventListener;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
function isArrayOfListeners(
|
|
10
|
+
listeners: EventListener | EventListener[]
|
|
11
|
+
): listeners is EventListener[] {
|
|
12
|
+
return Array.isArray(listeners);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function isOnlyListener(
|
|
16
|
+
listeners: EventListener | EventListener[]
|
|
17
|
+
): listeners is EventListener {
|
|
18
|
+
return !Array.isArray(listeners);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface IEventEmitter {
|
|
22
|
+
emit: (type: string, ...args: unknown[]) => void;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class EventEmitter implements IEventEmitter {
|
|
26
|
+
private _events?: EventListenerMap;
|
|
27
|
+
|
|
28
|
+
constructor() {
|
|
29
|
+
this._events = {};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
addListener(type: string, listener: EventListener) {
|
|
33
|
+
if (!this._events) {
|
|
34
|
+
this._events = {};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const listeners = this._events[type];
|
|
38
|
+
|
|
39
|
+
if (!listeners) {
|
|
40
|
+
this._events[type] = listener;
|
|
41
|
+
} else if (isArrayOfListeners(listeners)) {
|
|
42
|
+
listeners.push(listener);
|
|
43
|
+
} else if (isOnlyListener(listeners)) {
|
|
44
|
+
this._events[type] = [listeners, listener];
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
removeListener(type: string, listener: EventListener) {
|
|
49
|
+
if (!this._events || !this._events[type]) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const listenerOrListeners = this._events[type];
|
|
54
|
+
let position = -1;
|
|
55
|
+
|
|
56
|
+
if (listenerOrListeners === listener) {
|
|
57
|
+
delete this._events[type];
|
|
58
|
+
} else if (Array.isArray(listenerOrListeners)) {
|
|
59
|
+
for (let i = length; i-- > 0; ) {
|
|
60
|
+
if (listenerOrListeners[i] === listener) {
|
|
61
|
+
position = i;
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (position < 0) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (listenerOrListeners.length === 1) {
|
|
71
|
+
listenerOrListeners.length = 0;
|
|
72
|
+
delete this._events[type];
|
|
73
|
+
} else {
|
|
74
|
+
listenerOrListeners.splice(position, 1);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
removeAllListeners(type: string) {
|
|
80
|
+
if (!this._events) {
|
|
81
|
+
return;
|
|
82
|
+
} else if (type === undefined) {
|
|
83
|
+
delete this._events;
|
|
84
|
+
} else {
|
|
85
|
+
delete this._events[type];
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
emit(type: string, ...args: unknown[]) {
|
|
90
|
+
if (this._events) {
|
|
91
|
+
const handler = this._events[type];
|
|
92
|
+
if (handler) {
|
|
93
|
+
invokeHandler(handler, type, args);
|
|
94
|
+
}
|
|
95
|
+
const wildcardHandler = this._events["*"];
|
|
96
|
+
if (wildcardHandler) {
|
|
97
|
+
invokeHandler(wildcardHandler, type, args);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
once(type: string, listener: EventListener) {
|
|
103
|
+
const handler = (evtName: string, message: unknown) => {
|
|
104
|
+
this.removeListener(evtName, handler);
|
|
105
|
+
listener(evtName, message);
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
this.on(type, handler);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
on(type: string, listener: EventListener) {
|
|
112
|
+
return this.addListener(type, listener);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function invokeHandler(
|
|
117
|
+
handler: EventListener | EventListener[],
|
|
118
|
+
type: string,
|
|
119
|
+
args: unknown[]
|
|
120
|
+
) {
|
|
121
|
+
if (isArrayOfListeners(handler)) {
|
|
122
|
+
handler.slice().forEach((listener) => invokeHandler(listener, type, args));
|
|
123
|
+
} else {
|
|
124
|
+
switch (args.length) {
|
|
125
|
+
case 0:
|
|
126
|
+
handler(type);
|
|
127
|
+
break;
|
|
128
|
+
case 1:
|
|
129
|
+
handler(type, args[0]);
|
|
130
|
+
break;
|
|
131
|
+
case 2:
|
|
132
|
+
handler(type, args[0], args[1]);
|
|
133
|
+
break;
|
|
134
|
+
// slower
|
|
135
|
+
default:
|
|
136
|
+
handler.call(null, type, ...args);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const getUniqueId = () => `hw-${Math.round(Math.random() * 1e5)}`;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export { EventEmitter } from "./event-emitter";
|
|
2
|
+
export * from "./array-utils";
|
|
3
|
+
export * from "./column-utils";
|
|
4
|
+
export * from "./DataWindow";
|
|
5
|
+
export * from "./data-utils";
|
|
6
|
+
export * from "./date-utils";
|
|
7
|
+
export * from "./getUniqueId";
|
|
8
|
+
export * from "./input-utils";
|
|
9
|
+
export * from "./invariant";
|
|
10
|
+
export * from "./nanoid";
|
|
11
|
+
export * from "./constants";
|
|
12
|
+
export * from "./range-utils";
|
|
13
|
+
export * from "./row-utils";
|
|
14
|
+
export * from "./simple-store";
|
|
15
|
+
export * from "./text-utils";
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
const actionKeys = {
|
|
2
|
+
Enter: 'Enter',
|
|
3
|
+
Delete: 'Delete'
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
const navigationKeys = {
|
|
7
|
+
Home: 'Home',
|
|
8
|
+
End: 'End',
|
|
9
|
+
ArrowRight: 'ArrowRight',
|
|
10
|
+
ArrowLeft: 'ArrowLeft',
|
|
11
|
+
ArrowDown: 'ArrowDown',
|
|
12
|
+
ArrowUp: 'ArrowUp',
|
|
13
|
+
Tab: 'Tab'
|
|
14
|
+
};
|
|
15
|
+
const functionKeys = {
|
|
16
|
+
F1: 'F1',
|
|
17
|
+
F2: 'F2',
|
|
18
|
+
F3: 'F3',
|
|
19
|
+
F4: 'F4',
|
|
20
|
+
F5: 'F5',
|
|
21
|
+
F6: 'F6',
|
|
22
|
+
F7: 'F7',
|
|
23
|
+
F8: 'F8',
|
|
24
|
+
F9: 'F9',
|
|
25
|
+
F10: 'F10',
|
|
26
|
+
F11: 'F11',
|
|
27
|
+
F12: 'F12'
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const specialKeys = {
|
|
31
|
+
...actionKeys,
|
|
32
|
+
...navigationKeys,
|
|
33
|
+
...functionKeys
|
|
34
|
+
};
|
|
35
|
+
type specialKey = keyof typeof specialKeys;
|
|
36
|
+
|
|
37
|
+
const isSpecialKey = (key: string): key is specialKey => key in specialKeys;
|
|
38
|
+
|
|
39
|
+
export const isCharacterKey = (evt: KeyboardEvent) => {
|
|
40
|
+
if (isSpecialKey(evt.key)) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
if (typeof evt.which === 'number' && evt.which > 0) {
|
|
44
|
+
return !evt.ctrlKey && !evt.metaKey && !evt.altKey && evt.which !== 8;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const isQuoteKey = (evt: KeyboardEvent) => {
|
|
49
|
+
return evt.key === '"' || evt.key === "'";
|
|
50
|
+
};
|
package/src/invariant.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function invariant(condition: boolean, message: string) {
|
|
2
|
+
if (!condition) {
|
|
3
|
+
const error = new Error(message);
|
|
4
|
+
error.name = 'Invariant Violation';
|
|
5
|
+
// TODO what is framesToPop?
|
|
6
|
+
// @ts-ignore
|
|
7
|
+
error.framesToPop = 1; // we don't care about invariant's own frame
|
|
8
|
+
throw error;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// This is the index.browser.js file from nanoid
|
|
2
|
+
// couldn't get original code to work as npm import without crypro warnings -
|
|
3
|
+
// seemed to be picking up node version, not browser version
|
|
4
|
+
|
|
5
|
+
export const uuid = (size: number = 21): string => {
|
|
6
|
+
let id: string = '';
|
|
7
|
+
let bytes = crypto.getRandomValues(new Uint8Array(size));
|
|
8
|
+
|
|
9
|
+
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
|
10
|
+
while (size--) {
|
|
11
|
+
// It is incorrect to use bytes exceeding the alphabet size.
|
|
12
|
+
// The following mask reduces the random byte in the 0-255 value
|
|
13
|
+
// range to the 0-63 value range. Therefore, adding hacks, such
|
|
14
|
+
// as empty string fallback or magic numbers, is unneccessary because
|
|
15
|
+
// the bitmask trims bytes down to the alphabet size.
|
|
16
|
+
let byte = bytes[size] & 63;
|
|
17
|
+
if (byte < 36) {
|
|
18
|
+
// `0-9a-z`
|
|
19
|
+
id += byte.toString(36);
|
|
20
|
+
} else if (byte < 62) {
|
|
21
|
+
// `A-Z`
|
|
22
|
+
id += (byte - 26).toString(36).toUpperCase();
|
|
23
|
+
} else if (byte < 63) {
|
|
24
|
+
id += '_';
|
|
25
|
+
} else {
|
|
26
|
+
id += '-';
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return id;
|
|
30
|
+
};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
export interface VuuRange {
|
|
2
|
+
from: number;
|
|
3
|
+
to: number;
|
|
4
|
+
bufferSize?: number;
|
|
5
|
+
reset?: boolean;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
interface FromToRange {
|
|
9
|
+
from: number;
|
|
10
|
+
to: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function getFullRange(
|
|
14
|
+
{ from, to }: VuuRange,
|
|
15
|
+
bufferSize: number = 0,
|
|
16
|
+
rowCount: number = Number.MAX_SAFE_INTEGER
|
|
17
|
+
): FromToRange {
|
|
18
|
+
if (bufferSize === 0) {
|
|
19
|
+
return { from, to: Math.min(to, rowCount) };
|
|
20
|
+
} else if (from === 0) {
|
|
21
|
+
return { from, to: Math.min(to + bufferSize, rowCount) };
|
|
22
|
+
} else {
|
|
23
|
+
const rangeSize = to - from;
|
|
24
|
+
const buff = Math.round(bufferSize / 2);
|
|
25
|
+
const shortfallBefore = from - buff < 0;
|
|
26
|
+
const shortFallAfter = rowCount - (to + buff) < 0;
|
|
27
|
+
|
|
28
|
+
if (shortfallBefore && shortFallAfter) {
|
|
29
|
+
return { from: 0, to: rowCount };
|
|
30
|
+
} else if (shortfallBefore) {
|
|
31
|
+
return { from: 0, to: rangeSize + bufferSize };
|
|
32
|
+
} else if (shortFallAfter) {
|
|
33
|
+
return { from: Math.max(0, rowCount - (rangeSize + bufferSize)), to: rowCount };
|
|
34
|
+
} else {
|
|
35
|
+
return { from: from - buff, to: to + buff };
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function resetRange({ from, to, bufferSize = 0 }: VuuRange): VuuRange {
|
|
41
|
+
return {
|
|
42
|
+
from: 0,
|
|
43
|
+
to: to - from,
|
|
44
|
+
bufferSize,
|
|
45
|
+
reset: true
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export class WindowRange {
|
|
50
|
+
public from: number;
|
|
51
|
+
public to: number;
|
|
52
|
+
|
|
53
|
+
constructor(from: number, to: number) {
|
|
54
|
+
this.from = from;
|
|
55
|
+
this.to = to;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public isWithin(index: number) {
|
|
59
|
+
return index >= this.from && index < this.to;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
//find the overlap of this range and a new one
|
|
63
|
+
public overlap(from: number, to: number): [number, number] {
|
|
64
|
+
return from >= this.to || to < this.from
|
|
65
|
+
? [0, 0]
|
|
66
|
+
: [Math.max(from, this.from), Math.min(to, this.to)];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
public copy(): WindowRange {
|
|
70
|
+
return new WindowRange(this.from, this.to);
|
|
71
|
+
}
|
|
72
|
+
}
|
package/src/row-utils.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export type RowIndex = {
|
|
2
|
+
[field: string]: number;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
// TODO
|
|
6
|
+
export type Row = {
|
|
7
|
+
[strKey: string]: any;
|
|
8
|
+
} & any[];
|
|
9
|
+
|
|
10
|
+
export function addRowsToIndex(rows: Row[], index: RowIndex, indexField: string) {
|
|
11
|
+
for (let idx = 0, len = rows.length; idx < len; idx++) {
|
|
12
|
+
index[rows[idx][indexField]] = idx;
|
|
13
|
+
}
|
|
14
|
+
return index;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function indexRows(rows: Row[], indexField: string) {
|
|
18
|
+
return addRowsToIndex(rows, {}, indexField);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function isEmptyRow(row: Row) {
|
|
22
|
+
return row[0] === undefined;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// TODO rename
|
|
26
|
+
export function update(rows: Row[], updates: any) { // TODO
|
|
27
|
+
const results = rows.slice();
|
|
28
|
+
const [[offsetIdx]] = rows;
|
|
29
|
+
for (let i = 0; i < updates.length; i++) {
|
|
30
|
+
const idx = updates[i][0] - offsetIdx;
|
|
31
|
+
// slow, refactor for performance
|
|
32
|
+
if (rows[idx]) {
|
|
33
|
+
const row = rows[idx].slice();
|
|
34
|
+
for (let j = 1; j < updates[i].length; j += 3) {
|
|
35
|
+
row[updates[i][j]] = updates[i][j + 2];
|
|
36
|
+
}
|
|
37
|
+
results[idx] = row;
|
|
38
|
+
} else {
|
|
39
|
+
console.log(`row not found in rows`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return results;
|
|
44
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// intended use is singleton storage atom within a hook, see useViewserver for examples
|
|
2
|
+
import { EventEmitter } from "./event-emitter";
|
|
3
|
+
|
|
4
|
+
type StoreStatus = "" | "ready" | "loaded" | "loading";
|
|
5
|
+
|
|
6
|
+
export class SimpleStore<T = unknown> extends EventEmitter {
|
|
7
|
+
private _value: T;
|
|
8
|
+
private _status: StoreStatus = "";
|
|
9
|
+
|
|
10
|
+
constructor(value: T, status: StoreStatus = "") {
|
|
11
|
+
super();
|
|
12
|
+
this._value = value;
|
|
13
|
+
this._status = status;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
set value(value: T) {
|
|
17
|
+
this._value = value;
|
|
18
|
+
this._status = "ready";
|
|
19
|
+
this.emit("loaded", value);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
get value(): T {
|
|
23
|
+
return this._value;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
set status(status: StoreStatus) {
|
|
27
|
+
this._status = status;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
get status() {
|
|
31
|
+
return this._status;
|
|
32
|
+
}
|
|
33
|
+
}
|