# Unidirectional data flow in plugins?

**URL:** <https://discuss.prosemirror.net/t/unidirectional-data-flow-in-plugins/3235>\
**Category:** Uncategorized\
**Created:** [October 29, 2020, 10:28pm UTC](https://discuss.prosemirror.net/t/unidirectional-data-flow-in-plugins/3235 "2020-10-29T22:28:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steve](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/steve/32/2195_2.png) [@Steve](https://discuss.prosemirror.net/u/Steve)\
**Post date:** [October 29, 2020, 10:28pm UTC](https://discuss.prosemirror.net/t/unidirectional-data-flow-in-plugins/3235/1 "2020-10-29T22:28:12Z")

</div>

I am trying to organize my plugin’s data flow in unidirectional fashion, like so:

1. In [handleKeyDown](https://prosemirror.net/docs/ref/#view.EditorProps.handleKeyDown), I convert key presses to plugin actions, where I define a plugin action to be the payload of a transaction’s [meta](https://prosemirror.net/docs/ref/#state.Transaction.getMeta).
2. In `StateField` [apply](https://prosemirror.net/docs/ref/#state.StateField.apply), I extract the action from the transaction and use it to reduce my plugin’s state, obtaining a new _plugin_ state.
3. In `PluginSpec` [view](https://prosemirror.net/docs/ref/#state.PluginSpec.view), I update the plugin-managed view with the plugin state from step 2.

All this works fine provided that the plugin is only interested in updating its own state. What I can’t figure out is how to update _editor_ state within the separation of responsibilities outlined above. Specifically, what I would like to be able to do, is be able to emit _editor_ state updates from within step 2 above, which I _think_ is the cleanest place to do this in. But `StateField`’ `apply` receives no reference to the view, which I would need in order to request an editor state update.

I suspect that the above scheme, heavily informed by Redux, may be unnatural to the way ProseMirror plugins are intended to work, but I’m not quite sure what that is. Any suggestions for improvement would be appreciated.

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)\
**Post date:** [October 30, 2020, 7:46am UTC](https://discuss.prosemirror.net/t/unidirectional-data-flow-in-plugins/3235/2 "2020-10-30T07:46:54Z")

</div>

> But StateField’ apply receives no reference to the view, which I would need in order to request an editor state update.

You have a reference to the _state_, which is usually where editor state lives. The view is just a shallow wrapper around its current state.

If I understand what you’re trying to do correctly, you’ll want to fire a transaction which updates both general editor state (document or selection) and your plugin state (via transaction metadata).

> I suspect that the above scheme, heavily informed by Redux, may be unnatural to the way ProseMirror plugins are intended to work

No, that’s exactly how they are intended to work (`apply` methods are basically reducers).

---

<div class="post-metadata">

**Author:** ![Steve](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/steve/32/2195_2.png) [@Steve](https://discuss.prosemirror.net/u/Steve)\
**Post date:** [October 30, 2020, 1:06pm UTC](https://discuss.prosemirror.net/t/unidirectional-data-flow-in-plugins/3235/3 "2020-10-30T13:06:22Z")

</div>

Yes, I’m trying to do something like this:

```auto
handleKeyDown() {
    view.dispatch(transactionWithMeta("myAction")) // all good
}
apply(tr, pluginState, oldState, newState) {
    // fire off a new transaction without reference to view?
    return newPluginState
}

```

`StateField` [apply](https://prosemirror.net/docs/ref/#state.StateField.apply) does have a reference to editor state but I thought that the only way to change the current state is via `EditorView`.[updateState](https://prosemirror.net/docs/ref/#view.EditorView.updateState) or [dispatch](https://prosemirror.net/docs/ref/#view.EditorView.dispatch). Either way, I thought that I would need a reference to the view in order to persist changes to state.

I think I might be missing something very simple here…

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)\
**Post date:** [October 30, 2020, 1:18pm UTC](https://discuss.prosemirror.net/t/unidirectional-data-flow-in-plugins/3235/4 "2020-10-30T13:18:10Z")

</div>

The idea is to create the original transaction in such a way that it includes those editor state changes. Redux reducers also don’t fire off new actions.

---

<div class="post-metadata">

**Author:** ![Steve](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/steve/32/2195_2.png) [@Steve](https://discuss.prosemirror.net/u/Steve)\
**Post date:** [October 30, 2020, 1:33pm UTC](https://discuss.prosemirror.net/t/unidirectional-data-flow-in-plugins/3235/5 "2020-10-30T13:33:17Z")

</div>

Yes, that was it. Thank you.
