GithubHelp home page GithubHelp logo

Asynchronous Packing and Unpacking about jsonpack HOT 7 OPEN

rgcl avatar rgcl commented on August 20, 2024
Asynchronous Packing and Unpacking

from jsonpack.

Comments (7)

quintopia avatar quintopia commented on August 20, 2024 2

Maybe I am not very knowledgeable about javascript, but I'm not sure how
you can make jsonpack.pack return before it is finished packing without
actually modifying jsonpack.pack. To me it looks like this
jsonpackPackAsync just runs jsonpack.pack after the I/O queue is clear, and
then doesn't give up cpu control until the json is completely packed. Which
means all events that get queued after jsonpack.pack gets called will have
to wait on it, freezing the page. Am I incorrect?

On Thu, Aug 6, 2015 at 11:41 PM, Rodrigo Gonzalez [email protected]
wrote:

You can convert any function to asynchronously simply with setTimeout,
example:

function jsonpackPackAsync(json, next) {
setTimeout(function() {
var packed;
try {
packed = jsonpack.pack(json)
} catch(e) {
next(e);
}
if(Boolean(packed)) {
next(null, packed)
}
}, 0)
}
// Example of use:
jsonpackPackAsync(json, function(err, packed) {
console.log(packed);
})

In any case it is a good idea! I think the ideal would add the methods jsonpack.packAsync(json,
callback) and jsonpack.unpackAsync(packed, callback)

What do you think?

(Consider setImmediate http://stackoverflow.com/a/24119936/1044759 )


Reply to this email directly or view it on GitHub
#14 (comment).

Regards,
David Rutter

from jsonpack.

rgcl avatar rgcl commented on August 20, 2024

You can convert any function to asynchronously simply with setTimeout, example:

function jsonpackPackAsync(json, next) {
  setTimeout(function() {
    var packed;
    try {
      packed = jsonpack.pack(json)
    } catch(e) {
       next(e);
    }
    if(Boolean(packed)) {
      next(null, packed)
    }
  }, 0)
}

// Example of use:
jsonpackPackAsync(json, function(err, packed) {
  console.log(packed);
})

In any case it is a good idea! I think the ideal would add the methods jsonpack.packAsync(json, callback) and jsonpack.unpackAsync(packed, callback)

What do you think?

(Consider setImmediate )

from jsonpack.

rgcl avatar rgcl commented on August 20, 2024

Good question, depend of the virtual machine where javascript run, but all have in common that uses a continuous event loop in a single thread, that loop simulates multithread in a single thread, so the important topic here is not to block that only one thread.

I think that with setImmediate or (if setImmediate is not implemented) setInterval we already not block the thread (only block the "soft" thread).

Of course, we can use a grid but it is out of the scope of jsonpack at the moment (that can be supplied "ad hoc" like anything else :)

The native implementation in C is an interesting TODO (If anyone is encouraged would be great!)

After thinking i think that jsonpack can be sync and async:

  • jsonpack.pack(json) <- synchronously
  • jsonpack.pack(json, callback) <- asynchronously

In that case, this two examples will be valid:

// sync:
var packed = jsonpack.pack({ name: 'pepe' });
var json = jsonpack.unpack(packed);

// async
jsonpack.pack({ name: 'pepe' }, function(err, packed) {
  jsonpack.unpack(packed, function(err, json) {
    // json
  });
})

If the last parameter is a function, then work in async mode, else in sync mode.

from jsonpack.

rgcl avatar rgcl commented on August 20, 2024

Anyway I'm also not sure 100% about this, before approving this I need to do some tests with large files to see if the asynchronously blocks the rest of the I / O events (as you say). Just in case ...

from jsonpack.

quintopia avatar quintopia commented on August 20, 2024

I like the idea of an overloaded pack and unpack. (moreso the unpack, of
course). I'm not clear on the purpose of the callback parameter though.
What should the function you pass be for? What does it do?
On Aug 16, 2015 2:48 AM, "Rodrigo Gonzalez" [email protected]
wrote:

Good question, depend of the virtual machine where javascript run, but all
have in common that uses a continuous event loop in a single thread, that
loop simulates multithread in a single thread, so the important topic here
is not to block that only one thread.

I think that with setImmediate or (if setImmediate is not implemented)
setInterval we already not block the thread (only block the "soft" thread).

Of course, we can use a grid but it is out of the scope of jsonpack at the
moment (that can be supplied "ad hoc" like anything else :)

The native implementation in C is an interesting TODO (If anyone is
encouraged would be great!)

After thinking i think that jsonpack can be sync and async:

  • jsonpack.pack(json) <- synchronously
  • jsonpack.pack(json, callback) <- asynchronously

In that case, this two examples will be valid:

// sync:var packed = jsonpack.pack({ name: 'pepe' });var json = jsonpack.unpack(packed);
// async
jsonpack.pack({ name: 'pepe' }, function(err, packed) {
jsonpack.unpack(packed, function(err, json) {
// json
});
})

If the last parameter is a function, then work in async mode, else in sync
mode.


Reply to this email directly or view it on GitHub
#14 (comment).

from jsonpack.

bchr02 avatar bchr02 commented on August 20, 2024

+1 on true async (non-blocking) support

from jsonpack.

deviousasti avatar deviousasti commented on August 20, 2024

Just run in it a webworker?

from jsonpack.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.