Coder Social home page Coder Social logo

kromdaniel / browser-cancelable-events Goto Github PK

View Code? Open in Web Editor NEW
6.0 3.0 0.0 50 KB

Automatically invalidate async listeners and promises in one place. Lightweight zero dependent library for browsers.

License: MIT License

TypeScript 30.71% JavaScript 69.29%
async-canncelable cancelable-events listeners react cancelable promise promise-cancelling

browser-cancelable-events's Introduction

Browser Cancelable Events

Automatically invalidate async listeners and promises in one place.
Lightweight zero dependent library for browsers.

Motivation

Libraries like React, should invalidate all async tasks once component is unmounted, using isMounted is anti pattern.

Table of Contents

  1. Quick start
  2. API
  3. Testing
  4. License

Quick start

Install

npm i --save browser-cancelable-events

Typescript

The project is written with typescript and comes with a built-in index.d.ts

Import

import { CancelableEvents, isCancelledPromiseError } from "browser-cancelable-events";

Require

const { CancelableEvents, isCancelledPromiseError } = require("browser-cancelable-events");

Usage

Usage example using react component

import { Component } from 'react'
import { CancelableEvents, isCancelledPromiseError } from "browser-cancelable-events";

class MyComponent extends Component {
    constructor(props) {
        super(props);
        this.cancelable = new CancelableEvents();
    }

    componentDidMount() {
        this.cancelable.addWindowEventListener("resize", this.onWindowResize.bind(this));
        this.cancelable.addDocumentEventListener("keydown", (e) => {
            if (e.shiftKey) {
                // shift clicked
            }
        });
        this.updateEverySecond();
        this.fetchDataFromApi();
    }

    // invalidate all events
    componentWillUnmount() {
        this.cancelable.cancelAll(); // this is the magic line
    }

    render() {
        return (
            <div> ... </div>
        );
    }

    // interval that updates every second
    updateEverySecond() {
        this.cancelable.setInterval(() => {
            this.setState({
                counter: this.state.counter + 1,
            })
        }, 1000);
    }

    // do task with timeout
    doSomeAnimation() {
        this.setState({
            isInAnimation: true,
        }, () => {
            this.cancelable.setTimeout(() => {
                this.setState({
                    isInAnimation: false,
                });
            }, 500);
        })
    }

    // use invalidated promise
    async fetchDataFromApi() {
        try {
            const apiResult = await this.cancelable.promise(() => {
                return APIService.fetchSomeData();
            });

            const someVeryLongPromise = this.cancelable.promise(() => {
                return APIService.fetchSomeReallyLongTask();
            });

            this.cancelable.setTimeout(() => {
                // 1 second is too much, let's cancel
                someVeryLongPromise.cancel();
            }, 1000);

            await someVeryLongPromise;
        } catch (err) {
            if (isCancelledPromiseError(err)) {
                // all good, component is not mounted or promise is cancelled
                return;
            }

            // it's real error, should handle
        }
    }

    // callback for window.addEventListener
    onWindowResize(e) {
        // do something with resize event
    }
}

API

Cancelable object

Each one of the cancelable methods returns object with cancel method

{
    cancel: Function
}
const timer = cancelable.setInterval(intervalCallback, 100);
timer.cancel();

Cancel All

Cancel all listeners method, invalidated everything immediately. Adding new listeners on cancelled event will throw exception

cancelable.cancelAll();

Timeout

cancelable.setTimeout(callback, time, ...args[]) -> CancelableObject

Interval

cancelable.setInterval(callback, time, ...args[]) -> CancelableObject

Promise

cancelable.promise(functionThatReturnsPromise, ...args[]) -> Promise & CancelableObject
cancelable.promise(promise) -> Promise<T> & CancelableObject
Promise example
const cancelable = new CancelableEvents();

cancelable.promise(new Promise((resolve, reject) => {
    resolve("foo");
})).then((res) => {
    console.log(res); // foo
});

let cancelTimer;
// invalidate promise after 1 second
const promise = cancelable.promise(() => API.fetchSomeLongData());

promise.then((data) => {
    if(cancelTimer){
        cancelTimer.cancel();
    }
}).catch((err) => {
    if(isCancelledPromiseError(err)){
        // timeout, took too long
        return;
    };
    // real error
});

// if promise.cancel() called after fulfilled, nothing will happen
cancelTimer = cancelable.setTimeout(() => {
    promise.cancel();
}, 1000);

Document event listener

Observes document.addEventListener

cancelable.addDocumentEventListener(eventKey, callback) -> CancelableObject

Window event listener

Observes window.addEventListener

cancelable.addWindowEventListener(eventKey, callback) -> CancelableObject
Event listeners example
const cancelable = new CancelableEvents();

cancelable.addDocumentEventListener("mousewheel", (e) => {
   // do something with event 
});
cancelable.addWindowEventListener("submit", (e) => {
    // do something with event
});

// remove all cancelable listeners
cancelable.cancelAll();

Custom event emitter

You can use custom event emitter with cancelable events.

cancelable.addCustomCancelable(subscription, removeKey) -> CancelableObject
Custom event emitter example
const { EventEmitter } = require("fbemitter");

const emitter = new EventEmitter();
// key "remove" because emitter.addListener(...).remove()
cancelable.addCustomCancelable(emitter.addListener("myCustomEvent", callback), "remove");

Testing

npm run tests

License

MIT

browser-cancelable-events's People

Contributors

kromdaniel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.