@astral/mobx-query 1.15.1 → 1.16.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.
Files changed (42) hide show
  1. package/MobxQuery/MobxQuery.d.ts +13 -8
  2. package/MobxQuery/MobxQuery.js +19 -18
  3. package/MobxQuery/types.d.ts +22 -0
  4. package/MobxQuery/types.js +1 -0
  5. package/PollingService/PollingService.d.ts +34 -0
  6. package/PollingService/PollingService.js +95 -0
  7. package/PollingService/index.d.ts +1 -0
  8. package/PollingService/index.js +1 -0
  9. package/README.md +22 -0
  10. package/SynchronizationService/SynchronizationService.d.ts +5 -5
  11. package/SynchronizationService/SynchronizationService.js +7 -9
  12. package/__test/BroadcastChannel/BroadcastChannel.d.ts +1 -0
  13. package/__test/BroadcastChannel/BroadcastChannel.js +13 -0
  14. package/__test/BroadcastChannel/index.d.ts +1 -0
  15. package/__test/BroadcastChannel/index.js +1 -0
  16. package/__test/documentMock/documentMock.d.ts +380 -0
  17. package/__test/documentMock/documentMock.js +18 -0
  18. package/__test/documentMock/index.d.ts +1 -0
  19. package/__test/documentMock/index.js +1 -0
  20. package/__test/index.d.ts +2 -0
  21. package/__test/index.js +2 -0
  22. package/node/MobxQuery/MobxQuery.d.ts +13 -8
  23. package/node/MobxQuery/MobxQuery.js +19 -18
  24. package/node/MobxQuery/types.d.ts +22 -0
  25. package/node/MobxQuery/types.js +2 -0
  26. package/node/PollingService/PollingService.d.ts +34 -0
  27. package/node/PollingService/PollingService.js +99 -0
  28. package/node/PollingService/index.d.ts +1 -0
  29. package/node/PollingService/index.js +17 -0
  30. package/node/SynchronizationService/SynchronizationService.d.ts +5 -5
  31. package/node/SynchronizationService/SynchronizationService.js +7 -9
  32. package/node/__test/BroadcastChannel/BroadcastChannel.d.ts +1 -0
  33. package/node/__test/BroadcastChannel/BroadcastChannel.js +17 -0
  34. package/node/__test/BroadcastChannel/index.d.ts +1 -0
  35. package/node/__test/BroadcastChannel/index.js +17 -0
  36. package/node/__test/documentMock/documentMock.d.ts +380 -0
  37. package/node/__test/documentMock/documentMock.js +22 -0
  38. package/node/__test/documentMock/index.d.ts +1 -0
  39. package/node/__test/documentMock/index.js +17 -0
  40. package/node/__test/index.d.ts +2 -0
  41. package/node/__test/index.js +18 -0
  42. package/package.json +1 -1
