From ac50f73aaeb18104d5a6fa8b7125857cf853b350 Mon Sep 17 00:00:00 2001 From: divy-work Date: Tue, 3 Nov 2020 16:38:47 +0530 Subject: [PATCH] refract infra --- .gitignore | 1 + core_type.ts | 10 ++++++++++ example_mp3.ts | 4 ++++ mod.ts | 1 + plugin.ts | 29 ++++++----------------------- 5 files changed, 22 insertions(+), 23 deletions(-) create mode 100644 core_type.ts create mode 100644 example_mp3.ts create mode 100644 mod.ts diff --git a/.gitignore b/.gitignore index 96ef6c0..c57a46b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target Cargo.lock +.deno_plugins/ \ No newline at end of file diff --git a/core_type.ts b/core_type.ts new file mode 100644 index 0000000..b022bd8 --- /dev/null +++ b/core_type.ts @@ -0,0 +1,10 @@ +// @ts-ignore +export const core = Deno.core as { + ops: () => { [key: string]: number }; + setAsyncHandler(rid: number, handler: (response: Uint8Array) => void): void; + dispatch( + rid: number, + msg?: any, + buf?: ArrayBufferView, + ): Uint8Array | undefined; +}; diff --git a/example_mp3.ts b/example_mp3.ts new file mode 100644 index 0000000..312f143 --- /dev/null +++ b/example_mp3.ts @@ -0,0 +1,4 @@ +import { play } from "./mod.ts"; + +await play("music.mp3"); + diff --git a/mod.ts b/mod.ts new file mode 100644 index 0000000..f031f92 --- /dev/null +++ b/mod.ts @@ -0,0 +1 @@ +export { play } from "./plugin.ts"; diff --git a/plugin.ts b/plugin.ts index 167df6c..ab99abe 100644 --- a/plugin.ts +++ b/plugin.ts @@ -1,16 +1,6 @@ import filename from "./detect.ts"; import { prepare } from "https://deno.land/x/plugin_prepare@v0.8.0/mod.ts"; - -// @ts-ignore -export const core = Deno.core as { - ops: () => { [key: string]: number }; - setAsyncHandler(rid: number, handler: (response: Uint8Array) => void): void; - dispatch( - rid: number, - msg?: any, - buf?: ArrayBufferView, - ): Uint8Array | undefined; - }; +import { core } from "./core_type.ts"; const { filenameBase, pluginBase } = { "filenameBase": "deno_audio", @@ -21,10 +11,6 @@ const { filenameBase, pluginBase } = { const isDev = Deno.env.get("DEV"); if (isDev) { - // This will be checked against open resources after Plugin.close() - // in runTestClose() below. - const resourcesPre = Deno.resources(); - const rid = Deno.openPlugin("./target/debug/" + filename(filenameBase)); } else { // logger.info(`Downloading latest Autopilot release from Github`); @@ -40,23 +26,20 @@ if (isDev) { const { - play, + play: op_play, } = core.ops(); const textDecoder = new TextDecoder(); const decoder = new TextDecoder(); -export async function play_audio(arg: string) { +export async function play(file: string) { const encoder = new TextEncoder(); - const view = encoder.encode(arg); + const view = encoder.encode(file); return new Promise((resolve, reject) => { - core.setAsyncHandler(play, (bytes) => { + core.setAsyncHandler(op_play, (bytes) => { resolve(textDecoder.decode(bytes)); }); - core.dispatch(play, view); + core.dispatch(op_play, view); }); } - -await play_audio("music.mp3"); -