jbenet / transformer

transformer - multiformat data conversion
transform.datadex.io
130 stars 7 forks source link

async conversions #11

Closed jbenet closed 10 years ago

jbenet commented 10 years ago

Some conversions will be async, for example us-street-address-to-us-city will use node-geocoder. The Api needs to move from

var conversion = transformer('typeA', 'typeB');
var dataB = conversion(dataA);

to

var conversion = transformer('typeA', 'typeB');
conversion(dataA, function(dataB) {
  // ...
});

(Unless fibers are used).

jbenet commented 10 years ago

Done, now all async.

jbenet commented 10 years ago

Damn. switching to conversions broke the nice api. can no longer do:

var transformer = require('dat-transformer');
var iso2js = transformer('iso-date', 'js-date');
console.log(iso2js('2014-04-26T12:02:23.000Z'));

Now have to do

var transformer = require('dat-transformer');
var iso2js = transformer('iso-date', 'js-date');
return iso2js('2014-04-26T12:02:23.000Z', function(err, out) {
  console.log(out);
});

Which really sucks for simple things. I may not be the best thing to force all conversions to be async. The problem is some have to, and detecting + composing sync + async correctly will get complicated.

Maybe promises is the right thing to do here (ew).

jbenet commented 10 years ago

Or having an optimistic sync call that only resolves sync conversions.

jbenet commented 10 years ago

Basically, make both of these possible:

var transformer = require('dat-transformer');
var iso2js = transformer.sync('iso-date', 'js-date');
console.log(iso2js('2014-04-26T12:02:23.000Z'));
var transformer = require('dat-transformer');
var iso2js = transformer.async('iso-date', 'js-date');
iso2js('2014-04-26T12:02:23.000Z', function(err, out) {
  console.log(out);
});
jbenet commented 10 years ago

Ok, so this interface now works. Supports:

Error: async conversion foo-to-bar should take 2 args (input, callback):
function convert(foo) {
  return 'bar';
}
Error: sync conversion foo-to-bar should take 1 arg (input):
function convert(foo, callback) {
  return 'bar';
}

(thanks for the help @mafintosh @maxogden @groundwater!)

groundwater commented 10 years ago

:+1: glad to have helped