2.2.0 • Published 6 months ago

smart-resource v2.2.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
6 months ago

Smart Resource

An observable helper for asynchronous resources

Installation

npm install smart-resource

Usage

A SmartResource takes any function that returns a promise and converts it into an observable that automatically notifies subscribers when a refetch is triggered.

const RandomNumberResource = new SmartPromise(() => {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve(Math.random());
        }, 1000);
    });
});

const subscriber = RandomNumberResource.subscribe((resource) => {
    console.log(`New random number: ${resource.value}!`);
});

RandomNumberResource.fetch();
RandomNumberResource.fetch();

subscriber.unsubscribe();

RandomNumberResource.fetch();

Output:

New random number: 0.5081273919151901!
New random number: 0.44005522329031255!
2.2.0

6 months ago

2.1.0

6 months ago

2.0.0

6 months ago

1.0.0

1 year ago

0.3.8

1 year ago

0.3.6

1 year ago

0.1.0

2 years ago

0.3.0

2 years ago

0.2.0

2 years ago

0.3.5

2 years ago

0.3.2

2 years ago

0.3.1

2 years ago

0.3.4

2 years ago

0.3.3

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago