UNPKG

1.51 kBJavaScriptView Raw
1// @flow
2
3/**
4 * A Log object
5 */
6export type Log = {
7 type: string,
8 message?: string,
9 data?: any,
10 id: string, // unique amount all logs
11 date: Date, // date of the log
12};
13
14export type Unsubscribe = () => void;
15
16let id = 0;
17const subscribers = [];
18
19/**
20 * log something
21 * @param type a namespaced identifier of the log (it is not a level like "debug", "error" but more like "apdu-in", "apdu-out", etc...)
22 * @param message a clear message of the log associated to the type
23 */
24export const log = (type: string, message?: string, data?: any) => {
25 const obj: Log = { type, id: String(++id), date: new Date() };
26 if (message) obj.message = message;
27 if (data) obj.data = data;
28 dispatch(obj);
29};
30
31/**
32 * listen to logs.
33 * @param cb that is called for each future log() with the Log object
34 * @return a function that can be called to unsubscribe the listener
35 */
36export const listen = (cb: (Log) => void): Unsubscribe => {
37 subscribers.push(cb);
38 return () => {
39 const i = subscribers.indexOf(cb);
40 if (i !== -1) {
41 // equivalent of subscribers.splice(i, 1) // https://twitter.com/Rich_Harris/status/1125850391155965952
42 subscribers[i] = subscribers[subscribers.length - 1];
43 subscribers.pop();
44 }
45 };
46};
47
48function dispatch(log: Log) {
49 for (let i = 0; i < subscribers.length; i++) {
50 try {
51 subscribers[i](log);
52 } catch (e) {
53 console.error(e);
54 }
55 }
56}
57
58// for debug purpose
59if (typeof window !== "undefined") {
60 window.__ledgerLogsListen = listen;
61}