Skip to content

Commit

Permalink
add comment to explain nextTick/setImmediate
Browse files Browse the repository at this point in the history
  • Loading branch information
owenpearson committed Jan 8, 2021
1 parent 9abeb51 commit 7b9bf35
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/lib/AssetConnection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Ably, { Types as AblyTypes } from 'ably';
import { ClientTypes, LocationListener, Resolution, StatusListener } from '../types';
import Logger from './utils/Logger';
import { nextTick } from './utils/utils';
import { setImmediate } from './utils/utils';

enum EventNames {
Raw = 'raw',
Expand Down Expand Up @@ -67,7 +67,7 @@ class AssetConnection {
this.channel.subscribe(EventNames.Raw, (message) => {
const parsedMessage = typeof message.data === 'string' ? JSON.parse(message.data) : message.data;
if (Array.isArray(parsedMessage)) {
parsedMessage.forEach((msg) => nextTick(() => rawLocationListener(msg)));
parsedMessage.forEach((msg) => setImmediate(() => rawLocationListener(msg)));
} else {
rawLocationListener(parsedMessage);
}
Expand All @@ -78,9 +78,9 @@ class AssetConnection {
this.channel.subscribe(EventNames.Enhanced, (message) => {
const parsedMessage = typeof message.data === 'string' ? JSON.parse(message.data) : message.data;
if (Array.isArray(parsedMessage)) {
parsedMessage.forEach((msg) => nextTick(() => enhancedLocationListener(msg)));
parsedMessage.forEach((msg) => setImmediate(() => enhancedLocationListener(msg)));
} else {
nextTick(() => enhancedLocationListener(parsedMessage));
setImmediate(() => enhancedLocationListener(parsedMessage));
}
});
};
Expand Down
4 changes: 3 additions & 1 deletion src/lib/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const nextTick = (fn: () => unknown): void => {
// simple implementation of window.setImmediate or NodeJS process.nextTick
// used to execute user-provided callbacks asynchronously
export const setImmediate = (fn: () => unknown): void => {
setTimeout(fn, 0);
};

0 comments on commit 7b9bf35

Please sign in to comment.