Skip to content

Commit

Permalink
Update package references and add support for wasm-didjs package
Browse files Browse the repository at this point in the history
  • Loading branch information
b3hr4d committed Apr 18, 2024
1 parent fd09de9 commit 638de5e
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ examples/*/deps/candid
docs
.nx
umd
nodejs

packages/svelte
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
"typescript.tsdk": "node_modules/typescript/lib",
"liveServer.settings.port": 3001
}
3 changes: 2 additions & 1 deletion examples/javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"build": "webpack --mode production"
},
"dependencies": {
"@ic-reactor/core": "^*"
"@ic-reactor/core": "^*",
"@ic-reactor/parser": "^*"
},
"devDependencies": {
"html-webpack-plugin": "^5.0.0",
Expand Down
9 changes: 7 additions & 2 deletions examples/javascript/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
}

input,
textarea,
select,
fieldset,
button {
Expand Down Expand Up @@ -71,6 +72,12 @@ <h1>ICRC Token</h1>
<button type="submit" style="width: 30%;">Fetch</button>
</div>
</form>
<form id="candidForm">
<div class="flex-row">
<textarea id="candidInput" rows="3" style="width: 70%;" required>icrc1_name:()->(text) query</textarea>
<button type="submit" style="width: 30%;">Compile</button>
</div>
</form>
<hr />
<div id="result" style="margin-top: 10px;margin-bottom: 10px;"></div>
<fieldset class="flex-column" style="padding: 10px;">
Expand All @@ -89,8 +96,6 @@ <h1>ICRC Token</h1>
<button id="loginButton">Login</button>
</fieldset>
</div>

<script type="module" src="./main.js"></script>
</body>

</html>
23 changes: 17 additions & 6 deletions examples/javascript/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,36 @@ import {
LOCAL_HOST_NETWORK_URI,
jsonToString,
} from "@ic-reactor/core/dist/utils"
import {
createCandidAdapter,
createAgentManager,
createReactorCore,
} from "@ic-reactor/core"
import { createAgentManager, createReactorCore } from "@ic-reactor/core"
import { Principal } from "@dfinity/principal"
import { CandidAdapter } from "@ic-reactor/parser"

const agentManager = createAgentManager({ withDevtools: true })
const candidAdapter = createCandidAdapter({ agentManager })
const candidAdapter = new CandidAdapter({ agentManager })

let previousActorCleanup = null
let balanceUnsub = null
let transferUnsub = null

const canisterForm = document.getElementById("canisterForm")
const candidForm = document.getElementById("candidForm")

document.addEventListener("DOMContentLoaded", function () {
canisterForm.addEventListener("submit", renderActor, false)
})
document.addEventListener("DOMContentLoaded", function () {
candidForm.addEventListener(
"submit",
async (e) => {
e?.preventDefault()
const text = e.target.elements.candidInput.textContent

const idlFactory = candidAdapter.didTojs(text)
console.log("🚀 ~ idlFactory:", idlFactory)
},
false
)
})

const canisterIdInput = document.getElementById("canisterIdInput")
const networkSelect = document.getElementById("networkSelect")
Expand Down
3 changes: 3 additions & 0 deletions examples/javascript/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ module.exports = {
compress: true,
port: 3000,
},
experiments: {
asyncWebAssembly: true,
},
module: {
rules: [
// You can add loaders here for other file types (CSS, SASS, images, etc.)
Expand Down
4 changes: 3 additions & 1 deletion packages/parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@
},
"scripts": {
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" npx jest",
"build:node": "npx rimraf src/pkg dist && wasm-pack build --no-pack --target nodejs --out-dir src/pkg && npx tsc && cp -r src/pkg dist",
"build:browser": "npx rimraf src/pkg dist && wasm-pack build --no-pack --target web --out-dir src/pkg && npx tsc && cp -r src/pkg dist",
"build": "npx rimraf src/pkg dist && wasm-pack build --no-pack --out-dir src/pkg && npx tsc && cp -r src/pkg dist",
"start": "tsc watch",
"build": "npx tsc",
"clean": "npx rimraf dist"
},
"engines": {
Expand Down
8 changes: 4 additions & 4 deletions packages/parser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from "@ic-reactor/core/dist/utils/constants"
import { CandidAdapterParameters, CandidDefenition } from "./types"
import { CanisterId, IDL, Principal } from "@ic-reactor/core/dist/types"
import { did_to_js } from "./pkg/didjs"

export class CandidAdapter {
public agent: HttpAgent
Expand Down Expand Up @@ -93,10 +94,9 @@ export class CandidAdapter {
return data ? this.didTojs(data) : undefined
}

public async didTojs(candidSource: string): Promise<CandidDefenition> {
const js = await import("../pkg/didjs").then(({ did_to_js }) =>
did_to_js(candidSource)
)
public didTojs(candidSource: string): Promise<CandidDefenition> {
const js = did_to_js(candidSource)
console.log("🚀 ~ CandidAdapter ~ didTojs ~ js:", js)

const dataUri =
"data:text/javascript;charset=utf-8," + encodeURIComponent(js as string)
Expand Down

0 comments on commit 638de5e

Please sign in to comment.