1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00

[fix] rename the sendMsg to pubInternal and fix the redundant code

This commit is contained in:
monkingxue 2018-06-05 09:25:46 +08:00
parent 71d789198b
commit df3bf51b14
5 changed files with 23 additions and 23 deletions

View file

@ -3,6 +3,7 @@
import { typedArrayToArrayBuffer } from "./util";
import { _global } from "./globals";
import { main as pb } from "./msg.pb";
const { fromObject, encode, decode } = pb.Msg;
export type MessageCallback = (msg: Uint8Array) => void;
//type MessageStructCallback = (msg: pb.IMsg) => void;
@ -33,21 +34,20 @@ export function subMsg(channel: string, cb: MessageStructCallback): void {
*/
export function pub(channel: string, payload: Uint8Array): null | ArrayBuffer {
const msg = pb.BaseMsg.fromObject({ channel, payload });
const ui8 = pb.BaseMsg.encode(msg).finish();
const msg = fromObject({ channel, payload });
const ui8 = encode(msg).finish();
const ab = typedArrayToArrayBuffer(ui8);
return send(ab);
}
// Internal version of "pub".
// TODO add internal version of "sub"
// TODO rename to pubInternal()
export function sendMsg(channel: string, obj: pb.IMsg): null | pb.Msg {
const msg = pb.Msg.fromObject(obj);
const ui8 = pb.Msg.encode(msg).finish();
export function pubInternal(channel: string, obj: pb.IMsg): null | pb.Msg {
const msg = fromObject(obj);
const ui8 = encode(msg).finish();
const resBuf = pub(channel, ui8);
if (resBuf != null && resBuf.byteLength > 0) {
const res = pb.Msg.decode(new Uint8Array(resBuf));
const res = decode(new Uint8Array(resBuf));
if (res != null && res.error != null && res.error.length > 0) {
throw Error(res.error);
}

View file

@ -111,7 +111,7 @@ class FetchRequest {
start() {
log("dispatch FETCH_REQ", this.id, this.url);
const res = dispatch.sendMsg("fetch", {
const res = dispatch.pubInternal("fetch", {
command: pb.Msg.Command.FETCH_REQ,
fetchReqId: this.id,
fetchReqUrl: this.url

14
os.go
View file

@ -3,13 +3,14 @@
package deno
import (
"github.com/golang/protobuf/proto"
"github.com/spf13/afero"
"io/ioutil"
"net/url"
"os"
"path"
"strings"
"github.com/golang/protobuf/proto"
"github.com/spf13/afero"
)
const assetPrefix string = "/$asset$/"
@ -62,9 +63,8 @@ func SrcFileToUrl(filename string) string {
}
return "http://" + rest
} else {
return filename
}
return filename
}
func ResolveModule(moduleSpecifier string, containingFile string) (
@ -79,15 +79,15 @@ func ResolveModule(moduleSpecifier string, containingFile string) (
logDebug("os.go ResolveModule after moduleSpecifier %s containingFile %s",
moduleSpecifier, containingFile)
moduleUrl, err := url.Parse(moduleSpecifier)
moduleURL, err := url.Parse(moduleSpecifier)
if err != nil {
return
}
baseUrl, err := url.Parse(containingFile)
baseURL, err := url.Parse(containingFile)
if err != nil {
return
}
resolved := baseUrl.ResolveReference(moduleUrl)
resolved := baseURL.ResolveReference(moduleURL)
moduleName = resolved.String()
if resolved.IsAbs() {
filename = path.Join(SrcDir, resolved.Host, resolved.Path)

12
os.ts
View file

@ -1,12 +1,12 @@
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
// All rights reserved. MIT License.
import { ModuleInfo } from "./types";
import { sendMsg } from "./dispatch";
import { pubInternal } from "./dispatch";
import { main as pb } from "./msg.pb";
import { assert } from "./util";
export function exit(exitCode = 0): void {
sendMsg("os", {
pubInternal("os", {
command: pb.Msg.Command.EXIT,
exitCode
});
@ -16,7 +16,7 @@ export function codeFetch(
moduleSpecifier: string,
containingFile: string
): ModuleInfo {
const res = sendMsg("os", {
const res = pubInternal("os", {
command: pb.Msg.Command.CODE_FETCH,
codeFetchModuleSpecifier: moduleSpecifier,
codeFetchContainingFile: containingFile
@ -35,7 +35,7 @@ export function codeCache(
sourceCode: string,
outputCode: string
): void {
sendMsg("os", {
pubInternal("os", {
command: pb.Msg.Command.CODE_CACHE,
codeCacheFilename: filename,
codeCacheSourceCode: sourceCode,
@ -44,7 +44,7 @@ export function codeCache(
}
export function readFileSync(filename: string): Uint8Array {
const res = sendMsg("os", {
const res = pubInternal("os", {
command: pb.Msg.Command.READ_FILE_SYNC,
readFileSyncFilename: filename
});
@ -56,7 +56,7 @@ export function writeFileSync(
data: Uint8Array,
perm: number
): void {
sendMsg("os", {
pubInternal("os", {
command: pb.Msg.Command.WRITE_FILE_SYNC,
writeFileSyncFilename: filename,
writeFileSyncData: data,

View file

@ -54,7 +54,7 @@ function setTimer(
cb
};
timers.set(timer.id, timer);
dispatch.sendMsg("timers", {
dispatch.pubInternal("timers", {
command: pb.Msg.Command.TIMER_START,
timerStartId: timer.id,
timerStartInterval: timer.interval,
@ -82,7 +82,7 @@ export function setInterval(
}
export function clearTimer(id: number) {
dispatch.sendMsg("timers", {
dispatch.pubInternal("timers", {
command: pb.Msg.Command.TIMER_CLEAR,
timerClearId: id
});