@hawksightco/hawk-sdk 0.0.50 → 0.0.51
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.
|
@@ -1,9 +1,32 @@
|
|
|
1
|
-
import { Token } from "../types";
|
|
1
|
+
import { SearchIndices, Token } from "../types";
|
|
2
|
+
export interface SearchTokenStore {
|
|
3
|
+
tokenIndices: SearchIndices;
|
|
4
|
+
tokens: Token[];
|
|
5
|
+
}
|
|
6
|
+
export type LoadFromPersistenceFn = () => Promise<[SearchIndices | undefined, Token[] | undefined]>;
|
|
7
|
+
export type StoreToPersistenceFn = (tokenIndices: SearchIndices, tokens: Token[]) => Promise<void>;
|
|
2
8
|
export declare class Search {
|
|
3
9
|
protected readonly url: string;
|
|
4
10
|
private tokenIndices?;
|
|
5
11
|
private tokens?;
|
|
6
12
|
private _loaded;
|
|
13
|
+
private _loadedFromPersistence;
|
|
14
|
+
private previousHash;
|
|
15
|
+
private latestHash;
|
|
16
|
+
private loadFromPersistenceFn?;
|
|
17
|
+
private storeToPersistenceFn?;
|
|
18
|
+
/**
|
|
19
|
+
* Set load persistence callback
|
|
20
|
+
*
|
|
21
|
+
* @param loadFromPersistenceFn
|
|
22
|
+
*/
|
|
23
|
+
setLoadFromPersistenceFn(loadFromPersistenceFn: LoadFromPersistenceFn): void;
|
|
24
|
+
/**
|
|
25
|
+
* Set store to persistence callback
|
|
26
|
+
*
|
|
27
|
+
* @param storeToPersistenceFn
|
|
28
|
+
*/
|
|
29
|
+
setStoreToPersistenceFn(storeToPersistenceFn: StoreToPersistenceFn): void;
|
|
7
30
|
/**
|
|
8
31
|
* Checks if the token indices and tokens have been loaded.
|
|
9
32
|
*
|
|
@@ -37,6 +60,14 @@ export declare class Search {
|
|
|
37
60
|
* @returns {Promise<void>} A promise that resolves when the initial load is complete.
|
|
38
61
|
*/
|
|
39
62
|
load(): Promise<void>;
|
|
63
|
+
/**
|
|
64
|
+
* Load tokens and indices from persistence
|
|
65
|
+
*
|
|
66
|
+
* @param tokenIndices
|
|
67
|
+
* @param tokens
|
|
68
|
+
* @returns
|
|
69
|
+
*/
|
|
70
|
+
private loadFromPersistence;
|
|
40
71
|
/**
|
|
41
72
|
* Retrieves and updates the token indices and tokens if necessary.
|
|
42
73
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Search.d.ts","sourceRoot":"","sources":["../../../src/classes/Search.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"Search.d.ts","sourceRoot":"","sources":["../../../src/classes/Search.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAGhD,MAAM,WAAW,gBAAgB;IAC/B,YAAY,EAAE,aAAa,CAAC;IAC5B,MAAM,EAAE,KAAK,EAAE,CAAC;CACjB;AAED,MAAM,MAAM,qBAAqB,GAAG,MAAM,OAAO,CAAC,CAAC,aAAa,GAAG,SAAS,EAAE,KAAK,EAAE,GAAG,SAAS,CAAC,CAAC,CAAC;AACpG,MAAM,MAAM,oBAAoB,GAAG,CAAC,YAAY,EAAE,aAAa,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;AAEnG,qBAAa,MAAM;IA2Cf,SAAS,CAAC,QAAQ,CAAC,GAAG,EAAE,MAAM;IAzChC,OAAO,CAAC,YAAY,CAAC,CAAgB;IACrC,OAAO,CAAC,MAAM,CAAC,CAAU;IACzB,OAAO,CAAC,OAAO,CAAkB;IACjC,OAAO,CAAC,sBAAsB,CAAS;IACvC,OAAO,CAAC,YAAY,CAAc;IAClC,OAAO,CAAC,UAAU,CAAc;IAEhC,OAAO,CAAC,qBAAqB,CAAC,CAAwB;IACtD,OAAO,CAAC,oBAAoB,CAAC,CAAuB;IAEpD;;;;OAIG;IACH,wBAAwB,CAAC,qBAAqB,EAAE,qBAAqB;IAIrE;;;;OAIG;IACH,uBAAuB,CAAC,oBAAoB,EAAE,oBAAoB;IAIlE;;;;OAIG;IACH,IAAI,MAAM,IAAI,OAAO,CAAwB;IAE7C;;;;OAIG;gBAEkB,GAAG,EAAE,MAAM;IAGhC;;;;;;;;;;OAUG;IACH,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,GAAE,MAAY,EAAE,OAAO,GAAE,OAAe,GAAG,KAAK,EAAE;IAiB9E;;;;;;;OAOG;IACG,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IA2B3B;;;;;;OAMG;YACW,mBAAmB;IAWjC;;;;;;;;;;OAUG;YACW,eAAe;IAmB7B;;;;;;;OAOG;YACW,YAAY;IAI1B;;;;;;;;;;OAUG;YACW,gBAAgB;CAO/B"}
|
|
@@ -14,7 +14,24 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.Search = void 0;
|
|
16
16
|
const axios_1 = __importDefault(require("axios"));
|
|
17
|
+
;
|
|
17
18
|
class Search {
|
|
19
|
+
/**
|
|
20
|
+
* Set load persistence callback
|
|
21
|
+
*
|
|
22
|
+
* @param loadFromPersistenceFn
|
|
23
|
+
*/
|
|
24
|
+
setLoadFromPersistenceFn(loadFromPersistenceFn) {
|
|
25
|
+
this.loadFromPersistenceFn = loadFromPersistenceFn;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Set store to persistence callback
|
|
29
|
+
*
|
|
30
|
+
* @param storeToPersistenceFn
|
|
31
|
+
*/
|
|
32
|
+
setStoreToPersistenceFn(storeToPersistenceFn) {
|
|
33
|
+
this.storeToPersistenceFn = storeToPersistenceFn;
|
|
34
|
+
}
|
|
18
35
|
/**
|
|
19
36
|
* Checks if the token indices and tokens have been loaded.
|
|
20
37
|
*
|
|
@@ -29,6 +46,9 @@ class Search {
|
|
|
29
46
|
constructor(url) {
|
|
30
47
|
this.url = url;
|
|
31
48
|
this._loaded = false;
|
|
49
|
+
this._loadedFromPersistence = false;
|
|
50
|
+
this.previousHash = '';
|
|
51
|
+
this.latestHash = '';
|
|
32
52
|
}
|
|
33
53
|
/**
|
|
34
54
|
* Retrieves tokens that match the given keyword.
|
|
@@ -70,14 +90,52 @@ class Search {
|
|
|
70
90
|
return __awaiter(this, void 0, void 0, function* () {
|
|
71
91
|
if (!this.loaded) {
|
|
72
92
|
this._loaded = true;
|
|
93
|
+
let firstRun = true;
|
|
73
94
|
const update = () => __awaiter(this, void 0, void 0, function* () {
|
|
74
95
|
yield this.getTokenIndices();
|
|
75
|
-
setTimeout(update,
|
|
96
|
+
setTimeout(update, 60 * 1000);
|
|
97
|
+
});
|
|
98
|
+
const updateFromPersistence = () => __awaiter(this, void 0, void 0, function* () {
|
|
99
|
+
yield this.loadFromPersistence();
|
|
100
|
+
if (firstRun && this.loadFromPersistenceFn !== undefined) {
|
|
101
|
+
yield update();
|
|
102
|
+
firstRun = false;
|
|
103
|
+
}
|
|
104
|
+
if (!!this.tokens && !!this.tokenIndices) {
|
|
105
|
+
if (!!this.storeToPersistenceFn && this.tokens.length > 0 && this.previousHash !== this.latestHash) {
|
|
106
|
+
yield this.storeToPersistenceFn(this.tokenIndices, this.tokens);
|
|
107
|
+
}
|
|
108
|
+
setTimeout(updateFromPersistence, 60 * 1000);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
setTimeout(updateFromPersistence, 1 * 1000);
|
|
76
112
|
});
|
|
77
|
-
yield
|
|
113
|
+
yield updateFromPersistence();
|
|
78
114
|
}
|
|
79
115
|
});
|
|
80
116
|
}
|
|
117
|
+
/**
|
|
118
|
+
* Load tokens and indices from persistence
|
|
119
|
+
*
|
|
120
|
+
* @param tokenIndices
|
|
121
|
+
* @param tokens
|
|
122
|
+
* @returns
|
|
123
|
+
*/
|
|
124
|
+
loadFromPersistence() {
|
|
125
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
126
|
+
if (this._loadedFromPersistence)
|
|
127
|
+
return;
|
|
128
|
+
if (this.loadFromPersistenceFn === undefined)
|
|
129
|
+
return;
|
|
130
|
+
const [tokenIndices, tokens] = yield this.loadFromPersistenceFn();
|
|
131
|
+
if (tokenIndices === undefined || tokens === undefined)
|
|
132
|
+
return;
|
|
133
|
+
this.tokenIndices = tokenIndices;
|
|
134
|
+
this.tokens = tokens;
|
|
135
|
+
this.latestHash = tokenIndices.hash;
|
|
136
|
+
this._loadedFromPersistence = true;
|
|
137
|
+
});
|
|
138
|
+
}
|
|
81
139
|
/**
|
|
82
140
|
* Retrieves and updates the token indices and tokens if necessary.
|
|
83
141
|
*
|
|
@@ -91,16 +149,19 @@ class Search {
|
|
|
91
149
|
*/
|
|
92
150
|
getTokenIndices() {
|
|
93
151
|
return __awaiter(this, void 0, void 0, function* () {
|
|
94
|
-
if (this.tokenIndices === undefined) {
|
|
152
|
+
if (this.tokenIndices === undefined || this.latestHash === '') {
|
|
95
153
|
this.tokenIndices = yield this.getSearchIndices('tokens');
|
|
96
|
-
}
|
|
97
|
-
if (this.tokens === undefined) {
|
|
98
154
|
this.tokens = yield this.getAllTokens();
|
|
155
|
+
this.previousHash = '';
|
|
156
|
+
this.latestHash = this.tokenIndices.hash;
|
|
157
|
+
return;
|
|
99
158
|
}
|
|
100
159
|
const tokenIndices = yield this.getSearchIndices('tokens', this.tokenIndices.hash);
|
|
160
|
+
this.previousHash = this.tokenIndices.hash;
|
|
161
|
+
this.latestHash = tokenIndices.hash;
|
|
101
162
|
// If hash doesn't match, it means there's a new token.
|
|
102
163
|
if (tokenIndices.hash !== this.tokenIndices.hash) {
|
|
103
|
-
this.tokenIndices =
|
|
164
|
+
this.tokenIndices = tokenIndices;
|
|
104
165
|
this.tokens = yield this.getAllTokens();
|
|
105
166
|
}
|
|
106
167
|
});
|