Skip to content

Commit

Permalink
perf(main-thread): Optimise subscribers for notification speed
Browse files Browse the repository at this point in the history
  • Loading branch information
Alorel committed Sep 27, 2020
1 parent 6f9fc43 commit ba142d6
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions projects/core/main-thread/lib/wrapped-store/createSubscribers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,23 @@ type Subscribers<S, A extends Action> = [

/** @internal */
export function createSubscribers<S, A extends Action>(): Subscribers<S, A> {
const subscribers = new Set<SubscribersFn<S, A>>();
const subscribers: Array<SubscribersFn<S, A>> = [];

return [

// subscribe
listener => {
subscribers.add(listener);
function subscribe(listener) {
!subscribers.includes(listener) && subscribers.push(listener);

return () => {
subscribers.delete(listener);
const idx = subscribers.indexOf(listener);
if (idx !== -1) {
subscribers.splice(idx, 1);
}
};
},

// Notify
(action, newState, oldState) => {
if (subscribers.size) {
for (const s of subscribers) {
s(action, newState, oldState);
}
function notifySubscribers(action, newState, oldState) {
for (let i = 0; i < subscribers.length; i++) {
subscribers[i](action, newState, oldState);
}
}
];
Expand Down

0 comments on commit ba142d6

Please sign in to comment.