Skip to content

Commit ed3a787

Browse files
committed
Use microtasks.
1 parent fa442a3 commit ed3a787

File tree

5 files changed

+15
-10
lines changed

5 files changed

+15
-10
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ or
4646
[_Introduction to browserify_](https://writingjavascript.org/posts/introduction-to-browserify).
4747
You will need to create a "UMD bundle" and supply a name (e.g. with the `-s N3` option in browserify).
4848

49-
> **Note:** In some browser environments you may need to include a shim for the [setImmediate](https://developer.mozilla.org/en-US/docs/Web/API/Window/setImmediate) method.
50-
5149
## Creating triples/quads
5250
N3.js follows the [RDF.js low-level specification](http://rdf.js.org/).
5351

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,8 @@
6868
"pre-commit": [
6969
"lint",
7070
"test"
71-
]
71+
],
72+
"dependencies": {
73+
"queue-microtask": "^1.1.2"
74+
}
7275
}

src/N3Lexer.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// **N3Lexer** tokenizes N3 documents.
22
import namespaces from './IRIs';
3-
const { xsd } = namespaces;
3+
import queueMicrotask from 'queue-microtask';
44

5+
const { xsd } = namespaces;
56
const { fromCharCode } = String;
67

78
// Regular expression and replacement string to escape N3 strings.
@@ -437,7 +438,7 @@ export default class N3Lexer {
437438
this._input = input;
438439
// If a callback was passed, asynchronously call it
439440
if (typeof callback === 'function')
440-
setImmediate(function () { self._tokenizeToEnd(callback, true); });
441+
queueMicrotask(() => self._tokenizeToEnd(callback, true));
441442
// If no callback was passed, tokenize synchronously and return
442443
else {
443444
var tokens = [], error;

test/N3Lexer-test.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Lexer } from '../src/';
2+
import queueMicrotask from 'queue-microtask';
23

34
import { EventEmitter } from 'events';
45

@@ -1016,16 +1017,13 @@ function shouldNotTokenize(lexer, input, expectedError) {
10161017
};
10171018
}
10181019

1019-
var immediately = typeof setImmediate === 'function' ? setImmediate :
1020-
function setImmediate(func) { setTimeout(func, 0); };
1021-
10221020
function streamOf() {
10231021
var elements = Array.prototype.slice.call(arguments),
10241022
stream = new EventEmitter();
10251023

10261024
stream.setEncoding = function (encoding) {
10271025
if (encoding === 'utf8')
1028-
immediately(next, 0);
1026+
queueMicrotask(next);
10291027
};
10301028

10311029
function next() {
@@ -1034,7 +1032,7 @@ function streamOf() {
10341032
// use "null" to stall the stream
10351033
if (element !== null) {
10361034
stream.emit('data', element);
1037-
immediately(next, 0);
1035+
queueMicrotask(next);
10381036
}
10391037
}
10401038
else {

0 commit comments

Comments
 (0)