Skip to content

Commit fda64a9

Browse files
committed
Simplify pubsub
1 parent cd2dbe5 commit fda64a9

File tree

3 files changed

+28
-29
lines changed

3 files changed

+28
-29
lines changed

@types/services/pubsub/pubsub.d.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,18 @@ export class PubSubProvider {
1414
*/
1515
$get: () => PubSub;
1616
}
17+
/**
18+
* A lightweight PubSub implementation optimized for
19+
* modern JavaScript engines.
20+
*
21+
* Features:
22+
* - Constant-time subscribe / unsubscribe
23+
* - Minimal memory churn & stable hidden-class shapes
24+
* - Fast publish using flat arrays
25+
* - Preserves listener order
26+
*/
1727
export class PubSub {
1828
static $nonscope: boolean;
19-
/**
20-
* Runs a function asynchronously.
21-
*
22-
* @private
23-
* @param {Function} fn Function to run.
24-
* @param {Object} context Context in which to run the function.
25-
* @param {Array} args Arguments to pass to the function.
26-
*/
27-
private static runAsync;
2829
disposed: boolean;
2930
/**
3031
* The next available subscription key. Internally, this is an index into the

docs/content/docs/service/eventBus.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ A messaging service, backed by an instance of
1111
the original
1212
[Google Closure PubSub](https://google.github.io/closure-library/api/goog.pubsub.PubSub.html)
1313
but uses
14-
[`Promise.resolve()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve)
14+
[`queueMicrotask`](https://developer.mozilla.org/en-US/docs/Web/API/Window/queueMicrotask)
1515
instead of
1616
[`Window.setTimeout()`](https://developer.mozilla.org/en-US/docs/Web/API/Window/setTimeout)
1717
for its async implementation. `$eventBus` allows communication between an

src/services/pubsub/pubsub.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ export class PubSubProvider {
1818
$get = () => this.eventBus;
1919
}
2020

21+
/**
22+
* A lightweight PubSub implementation optimized for
23+
* modern JavaScript engines.
24+
*
25+
* Features:
26+
* - Constant-time subscribe / unsubscribe
27+
* - Minimal memory churn & stable hidden-class shapes
28+
* - Fast publish using flat arrays
29+
* - Preserves listener order
30+
*/
2131
export class PubSub {
2232
static $nonscope = true;
2333
/**
@@ -162,20 +172,6 @@ export class PubSub {
162172
return key;
163173
}
164174

165-
/**
166-
* Runs a function asynchronously.
167-
*
168-
* @private
169-
* @param {Function} fn Function to run.
170-
* @param {Object} context Context in which to run the function.
171-
* @param {Array} args Arguments to pass to the function.
172-
*/
173-
static runAsync(fn, context, args) {
174-
queueMicrotask(() => {
175-
fn.apply(context, args);
176-
});
177-
}
178-
179175
/**
180176
* Unsubscribes a function from a topic. Only deletes the first match found.
181177
* Returns a Boolean indicating whether a subscription was removed.
@@ -263,11 +259,13 @@ export class PubSub {
263259
for (let i = 0, l = keys.length; i < l; i++) {
264260
const key = keys[i];
265261

266-
PubSub.runAsync(
267-
this.subscriptions[key + 1],
268-
this.subscriptions[key + 2],
269-
args,
270-
);
262+
const fn = this.subscriptions[key + 1];
263+
264+
const context = this.subscriptions[key + 2];
265+
266+
queueMicrotask(() => {
267+
fn.apply(context, args);
268+
});
271269
}
272270

273271
return true;

0 commit comments

Comments
 (0)