@opengeoweb/metronome 19.1.0 → 19.2.0

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.
@@ -0,0 +1 @@
1
+ export * from "./src/index";
@@ -0,0 +1,221 @@
1
+ function _arrayLikeToArray(r, a) {
2
+ (null == a || a > r.length) && (a = r.length);
3
+ for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
4
+ return n;
5
+ }
6
+ function _createForOfIteratorHelperLoose(r, e) {
7
+ var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
8
+ if (t) return (t = t.call(r)).next.bind(t);
9
+ if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e) {
10
+ t && (r = t);
11
+ var o = 0;
12
+ return function () {
13
+ return o >= r.length ? {
14
+ done: true
15
+ } : {
16
+ done: false,
17
+ value: r[o++]
18
+ };
19
+ };
20
+ }
21
+ throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
22
+ }
23
+ function _unsupportedIterableToArray(r, a) {
24
+ if (r) {
25
+ if ("string" == typeof r) return _arrayLikeToArray(r, a);
26
+ var t = {}.toString.call(r).slice(8, -1);
27
+ return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
28
+ }
29
+ }
30
+
31
+ /* *
32
+ * Licensed under the Apache License, Version 2.0 (the "License");
33
+ * you may not use this file except in compliance with the License.
34
+ * You may obtain a copy of the License at
35
+ *
36
+ * http://www.apache.org/licenses/LICENSE-2.0
37
+ *
38
+ * Unless required by applicable law or agreed to in writing, software
39
+ * distributed under the License is distributed on an "AS IS" BASIS,
40
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
41
+ * See the License for the specific language governing permissions and
42
+ * limitations under the License.
43
+ *
44
+ * Copyright 2024 - Koninklijk Nederlands Meteorologisch Instituut (KNMI)
45
+ * Copyright 2024 - Finnish Meteorological Institute (FMI)
46
+ * Copyright 2024 - The Norwegian Meteorological Institute (MET Norway)
47
+ * */
48
+ /**
49
+ * This class should NEVER be initialized with new Metronome(). Use the
50
+ * exported variable. To reset the metronome use unregisterAllTimers()
51
+ * to remove all existing timers followed by init() to initialize the
52
+ * ticks.
53
+ */
54
+ var Metronome = /*#__PURE__*/function () {
55
+ function Metronome() {
56
+ this.tickId = 0;
57
+ this.step = this.step.bind(this);
58
+ this.isPaused = this.isPaused.bind(this);
59
+ this.continueTimer = this.continueTimer.bind(this);
60
+ this.pauseTimer = this.pauseTimer.bind(this);
61
+ this.toggleTimer = this.toggleTimer.bind(this);
62
+ this.init = this.init.bind(this);
63
+ this.unregisterAllTimers = this.unregisterAllTimers.bind(this);
64
+ this.setSpeed = this.setSpeed.bind(this);
65
+ this.interval = null;
66
+ this.init();
67
+ this.handleTimerTicks = null;
68
+ this.timers = [];
69
+ }
70
+ var _proto = Metronome.prototype;
71
+ _proto.init = function init() {
72
+ if (this.interval != null) {
73
+ clearInterval(this.interval);
74
+ }
75
+ this.interval = setInterval(this.step, 10);
76
+ };
77
+ _proto.unregisterAllTimers = function unregisterAllTimers() {
78
+ this.timers.length = 0;
79
+ };
80
+ _proto.step = function step() {
81
+ var _this = this;
82
+ this.tickId += 1;
83
+ var triggeredTimerIds = [];
84
+ this.timers.forEach(function (timer) {
85
+ // Delay in x10 ms
86
+ var delay = 100 / timer.hertz;
87
+ // Get nearest multiple of delay
88
+ var nearestDelay = delay * Math.floor((_this.tickId + delay / 2) / delay);
89
+ var distanceToDelay = _this.tickId - nearestDelay;
90
+ if (Math.abs(distanceToDelay) < 0.5 || distanceToDelay === -0.5) {
91
+ if (!timer.isPaused) {
92
+ timer.timerFunc == null || timer.timerFunc(timer.timerId, _this.tickId);
93
+ triggeredTimerIds.push(timer.timerId);
94
+ }
95
+ }
96
+ });
97
+ // Trigger the handleTimerTicks function with all collected timerIds
98
+ if (triggeredTimerIds.length > 0) {
99
+ var _this$handleTimerTick;
100
+ (_this$handleTimerTick = this.handleTimerTicks) == null || _this$handleTimerTick.call(this, triggeredTimerIds);
101
+ }
102
+ }
103
+ /**
104
+ * Multiple timers with varying frequencies can be made at the same
105
+ * time however each timerId must be unique. If you attempt to create
106
+ * two timers with the same timerId, the second will silently be ignored.
107
+ *
108
+ * @param timerFunc The function to be executed at the given hertz frequency
109
+ * @param hertz Frequency of the timer given in hertz
110
+ * @param timerId Id of the timer
111
+ * @returns Timer or nothing if no timer was created
112
+ */;
113
+ _proto.register = function register(timerFunc, hertz, timerId) {
114
+ var existingTimer = this.timers.find(function (timer) {
115
+ return timer.timerId === timerId;
116
+ });
117
+ if (existingTimer) {
118
+ existingTimer.hertz = hertz;
119
+ existingTimer.timerFunc = timerFunc;
120
+ return existingTimer;
121
+ }
122
+ this.timers.push({
123
+ timerFunc: timerFunc,
124
+ hertz: hertz,
125
+ timerId: timerId,
126
+ isPaused: false
127
+ });
128
+ return this.timers.at(-1);
129
+ }
130
+ /**
131
+ * Unregistering a timer completely removes it, if you want to pause a
132
+ * timer, then use pauseTimer()
133
+ *
134
+ * @param timerId Id of the timer
135
+ */;
136
+ _proto.unregister = function unregister(timerId) {
137
+ var indexToRemove = this.timers.findIndex(function (timer) {
138
+ return timer.timerId === timerId;
139
+ });
140
+ if (indexToRemove > -1) {
141
+ this.timers.splice(indexToRemove, 1);
142
+ }
143
+ }
144
+ /**
145
+ * Checks if a timer is paused
146
+ *
147
+ * @param timerId Id of the timer
148
+ * @returns true if the timer is paused, false if it is running
149
+ */;
150
+ _proto.isPaused = function isPaused(timerId) {
151
+ for (var _iterator = _createForOfIteratorHelperLoose(this.timers), _step; !(_step = _iterator()).done;) {
152
+ var timer = _step.value;
153
+ if (timer.timerId === timerId && timer.isPaused) {
154
+ return true;
155
+ }
156
+ }
157
+ return false;
158
+ }
159
+ /**
160
+ * Continues running a timer
161
+ *
162
+ * @param timerId Id of the timer
163
+ */;
164
+ _proto.continueTimer = function continueTimer(timerId) {
165
+ for (var _iterator2 = _createForOfIteratorHelperLoose(this.timers), _step2; !(_step2 = _iterator2()).done;) {
166
+ var timer = _step2.value;
167
+ if (timer.timerId === timerId) {
168
+ timer.isPaused = false;
169
+ break;
170
+ }
171
+ }
172
+ }
173
+ /**
174
+ * Pauses a timer
175
+ *
176
+ * @param timerId Id of the timer
177
+ */;
178
+ _proto.pauseTimer = function pauseTimer(timerId) {
179
+ for (var _iterator3 = _createForOfIteratorHelperLoose(this.timers), _step3; !(_step3 = _iterator3()).done;) {
180
+ var timer = _step3.value;
181
+ if (timer.timerId === timerId) {
182
+ timer.isPaused = true;
183
+ break;
184
+ }
185
+ }
186
+ }
187
+ /**
188
+ * Toggles a timer to be paused or running. Be careful when asking redux
189
+ * to toggle a timer, it might toggle twice and therefore do nothing
190
+ *
191
+ * @param timerId Id of the timer
192
+ */;
193
+ _proto.toggleTimer = function toggleTimer(timerId) {
194
+ for (var _iterator4 = _createForOfIteratorHelperLoose(this.timers), _step4; !(_step4 = _iterator4()).done;) {
195
+ var timer = _step4.value;
196
+ if (timer.timerId === timerId) {
197
+ timer.isPaused = !timer.isPaused;
198
+ break;
199
+ }
200
+ }
201
+ }
202
+ /**
203
+ * Sets the speed of a timer to a new frequency given in hertz
204
+ *
205
+ * @param timerId Id of the timer
206
+ * @param hertz Frequency of the timer given in hertz
207
+ */;
208
+ _proto.setSpeed = function setSpeed(timerId, hertz) {
209
+ for (var _iterator5 = _createForOfIteratorHelperLoose(this.timers), _step5; !(_step5 = _iterator5()).done;) {
210
+ var timer = _step5.value;
211
+ if (timer.timerId === timerId) {
212
+ timer.hertz = hertz;
213
+ break;
214
+ }
215
+ }
216
+ };
217
+ return Metronome;
218
+ }();
219
+ var metronome = new Metronome();
220
+
221
+ export { metronome };
@@ -0,0 +1 @@
1
+ export { metronome } from './lib/metronome';
@@ -0,0 +1,76 @@
1
+ type TimerFunction = (timerId: string, tickId: number) => void;
2
+ interface Timer {
3
+ timerFunc: TimerFunction | null;
4
+ hertz: number;
5
+ timerId: string;
6
+ isPaused: boolean;
7
+ }
8
+ /**
9
+ * This class should NEVER be initialized with new Metronome(). Use the
10
+ * exported variable. To reset the metronome use unregisterAllTimers()
11
+ * to remove all existing timers followed by init() to initialize the
12
+ * ticks.
13
+ */
14
+ declare class Metronome {
15
+ tickId: number;
16
+ timers: Timer[];
17
+ interval: NodeJS.Timeout | null;
18
+ handleTimerTicks: ((timerIds: string[]) => void) | null;
19
+ constructor();
20
+ init(): void;
21
+ unregisterAllTimers(): void;
22
+ step(): void;
23
+ /**
24
+ * Multiple timers with varying frequencies can be made at the same
25
+ * time however each timerId must be unique. If you attempt to create
26
+ * two timers with the same timerId, the second will silently be ignored.
27
+ *
28
+ * @param timerFunc The function to be executed at the given hertz frequency
29
+ * @param hertz Frequency of the timer given in hertz
30
+ * @param timerId Id of the timer
31
+ * @returns Timer or nothing if no timer was created
32
+ */
33
+ register(timerFunc: TimerFunction | null, hertz: number, timerId: string): Timer | undefined;
34
+ /**
35
+ * Unregistering a timer completely removes it, if you want to pause a
36
+ * timer, then use pauseTimer()
37
+ *
38
+ * @param timerId Id of the timer
39
+ */
40
+ unregister(timerId: string): void;
41
+ /**
42
+ * Checks if a timer is paused
43
+ *
44
+ * @param timerId Id of the timer
45
+ * @returns true if the timer is paused, false if it is running
46
+ */
47
+ isPaused(timerId: string): boolean;
48
+ /**
49
+ * Continues running a timer
50
+ *
51
+ * @param timerId Id of the timer
52
+ */
53
+ continueTimer(timerId: string): void;
54
+ /**
55
+ * Pauses a timer
56
+ *
57
+ * @param timerId Id of the timer
58
+ */
59
+ pauseTimer(timerId: string): void;
60
+ /**
61
+ * Toggles a timer to be paused or running. Be careful when asking redux
62
+ * to toggle a timer, it might toggle twice and therefore do nothing
63
+ *
64
+ * @param timerId Id of the timer
65
+ */
66
+ toggleTimer(timerId: string): void;
67
+ /**
68
+ * Sets the speed of a timer to a new frequency given in hertz
69
+ *
70
+ * @param timerId Id of the timer
71
+ * @param hertz Frequency of the timer given in hertz
72
+ */
73
+ setSpeed(timerId: string, hertz: number): void;
74
+ }
75
+ declare const metronome: Metronome;
76
+ export { metronome };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengeoweb/metronome",
3
- "version": "19.1.0",
3
+ "version": "19.2.0",
4
4
  "description": "GeoWeb metronome library for the opengeoweb project",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -24,6 +24,7 @@
24
24
  },
25
25
  "files": [
26
26
  "dist",
27
+ "!dist/package.json",
27
28
  "README.md",
28
29
  "package.json"
29
30
  ]