Skip to content

Commit

Permalink
Added new Convo methods for simulating and controlling storage.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmtek committed Oct 20, 2018
1 parent e766e3c commit 9fcf72e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tmtek/convo",
"version": "0.1.5",
"version": "0.1.7",
"description": "A uility for building conversational responses in DialogFlow fufillments",
"main": "index.js",
"scripts": {
Expand Down
24 changes: 24 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,26 @@ new MyApplication()
```
We use`then()` of the resulting promises returned from `intent()` to simulate the multiple conversation steps. Notice how for each intent call we create a new instance of Convo derived from the previous: `new Convo(convo)`. This allows us to create a new response for each intent, but still carry over the context and storage data to simulate how things work in DialogFlow with a standard `conv` object.

### Using Storage

Convo allows you to simulate DialogFlow's storage capabilities. Storage lets you store arbitrary data for the user that is accessible across sessions of usage. Convo offers methods to simply interaction with storage, but to also simulate it in the dev/test environment.

```javascript
let convo = new Convo()
.onStorageUpdated(storage => {console.log(storage)}) //fires after setToStorage call.
.setStorage({}) //populates storage with data (doesn't trigger onStorageUpdated).
.setToStorage("list", ["one","two","three"]); //Add value to storage.

convo.isInStorage("list", list => list.length > 0); //returns true
convo.getFromStorage("list"); //returns ["one","two","three"]

```

`setToStorage()`, `getFromStorage()`, and `isInStorage()` will be the methods that are most commonly used in application development.

`onStorageUpdated()` and `setStorage()` are useful when testing your application outside of DialogFlow, because you can use those methods to simulate persisted data, or actually persist it yourself.



## API Reference

Expand All @@ -182,6 +202,10 @@ We use`then()` of the resulting promises returned from `intent()` to simulate th
* setConext(contextName, lifespan, value)
* getContext(contextName)
* getStorage()
* setStorage(data)
* setToStorage(name, data)
* getFromStorage(name)
* isInStorage(name, predicate)

#### Convo Rich Responses
* Convo.SimpleResponse()
Expand Down
46 changes: 45 additions & 1 deletion src/convo.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,18 @@ class Convo {
}

constructor(obj) {
this.conv = !obj ? Convo.mockConv() : obj.conv || obj;
this.conv = !obj ? Convo.mockConv() : copyConvo(obj);
this.clear();
}

copyConvo(obj) {
if (obj.conv) {
this_onStorageUpdated = obj._onStorageUpdated;
return obj.conv;
}
return obj;
}

clear() {
this._write = [];
this._speak = [];
Expand Down Expand Up @@ -213,6 +221,42 @@ class Convo {
getStorage() {
return this.conv && this.conv.user && this.conv.user.storage || {};
}

setStorage(data) {
if (this.conv && this.conv.user) {
this.conv.user.storage = data;
}
return this;
}

setToStorage(name, value) {
if (this.conv && this.conv.user && this.conv.user.storage) {
this.conv.user.storage[name] = value;
if(this._onStorageUpdated) {this._onStorageUpdated(this.conv.user.storage)};
}
return this;
}

onStorageUpdated(callback) {
this._onStorageUpdated = callback;
return this;
}

getFromStorage(name) {
if (this.conv && this.conv.user && this.conv.user.storage) {
return this.conv.user.storage[name];
}
return null;
}

isInStorage(name, predicate = null) {
return this.conv &&
this.conv.user &&
this.conv.user.storage &&
this.conv.user.storage[name] &&
(!predicate || predicate(this.conv.user.storage[name]));
}

}

function isPromise(obj) {
Expand Down

0 comments on commit 9fcf72e

Please sign in to comment.