Created
April 7, 2016 14:50
-
-
Save kenchris/e61dab39fc04bd6371178162b027f482 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) 2014 Intel Corporation. All rights reserved. | |
// Use of this source code is governed by a BSD-style license that can be | |
// found in the LICENSE file. | |
let IsolatedFileSystem = requireNative('isolated_file_system'); | |
let listeners = {}; | |
function addMessageListener(fn) { | |
var uid = new Date().getTime() + (parseInt(Math.random() * 100)).toString(); | |
listeners[uid] = fn; | |
return uid; | |
} | |
function removeMessageListener(uid) { | |
delete listeners[uid]; | |
} | |
extension.setMessageListener(function(msg) { | |
var json = JSON.parse(msg); | |
if (json.uid && listeners[uid]) { | |
listeners[uid](json); | |
} | |
}); | |
function sendMessage(message) { | |
return new Promise((resolve, reject) => { | |
message.uid = addMessageListener(event => { | |
removeMessageListener(message.uid); | |
if (event.data.error) { | |
reject(event.data.error); | |
} else { | |
resolve(event.data); | |
} | |
}); | |
extension.postMessage(message); | |
}); | |
} | |
class NativeFileSystem { | |
constructor() { | |
} | |
requestNativeFileSystem(path, success, error) { | |
sendMessage({ | |
data: { | |
cmd: "requestNativeFileSystem", | |
virtual_root: path | |
} | |
}).then(data => { | |
let fs = IsolatedFileSystem.getIsolatedFileSystem(data.file_system_id); | |
success(fs); | |
}).catch(err => { | |
error(err); | |
}); | |
} | |
getDirectoryList() { | |
extension.internal.sendSyncMessage("get"); | |
} | |
getRealPath(virtual_root) { | |
var _msg = { | |
cmd : "getRealPath", | |
path : virtual_root | |
} | |
return extension.internal.sendSyncMessage(_msg); | |
} | |
} | |
exports = new NativeFileSystem(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment