Commit fdcc9b4a authored by Avi's avatar Avi

Current state of factory function wip

parent a129cce2
Pipeline #16373 failed with stages
in 12 minutes and 52 seconds
// This doesn't work either,
// const promisify = require('examples/promisify.js');
/* ES6 is not supported yet */
// import * as promisify from "/examples/promisify.js";
const {GLib, Gio} = imports.gi;
imports.searchPath.push('.');
// print(`Please display my stuff: ${imports.searchPath}`);
const {promisify} = imports.promisify;
const cancel = new Gio.Cancellable();
let loop = GLib.MainLoop.new(null, false);
/* This is a workaround for now */
const Gio_File_prototype = Gio.File.new_for_path('').constructor.prototype;
Gio_File_prototype.load_contents_promise = function(...args){
return new Promise((resolve, reject)=>{
this.load_contents_async(...args, function(f, res) {
try {
let result = f.load_contents_finish(res);
resolve(result);
} catch (error) {
reject(error);
}
});
});
}
// const Gio_File_prototype = Gio.File.new_for_path('').constructor.prototype;
// Gio_File_prototype.load_contents_promise = function(...args){
// return new Promise((resolve, reject)=>{
// this.load_contents_async(...args, function(f, res) {
// try {
// let result = f.load_contents_finish(res);
// resolve(result);
// } catch (error) {
// reject(error);
// }
// });
// });
// }
/* This is what it should be once the bug is fixed! */
// Gio.File.prototype.load_contents_promise = function(cancellable){
......@@ -36,8 +43,10 @@ const head = async(filename)=>{
let file = Gio.file_new_for_commandline_arg(filename);
let content_formatted;
try {
let load_contents = promisify.load_contents_async(Gio_File_prototype,load_contents_async,load_contents_finish);
print (`This should be load_contents: ${load_contents}`);
/* To cancel the operation toggle the 'null' with the 'cancel' param */
let [, raw_content] = await file.load_contents_promise(null/*cancel*/);
let [, raw_content] = await file.load_contents(/*nullcancel*/);
content_formatted = raw_content.toString();
print(content_formatted.split('\n', 10).join('\n'));
} catch (error) {
......
const {GLib, Gio} = imports.gi;
// const promisify = ({
// selectFunc,
// ...params
// } = {}) => ({
// selectFunc,
// ...params,
// promisifyFunc(selectFunc){
// return new Promise((resolve,reject)=>{
// });
// }
// });
// Gio.File.prototype.load_contents_promise = promisify(Gio.File.prototype, 'load_contents_async', 'load_contents_finish');
/* Promisify Gio functions for async/await! */
var promisify = ( /* Apparently const isn't supported thus using var */
GioType, /* Gio_File_prototype */
asyncStuff, /* load_contents_async */
finishStuff /* load_contents_finish */
) => {
// Gio_File_prototype.load_contents_promise = function(...args){
return GioType[asyncStuff] = function(...args){
return new Promise((resolve, reject)=>{
/* this[asyncStuff] == this.load_contents_async */
// this.load_contents_async(...args, function(source,res){
this[asyncStuff](...args, function(source) {
try {
/* source[finishStuff] == source.load_contents_finish */
// let result=source.load_contents_finish(res);
let result = source[finishStuff];
resolve(result);
} catch (error) {
reject(error);
}
});
});
}
}
/* Original (This one works) */
// Gio_File_prototype.load_contents_promise = function(...args){
// return new Promise((resolve, reject)=>{
// this.load_contents_async(...args, function(f, res) {
// try {
// let result = f.load_contents_finish(res);
// resolve(result);
// } catch (error) {
// reject(error);
// }
// });
// });
// }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment