hackape / talib.js

TA-Lib C code ported to WebAssembly
https://hackape.github.io/talib.js
Other
30 stars 9 forks source link

Is there any demo code for vue3 with typescript run in browser? #1

Closed newproplus closed 2 years ago

newproplus commented 2 years ago

Could you provide a simple full demo code?I mean the code include "import" statement.

Thank you.

hackape commented 2 years ago

Not sure how vue3 is related. I'mma just show you a TS example.

import { init, ADD } from "talib.js";

async function main() {
  // remember to call init at least once.
  await init(/* optionally pass in .wasm file path here */);

  // usage:
  const inReal = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  console.log("talib.ADD result:", ADD({ inReal0: inReal, inReal1: inReal }));
}

main();
newproplus commented 2 years ago

@hackape I tried the code you given, it shows:

index.esm.js?80e5:561 

       Uncaught (in promise) RuntimeError: abort(CompileError: WebAssembly.instantiate(): expected magic word 00 61 73 6d, found 3c 21 44 4f @+0). Build with -s ASSERTIONS=1 for more info.
    at abort (webpack-internal:///./node_modules/talib.js/lib/index.esm.js:886)
    at eval (webpack-internal:///./node_modules/talib.js/lib/index.esm.js:962)
index.esm.js?80e5:2146 

       Uncaught (in promise) Error: TA-Lib WASM runtime init fail.
Error: 
abort(CompileError: WebAssembly.instantiate(): expected magic word 00 61 73 6d, found 3c 21 44 4f @+0). Build with -s ASSERTIONS=1 for more info.
    at eval (webpack-internal:///./node_modules/talib.js/lib/index.esm.js:2471)
hackape commented 2 years ago

The issue is webpack related. I’m sorry I cannot address bundler problem, it’s out of scope.

For now I can only suggest a workaround. The idea is to supply the URL to the .wasm file as argument to init() function.

await init("https://unpkg.com/talib.js@0.1.0/lib/talib.wasm");

You can host this file yourself if you like. It’s just a static asset, like a picture, nothing magical.

If you decide to host it, 2 things to check:

  1. Make sure the URL is accessible from your origin, e.g. double check CORS config if involved.
  2. Make sure the MIME type is correctly set to application/wasm.
newproplus commented 2 years ago

@hackape It works,thank yuou