Skip to content

Instantly share code, notes, and snippets.

@brysonian
Last active December 27, 2015 08:39
Show Gist options
  • Select an option

  • Save brysonian/7298465 to your computer and use it in GitHub Desktop.

Select an option

Save brysonian/7298465 to your computer and use it in GitHub Desktop.
Prototype of idea for preload function in p5js.
[
{
"id": 0,
"species": "Capra hircus",
"name": "Goat"
},
{
"id": 1,
"species": "Panthera pardus",
"name": "Leopard"
},
{
"id": 2,
"species": "Equus zebra",
"name": "Zebra"
}
]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>load test</title>
</head>
<body>
<p>Open your console to see anything. Uses <a href="https://github.com/ded/reqwest">reqwest</a> for ajax calls.</p>
<!-- reqwest hosted on my server -->
<script src="http://aslsp.com/reqwest.min.js"></script>
<script src="main.js"></script>
</body>
</html>
// =======================================================
// This is just a little module to simulate the behavior
// of swizzling the loadJSON method for calls made in the
// preload function.
//
// I use https://github.com/ded/reqwest for the ajax
// calls.
// =======================================================
var MockApp = (function(reqwest, exports) {
var preload_count = 0;
exports.run = function() {
if (typeof preload == 'function') {
exports.loadJSON = preloadJSON;
preload();
exports.loadJSON = loadJSON;
} else {
exports.loadJSON = loadJSON;
setup();
}
};
// just to make sure i can call other things
exports.println = function(msg) {
console.log(msg);
};
// JSON sketches
function loadJSON(url, callback) {
var self = [];
reqwest(url, function (resp) {
for (var k in resp) self[k] = resp[k];
callback(resp);
});
return self;
}
function preloadJSON(url) {
preload_count++;
return loadJSON(url, function (resp) {
if (--preload_count === 0) setup();
});
}
return exports;
})(reqwest, window);
// =======================================================
// Simple "sketch" with both preloaded and async loading
// of JSON.
// =======================================================
var animals, lion;
function preload() {
animals = loadJSON('array.json');
}
function setup() {
console.log("Preloaded json is: ", animals);
println("Printed before an async call.");
lion = loadJSON('object.json', function(resp) {
console.log("Async call is done. I loaded: ", resp);
});
println("Printed right after the async call, but not in the callback.");
}
// =======================================================
// run the mock app
// =======================================================
MockApp.run();
{
"id": 0,
"species": "Panthera leo",
"name": "Lion"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment