UNPKG

@polkadot/react-query

Version:

A collection of RxJs React components the Polkadot JS API

60 lines (49 loc) 1.8 kB
"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); Object.defineProperty(exports, "__esModule", { value: true }); exports.Events = Events; exports.EventsContext = void 0; var _react = _interopRequireWildcard(require("react")); var _reactHooks = require("@polkadot/react-hooks"); var _util = require("@polkadot/util"); var _utilCrypto = require("@polkadot/util-crypto"); // Copyright 2017-2020 @polkadot/react-query authors & contributors // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. const MAX_EVENTS = 25; const EventsContext = _react.default.createContext([]); exports.EventsContext = EventsContext; function Events({ children }) { const { api } = (0, _reactHooks.useApi)(); const [state, setState] = (0, _react.useState)([]); (0, _react.useEffect)(() => { // TODO We should really unsub - but since this should just be used once, // atm I'm rather typing this than doing it the way it is supposed to be api.isReady.then(() => { const prevEventHash = ''; let events = []; api.query.system.events(records => { const newEvents = records.filter(({ event }) => event.section !== 'system').map((record, index) => ({ key: "".concat(Date.now(), "-").concat(index), record })); const newEventHash = (0, _utilCrypto.xxhashAsHex)((0, _util.stringToU8a)(JSON.stringify(newEvents))); if (newEventHash !== prevEventHash) { events = [...newEvents, ...events].slice(0, MAX_EVENTS); setState(events); } }); }); }, []); return _react.default.createElement(EventsContext.Provider, { value: state }, children); }