Skip to content

Commit

Permalink
fix(whatsapp.gblib): BASIC enabled with Promises.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigorodriguez committed Aug 29, 2019
1 parent fa9f260 commit 47df1f1
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 54 deletions.
141 changes: 116 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"azure-arm-search": "1.3.0-preview",
"azure-arm-sql": "5.7.0",
"azure-arm-website": "5.7.0",
"azure-search-client": "^3.1.5",
"azure-search-client": "3.1.5",
"bluebird": "3.5.4",
"body-parser": "1.19.0",
"botbuilder": "4.4.0",
Expand All @@ -67,18 +67,18 @@
"botbuilder-choices": "4.0.0-preview1.2",
"botbuilder-dialogs": "4.4.0",
"botbuilder-prompts": "4.0.0-preview1.2",
"botlib": "1.2.2",
"botlib": "1.2.3",
"chai": "4.2.0",
"child_process": "1.0.2",
"chokidar": "3.0.0",
"cli-spinner": "0.2.10",
"commonmark": "^0.29.0",
"commonmark": "0.29.0",
"csv-parse": "4.4.1",
"dotenv-extended": "2.4.0",
"exceljs": "^1.15.0",
"exceljs": "1.15.0",
"express": "4.16.4",
"express-promise-router": "3.0.3",
"express-remove-route": "^1.0.0",
"express-remove-route": "1.0.0",
"fs-extra": "8.0.0",
"ip": "1.1.5",
"js-beautify": "1.10.0",
Expand All @@ -104,10 +104,10 @@
"sequelize-typescript": "0.6.10",
"shx": "0.3.2",
"simple-git": "1.113.0",
"sppull": "^2.4.1",
"sppull": "2.4.1",
"sqlite3": "4.0.8",
"strict-password-generator": "1.1.2",
"swagger-client": "^2.1.18",
"swagger-client": "2.1.18",
"tedious": "6.1.1",
"temperature-js": "0.1.0",
"ts-node": "8.1.0",
Expand All @@ -120,7 +120,7 @@
"wait-until": "0.0.2",
"walk-promise": "0.2.0",
"winston": "3.2.1",
"xlsx": "^0.15.1"
"xlsx": "0.15.1"
},
"devDependencies": {
"@semantic-release/changelog": "3.0.2",
Expand Down
12 changes: 6 additions & 6 deletions packages/core.gbapp/services/GBAPIService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,16 @@ export class DialogClass {
return this.internalSys;
}

public async hear(cb) {
public async hear(step, promise, previousResolve) {
function random(low, high) {
return Math.random() * (high - low) + low
}
const idCallback = random(0, 120000000);
this.min.cbMap[idCallback] = cb;
await this.step.beginDialog('/hear', { id: idCallback });
const idPromise = random(0, 120000000);
this.min.cbMap[idPromise] = promise;
return await step.beginDialog('/hear', { id: idPromise, previousResolve: previousResolve });
}

public async talk(text: string) {
return await this.context.sendActivity(text);
public async talk(step, text: string) {
return await step.context.sendActivity(text);
}
}
11 changes: 4 additions & 7 deletions packages/core.gbapp/services/GBMinService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,9 @@ export class GBMinService {
}
GBServer.globals.minInstances.push(min);

// Install default VBA module.
//this.deployer.deployPackage(min, 'packages/default.gbdialog');
// Install default BASIC module.

this.deployer.deployPackage(min, 'packages/default.gbdialog');

// Call the loadBot context.activity for all packages.
this.invokeLoadBot(GBServer.globals.appPackages, GBServer.globals.sysPackages, min, GBServer.globals.server);
Expand Down Expand Up @@ -537,16 +538,12 @@ export class GBMinService {
return utterance.match(Messages[locale].global_quit);
}


const isVMCall = Object.keys(min.scriptMap).find(key => min.scriptMap[key] === context.activity.text) !== undefined;

if (isVMCall) {
const mainMethod = context.activity.text;

min.sandBoxMap[mainMethod].context = context;
min.sandBoxMap[mainMethod].step = step;
min.sandBoxMap[mainMethod][mainMethod].bind(min.sandBoxMap[mainMethod]);
await min.sandBoxMap[mainMethod][mainMethod]();
await min.sandBoxMap[mainMethod][mainMethod](step);
} else if (context.activity.text.charAt(0) === '/') {
await step.beginDialog(context.activity.text);

Expand Down
25 changes: 17 additions & 8 deletions packages/core.gbapp/services/GBVMService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export class GBVMService extends GBService {
});

code = code.replace(/(talk)(\s)(.*)/g, ($0, $1, $2, $3) => {
return `talk (${$3})\n`;
return `talk (step, ${$3})\n`;
});

code = `${code}\n%>`;
Expand All @@ -144,7 +144,7 @@ export class GBVMService extends GBService {
// Convert TS into JS.
const tsfile: string = `${filename}.ts`;
let tsCode: string = fs.readFileSync(tsfile, 'utf8');
tsCode = tsCode.replace(/export.*\n/g, `export function ${mainName}() {`);
tsCode = tsCode.replace(/export.*\n/g, `export function ${mainName}(step) { let resolve = undefined;`);
fs.writeFileSync(tsfile, tsCode);

const tsc = new TSCompiler();
Expand All @@ -171,9 +171,12 @@ export class GBVMService extends GBService {
// Writes async body.

const variable = match1[1]; // Construct variable = hear ().
const promiseName = `promiseFor${variable}`;

parsedCode = code.substring(pos, pos + match1.index);
parsedCode += `hear (async (${variable}) => {\n`;
parsedCode += ``;
parsedCode += `const ${promiseName}= async (step, ${variable}) => {`
parsedCode += ` return new Promise(async (resolve) => {`

// Skips old construction and point to the async block.

Expand Down Expand Up @@ -205,6 +208,8 @@ export class GBVMService extends GBService {

parsedCode += code.substring(start + match1[0].length + 1, pos + match1[0].length);
parsedCode += '});\n';
parsedCode += '}\n';
parsedCode += `hear (step, ${promiseName}, resolve);\n`;
parsedCode += code.substring(pos + match1[0].length);

// A interaction will be made for each hear.
Expand Down Expand Up @@ -252,15 +257,19 @@ export class GBVMService extends GBService {
min.dialogs.add(
new WaterfallDialog('/hear', [
async step => {
step.activeDialog.state.cbId = (step.options as any).id;
step.activeDialog.state.options = {};
step.activeDialog.state.options.cbId = (step.options as any).id;
step.activeDialog.state.options.previousResolve = (step.options as any).previousResolve;

return await step.prompt('textPrompt', {});
},
async step => {
const cbId = step.activeDialog.state.cbId;
const cb = min.cbMap[cbId];
cb.bind({ step: step, context: step.context });
await cb(step.result);
const cbId = step.activeDialog.state.options.cbId;
const promise = min.cbMap[cbId];
const res = await promise(step, step.result);
if (step.activeDialog.state.options.previousResolve != undefined){
step.activeDialog.state.options.previousResolve();
}
return await step.next();
}
])
Expand Down

0 comments on commit 47df1f1

Please sign in to comment.