UNPKG

3.04 kBSource Map (JSON)View Raw
1{"version":3,"sources":["../src/index.js"],"names":["id","subscribers","log","type","message","data","obj","String","date","Date","dispatch","listen","cb","push","i","indexOf","length","pop","e","console","error","window","__ledgerLogsListen"],"mappings":";;;;;;;AAEA;AACA;AACA;AAWA,IAAIA,EAAE,GAAG,CAAT;AACA,MAAMC,WAAW,GAAG,EAApB;AAEA;AACA;AACA;AACA;AACA;;AACO,MAAMC,GAAG,GAAG,CAACC,IAAD,EAAeC,OAAf,EAAiCC,IAAjC,KAAgD;AACjE,QAAMC,GAAQ,GAAG;AAAEH,IAAAA,IAAF;AAAQH,IAAAA,EAAE,EAAEO,MAAM,CAAC,EAAEP,EAAH,CAAlB;AAA0BQ,IAAAA,IAAI,EAAE,IAAIC,IAAJ;AAAhC,GAAjB;AACA,MAAIL,OAAJ,EAAaE,GAAG,CAACF,OAAJ,GAAcA,OAAd;AACb,MAAIC,IAAJ,EAAUC,GAAG,CAACD,IAAJ,GAAWA,IAAX;AACVK,EAAAA,QAAQ,CAACJ,GAAD,CAAR;AACD,CALM;AAOP;AACA;AACA;AACA;AACA;;;;;AACO,MAAMK,MAAM,GAAIC,EAAD,IAAoC;AACxDX,EAAAA,WAAW,CAACY,IAAZ,CAAiBD,EAAjB;AACA,SAAO,MAAM;AACX,UAAME,CAAC,GAAGb,WAAW,CAACc,OAAZ,CAAoBH,EAApB,CAAV;;AACA,QAAIE,CAAC,KAAK,CAAC,CAAX,EAAc;AACZ;AACAb,MAAAA,WAAW,CAACa,CAAD,CAAX,GAAiBb,WAAW,CAACA,WAAW,CAACe,MAAZ,GAAqB,CAAtB,CAA5B;AACAf,MAAAA,WAAW,CAACgB,GAAZ;AACD;AACF,GAPD;AAQD,CAVM;;;;AAYP,SAASP,QAAT,CAAkBR,GAAlB,EAA4B;AAC1B,OAAK,IAAIY,CAAC,GAAG,CAAb,EAAgBA,CAAC,GAAGb,WAAW,CAACe,MAAhC,EAAwCF,CAAC,EAAzC,EAA6C;AAC3C,QAAI;AACFb,MAAAA,WAAW,CAACa,CAAD,CAAX,CAAeZ,GAAf;AACD,KAFD,CAEE,OAAOgB,CAAP,EAAU;AACVC,MAAAA,OAAO,CAACC,KAAR,CAAcF,CAAd;AACD;AACF;AACF,C,CAED;;;AACA,IAAI,OAAOG,MAAP,KAAkB,WAAtB,EAAmC;AACjCA,EAAAA,MAAM,CAACC,kBAAP,GAA4BX,MAA5B;AACD","sourcesContent":["// @flow\n\n/**\n * A Log object\n */\nexport type Log = {\n type: string,\n message?: string,\n data?: any,\n id: string, // unique amount all logs\n date: Date, // date of the log\n};\n\nexport type Unsubscribe = () => void;\n\nlet id = 0;\nconst subscribers = [];\n\n/**\n * log something\n * @param type a namespaced identifier of the log (it is not a level like \"debug\", \"error\" but more like \"apdu-in\", \"apdu-out\", etc...)\n * @param message a clear message of the log associated to the type\n */\nexport const log = (type: string, message?: string, data?: any) => {\n const obj: Log = { type, id: String(++id), date: new Date() };\n if (message) obj.message = message;\n if (data) obj.data = data;\n dispatch(obj);\n};\n\n/**\n * listen to logs.\n * @param cb that is called for each future log() with the Log object\n * @return a function that can be called to unsubscribe the listener\n */\nexport const listen = (cb: (Log) => void): Unsubscribe => {\n subscribers.push(cb);\n return () => {\n const i = subscribers.indexOf(cb);\n if (i !== -1) {\n // equivalent of subscribers.splice(i, 1) // https://twitter.com/Rich_Harris/status/1125850391155965952\n subscribers[i] = subscribers[subscribers.length - 1];\n subscribers.pop();\n }\n };\n};\n\nfunction dispatch(log: Log) {\n for (let i = 0; i < subscribers.length; i++) {\n try {\n subscribers[i](log);\n } catch (e) {\n console.error(e);\n }\n }\n}\n\n// for debug purpose\nif (typeof window !== \"undefined\") {\n window.__ledgerLogsListen = listen;\n}\n"],"file":"index.js"}
\No newline at end of file