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

View file

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

14
os.go
View file

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

12
os.ts
View file

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

View file

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