Skip to content

Commit

Permalink
Merge pull request #2 from maximecb/litty/autosaving
Browse files Browse the repository at this point in the history
Autosaving & autoloading
  • Loading branch information
maximecb authored Jun 18, 2021
2 parents 7191b18 + ef93697 commit 16d82d6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 46 deletions.
64 changes: 19 additions & 45 deletions public/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,61 +22,23 @@ let audioView = new AudioView(model);
// Create a new project
model.new();

/*
export function importData(jsonData)
{
//console.log('json data:', jsonData);
// Stop playback to avoid glitching
stopPlayback();
// Show the Edit tab before loading the graph,
// so it can resize itself correctly
showTab('edit');
let graph = JSON.parse(jsonData);
editor.load(graph);
}
export function exportData()
{
return JSON.stringify(editor.graph);
}
document.body.onload = function ()
{
let graphData = localStorage.getItem('graph');
if (window.location.hash)
return;

if (graphData && !window.location.hash)
{
console.log('loading saved graph');
let serializedModelData = localStorage.getItem('latestModelData');
if (!serializedModelData)
return;

try
{
importData(graphData);
}
catch (exc)
{
//alert('Graph failed to load');
console.log(exc);
localStorage.removeItem('graph');
editor = new GraphEditor();
editor.newGraph();
//location.reload(false);
}
}
else
{
editor.newGraph();
}
importModel(serializedModelData);
}

window.onunload = function ()
{
// Save the graph when unloading the page
localStorage.setItem('graph', exportData());
localStorage.setItem('latestModelData', model.serialize());
}
*/

window.onkeydown = function (event)
{
Expand Down Expand Up @@ -132,6 +94,18 @@ window.onkeydown = function (event)
}
}

export function importModel(serializedModelData)
{
// Stop playback to avoid glitching
stopPlayback();

if (model.deserialize(serializedModelData)) {
console.log('model restored from previous session');
} else {
console.warn('could not restore model from previous session');
}
}

export function startPlayback()
{
if (playing)
Expand Down
34 changes: 33 additions & 1 deletion public/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ undo, however. It could be more of a direct state update,
or it's a special undoable action.
*/

import { assert, treeCopy, treeEq } from './utils.js';
import { assert, treeCopy, treeEq, isString, isObject } from './utils.js';

/**
* High-level description/scheme for each type of node
Expand Down Expand Up @@ -693,6 +693,38 @@ export class Model
this.broadcast(this.state, null);
}

// Serializes the model into a string representation
serialize()
{
return JSON.stringify({
state: this.state
});
}

// Tries to deserialize a string representation of a model
//
// Returns true if successfully deserialized and loaded, false otherwise
deserialize(data)
{
if (!isString(data)) {
return false;
}

let json;
try {
json = JSON.parse(data);
} catch (e) {
return false;
}

if (!isObject(json) || !isObject(json.state)) {
return false;
}

this.load(json.state);
return true;
}

/** Check if the graph contains a specific type of node */
hasNode(nodeType)
{
Expand Down
16 changes: 16 additions & 0 deletions public/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,22 @@ export function treeEq(a, b)
return a === b;
}

/**
Test that a value is an object
*/
export function isObject(val)
{
return (typeof val === 'object') && (val !== null);
}

/**
Test that a value is a string
*/
export function isString(val)
{
return (typeof val === 'string') || (val instanceof String);
}

/**
Test that a value is integer
*/
Expand Down

0 comments on commit 16d82d6

Please sign in to comment.