Skip to content

Commit c207b2d

Browse files
added 9pfs.create_file_from_url()
1 parent c97875e commit c207b2d

File tree

5 files changed

+31
-19
lines changed

5 files changed

+31
-19
lines changed

com/isoterminal.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,10 @@ if( typeof AFRAME != 'undefined '){
439439
this.el.addEventListener('exec', (e) => this.term.exec( e.detail ) )
440440
this.el.addEventListener('hook', (e) => this.term.hook( e.detail[0], e.detail[1] ) )
441441
this.el.addEventListener('send', (e) => this.term.send( e.detail[0], e.detail[1] || 0 ) )
442-
this.el.addEventListener('create_file', async (e) => await this.term.worker.create_file( e.detail[0], this.term.convert.toUint8Array(e.detail[1]) ) )
443-
this.el.addEventListener('update_file', async (e) => await this.term.worker.update_file( e.detail[0], this.term.convert.toUint8Array(e.detail[1]) ) )
444-
this.el.addEventListener('append_file', async (e) => await this.term.worker.append_file( e.detail[0], this.term.convert.toUint8Array(e.detail[1]) ) )
442+
this.el.addEventListener('create_file', async (e) => await this.term.worker.create_file( e.detail[0], e.detail[1] ) )
443+
this.el.addEventListener('create_file_from_url', async (e) => await this.term.worker.create_file_from_url( e.detail[0], e.detail[1] ) )
444+
this.el.addEventListener('update_file', async (e) => await this.term.worker.update_file( e.detail[0], e.detail[1] ) )
445+
this.el.addEventListener('append_file', async (e) => await this.term.worker.append_file( e.detail[0], e.detail[1] ) )
445446
this.el.addEventListener('read_file', async (e) => {
446447
const buf = await this.term.worker.read_file( e.detail[0] )
447448
const str = new TextDecoder().decode(buf)

com/isoterminal/ISOTerminal.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@ function ISOTerminal(instance,opts){
1717

1818
ISOTerminal.prototype.emit = function(event,data,sender){
1919
data = data || false
20+
// *TODO* wrap certain events into this.preventFrameDrop( () => { .. }) to boost performance
2021
const evObj = new CustomEvent(event, {detail: data} )
21-
this.preventFrameDrop( () => {
22-
// forward event to worker/instance/AFRAME element or component-function
23-
// this feels complex, but actually keeps event- and function-names more concise in codebase
24-
this.dispatchEvent( evObj )
25-
if( sender != "instance" && this.instance ) this.instance.dispatchEvent(evObj)
26-
if( sender != "worker" && this.worker ) this.worker.postMessage({event,data}, PromiseWorker.prototype.getTransferable(data) )
27-
if( sender !== undefined && typeof this[event] == 'function' ) this[event].apply(this, data && data.push ? data : [data] )
28-
})
22+
// forward event to worker/instance/AFRAME element or component-function
23+
// this feels complex, but actually keeps event- and function-names more concise in codebase
24+
this.dispatchEvent( evObj )
25+
if( sender != "instance" && this.instance ) this.instance.dispatchEvent(evObj)
26+
if( sender != "worker" && this.worker ) this.worker.postMessage({event,data}, PromiseWorker.prototype.getTransferable(data) )
27+
if( sender !== undefined && typeof this[event] == 'function' ) this[event].apply(this, data && data.push ? data : [data] )
2928
}
3029

3130
ISOTerminal.addEventListener = (event,cb) => {

com/isoterminal/PromiseWorker.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ function PromiseWorker(file, onmessage){
4444
this.resolvers = this.resolvers || {last:1,pending:{}}
4545
msg.data.promiseId = this.resolvers.last++
4646
// Send id and task to WebWorker
47-
worker.postMessage(msg, PromiseWorker.prototype.getTransferable(msg.data) )
47+
let dataTransferable = PromiseWorker.prototype.getTransferable(msg.data)
48+
worker.postMessage(msg, dataTransferable )
4849
return new Promise( resolve => this.resolvers.pending[ msg.data.promiseId ] = resolve );
4950
},
5051

@@ -72,6 +73,5 @@ PromiseWorker.prototype.getTransferable = function(data){
7273
for( var i in data ){
7374
if( isTransferable(data[i]) ) objs.push(data[i])
7475
}
75-
if( objs.length ) debugger
7676
return objs.length ? objs : undefined
7777
}

com/isoterminal/feat/9pfs_utils.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ emulator.fs9p.update_file = async function(file,data){
2828
}
2929
}
3030

31+
emulator.fs9p.create_file_from_url = async function(file,url){
32+
const convert = ISOTerminal.prototype.convert
33+
return fetch(url)
34+
.then( (res) => res.arrayBuffer() )
35+
.then( (buf) => {
36+
let arr = new Uint8Array(buf)
37+
return emulator.create_file(file, arr )
38+
})
39+
.catch( console.error )
40+
}
41+
3142
emulator.fs9p.append_file = async function(file,data){
3243
const convert = ISOTerminal.prototype.convert
3344

com/isoterminal/worker.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ importScripts("ISOTerminal.js") // we don't instance it again here (just use it
33

44
this.runISO = async function(opts){
55
this.opts = opts
6-
if( opts.debug ) console.dir(opts)
6+
if( opts.debug ) console.dir(opts)
77

88
if( opts.cdrom && !opts.cdrom.url.match(/^http/) ) opts.cdrom.url = "../../"+opts.cdrom.url
99
if( opts.bzimage && !opts.cdrom.url.match(/^http/) ) opts.bzimage.url = "../../"+opts.bzimage.url
@@ -52,11 +52,12 @@ this.runISO = async function(opts){
5252
arr[0] = String(arr[0]).replace(/^\/mnt/,'')
5353
return arr
5454
}
55-
this.create_file = async function(){ return emulator.create_file.apply(emulator, stripMountDir(arguments[0]) ) }
56-
this.read_file = async function(){ return emulator.read_file.apply(emulator, stripMountDir(arguments[0]) ) }
57-
this.read_file_world = async function(){ return emulator.fs9p.read_file_world.apply(emulator.fs9p, stripMountDir(arguments[0]) ) }
58-
this.append_file = async function(){ emulator.fs9p.append_file.apply(emulator.fs9p, stripMountDir(arguments[0])) }
59-
this.update_file = async function(){ emulator.fs9p.update_file.apply(emulator.fs9p, stripMountDir(arguments[0])) }
55+
this.create_file = async function(){ return emulator.create_file.apply(emulator, stripMountDir(arguments[0]) ) }
56+
this.create_file_from_url = async function(){ return emulator.fs9p.create_file_from_url.apply(emulator, stripMountDir(arguments[0]) ) }
57+
this.read_file = async function(){ return emulator.read_file.apply(emulator, stripMountDir(arguments[0]) ) }
58+
this.read_file_world = async function(){ return emulator.fs9p.read_file_world.apply(emulator.fs9p, stripMountDir(arguments[0]) ) }
59+
this.append_file = async function(){ emulator.fs9p.append_file.apply(emulator.fs9p, stripMountDir(arguments[0])) }
60+
this.update_file = async function(){ emulator.fs9p.update_file.apply(emulator.fs9p, stripMountDir(arguments[0])) }
6061

6162
// filename will be read from 9pfs: "/mnt/"+filename
6263
emulator.readFromPipe = function(filename,cb){

0 commit comments

Comments
 (0)