@@ -0,0 +1,34 @@
1
+ import { type AdaptableMap } from '../AdaptableMap';
2
+ import { type KeyHash, type UnknownCachedQuery } from '../MobxQuery/types';
3
+ /**
4
+ * Сущность для инкапсулирования логики работы с устареванием данных
5
+ */
6
+ export declare class PollingService {
7
+ private readonly _queryStorage;
8
+ private readonly _invalidateByKeyHash;
9
+ private readonly _document;
10
+ private timers;
11
+ private timerDates;
12
+ private isVisible;
13
+ constructor(_queryStorage: AdaptableMap<UnknownCachedQuery>, _invalidateByKeyHash: (keyHash: KeyHash) => void, _document?: Document);
14
+ private updateIsVisible;
15
+ private init;
16
+ private handleVisibilityChange;
17
+ private restartPausedTimers;
18
+ private pauseTimers;
19
+ /**
20
+ * Очистка таймеров,
21
+ * предполагается использование либо при срабатывании таймера,
22
+ * либо при досрочном вызове из внешней инвалидации
23
+ */
24
+ clean: (key: KeyHash) => void;
25
+ /**
26
+ * Перезапуск имеющегося таймера, предполагается использование при срабатывании синхронизации между вкладками
27
+ */
28
+ restart: (key: KeyHash) => void;
29
+ private invalidate;
30
+ /**
31
+ * Установка таймера устаревания данных
32
+ */
33
+ setupTimer: (key: KeyHash, pollingTime: number) => void;
34
+ }
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PollingService = void 0;
4
+ /**
5
+ * Сущность для инкапсулирования логики работы с устареванием данных
6
+ */
7
+ class PollingService {
8
+ constructor(_queryStorage, _invalidateByKeyHash, _document = globalThis.document) {
9
+ this._queryStorage = _queryStorage;
10
+ this._invalidateByKeyHash = _invalidateByKeyHash;
11
+ this._document = _document;
12
+ this.timers = new Map();
13
+ this.timerDates = new Map();
14
+ this.isVisible = true;
15
+ this.updateIsVisible = () => {
16
+ var _a;
17
+ this.isVisible = ((_a = this._document) === null || _a === void 0 ? void 0 : _a.visibilityState) === 'visible';
18
+ };
19
+ this.init = () => {
20
+ var _a;
21
+ this.updateIsVisible();
22
+ (_a = this._document) === null || _a === void 0 ? void 0 : _a.addEventListener('visibilitychange', this.handleVisibilityChange);
23
+ };
24
+ this.handleVisibilityChange = () => {
25
+ this.updateIsVisible();
26
+ if (this.isVisible) {
27
+ this.restartPausedTimers();
28
+ }
29
+ else {
30
+ this.pauseTimers();
31
+ }
32
+ };
33
+ this.restartPausedTimers = () => {
34
+ this.timerDates.keys().forEach((key) => {
35
+ const dateNow = Date.now();
36
+ const { pollingTime, targetDate } = this.timerDates.get(key);
37
+ const isAlreadyExpired = targetDate < dateNow;
38
+ if (isAlreadyExpired) {
39
+ this.invalidate(key);
40
+ }
41
+ else {
42
+ this.setupTimer(key, pollingTime);
43
+ }
44
+ });
45
+ };
46
+ this.pauseTimers = () => {
47
+ this.timers.keys().forEach((key) => {
48
+ globalThis.clearTimeout(this.timers.get(key));
49
+ this.timers.delete(key);
50
+ });
51
+ };
52
+ /**
53
+ * Очистка таймеров,
54
+ * предполагается использование либо при срабатывании таймера,
55
+ * либо при досрочном вызове из внешней инвалидации
56
+ */
57
+ this.clean = (key) => {
58
+ globalThis.clearTimeout(this.timers.get(key));
59
+ this.timers.delete(key);
60
+ this.timerDates.delete(key);
61
+ };
62
+ /**
63
+ * Перезапуск имеющегося таймера, предполагается использование при срабатывании синхронизации между вкладками
64
+ */
65
+ this.restart = (key) => {
66
+ const saved = this.timerDates.get(key);
67
+ if (saved) {
68
+ this.setupTimer(key, saved.pollingTime);
69
+ }
70
+ };
71
+ this.invalidate = (key) => {
72
+ const query = this._queryStorage.get(key);
73
+ if (query) {
74
+ this._invalidateByKeyHash(key);
75
+ }
76
+ this.clean(key);
77
+ };
78
+ /**
79
+ * Установка таймера устаревания данных
80
+ */
81
+ this.setupTimer = (key, pollingTime) => {
82
+ if (this.timers.has(key)) {
83
+ globalThis.clearTimeout(this.timers.get(key));
84
+ }
85
+ if (!this._queryStorage.get(key)) {
86
+ return;
87
+ }
88
+ this.timerDates.set(key, {
89
+ pollingTime,
90
+ targetDate: Date.now() + pollingTime,
91
+ });
92
+ if (this.isVisible) {
93
+ this.timers.set(key, globalThis.setTimeout(() => this.invalidate(key), pollingTime));
94
+ }
95
+ };
96
+ this.init();
97
+ }
98
+ }
99
+ exports.PollingService = PollingService;
@@ -0,0 +1 @@
1
+ export * from './PollingService';
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./PollingService"), exports);
@@ -1,16 +1,16 @@
1
1
  import { type DataStorageFactory } from '../DataStorage';
2
+ import { type Keys } from '../MobxQuery/types';
3
+ import { type PollingService } from '../PollingService';
2
4
  import { type StatusStorageFactory } from '../StatusStorage';
