Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scan with halt #1957

Merged
merged 2 commits into from
May 3, 2018
Merged

Scan with halt #1957

merged 2 commits into from
May 3, 2018

Conversation

gamb
Copy link

@gamb gamb commented Sep 2, 2017

Description

If the reducer function doesn't change the value, then don't call the dependant streams.

Types of changes

  • New feature (non-breaking change which adds functionality)

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly. (awaiting feedback)
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • I have updated docs/change-log.md (awaiting feedback)

Does this seem like reasonable expectation for scan? Feedback greatly appreciated.

Test case provided, and style guide followed :)

@gamb gamb requested a review from lhorie as a code owner September 2, 2017 12:00
@Gandalf-the-Bot
Copy link
Contributor

Gandalf-the-Bot commented Sep 2, 2017

Warnings
⚠️

Please add an entry to docs/change-log.md.

Generated by 🚫 dangerJS

@dead-claudia dead-claudia requested review from tivac and pygy and removed request for lhorie September 3, 2017 15:28
@dead-claudia
Copy link
Member

From an API standpoint, I'm not so sure. I'd rather not prevent stateful reducers like these:

function update(store, {type, value}) {
    switch (type) {
        case UNDO: store.undo(); return store
        case REDO: store.redo(); return store
        case INSERT: store.insert(value); return store
        case FLUSH: return history()
        default: return stream.HALT
    }
}

Stream.scan(update, new Store(), dispatch)

I personally hardly ever use streams, though (since Mithril core doesn't have any idea of streams), so I'm not super invested in anything.

@gamb
Copy link
Author

gamb commented Sep 4, 2017

Thanks @isiahmeadows, that seems reasonable. So perhaps scan could be implemented like this:

function scan(reducer, seed, stream) {
	var newStream = combine(function (s) {
		var next = reducer(seed, s._state.value)
		if (next !== HALT) return seed = next
		return HALT
	}, [stream])

	if (newStream._state.state === 0) newStream(seed)

	return newStream
}

Then behaviour is the same, except reducers can return HALT if nothing happens.

@dead-claudia
Copy link
Member

That seems better to me.

@gamb
Copy link
Author

gamb commented Sep 28, 2017

Agreed. Updated as discussed, and rebased against the latest.

@gamb
Copy link
Author

gamb commented Oct 8, 2017

Anything I can do to get this one moved along? cc. @tivac @pygy

Copy link
Member

@pygy pygy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@gamb gamb force-pushed the scan-with-halt branch from 997ed18 to 15fcf42 Compare May 3, 2018 15:41
@gamb gamb force-pushed the scan-with-halt branch from 15fcf42 to a419071 Compare May 3, 2018 15:43
@gamb
Copy link
Author

gamb commented May 3, 2018

Updated docs with a few notes on this change

@barneycarroll barneycarroll merged commit fb3c344 into MithrilJS:next May 3, 2018
gamb added a commit to gamb/mithril.js that referenced this pull request May 3, 2018
dead-claudia pushed a commit to dead-claudia/mithril.js that referenced this pull request Oct 12, 2018
* HALT if scan reducer doesn't change value

* Updated docs to reflect new scan behaviour with HALT
@gamb gamb mentioned this pull request Nov 8, 2018
11 tasks
@gamb gamb mentioned this pull request Jan 7, 2019
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants