re_reselect 0.2.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 50

Re-reselect for Dart #

A Selector library for Dart, based on the original Re-reselect JavaScript library.

Motivation #

re-reselect is a lightweight wrapper around Reselect meant to enhance selectors with deeper memoization and cache management.

Switching between different arguments using standard reselect selectors causes cache invalidation since default reselect cache has a limit of one.

re-reselect forwards different calls to different reselect selectors stored in cache, so that computed/memoized values are retained.

re-reselect selectors work as normal reselect selectors but they are able to determine when creating a new selector or querying a cached one on the fly, depending on the supplied arguments.

Useful to:

  • Retain selector's cache when sequentially called with one/few different arguments
  • Join similar selectors into one
  • Share selectors with props across multiple component instances
  • Instantiate selectors on runtime
  • Enhance reselect with custom caching strategies

See example and tests for details.

Changelog #

0.2.2 #

  • Minor typo fix

0.2.1 #

  • Added reselect export

0.2.0 #

  • Added readme
  • Updated license

0.1.3 #

  • Added example

0.1.2 #

  • Description updated

0.1.1 #

  • Updated author details

0.1.0 #

  • Initial version

example/example.dart

import 'package:re_reselect/rereselect.dart';

class OriginalState {
  final String key;
  final int a;
  final int b;
  final int c;
  final int d;
  OriginalState(this.key, this.a, this.b, this.c, this.d);
}

void main() {
  // First state selector
  final stateSelector1 = (OriginalState state) => state.a + state.b;

  // Second state selector
  final stateSelector2 = (OriginalState state) => state.c + state.d;

  // Computation method
  final combine = (int state1, int state2) => state1 + state2;

  // Create Re-reselect selector from two selector methods and Key as cache key
  final selector = createCachedSelector2(stateSelector1, stateSelector2, combine, (OriginalState state) => state.key);

  // Will execute combine method and print 10
  print(selector(OriginalState('test', 1, 2, 3, 4)));

  // Will NOT execute combine method and print 10 from cache
  print(selector(OriginalState('test', 1, 2, 3, 4)));

  // Will execute combine method and print 10
  print(selector(OriginalState('test2', 1, 2, 3, 4)));

  // Will execute combine method and print 18 (because of params changes)
  print(selector(OriginalState('test2', 1, 10, 3, 4)));

  // Clear cache
  selector.clearCache();
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  re_reselect: ^0.2.2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:re_reselect/cache/fifo_cache.dart';
import 'package:re_reselect/cache/flat_cache.dart';
import 'package:re_reselect/cache/lru_cache.dart';
import 'package:re_reselect/rereselect.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
50
Learn more about scoring.

We analyzed this package on Jan 19, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4

Health suggestions

Format lib/cache/flat_cache.dart.

Run dartfmt to format lib/cache/flat_cache.dart.

Format lib/rereselect.dart.

Run dartfmt to format lib/rereselect.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
reselect ^0.4.0 0.4.0
Transitive dependencies
memoize 2.0.0
Dev dependencies
test ^1.3.0