worker-timers 7.0.53 → 7.0.54

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 (2) hide show
  1. package/README.md +20 -48
  2. package/package.json +5 -5
package/README.md CHANGED
@@ -8,27 +8,20 @@
8
8
 
9
9
  ## Motivation
10
10
 
11
- For scripts that rely on [WindowTimers](http://www.w3.org/TR/html5/webappapis.html#timers) like
12
- setInterval() or setTimeout() things get confusing when the site which the script is running on
13
- loses focus. Chrome, Firefox and maybe others throttle the frequency of firing those timers to a
14
- maximum of once per second in such a situation. However this is only true for the main thread and
15
- does not affect the behavior of [Web Workers](http://www.w3.org/TR/workers/). Therefore it is
16
- possible to avoid the throttling by using a worker to do the actual scheduling. This is exactly what
17
- WorkerTimers do.
11
+ For scripts that rely on [WindowTimers](http://www.w3.org/TR/html5/webappapis.html#timers) like `setInterval()` or `setTimeout()` things get confusing when the site which the script is running on loses focus. Chrome, Firefox and maybe others throttle the frequency at which they invoke those timers to a maximum of once per second in such a situation. However this is only true for the main thread and does not affect the behavior of [Web Workers](http://www.w3.org/TR/workers/). Therefore it is possible to avoid the throttling by using a worker to do the actual scheduling. This is exactly what `worker-timers` does.
18
12
 
19
13
  ## Getting Started
20
14
 
21
- WorkerTimers are available as a package on [npm](https://www.npmjs.org/package/worker-timers).
22
- Simply run the following command to install it:
15
+ `worker-timers` is available as a package on [npm](https://www.npmjs.org/package/worker-timers). Run the following command to install it:
23
16
 
24
17
  ```shell
25
18
  npm install worker-timers
26
19
  ```
27
20
 
28
- You can then require the workerTimers instance from within your code like this:
21
+ You can then import the exported functions in your code like this:
29
22
 
30
23
  ```js
31
- import * as workerTimers from 'worker-timers';
24
+ import { clearInterval, clearTimeout, setInterval, setTimeout } from 'worker-timers';
32
25
  ```
33
26
 
34
27
  The usage is exactly the same (despite of the [error handling](#error-handling) and the
@@ -36,40 +29,34 @@ The usage is exactly the same (despite of the [error handling](#error-handling)
36
29
  as with the corresponding functions on the global scope.
37
30
 
38
31
  ```js
39
- var intervalId = workerTimers.setInterval(() => {
32
+ var intervalId = setInterval(() => {
40
33
  // do something many times
41
34
  }, 100);
42
35
 
43
- workerTimers.clearInterval(intervalId);
36
+ clearInterval(intervalId);
44
37
 
45
- var timeoutId = workerTimers.setTimeout(() => {
38
+ var timeoutId = setTimeout(() => {
46
39
  // do something once
47
40
  }, 100);
48
41
 
49
- workerTimers.clearTimeout(timeoutId);
42
+ clearTimeout(timeoutId);
50
43
  ```
51
44
 
52
45
  ## Error Handling
53
46
 
54
- The native WindowTimers are very forgiving. Calling `clearInterval()` or `clearTimeout()` without
55
- a value or with an id which doesn't exist will just get ignored. In contrast to that workerTimers
56
- will throw an error when doing so.
47
+ The native WindowTimers are very forgiving. Calling `clearInterval()` or `clearTimeout()` without a value or with an id which doesn't exist will get ignored. In contrast to that `worker-timers` will throw an error when doing so.
57
48
 
58
49
  ```js
59
- // This will just return undefined.
60
- window.clearTimeout('not-an-timeout-id');
50
+ // This will return undefined.
51
+ window.clearTimeout('not-a-timeout-id');
61
52
 
62
53
  // This will throw an error.
63
- workerTimers.clearTimeout('not-an-timeout-id');
54
+ clearTimeout('not-a-timeout-id');
64
55
  ```
65
56
 
66
57
  ## Differentiation between Intervals and Timeouts
67
58
 
68
- Another difference between workerTimers and WindowTimers is that this package maintains two
69
- separate lists to store the ids of intervals and timeouts internally. WindowTimers do only have one
70
- list which allows intervals to be cancelled by calling `clearTimeout()` and the other way round.
71
- This is not possible with workerTimers. As mentioned above workerTimers will throw an error when
72
- provided with an unknown id.
59
+ Another difference between `worker-timers` and WindowTimers is that this package maintains two separate lists to store the ids of intervals and timeouts internally. WindowTimers do only have one list which allows intervals to be cancelled by calling `clearTimeout()` and the other way round. This is not possible with `worker-timers`. As mentioned above `worker-timers` will throw an error when provided with an unknown id.
73
60
 
74
61
  ```js
75
62
  const periodicWork = () => {};
@@ -79,29 +66,14 @@ const windowId = window.setInterval(periodicWork, 100);
79
66
  window.clearTimeout(windowId);
80
67
 
81
68
  // This will throw an error.
82
- const workerId = workerTimers.setInterval(periodicWork, 100);
83
- workerTimers.clearTimeout(workerId);
69
+ const workerId = setInterval(periodicWork, 100);
70
+ clearTimeout(workerId);
84
71
  ```
85
72
 
86
73
  ## Server-Side Rendering
87
74
 
88
- This package is intended to be used in the browser and requires the browser to have [support for
89
- Web Workers](https://caniuse.com/#feat=webworkers). It does not contain any fallback which would
90
- allow it to run in another environment like Node.js which doesn't know about Web Workers. This is to
91
- prevent this package from silently failing in an unsupported browser. But it also means that it
92
- needs to be replaced when used in a web project which also supports server-side rendering. That
93
- should be easy, at least in theory, because each function has the exact same signature as its
94
- corresponding builtin function. But the configuration of a real-life project can of course be
95
- tricky. For a concrete example, please have a look at the
96
- [worker-timers-ssr-example](https://github.com/newyork-anthonyng/worker-timers-ssr-example)
97
- provided by [@newyork-anthonyng](https://github.com/newyork-anthonyng). It shows the usage inside
98
- of a server-side rendered React app.
99
-
100
- ## Angular (& zone.js)
101
-
102
- If WorkerTimers are used inside of an Angular App and Zones are used to detect changes, the
103
- behavior of WorkerTimers can be confusing. Angular is using a Zone which is patching the native
104
- setInterval() and setTimeout() functions to get notified about the execution of their callback
105
- functions. But Angular (more specifically zone.js) is not aware of WorkerTimers and doesn't patch
106
- them. Therefore Angular needs to be notified manually about state changes that occur inside of a
107
- callback function which was scheduled with the help of WorkerTimers.
75
+ This package is intended to be used in the browser and requires the browser to have [support for Web Workers](https://caniuse.com/#feat=webworkers). It does not contain any fallback which would allow it to run in another environment like Node.js which doesn't know about Web Workers. This is to prevent this package from silently failing in an unsupported browser. But it also means that it needs to be replaced when used in a web project which also supports server-side rendering. The replacement should be straightforward, at least in theory, because each function has the exact same signature as its corresponding builtin function. But the configuration of a real-life project can be tricky. For a concrete example, please have a look at the [worker-timers-ssr-example](https://github.com/newyork-anthonyng/worker-timers-ssr-example) provided by [@newyork-anthonyng](https://github.com/newyork-anthonyng). It shows the usage inside of a server-side rendered React app.
76
+
77
+ ## Angular (& Zone.js)
78
+
79
+ If `worker-timers` is used inside of an Angular app and Zone.js (which is the default) is used to detect changes, the behavior of `worker-timers` can be confusing. Angular is using Zone.js which is patching the native `setInterval()` and `setTimeout()` functions to get notified about the invocation of their callback functions. But Angular (more specifically Zone.js) is not aware of `worker-timers` and doesn't get notified about any callback invocations. Therefore Angular needs to be notified manually about state changes that occur inside of a callback function which was scheduled with the help of `worker-timers`.
package/package.json CHANGED
@@ -17,7 +17,7 @@
17
17
  "dependencies": {
18
18
  "@babel/runtime": "^7.19.0",
19
19
  "tslib": "^2.4.0",
20
- "worker-timers-broker": "^6.0.75",
20
+ "worker-timers-broker": "^6.0.76",
21
21
  "worker-timers-worker": "^7.0.42"
22
22
  },
23
23
  "description": "A replacement for setInterval() and setTimeout() which works in unfocused windows.",
@@ -35,7 +35,7 @@
35
35
  "commitizen": "^4.2.5",
36
36
  "cz-conventional-changelog": "^3.3.0",
37
37
  "eslint": "^8.23.0",
38
- "eslint-config-holy-grail": "^52.0.31",
38
+ "eslint-config-holy-grail": "^52.0.32",
39
39
  "grunt": "^1.5.3",
40
40
  "grunt-cli": "^1.4.3",
41
41
  "grunt-sh": "^0.2.0",
@@ -59,9 +59,9 @@
59
59
  "sinon-chai": "^3.7.0",
60
60
  "terser-webpack-plugin": "^5.3.6",
61
61
  "ts-loader": "^9.3.1",
62
- "tsconfig-holy-grail": "^11.1.35",
62
+ "tsconfig-holy-grail": "^11.1.36",
63
63
  "tslint": "^6.1.3",
64
- "tslint-config-holy-grail": "^53.2.32",
64
+ "tslint-config-holy-grail": "^53.2.33",
65
65
  "typescript": "^4.8.2",
66
66
  "webpack": "^5.74.0",
67
67
  "webpack-cli": "^4.10.0"
@@ -95,5 +95,5 @@
95
95
  "test": "grunt lint && grunt test"
96
96
  },
97
97
  "types": "build/es2019/module.d.ts",
98
- "version": "7.0.53"
98
+ "version": "7.0.54"
99
99
  }