Skip to content

Commit

Permalink
revert changes made by Andrew Lee, fix issue #288
Browse files Browse the repository at this point in the history
  • Loading branch information
ts-thomas committed Oct 3, 2022
1 parent 729e660 commit 858f018
Showing 1 changed file with 35 additions and 23 deletions.
58 changes: 35 additions & 23 deletions src/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,33 @@
import { IndexInterface, DocumentInterface } from "./type.js";
import { create_object, is_string } from "./common.js";

async function lazyExport(callback, self, key, index_doc, index, data){
// Run the callback on the given data
const res = callback(key, JSON.stringify(data));
// If the callback gives a promise, then wait on that
if (res && res["then"]) { await res; }

// Recurse to export the next property
return await self.export(callback, self, key, index_doc, index + 1);
function async(callback, self, key, index_doc, index, data){

setTimeout(function(){

const res = callback(key, JSON.stringify(data));

// await isn't supported by ES5

if(res && res["then"]){

res["then"](function(){

self.export(callback, self, key, index_doc, index + 1);
})
}
else{

self.export(callback, self, key, index_doc, index + 1);
}
});
}

/**
* @this IndexInterface
*/

export async function exportIndex(callback, self, field, index_doc, index){
export function exportIndex(callback, self, field, index_doc, index){

let key, data;

Expand Down Expand Up @@ -69,12 +81,12 @@ export async function exportIndex(callback, self, field, index_doc, index){

default:

// If there are no properties remaining to export, then return an empty promise with
// 'true'
return true;
return;
}

return await lazyExport(callback, self || this, field ? field + "." + key : key, index_doc, index, data);
async(callback, self || this, field ? field + "." + key : key, index_doc, index, data);

return true;
}

/**
Expand Down Expand Up @@ -124,7 +136,7 @@ export function importIndex(key, data){
* @this DocumentInterface
*/

export async function exportDocument(callback, self, field, index_doc, index){
export function exportDocument(callback, self, field, index_doc, index){

index || (index = 0);
index_doc || (index_doc = 0);
Expand All @@ -136,16 +148,16 @@ export async function exportDocument(callback, self, field, index_doc, index){

self = this;

setTimeout(function(){

if(!(await idx.export(callback, self, index ? field/*.replace(":", "-")*/ : "", index_doc, index++))){

index_doc++;
index = 1;
if(!idx.export(callback, self, index ? field/*.replace(":", "-")*/ : "", index_doc, index++)){

await self.export(callback, self, field, index_doc, index);
}
index_doc++;
index = 1;

return true;
self.export(callback, self, field, index_doc, index);
}
});
}
else{

Expand Down Expand Up @@ -173,10 +185,10 @@ export async function exportDocument(callback, self, field, index_doc, index){

default:

return true;
return;
}

return await lazyExport(callback, this, key, index_doc, index, data);
async(callback, this, key, index_doc, index, data);
}
}

Expand Down

0 comments on commit 858f018

Please sign in to comment.