Last active
August 29, 2015 14:14
-
-
Save IstoraMandiri/c2a30c5f1a723de46663 to your computer and use it in GitHub Desktop.
Sync Experiments with Node and Meteor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Date.now = -> return new Date().getTime() | |
if Meteor.isServer | |
logs = [] | |
Meteor.defer -> | |
logs.push('setImmediate 1') | |
Meteor.setTimeout -> | |
logs.push('setTimeout 1') | |
, 0 | |
Meteor.setTimeout -> | |
Meteor.defer -> | |
logs.push('setImmediate 2') | |
Meteor.setTimeout -> | |
logs.push('setTimeout 2') | |
, 0 | |
, 0 | |
Meteor.setTimeout -> | |
# commenting out this line | |
# changes the order of logs.push being printed | |
# logs.push 'before setTimeout 3' | |
Meteor.defer -> | |
logs.push('setImmediate 3') | |
Meteor.setTimeout -> | |
logs.push('setTimeout 3') | |
, 0 | |
, 1000 | |
logs.push 'before long process' | |
newDate = Date.now() | |
while Date.now() < newDate + 5000 | |
true | |
logs.push 'done long process' | |
Meteor.publish "me", -> | |
newDate = Date.now() | |
Meteor.setTimeout -> | |
while Date.now() < newDate + 3000 | |
truenull | |
, 0 | |
return | |
Meteor.methods | |
printLogs: -> | |
return logs | |
longMethod: -> | |
# unblock has no effect for same client | |
# due to CPU intensive operation | |
# does not block meteor main thread | |
@unblock() | |
newDate = Date.now() | |
console.log 'started LM1' | |
while Date.now() < newDate + 5000 | |
true | |
return 'done1' | |
longMethod2: -> | |
@unblock() | |
newDate = Date.now() | |
console.log 'started LM2' | |
asyncFn = (callback) -> | |
# Set timout will not block the same client | |
# despite having blocking process | |
# probably spawns a fiber | |
Meteor.setTimeout -> | |
while Date.now() < newDate + 5000 | |
true | |
callback null, 'done' | |
, 0 | |
result = do Meteor.wrapAsync(asyncFn) | |
console.log 'done2' | |
return 'done2' | |
longMethod3: -> | |
@unblock() | |
newDate = Date.now() | |
console.log 'started LM3' | |
asyncFn = (callback) -> | |
# defer will block, unlike setTimeout | |
Meteor.defer -> | |
while Date.now() < newDate + 5000 | |
true | |
callback null, 'done' | |
result = do Meteor.wrapAsync(asyncFn) | |
console.log 'done 3' | |
return 'done2' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment