Comments (7)
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.
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.
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)
<- synchronouslyjsonpack.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.
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.
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.
+1 on true async (non-blocking) support
from jsonpack.
Just run in it a webworker?
from jsonpack.
Related Issues (13)
- Unpacking issue with IE HOT 3
- can't import via bower
- unable to unpack (readable) 'objectId' of mongo docs. help! HOT 1
- Your cpm link is dead
- Suggestion: using zlib deflate dictionary to compress json data HOT 1
- Can not distinguish JSON string and JSON object HOT 2
- A review HOT 1
- Timestamps/Date not working?
- Packing array that is not instance of Array results in a object
- take JavaScript transformation to GoLang
- Other Languages
- TODO: Adding some tests HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonpack.