11import { hashContext } from './context' ;
22import EventSummarizer from './EventSummarizer' ;
33/**
4- *
5- * @param {{filter: (context: any) => any} } contextFilter
6- * @param {() => {update: (value: string) => void, digest: (format: string) => Promise<string>} } hasherFactory
4+ *
5+ * @param {{filter: (context: any) => any} } contextFilter
6+ * @param {() => {update: (value: string) => void, digest: (format: string) => Promise<string>} } hasherFactory
77 */
88function MultiEventSummarizer ( contextFilter , hasherFactory ) {
9- let summarizers = { } ;
10- let contexts = { } ;
11- const pendingPromises = [ ] ;
9+ let summarizers = { } ;
10+ let contexts = { } ;
11+ const pendingPromises = [ ] ;
1212
13- /**
14- * Summarize the given event.
15- * @param {{
16- * kind: string,
17- * context?: any,
18- * }} event
19- */
20- function summarizeEvent ( event ) {
21- // This will execute asynchronously, which means that a flush could happen before the event
22- // is summarized. When that happens, then the event will just be in the next batch of summaries.
23- const promise = ( async ( ) => {
24- if ( event . kind === 'feature' ) {
25- const hash = await hashContext ( event . context , hasherFactory ( ) ) ;
26- if ( ! hash ) {
27- return ;
28- }
13+ /**
14+ * Summarize the given event.
15+ * @param {{
16+ * kind: string,
17+ * context?: any,
18+ * }} event
19+ */
20+ function summarizeEvent ( event ) {
21+ // This will execute asynchronously, which means that a flush could happen before the event
22+ // is summarized. When that happens, then the event will just be in the next batch of summaries.
23+ const promise = ( async ( ) => {
24+ if ( event . kind === 'feature' ) {
25+ const hash = await hashContext ( event . context , hasherFactory ( ) ) ;
26+ if ( ! hash ) {
27+ return ;
28+ }
2929
30- let summarizer = summarizers [ hash ] ;
31- if ( ! summarizer ) {
32- summarizers [ hash ] = EventSummarizer ( ) ;
33- summarizer = summarizers [ hash ] ;
34- contexts [ hash ] = event . context ;
35- }
36-
37- summarizer . summarizeEvent ( event ) ;
38- }
39- } ) ( ) ;
40- pendingPromises . push ( promise ) ;
41- promise . finally ( ( ) => {
42- const index = pendingPromises . indexOf ( promise ) ;
43- if ( index !== - 1 ) {
44- pendingPromises . splice ( index , 1 ) ;
45- }
46- } ) ;
47- }
30+ let summarizer = summarizers [ hash ] ;
31+ if ( ! summarizer ) {
32+ summarizers [ hash ] = EventSummarizer ( ) ;
33+ summarizer = summarizers [ hash ] ;
34+ contexts [ hash ] = event . context ;
35+ }
4836
49- /**
50- * Get the summaries of the events that have been summarized.
51- * @returns {any[] }
52- */
53- async function getSummaries ( ) {
54- // Wait for any pending summarizations to complete
55- // Additional tasks queued while waiting will not be waited for.
56- await Promise . all ( [ ...pendingPromises ] ) ;
37+ summarizer . summarizeEvent ( event ) ;
38+ }
39+ } ) ( ) ;
40+ pendingPromises . push ( promise ) ;
41+ promise . finally ( ( ) => {
42+ const index = pendingPromises . indexOf ( promise ) ;
43+ if ( index !== - 1 ) {
44+ pendingPromises . splice ( index , 1 ) ;
45+ }
46+ } ) ;
47+ }
5748
58- const summarizersToFlush = summarizers ;
59- const contextsForSummaries = contexts ;
49+ /**
50+ * Get the summaries of the events that have been summarized.
51+ * @returns {any[] }
52+ */
53+ async function getSummaries ( ) {
54+ // Wait for any pending summarizations to complete
55+ // Additional tasks queued while waiting will not be waited for.
56+ await Promise . all ( [ ...pendingPromises ] ) ;
6057
61- summarizers = { } ;
62- contexts = { } ;
63- return Object . entries ( summarizersToFlush ) . map ( ( [ hash , summarizer ] ) => {
64- const summary = summarizer . getSummary ( ) ;
65- summary . context = contextFilter . filter ( contextsForSummaries [ hash ] ) ;
66- return summary ;
67- } ) ;
68- }
58+ const summarizersToFlush = summarizers ;
59+ const contextsForSummaries = contexts ;
6960
70- return {
71- summarizeEvent,
72- getSummaries
73- } ;
61+ summarizers = { } ;
62+ contexts = { } ;
63+ return Object . entries ( summarizersToFlush ) . map ( ( [ hash , summarizer ] ) => {
64+ const summary = summarizer . getSummary ( ) ;
65+ summary . context = contextFilter . filter ( contextsForSummaries [ hash ] ) ;
66+ return summary ;
67+ } ) ;
68+ }
69+
70+ return {
71+ summarizeEvent,
72+ getSummaries,
73+ } ;
7474}
7575
76- module . exports = MultiEventSummarizer ;
76+ module . exports = MultiEventSummarizer ;
0 commit comments