Skip to content

Commit

Permalink
refract infra
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy committed Nov 3, 2020
1 parent 1b38634 commit ac50f73
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
Cargo.lock
.deno_plugins/
10 changes: 10 additions & 0 deletions core_type.ts
Original file line number Diff line number Diff line change
@@ -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;
};
4 changes: 4 additions & 0 deletions example_mp3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { play } from "./mod.ts";

await play("music.mp3");

1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { play } from "./plugin.ts";
29 changes: 6 additions & 23 deletions plugin.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import filename from "./detect.ts";
import { prepare } from "https://deno.land/x/[email protected]/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",
Expand All @@ -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`);
Expand All @@ -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");

0 comments on commit ac50f73

Please sign in to comment.