3
5
  export declare class SynchronizationService {
4
6
  private readonly _statusStorageFactory;
5
7
  private readonly _dataStorageFactory;
8
+ private readonly _pollingService;
6
9
  private readonly broadcastChannel?;
7
- constructor(_statusStorageFactory: StatusStorageFactory, _dataStorageFactory: DataStorageFactory, _BroadcastChannel?: {
10
+ constructor(_statusStorageFactory: StatusStorageFactory, _dataStorageFactory: DataStorageFactory, _pollingService: PollingService, _BroadcastChannel?: {
8
11
  new (name: string): BroadcastChannel;
9
12
  prototype: BroadcastChannel;
10
13
  });
11
14
  private init;
12
- emit: (keys: {
13
- dataKeyHash: string;
14
- statusKeyHash: string;
15
- }) => void;
15
+ emit: (keys: Keys) => void;
16
16
  }
@@ -2,27 +2,25 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SynchronizationService = void 0;
4
4
  class SynchronizationService {
5
- constructor(_statusStorageFactory, _dataStorageFactory, _BroadcastChannel = globalThis.BroadcastChannel) {
5
+ constructor(_statusStorageFactory, _dataStorageFactory, _pollingService, _BroadcastChannel = globalThis.BroadcastChannel) {
6
6
  this._statusStorageFactory = _statusStorageFactory;
7
7
  this._dataStorageFactory = _dataStorageFactory;
8
+ this._pollingService = _pollingService;
8
9
  this.init = () => {
9
10
  var _a;
10
11
  (_a = this.broadcastChannel) === null || _a === void 0 ? void 0 : _a.addEventListener('message', (event) => {
11
12
  var _a, _b;
13
+ const data = JSON.parse(event.data);
12
14
  (_a = this._dataStorageFactory
13
- .getStorage(event.data.dataKeyHash)) === null || _a === void 0 ? void 0 : _a.setData(event.data.data);
15
+ .getStorage(data.dataKeyHash)) === null || _a === void 0 ? void 0 : _a.setData(data.data);
14
16
  (_b = this._statusStorageFactory
15
- .getStorage(event.data.statusKeyHash)) === null || _b === void 0 ? void 0 : _b.setStatues(event.data.statuses);
17
+ .getStorage(data.statusKeyHash)) === null || _b === void 0 ? void 0 : _b.setStatues(data.statuses);
18
+ this._pollingService.restart(data.queryKeyHash);
16
19
  });
17
20
  };
18
21
  this.emit = (keys) => {
19
22
  var _a, _b, _c;
20
- (_a = this.broadcastChannel) === null || _a === void 0 ? void 0 : _a.postMessage({
21
- dataKeyHash: keys.dataKeyHash,
22
- statusKeyHash: keys.statusKeyHash,
23
- data: (_b = this._dataStorageFactory.getStorage(keys.dataKeyHash)) === null || _b === void 0 ? void 0 : _b.data,
24
- statuses: (_c = this._statusStorageFactory.getStorage(keys.statusKeyHash)) === null || _c === void 0 ? void 0 : _c.statuses,
25
- });
23
+ (_a = this.broadcastChannel) === null || _a === void 0 ? void 0 : _a.postMessage(JSON.stringify(Object.assign(Object.assign({}, keys), { data: (_b = this._dataStorageFactory.getStorage(keys.dataKeyHash)) === null || _b === void 0 ? void 0 : _b.data, statuses: (_c = this._statusStorageFactory.getStorage(keys.statusKeyHash)) === null || _c === void 0 ? void 0 : _c.statuses })));
26
24
  };
27
25
  if (_BroadcastChannel) {
28
26
  this.broadcastChannel = new _BroadcastChannel('@astral/mobx-query');
@@ -0,0 +1 @@
1
+ export declare const createBroadcastChannelMock: () => typeof globalThis.BroadcastChannel;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createBroadcastChannelMock = void 0;
4
+ // Мок не предусматривает полного повторения функционала оригинального BroadcastChannelMock
5
+ // поэтому он будет вызывать даже подписчиков вкладки инициатора,
6
+ // но благодаря общему подходу, ожидается, что это не вызовет проблем
7
+ const createBroadcastChannelMock = () => {
8
+ const listeners = [];
9
+ class BroadcastChannelMock {
10
+ constructor() {
11
+ this.addEventListener = (_, listener) => listeners.push(listener);
12
+ this.postMessage = (data) => listeners.forEach((listener) => listener({ data }));
13
+ }
14
+ }
15
+ return BroadcastChannelMock;
16
+ };
17
+ exports.createBroadcastChannelMock = createBroadcastChannelMock;
@@ -0,0 +1 @@
1
+ export * from './BroadcastChannel';
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./BroadcastChannel"), exports);