[added] dispatchOnKeyChange option to rewrite
ClosedPublic

Authored by appsforartists on Wed, Sep 6, 8:28 PM.

Details

Summary

rewrite uses _reactiveNextOperator, which will pass all the latest values into the operator whenever one of them changes. This isn't always desirable; the last value for a certain key might be stale when that key changes.

dispatchOnKeyChange enables authors to decide whether key changes should cause a dispatch, which can prevent stale data from being passed downsteam.

Diff Detail

Repository
R13 material-motion/material-motion-js
Lint
Lint OK
Unit
No Unit Test Coverage
appsforartists created this revision.Wed, Sep 6, 8:28 PM
Restricted Application added a reviewer: O2: Material Motion. · View Herald TranscriptWed, Sep 6, 8:28 PM
Restricted Application added a project: Material Motion. · View Herald Transcript
Restricted Application added a reviewer: O3: Material JavaScript platform reviewers. · View Herald Transcript
Restricted Application added a reviewer: Material Motion. · View Herald Transcript
Restricted Application completed building Restricted Buildable.Wed, Sep 6, 8:31 PM
featherless added inline comments.
packages/core/src/operators/rewrite.ts
76

I'm not sure I understand what's going on here - what is currentKey?

appsforartists added inline comments.Tue, Sep 12, 5:56 PM
packages/core/src/operators/rewrite.ts
76

The signature of _reactiveNextOperator is (dispatch, upstreamValue, ...argValues). The upstream value of rewrite is a key to lookup in its values dictionary:

trueOrFalse$.rewrite({ true: 'yup', false: 'nope' })

The key there would be true or false, and the values are yup and nope. Since this is reactive, we care about what the current values are. So, currentKey is the latest upstream value and currentValues are the latest emissions from the streams in the values dictionary:

trueOrFalse$.rewrite({ true: someNumber$, false: someLetter$ })

currentKey could be true, currentValues could be [5, 'a']. keys.indexOf(true) is 0, so the result is [5, 'a'][0] or 5.

This diff adds a dispatchOnKeyChange option, which gives the author the ability to prevent stale values from being dispatched. It does this by timestamping every key/value and only dispatching values that are newer than the current key. So currentKey and currentValue are maybe timestamped; key and value have had the timestamps stripped.

Hopefully, that helps you understand what's going on. Let me know if there's anything you think should change.

featherless accepted this revision.Wed, Sep 20, 5:14 PM
This revision is now accepted and ready to land.Wed, Sep 20, 5:14 PM
This revision was automatically updated to reflect the committed changes.