Comparing version 0.0.1 to 0.0.2
63
iox.js
// @flow | ||
// | ||
// Refactoring simulate() | ||
// - To see if I can break down the individual functions | ||
// - I don't like the switch statement. It could even be a hashmap | ||
@@ -13,38 +9,15 @@ /*:: | ||
const readEventFromQueue = then => ({ io : 'read-event-from-queue', then }); | ||
const writeToDisk = (value, then) => ({ io : 'write-to-disk', value, then }); | ||
const log = (m, then=() => 1) => ({ io : 'log', m, then }); | ||
function someProcess() /*:IO*/ { | ||
return readEventFromQueue(event => { | ||
if (event === 'END') { | ||
return 'The meaning of life is 41.99999999...'; | ||
} | ||
return writeToDisk(event + ' received', _ => | ||
log('Written event to disk: ' + event, someProcess) | ||
); | ||
}); | ||
} | ||
function makeSimulator() /*:Actions*/ { | ||
const events = ['first event', 'second event', 'END']; | ||
return { | ||
'read-event-from-queue': io => events.shift(), | ||
'write-to-disk': io => | ||
new Promise((resolve, reject) => setTimeout(x => resolve('👍'), 2000)), | ||
log: io => console.log('logging', io) | ||
}; | ||
} | ||
function isIO(value /*: IO | mixed */) { | ||
return Boolean( | ||
!!value && | ||
value != null && | ||
typeof value === 'object' && | ||
typeof value.io === 'string' && | ||
typeof value.then === 'function' | ||
(!value.then || typeof value.then === 'function') | ||
); | ||
} | ||
module.exports.run = run; | ||
function identity(x) { | ||
return x; | ||
} | ||
function run(actions /*:Actions*/, value /*:IO*/) { | ||
@@ -56,16 +29,18 @@ while (true) { | ||
const io /*:IO*/ = value; | ||
const ioFunc = actions[io.io]; | ||
if (ioFunc == null) throw new Error('Unknown io operation' + value.io); | ||
const next = ioFunc(io); | ||
if (next instanceof Promise) | ||
return next.then(io.then).then(v => run(actions, v)); | ||
else value = io.then(); | ||
// Execute io.action | ||
const action = actions[io.io]; | ||
if (action == null) throw new Error('Unknown io operation: ' + value.io); | ||
const next = action(io); | ||
// Call io.then with the result. | ||
const then = io.then || identity; | ||
if (next instanceof Promise) { | ||
return next.then(then).then(v => run(actions, v)); | ||
} else { | ||
value = then(next); | ||
} | ||
} | ||
} | ||
// run(makeSimulator(), writeToDisk('x', r=>r)).then(console.log) | ||
//run(makeSimulator(), log('⧴', _=>writeToDisk('x', r=>r))).then(console.log) | ||
run(makeSimulator(), log('⧴', _ => writeToDisk('x', log))).then(r => console.log('r:', r)) | ||
//console.log(result); | ||
module.exports.run = run; |
{ | ||
"name": "iox", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"main": "iox.js", | ||
@@ -8,7 +8,20 @@ "repository": "git@github.com:pokle/iox.git", | ||
"license": "MIT", | ||
"dependencies": { | ||
"scripts": { | ||
"test": "jest --coverage" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"jest": "21.2.1" | ||
"jest": "21.2.1", | ||
"jsverify": "0.8.3" | ||
}, | ||
"jest": { | ||
"coverageThreshold": { | ||
"global": { | ||
"branches": 100, | ||
"functions": 100, | ||
"lines": 100, | ||
"statements": 100 | ||
} | ||
} | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
48824
16
502
2
1