@stackbit/cms-core 0.0.17 → 0.0.19-alpha.1
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/dist/annotator/html.d.ts +1 -0
- package/dist/annotator/html.d.ts.map +1 -0
- package/dist/annotator/index.d.ts +3 -2
- package/dist/annotator/index.d.ts.map +1 -0
- package/dist/annotator/react.d.ts +1 -0
- package/dist/annotator/react.d.ts.map +1 -0
- package/dist/common/common-schema.d.ts +3 -10
- package/dist/common/common-schema.d.ts.map +1 -0
- package/dist/common/common-schema.js +3 -4
- package/dist/common/common-schema.js.map +1 -1
- package/dist/consts.d.ts +1 -0
- package/dist/consts.d.ts.map +1 -0
- package/dist/content-source-interface.d.ts +324 -0
- package/dist/content-source-interface.d.ts.map +1 -0
- package/dist/content-source-interface.js +28 -0
- package/dist/content-source-interface.js.map +1 -0
- package/dist/content-store-types.d.ts +328 -0
- package/dist/content-store-types.d.ts.map +1 -0
- package/dist/content-store-types.js +3 -0
- package/dist/content-store-types.js.map +1 -0
- package/dist/content-store.d.ts +207 -0
- package/dist/content-store.d.ts.map +1 -0
- package/dist/content-store.js +1743 -0
- package/dist/content-store.js.map +1 -0
- package/dist/encoder.d.ts +36 -7
- package/dist/encoder.d.ts.map +1 -0
- package/dist/encoder.js +63 -40
- package/dist/encoder.js.map +1 -1
- package/dist/index.d.ts +11 -6
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +37 -11
- package/dist/index.js.map +1 -1
- package/dist/stackbit/index.d.ts +7 -3
- package/dist/stackbit/index.d.ts.map +1 -0
- package/dist/stackbit/index.js +13 -10
- package/dist/stackbit/index.js.map +1 -1
- package/dist/utils/index.d.ts +8 -7
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/schema-utils.d.ts +3 -2
- package/dist/utils/schema-utils.d.ts.map +1 -0
- package/dist/utils/timer.d.ts +18 -0
- package/dist/utils/timer.d.ts.map +1 -0
- package/dist/utils/timer.js +36 -0
- package/dist/utils/timer.js.map +1 -0
- package/package.json +8 -4
- package/src/common/common-schema.ts +12 -0
- package/src/content-source-interface.ts +468 -0
- package/src/content-store-types.ts +416 -0
- package/src/content-store.ts +2233 -0
- package/src/{encoder.js → encoder.ts} +55 -17
- package/src/index.ts +10 -0
- package/src/stackbit/{index.js → index.ts} +5 -9
- package/src/utils/timer.ts +42 -0
- package/dist/utils/lazy-poller.d.ts +0 -21
- package/dist/utils/lazy-poller.js +0 -56
- package/dist/utils/lazy-poller.js.map +0 -1
- package/src/common/common-schema.js +0 -14
- package/src/index.js +0 -13
- package/src/utils/lazy-poller.ts +0 -74
|
@@ -1,8 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import _ from 'lodash';
|
|
2
|
+
import * as consts from './consts';
|
|
3
|
+
import { mergeAtPath, omitByNil } from './utils';
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
export type FieldData = Record<string, FieldDataRootItem>;
|
|
6
|
+
|
|
7
|
+
export interface FieldDataItem {
|
|
8
|
+
type: 'object';
|
|
9
|
+
srcObjectLabel: string;
|
|
10
|
+
srcModelName: string;
|
|
11
|
+
srcModelLabel: string;
|
|
12
|
+
fields: Record<string, any>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface FieldDataRootItem extends Omit<FieldDataItem, 'type'> {
|
|
16
|
+
type: 'object' | 'image' | 'file';
|
|
17
|
+
srcType: string;
|
|
18
|
+
srcProjectId: string;
|
|
19
|
+
srcProjectUrl: string;
|
|
20
|
+
srcEnvironment: string;
|
|
21
|
+
srcObjectId: string;
|
|
22
|
+
srcObjectUrl: string;
|
|
23
|
+
isChanged: boolean;
|
|
24
|
+
status: 'modified' | 'added' | 'deleted' | 'published';
|
|
25
|
+
createdAt: string;
|
|
26
|
+
createdBy?: string;
|
|
27
|
+
updatedAt: string;
|
|
28
|
+
updatedBy?: string[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function mapObjectFields({ data, model, fieldDataPath, fieldDataPathsInverted, fieldModelPath, encodeValue, delegate }: any): any {
|
|
6
32
|
if (!fieldModelPath) {
|
|
7
33
|
fieldModelPath = [model.name];
|
|
8
34
|
}
|
|
@@ -26,7 +52,7 @@ function mapObjectFields({ data, model, fieldDataPath, fieldDataPathsInverted, f
|
|
|
26
52
|
return accum;
|
|
27
53
|
}
|
|
28
54
|
|
|
29
|
-
let childFieldModelPath;
|
|
55
|
+
let childFieldModelPath: any;
|
|
30
56
|
if (_.get(model, 'isList') && field.name === 'items') {
|
|
31
57
|
childFieldModelPath = fieldModelPath;
|
|
32
58
|
} else {
|
|
@@ -35,7 +61,7 @@ function mapObjectFields({ data, model, fieldDataPath, fieldDataPathsInverted, f
|
|
|
35
61
|
|
|
36
62
|
const fieldPath = ['fields', field.name];
|
|
37
63
|
|
|
38
|
-
const mapLocalizedField = (accum, localizedField) => {
|
|
64
|
+
const mapLocalizedField = (accum: any, localizedField: any): any => {
|
|
39
65
|
const localePath = fieldModel.localized ? ['locales', localizedField.locale] : [];
|
|
40
66
|
const fullFieldDataPath = _.concat(fieldPath, localePath);
|
|
41
67
|
|
|
@@ -81,7 +107,7 @@ function mapObjectFields({ data, model, fieldDataPath, fieldDataPathsInverted, f
|
|
|
81
107
|
);
|
|
82
108
|
}
|
|
83
109
|
|
|
84
|
-
function mapField({ fieldValue, fieldModel, fieldDataPath, fieldModelPath, delegate }) {
|
|
110
|
+
function mapField({ fieldValue, fieldModel, fieldDataPath, fieldModelPath, delegate }: any): any {
|
|
85
111
|
if (_.includes(consts.SIMPLE_VALUE_FIELDS, fieldModel.type)) {
|
|
86
112
|
return {
|
|
87
113
|
fieldData: { value: fieldValue }
|
|
@@ -92,10 +118,10 @@ function mapField({ fieldValue, fieldModel, fieldDataPath, fieldModelPath, deleg
|
|
|
92
118
|
};
|
|
93
119
|
} else if (fieldModel.type === 'list') {
|
|
94
120
|
const itemsModel = _.get(fieldModel, 'items');
|
|
95
|
-
let getListItemModel;
|
|
121
|
+
let getListItemModel: any;
|
|
96
122
|
if (_.isArray(itemsModel)) {
|
|
97
123
|
// in Sanity, list items may have multiple types, in this case, 'items' will be an array
|
|
98
|
-
getListItemModel = (listItem, fieldModel) => delegate.getItemTypeForListItem(listItem, fieldModel);
|
|
124
|
+
getListItemModel = (listItem: any, fieldModel: any) => delegate.getItemTypeForListItem(listItem, fieldModel);
|
|
99
125
|
} else {
|
|
100
126
|
// get the type of list items, if type is not defined, set string as it is the default
|
|
101
127
|
const listItemsType = _.get(itemsModel, 'type', 'string');
|
|
@@ -124,7 +150,7 @@ function mapField({ fieldValue, fieldModel, fieldDataPath, fieldModelPath, deleg
|
|
|
124
150
|
};
|
|
125
151
|
},
|
|
126
152
|
{
|
|
127
|
-
fieldData: { items: [] }
|
|
153
|
+
fieldData: { items: [] as any[] }
|
|
128
154
|
}
|
|
129
155
|
);
|
|
130
156
|
} else if (fieldModel.type === 'object') {
|
|
@@ -188,7 +214,7 @@ function mapField({ fieldValue, fieldModel, fieldDataPath, fieldModelPath, deleg
|
|
|
188
214
|
}
|
|
189
215
|
}
|
|
190
216
|
|
|
191
|
-
function fieldModelToFieldData(fieldModel, overrides) {
|
|
217
|
+
function fieldModelToFieldData(fieldModel: any, overrides?: any): any {
|
|
192
218
|
const type = ['reference', 'model'].includes(fieldModel.type) ? 'object' : fieldModel.type;
|
|
193
219
|
return omitByNil(
|
|
194
220
|
_.assign(
|
|
@@ -224,7 +250,7 @@ function fieldModelToFieldData(fieldModel, overrides) {
|
|
|
224
250
|
);
|
|
225
251
|
}
|
|
226
252
|
|
|
227
|
-
function getFieldModelNames(fieldModel) {
|
|
253
|
+
function getFieldModelNames(fieldModel: any): any {
|
|
228
254
|
const fieldType = fieldModel.type;
|
|
229
255
|
if (fieldType === 'reference' || fieldType === 'model') {
|
|
230
256
|
return _.clone(_.get(fieldModel, 'models', []));
|
|
@@ -256,7 +282,7 @@ function unsetObject() {
|
|
|
256
282
|
};
|
|
257
283
|
}
|
|
258
284
|
|
|
259
|
-
function unresolvedReference(fieldValue, delegate) {
|
|
285
|
+
function unresolvedReference(fieldValue: any, delegate: any) {
|
|
260
286
|
return {
|
|
261
287
|
fieldData: {
|
|
262
288
|
type: 'unresolved_reference',
|
|
@@ -274,7 +300,7 @@ function unresolvedModel() {
|
|
|
274
300
|
};
|
|
275
301
|
}
|
|
276
302
|
|
|
277
|
-
function mapData(data, prevEncodingResult, delegate) {
|
|
303
|
+
function mapData(data: any, prevEncodingResult: any, delegate: any): any {
|
|
278
304
|
// scan model instances and replace their 'data' with an identity-mapped data
|
|
279
305
|
return _.reduce(
|
|
280
306
|
data,
|
|
@@ -304,12 +330,24 @@ function mapData(data, prevEncodingResult, delegate) {
|
|
|
304
330
|
);
|
|
305
331
|
}
|
|
306
332
|
|
|
307
|
-
|
|
333
|
+
export interface EncodingResult {
|
|
334
|
+
fieldData: FieldData;
|
|
335
|
+
encodedData?: any[];
|
|
336
|
+
hashedData?: Record<string, any>;
|
|
337
|
+
fieldDataPaths?: string[];
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
export interface EncodeDataOptions {
|
|
341
|
+
data: any[];
|
|
342
|
+
prevEncodingResult?: EncodingResult | null;
|
|
343
|
+
delegate: any;
|
|
344
|
+
encodeDelimiter: string;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
export default function encodeData({ data, prevEncodingResult, delegate }: EncodeDataOptions): EncodingResult {
|
|
308
348
|
data = _.cloneDeep(data);
|
|
309
349
|
const mappedData = mapData(data, prevEncodingResult, delegate);
|
|
310
350
|
return {
|
|
311
351
|
fieldData: mappedData.fieldData
|
|
312
352
|
};
|
|
313
353
|
}
|
|
314
|
-
|
|
315
|
-
module.exports = encodeData;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * as stackbit from './stackbit';
|
|
2
|
+
export * as annotator from './annotator';
|
|
3
|
+
export * as utils from './utils';
|
|
4
|
+
export * as consts from './consts';
|
|
5
|
+
export * from './common/common-schema';
|
|
6
|
+
export * from './content-store';
|
|
7
|
+
export * as ContentSourceTypes from './content-source-interface';
|
|
8
|
+
export * as ContentStoreTypes from './content-store-types';
|
|
9
|
+
export { default as encodeData } from './encoder';
|
|
10
|
+
export * from './encoder';
|
|
@@ -1,14 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import _ from 'lodash';
|
|
2
|
+
import { loadConfig, isListDataModel, Config } from '@stackbit/sdk';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
fetchAndConvertSchema
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
function fetchAndConvertSchema(options) {
|
|
4
|
+
export function fetchAndConvertSchema(options: { dirPath: string }): Promise<{ schema: Config; errors: Error[] }> {
|
|
9
5
|
return loadConfig({ dirPath: options.dirPath }).then(({ config, errors }) => {
|
|
10
6
|
if (!config) {
|
|
11
|
-
return { schema: {}, errors };
|
|
7
|
+
return { schema: {} as Config, errors };
|
|
12
8
|
}
|
|
13
9
|
wrapListDataModels(config);
|
|
14
10
|
const schema = _.pick(config, [
|
|
@@ -38,7 +34,7 @@ function fetchAndConvertSchema(options) {
|
|
|
38
34
|
});
|
|
39
35
|
}
|
|
40
36
|
|
|
41
|
-
function wrapListDataModels(config) {
|
|
37
|
+
function wrapListDataModels(config: Config) {
|
|
42
38
|
_.forEach(config.models, (model) => {
|
|
43
39
|
if (!isListDataModel(model)) {
|
|
44
40
|
return;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Logger } from '../content-store-types';
|
|
2
|
+
|
|
3
|
+
export class Timer {
|
|
4
|
+
private readonly timerCallback: () => void;
|
|
5
|
+
private readonly timerMs: number;
|
|
6
|
+
private readonly logger?: Logger;
|
|
7
|
+
private timeout: NodeJS.Timeout | null;
|
|
8
|
+
|
|
9
|
+
constructor({ timerCallback, timerMs = 30 * 60 * 1000, logger }: { timerCallback: () => void; timerMs?: number; logger?: Logger }) {
|
|
10
|
+
this.timerCallback = timerCallback;
|
|
11
|
+
this.timerMs = timerMs;
|
|
12
|
+
this.logger = logger?.createLogger({ label: 'timer' });
|
|
13
|
+
this.timeout = null;
|
|
14
|
+
this.handleTimeout = this.handleTimeout.bind(this);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
isRunning() {
|
|
18
|
+
return !!this.timeout;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
startTimer() {
|
|
22
|
+
this.resetTimer();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
resetTimer() {
|
|
26
|
+
this.stopTimer();
|
|
27
|
+
this.timeout = setTimeout(this.handleTimeout, this.timerMs);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
stopTimer() {
|
|
31
|
+
if (this.timeout) {
|
|
32
|
+
clearTimeout(this.timeout);
|
|
33
|
+
this.timeout = null;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
handleTimeout() {
|
|
38
|
+
this.logger?.debug('timer reached');
|
|
39
|
+
this.timeout = null;
|
|
40
|
+
this.timerCallback();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
export interface LazyPollerOptions<T> {
|
|
2
|
-
notificationCallback: T;
|
|
3
|
-
sleepTimeoutMs?: number;
|
|
4
|
-
pollingIntervalMs?: number;
|
|
5
|
-
logger: any;
|
|
6
|
-
}
|
|
7
|
-
export declare abstract class LazyPoller<T> {
|
|
8
|
-
protected notificationCallback: T;
|
|
9
|
-
private readonly sleepTimeoutMs;
|
|
10
|
-
private readonly pollingIntervalMs;
|
|
11
|
-
private sleepTimeout;
|
|
12
|
-
private pollTimeout;
|
|
13
|
-
private isSleeping;
|
|
14
|
-
private logger;
|
|
15
|
-
protected constructor({ notificationCallback, sleepTimeoutMs, pollingIntervalMs, logger }: LazyPollerOptions<T>);
|
|
16
|
-
resetSleepTimer(): void;
|
|
17
|
-
sleep(): void;
|
|
18
|
-
private setPollTimeout;
|
|
19
|
-
private callPoll;
|
|
20
|
-
protected abstract poll(notificationCallback: T): any;
|
|
21
|
-
}
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.LazyPoller = void 0;
|
|
4
|
-
class LazyPoller {
|
|
5
|
-
constructor({ notificationCallback, sleepTimeoutMs = 30 * 60 * 1000, pollingIntervalMs = 1000, logger }) {
|
|
6
|
-
this.notificationCallback = notificationCallback;
|
|
7
|
-
this.sleepTimeoutMs = sleepTimeoutMs;
|
|
8
|
-
this.pollingIntervalMs = pollingIntervalMs;
|
|
9
|
-
this.logger = logger;
|
|
10
|
-
this.sleepTimeout = null;
|
|
11
|
-
this.pollTimeout = null;
|
|
12
|
-
this.isSleeping = true;
|
|
13
|
-
this.sleep = this.sleep.bind(this);
|
|
14
|
-
this.callPoll = this.callPoll.bind(this);
|
|
15
|
-
}
|
|
16
|
-
resetSleepTimer() {
|
|
17
|
-
if (this.sleepTimeout) {
|
|
18
|
-
clearTimeout(this.sleepTimeout);
|
|
19
|
-
}
|
|
20
|
-
this.sleepTimeout = setTimeout(this.sleep, this.sleepTimeoutMs);
|
|
21
|
-
if (this.isSleeping) {
|
|
22
|
-
this.logger.debug('start polling');
|
|
23
|
-
this.isSleeping = false;
|
|
24
|
-
this.setPollTimeout();
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
sleep() {
|
|
28
|
-
this.logger.debug('sleep');
|
|
29
|
-
if (this.sleepTimeout) {
|
|
30
|
-
clearTimeout(this.sleepTimeout);
|
|
31
|
-
this.sleepTimeout = null;
|
|
32
|
-
}
|
|
33
|
-
this.isSleeping = true;
|
|
34
|
-
if (this.pollTimeout) {
|
|
35
|
-
clearTimeout(this.pollTimeout);
|
|
36
|
-
this.pollTimeout = null;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
setPollTimeout() {
|
|
40
|
-
this.pollTimeout = setTimeout(this.callPoll, this.pollingIntervalMs);
|
|
41
|
-
}
|
|
42
|
-
async callPoll() {
|
|
43
|
-
this.pollTimeout = null;
|
|
44
|
-
return Promise.resolve(this.poll(this.notificationCallback))
|
|
45
|
-
.catch(error => {
|
|
46
|
-
this.logger.error('error in pollCallback', { error: error.message });
|
|
47
|
-
})
|
|
48
|
-
.finally(() => {
|
|
49
|
-
if (!this.isSleeping) {
|
|
50
|
-
this.setPollTimeout();
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
exports.LazyPoller = LazyPoller;
|
|
56
|
-
//# sourceMappingURL=lazy-poller.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"lazy-poller.js","sourceRoot":"","sources":["../../src/utils/lazy-poller.ts"],"names":[],"mappings":";;;AASA,MAAsB,UAAU;IAS5B,YAAsB,EAAE,oBAAoB,EAAE,cAAc,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,EAAE,iBAAiB,GAAG,IAAI,EAAE,MAAM,EAAwB;QACnI,IAAI,CAAC,oBAAoB,GAAG,oBAAoB,CAAC;QACjD,IAAI,CAAC,cAAc,GAAG,cAAc,CAAC;QACrC,IAAI,CAAC,iBAAiB,GAAG,iBAAiB,CAAC;QAC3C,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC;QACzB,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC;QACxB,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC;QACvB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACnC,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC7C,CAAC;IAED,eAAe;QACX,IAAI,IAAI,CAAC,YAAY,EAAE;YACnB,YAAY,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;SACnC;QACD,IAAI,CAAC,YAAY,GAAG,UAAU,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,cAAc,CAAC,CAAC;QAChE,IAAI,IAAI,CAAC,UAAU,EAAE;YACjB,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,eAAe,CAAC,CAAC;YACnC,IAAI,CAAC,UAAU,GAAG,KAAK,CAAC;YACxB,IAAI,CAAC,cAAc,EAAE,CAAC;SACzB;IACL,CAAC;IAED,KAAK;QACD,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QAC3B,IAAI,IAAI,CAAC,YAAY,EAAE;YACnB,YAAY,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;YAChC,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC;SAC5B;QACD,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC;QACvB,IAAI,IAAI,CAAC,WAAW,EAAE;YAClB,YAAY,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;YAC/B,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC;SAC3B;IACL,CAAC;IAEO,cAAc;QAClB,IAAI,CAAC,WAAW,GAAG,UAAU,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,iBAAiB,CAAC,CAAC;IACzE,CAAC;IAEO,KAAK,CAAC,QAAQ;QAClB,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC;QACxB,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;aACvD,KAAK,CAAC,KAAK,CAAC,EAAE;YACX,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,uBAAuB,EAAE,EAAE,KAAK,EAAE,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;QACzE,CAAC,CAAC;aACD,OAAO,CAAC,GAAG,EAAE;YACV,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE;gBAClB,IAAI,CAAC,cAAc,EAAE,CAAC;aACzB;QACL,CAAC,CAAC,CAAC;IACX,CAAC;CAGJ;AAhED,gCAgEC"}
|
package/src/index.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
const encodeData = require('./encoder');
|
|
2
|
-
const stackbit = require('./stackbit');
|
|
3
|
-
const annotator = require('./annotator');
|
|
4
|
-
const utils = require('./utils');
|
|
5
|
-
const consts = require('./consts');
|
|
6
|
-
|
|
7
|
-
module.exports = {
|
|
8
|
-
utils,
|
|
9
|
-
consts,
|
|
10
|
-
annotator,
|
|
11
|
-
encodeData,
|
|
12
|
-
stackbit
|
|
13
|
-
};
|
package/src/utils/lazy-poller.ts
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export interface LazyPollerOptions<T> {
|
|
4
|
-
notificationCallback: T;
|
|
5
|
-
sleepTimeoutMs?: number;
|
|
6
|
-
pollingIntervalMs?: number;
|
|
7
|
-
logger: any;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export abstract class LazyPoller<T> {
|
|
11
|
-
protected notificationCallback: T;
|
|
12
|
-
private readonly sleepTimeoutMs: number;
|
|
13
|
-
private readonly pollingIntervalMs: number;
|
|
14
|
-
private sleepTimeout: null | ReturnType<typeof setTimeout>;
|
|
15
|
-
private pollTimeout: null | ReturnType<typeof setTimeout>;
|
|
16
|
-
private isSleeping: boolean;
|
|
17
|
-
private logger: any;
|
|
18
|
-
|
|
19
|
-
protected constructor({ notificationCallback, sleepTimeoutMs = 30 * 60 * 1000, pollingIntervalMs = 1000, logger }: LazyPollerOptions<T>) {
|
|
20
|
-
this.notificationCallback = notificationCallback;
|
|
21
|
-
this.sleepTimeoutMs = sleepTimeoutMs;
|
|
22
|
-
this.pollingIntervalMs = pollingIntervalMs;
|
|
23
|
-
this.logger = logger;
|
|
24
|
-
this.sleepTimeout = null;
|
|
25
|
-
this.pollTimeout = null;
|
|
26
|
-
this.isSleeping = true;
|
|
27
|
-
this.sleep = this.sleep.bind(this);
|
|
28
|
-
this.callPoll = this.callPoll.bind(this);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
resetSleepTimer() {
|
|
32
|
-
if (this.sleepTimeout) {
|
|
33
|
-
clearTimeout(this.sleepTimeout);
|
|
34
|
-
}
|
|
35
|
-
this.sleepTimeout = setTimeout(this.sleep, this.sleepTimeoutMs);
|
|
36
|
-
if (this.isSleeping) {
|
|
37
|
-
this.logger.debug('start polling');
|
|
38
|
-
this.isSleeping = false;
|
|
39
|
-
this.setPollTimeout();
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
sleep() {
|
|
44
|
-
this.logger.debug('sleep');
|
|
45
|
-
if (this.sleepTimeout) {
|
|
46
|
-
clearTimeout(this.sleepTimeout);
|
|
47
|
-
this.sleepTimeout = null;
|
|
48
|
-
}
|
|
49
|
-
this.isSleeping = true;
|
|
50
|
-
if (this.pollTimeout) {
|
|
51
|
-
clearTimeout(this.pollTimeout);
|
|
52
|
-
this.pollTimeout = null;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
private setPollTimeout() {
|
|
57
|
-
this.pollTimeout = setTimeout(this.callPoll, this.pollingIntervalMs);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
private async callPoll() {
|
|
61
|
-
this.pollTimeout = null;
|
|
62
|
-
return Promise.resolve(this.poll(this.notificationCallback))
|
|
63
|
-
.catch(error => {
|
|
64
|
-
this.logger.error('error in pollCallback', { error: error.message });
|
|
65
|
-
})
|
|
66
|
-
.finally(() => {
|
|
67
|
-
if (!this.isSleeping) {
|
|
68
|
-
this.setPollTimeout();
|
|
69
|
-
}
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
protected abstract poll(notificationCallback: T): any;
|
|
74
|
-
}
|