How to use the web3-core-subscriptions.subscription function in web3-core-subscriptions

To help you get started, we’ve selected a few web3-core-subscriptions examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ethereum / web3.js / packages / web3-eth-contract / src / index.js View on Github external
Contract.prototype._on = function(){
    var subOptions = this._generateEventOptions.apply(this, arguments);


    // prevent the event "newListener" and "removeListener" from being overwritten
    this._checkListener('newListener', subOptions.event.name, subOptions.callback);
    this._checkListener('removeListener', subOptions.event.name, subOptions.callback);

    // TODO check if listener already exists? and reuse subscription if options are the same.

    // create new subscription
    var subscription = new Subscription({
        subscription: {
            params: 1,
            inputFormatter: [formatters.inputLogFormatter],
            outputFormatter: this._decodeEventABI.bind(subOptions.event),
            // DUBLICATE, also in web3-eth
            subscriptionHandler: function (output) {
                if(output.removed) {
                    this.emit('changed', output);
                } else {
                    this.emit('data', output);
                }

                if (_.isFunction(this.callback)) {
                    this.callback(null, output, this);
                }
            }
github vechain / thorify / src / extend / contracts.ts View on Github external
if (subOptions.params.topics) {
            for (const [index, value] of subOptions.params.topics.entries()) {
                if (value === null) {
                    continue
                }
                if (typeof value === 'string') {
                    filterOptions['t' + index as 't0' | 't1' | 't2' | 't3' | 't4'] = value
                } else {
                    throw new Error('[thorify] Array filter option is not supported in thor, must be null or bytes32 string')
                }
            }
        }

        const decodeEventABI = this._decodeEventABI.bind(subOptions.event)
        const subscription = new Subscription({
            subscription: {
                params: 1,
                inputFormatter: [inputLogFilterFormatter],
                subscriptionHandler(subscriptionMsg: any) {
                    if (subscriptionMsg.error) {
                        this.emit('error', subscriptionMsg.error)
                        // web3-core-subscriptions/subscription sets a default value for this.callback
                        this.callback(subscriptionMsg.error, null, this)
                    } else {
                        const result = decodeEventABI(subscriptionMsg.data)
                        if (result.removed) {
                            this.emit('changed', result)
                        } else {
                            this.emit('data', result)
                        }
                        // web3-core-subscriptions/subscription sets a default value for this.callback