{"version":3,"file":"emitter.cjs","names":[],"sources":["../../src/utils/emitter.ts"],"sourcesContent":["export interface DataEmitter {\n\temit: VoidFunction;\n\tsubscribe: ( listener: VoidFunction ) => VoidFunction;\n\tpause: VoidFunction;\n\tresume: VoidFunction;\n\tisPaused: boolean;\n}\n\n/**\n * Create an event emitter.\n *\n * @return The event emitter.\n */\nexport function createEmitter(): DataEmitter {\n\tlet isPaused = false;\n\tlet isPending = false;\n\tconst listeners = new Set< VoidFunction >();\n\t// We use `Array.from` to clone the listeners `Set`. This ensures that we\n\t// don't run a listener that was added as a response to another listener.\n\tlet clonedListeners: VoidFunction[] | null = null;\n\n\tconst notifyListeners = () => {\n\t\tclonedListeners ??= Array.from( listeners );\n\t\tconst currentListeners = clonedListeners;\n\t\tfor ( let i = 0; i < currentListeners.length; i++ ) {\n\t\t\tcurrentListeners[ i ]();\n\t\t}\n\t};\n\n\treturn {\n\t\tget isPaused() {\n\t\t\treturn isPaused;\n\t\t},\n\n\t\tsubscribe( listener ) {\n\t\t\tlisteners.add( listener );\n\t\t\tclonedListeners = null;\n\t\t\treturn () => {\n\t\t\t\tlisteners.delete( listener );\n\t\t\t\tclonedListeners = null;\n\t\t\t};\n\t\t},\n\n\t\tpause() {\n\t\t\tisPaused = true;\n\t\t},\n\n\t\tresume() {\n\t\t\tisPaused = false;\n\t\t\tif ( isPending ) {\n\t\t\t\tisPending = false;\n\t\t\t\tnotifyListeners();\n\t\t\t}\n\t\t},\n\n\t\temit() {\n\t\t\tif ( isPaused ) {\n\t\t\t\tisPending = true;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tnotifyListeners();\n\t\t},\n\t};\n}\n"],"mappings":";;;;;;;;;;;AAaA,SAAgB,gBAA6B;CAC5C,IAAI,WAAW;CACf,IAAI,YAAY;CAChB,MAAM,4BAAY,IAAI,IAAoB;CAG1C,IAAI,kBAAyC;CAE7C,MAAM,wBAAwB;EAC7B,oBAAoB,MAAM,KAAM,SAAU;EAC1C,MAAM,mBAAmB;EACzB,KAAM,IAAI,IAAI,GAAG,IAAI,iBAAiB,QAAQ,KAC7C,iBAAkB,EAAG,CAAC;CAExB;CAEA,OAAO;EACN,IAAI,WAAW;GACd,OAAO;EACR;EAEA,UAAW,UAAW;GACrB,UAAU,IAAK,QAAS;GACxB,kBAAkB;GAClB,aAAa;IACZ,UAAU,OAAQ,QAAS;IAC3B,kBAAkB;GACnB;EACD;EAEA,QAAQ;GACP,WAAW;EACZ;EAEA,SAAS;GACR,WAAW;GACX,IAAK,WAAY;IAChB,YAAY;IACZ,gBAAgB;GACjB;EACD;EAEA,OAAO;GACN,IAAK,UAAW;IACf,YAAY;IACZ;GACD;GACA,gBAAgB;EACjB;CACD;AACD"}