@webitel/ui-sdk 26.8.130 → 26.8.131
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/package.json +1 -1
- package/src/store/ReactiveNowStoreModule/pinia/ReactiveNowStore.ts +31 -0
- package/src/store/ReactiveNowStoreModule/pinia/__tests__/ReactiveNowStore.spec.ts +53 -0
- package/types/.tsbuildinfo +1 -1
- package/types/store/ReactiveNowStoreModule/pinia/ReactiveNowStore.d.ts +13 -0
package/package.json
CHANGED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { defineStore } from 'pinia';
|
|
2
|
+
import { ref } from 'vue';
|
|
3
|
+
|
|
4
|
+
export const createReactiveNowStore = () => {
|
|
5
|
+
const namespace = 'now';
|
|
6
|
+
|
|
7
|
+
const store = defineStore(namespace, () => {
|
|
8
|
+
const now = ref(Date.now());
|
|
9
|
+
let interval: ReturnType<typeof setInterval> | null = null;
|
|
10
|
+
|
|
11
|
+
const startWatcher = () => {
|
|
12
|
+
if (interval) return;
|
|
13
|
+
interval = setInterval(() => {
|
|
14
|
+
now.value = Date.now();
|
|
15
|
+
}, 1000);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const stopWatcher = () => {
|
|
19
|
+
if (interval) clearInterval(interval);
|
|
20
|
+
interval = null;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
now,
|
|
25
|
+
startWatcher,
|
|
26
|
+
stopWatcher,
|
|
27
|
+
};
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
return store;
|
|
31
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { createPinia, setActivePinia } from 'pinia';
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
3
|
+
|
|
4
|
+
import { createReactiveNowStore } from '../ReactiveNowStore';
|
|
5
|
+
|
|
6
|
+
describe('createReactiveNowStore', () => {
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
setActivePinia(createPinia());
|
|
9
|
+
vi.useFakeTimers();
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
afterEach(() => {
|
|
13
|
+
vi.useRealTimers();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('ticks now every second once started', () => {
|
|
17
|
+
const useNowStore = createReactiveNowStore();
|
|
18
|
+
const store = useNowStore();
|
|
19
|
+
|
|
20
|
+
const initial = store.now;
|
|
21
|
+
store.startWatcher();
|
|
22
|
+
|
|
23
|
+
vi.advanceTimersByTime(3000);
|
|
24
|
+
|
|
25
|
+
expect(store.now).toBeGreaterThan(initial);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('stops ticking once stopped', () => {
|
|
29
|
+
const useNowStore = createReactiveNowStore();
|
|
30
|
+
const store = useNowStore();
|
|
31
|
+
|
|
32
|
+
store.startWatcher();
|
|
33
|
+
store.stopWatcher();
|
|
34
|
+
|
|
35
|
+
const afterStop = store.now;
|
|
36
|
+
vi.advanceTimersByTime(3000);
|
|
37
|
+
|
|
38
|
+
expect(store.now).toBe(afterStop);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('does not stack intervals when started twice', () => {
|
|
42
|
+
const useNowStore = createReactiveNowStore();
|
|
43
|
+
const store = useNowStore();
|
|
44
|
+
|
|
45
|
+
store.startWatcher();
|
|
46
|
+
store.startWatcher();
|
|
47
|
+
|
|
48
|
+
const setIntervalSpy = vi.spyOn(globalThis, 'setInterval');
|
|
49
|
+
store.startWatcher();
|
|
50
|
+
|
|
51
|
+
expect(setIntervalSpy).not.toHaveBeenCalled();
|
|
52
|
+
});
|
|
53
|
+
});
|