3

For the following javascript code snippet found at Health Bot Container Sample, the "store" object created from window.WebChat.createStore, what does the "store" object do? What is the "store" object's purpose?

I am reading the web chat api documentation, but the description and explanation of the "store" object is not clear.

Thank you very much for your help on this matter.

const store = window.WebChat.createStore({}, function(store) { return function(next) { return function(action) {
    if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
        store.dispatch({
            type: 'DIRECT_LINE/POST_ACTIVITY',
            meta: {method: 'keyboard'},
            payload: {
                activity: {
                    type: "invoke",
                    name: "InitConversation",
                    locale: user.locale,
                    value: {
                        // must use for authenticated conversation.
                        jsonWebToken: jsonWebToken,

                        // Use the following activity to proactively invoke a bot scenario
                        /*
                        triggeredScenario: {
                            trigger: "{scenario_id}",
                            args: {
                                myVar1: "{custom_arg_1}",
                                myVar2: "{custom_arg_2}"
                            }
                        }
                        */
                    }
                }
            }
        });

    }
    
    return next(action);
}}});
Sam Sam
  • 65
  • 5

1 Answers1

1

The store is a Redux store. You will need to be familiar with Redux to fully understand it, but for the purposes of Web Chat you can think of it as an object that allows you to dispatch actions and use middleware. The samples in this folder should help you get the hang of it.

Kyle Delaney
  • 11,616
  • 6
  • 39
  • 66
  • Thank you kyle for the links and info about the Redux "store" object and the link to the samples. When you were talking about dispatching actions and being able to use middleware, what middleware were you referring to? thank you kyle. – Sam Sam Oct 14 '20 at 07:41
  • I'm talking about Redux middleware. The chain of anonymous functions that's passed to `createStore` in the code sample you already posted is an example of Redux middleware. You can find a few more specific types of middleware in the Web Chat API reference: https://github.com/microsoft/BotFramework-WebChat/blob/master/docs/API.md – Kyle Delaney Oct 14 '20 at 17:03
  • To clarify, if you pass middleware using the Web Chat API then you won't need a store because Web Chat adds those to the store internally for you. But if you do use your own store then you'll be able to create a more generic middleware that intercepts all actions instead of specific types. – Kyle Delaney Oct 14 '20 at 17:24
  • hi Kyle, when you mean using "my own store", it's basically like the sample code I posted above? And just so I am on the same page, the "store" object that's used in the web chat, it's whole sole purpose is to just keep track and manage the state of the web chat, is that correct? Thank you Kyle. – Sam Sam Oct 15 '20 at 14:24
  • It sounds like you may need to post some new questions. Have I answered the question you originally asked? – Kyle Delaney Oct 15 '20 at 18:34
  • just the last comment I added is still waiting for an answer. I deleted the other comment I added and will convert that to a new question. Thank you Kyle. – Sam Sam Oct 15 '20 at 18:52
  • Yes to the first question. To the second question, that sounds incorrect. I don't believe I've said anything to indicate that the Redux store manages the state of Web Chat, so I presume you're thinking that because of the name "store." The store may be used to store things in other applications of Redux, but in Web Chat it's used for dispatching actions and using middleware like I said. – Kyle Delaney Oct 15 '20 at 19:00
  • I see now. Thank you Kyle for the clarification. I was reading the documentation on the Redux store and they mentioned something about that. It threw me off track. I apologize for the misunderstanding. My bad. Still, thank you Kyle for all your help on this. Much appreciate it!!! :) – Sam Sam Oct 15 '20 at 19:09
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/223111/discussion-between-kyle-delaney-and-sam-sam). – Kyle Delaney Oct 15 '20 at 19:17