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

[iov-cli] Add wait() function to repl #181

Merged
merged 2 commits into from Jul 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 2 additions & 8 deletions packages/iov-cli/README.md
Expand Up @@ -14,13 +14,7 @@ How to use the web4 cli
> profile.getIdentities(0)
[]

> profile.createIdentity(0)

> profile.getIdentities(0)
[ { pubkey: { algo: 'ed25519', data: [Object] },
label: undefined } ]

> const mainIdentity = profile.getIdentities(0)[0]
> const mainIdentity = wait(profile.createIdentity(0))

> mainIdentity.pubkey
{ algo: 'ed25519',
Expand Down Expand Up @@ -92,7 +86,7 @@ const sendTx: SendTx = {
7. and restore

```
> var profileFromDb; UserProfile.loadFrom(db, "secret passwd").then(p => { profileFromDb = p })
> const profileFromDb = wait(UserProfile.loadFrom(db, "secret passwd"));
> profileFromDb
UserProfile {
createdAt: 2018-07-04T16:07:14.583Z,
Expand Down
3 changes: 3 additions & 0 deletions packages/iov-cli/init
Expand Up @@ -6,6 +6,7 @@ import { bnsCodec } from "@iov/bns";
import { Encoding } from '@iov/encoding';
import { Ed25519SimpleAddressKeyringEntry, UserProfile } from '@iov/keycontrol';
import { AddressBytes, ChainId, Nonce, SendTx, TokenTicker, TransactionKind } from "@iov/types";
function wait<T>(promise: Promise<T>): T { return require('deasync2').await(promise); }
console.log(colors.green('Done initializing session for you. Have fun!'));
console.log(colors.yellow('Available imports:'));
console.log(colors.yellow(' * leveldown'));
Expand All @@ -26,3 +27,5 @@ console.log(colors.yellow(' - Nonce'));
console.log(colors.yellow(' - SendTx'));
console.log(colors.yellow(' - TokenTicker'));
console.log(colors.yellow(' - TransactionKind'));
console.log(colors.yellow(' * helper functions'));
console.log(colors.yellow(' - wait'));
3 changes: 2 additions & 1 deletion packages/iov-cli/package.json
Expand Up @@ -9,10 +9,11 @@
"url": "https://github.com/iov-one/web4/tree/master/packages/iov-cli"
},
"scripts": {
"web4": "echo '\\nTo get started type: .load init\\n' && ts-node"
"web4": "echo '\\nTo get started type: .load init\\n' && NODE_NO_WARNINGS=1 ts-node"
},
"dependencies": {
"colors": "^1.3.0",
"deasync2": "https://github.com/bluelovers/deasync.git#43ace7b11",
"ts-node": "^7.0.0"
}
}
20 changes: 20 additions & 0 deletions packages/iov-cli/wait-demo.ts
@@ -0,0 +1,20 @@
// This is an example of the non-trivial art of converting an asynchonous
// method into a synchonous one. This should only be used in the REPL where
// await is not available since the user is working in a top level scope.
//
// yarn run ts-node wait-demo.ts

function wait<T>(promise: Promise<T>): T {
return require('deasync2').await(promise);
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it make sense to do the require/import once at the top of the file and then just use it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using it like this such that I can easily convert it into a single line command for the init file.

}

const somePromise = new Promise((resolve, _) => {
setTimeout(() => {
console.log("done! Will resolve now");
resolve("abc");
}, 2000);
})

console.log("Let's wait until we get what we need …");
const value = wait(somePromise)
console.log(value)
13 changes: 12 additions & 1 deletion yarn.lock
Expand Up @@ -654,6 +654,10 @@ bindings@^1.3.0, bindings@~1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/bindings/-/bindings-1.3.0.tgz#b346f6ecf6a95f5a815c5839fc7cdb22502f1ed7"

bindings@~1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/bindings/-/bindings-1.2.1.tgz#14ad6113812d2d37d72e67b4cacb4bb726505f11"

bip39@^2.5.0:
version "2.5.0"
resolved "https://registry.yarnpkg.com/bip39/-/bip39-2.5.0.tgz#51cbd5179460504a63ea3c000db3f787ca051235"
Expand Down Expand Up @@ -1557,6 +1561,13 @@ dateformat@^3.0.0:
version "3.0.3"
resolved "https://registry.yarnpkg.com/dateformat/-/dateformat-3.0.3.tgz#a6e37499a4d9a9cf85ef5872044d62901c9889ae"

"deasync2@https://github.com/bluelovers/deasync.git#43ace7b11":
version "0.1.15"
resolved "https://github.com/bluelovers/deasync.git#43ace7b11b96f86a59255c398a48cb53d3f7c0dd"
dependencies:
bindings "~1.2.1"
nan "^2.0.7"

debug@2, debug@2.6.9, debug@^2.1.2, debug@^2.2.0, debug@^2.3.3, debug@~2.6.4, debug@~2.6.6:
version "2.6.9"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
Expand Down Expand Up @@ -3928,7 +3939,7 @@ mute-stream@0.0.7:
version "0.0.7"
resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.7.tgz#3075ce93bc21b8fab43e1bc4da7e8115ed1e7bab"

nan@^2.10.0, nan@^2.9.2, nan@~2.10.0:
nan@^2.0.7, nan@^2.10.0, nan@^2.9.2, nan@~2.10.0:
version "2.10.0"
resolved "https://registry.yarnpkg.com/nan/-/nan-2.10.0.tgz#96d0cd610ebd58d4b4de9cc0c6828cda99c7548f"

Expand Down