UNPKG

@patreon/studio

Version:

Patreon Studio Design System

36 lines 1.39 kB
'use client'; import React, { useMemo } from 'react'; const noop = () => undefined; // Create a context for the increment logger which is a no-op by default // // When this is used in StudioProvider, the incrementLogger prop will be passed in // by the host application const IncrementLoggerContext = React.createContext({ incrementLogger: noop, incrementLoggerOnce: noop, }); function makeOncelogger(incrementLogger) { const cache = new Set(); return (metric, tags) => { const key = metric + JSON.stringify(tags); if (!cache.has(key)) { cache.add(key); incrementLogger(metric, tags); } }; } /** * IncrementLoggerProvider is a component that provides an increment logger to its children. * The increment logger is a function that takes a metric name and optional tags object. */ export function IncrementLoggerProvider({ incrementLogger = noop, children }) { const value = useMemo(() => ({ incrementLogger, incrementLoggerOnce: makeOncelogger(incrementLogger) }), [incrementLogger]); return <IncrementLoggerContext.Provider value={value}>{children}</IncrementLoggerContext.Provider>; } /** * useIncrementLogger is a hook that returns the increment logger function from the context. */ export function useIncrementLogger() { return React.useContext(IncrementLoggerContext); } //# sourceMappingURL=index.jsx.map