Skip to content

Commit

Permalink
Make restarted-demo track multiple open windows and restore them on r…
Browse files Browse the repository at this point in the history
…estart.
  • Loading branch information
James Kozianski committed Nov 9, 2012
1 parent ecc58ed commit e11dc2a
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 33 deletions.
2 changes: 1 addition & 1 deletion restarted-demo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

When an app is restored after being unexpectedly terminated (eg, when the browser restarts) it will be sent an onRestarted event which should restore the app to the state it was in when it was last running.

This demo app contains a counter which is reset when the app is launched, but preserved across app restarts.
This demo app creates a new counter on launch and restores any existing counters across app restarts.


## APIs
Expand Down
127 changes: 119 additions & 8 deletions restarted-demo/background.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,135 @@
function runApp(startedBy, data) {
// All the counters.
Counter.all = [];

function Counter(id, clicks, startedBy) {
this.id = id;
this.clicks = clicks;
this.startedBy = startedBy;
this.saving = false;
this.listeners = [];
this.save();
Counter.all.push(this);
}

Counter.prototype.attachToWindow = function(win, document) {
win.onClosed.addListener(this.close.bind(this));

var self = this;
document.getElementById('started-by').innerText = this.startedBy;
this.addListener(function(clicks) {
document.getElementById('number').innerText = clicks;
});

document.getElementById('clickButton').addEventListener('click', function(e) {
self.increment();
}, false);

document.getElementById('logLocalStorage').addEventListener('click', function(e) {
logLocalStorage();
}, false);
};

Counter.prototype.addListener = function(l) {
this.listeners.push(l);
this.notifyClickCount();
};

Counter.prototype.clearListeners = function() {
this.listeners = [];
};

Counter.prototype.notifyClickCount = function() {
for (var i = 0; i < this.listeners.length; i++) {
this.listeners[i](this.clicks);
}
};

Counter.prototype.increment = function() {
this.clicks++;
this.notifyClickCount();
this.save();
};

Counter.prototype.save = function() {
if (this.saving)
return;

this.saving = true;

var self = this;
(function(clicks) {
var counters = {};
counters[self.id] = {'clicks': clicks};
var data = {'counters': counters};
chrome.storage.local.get('counters', function(data) {
if (!data.counters) {
data.counters = {};
}
data.counters[self.id] = {'clicks': clicks};
chrome.storage.local.set(data, function() {
self.saving = false;
if (self.clicks != clicks) {
// self.clicks changed while we were saving, so save again.
self.save();
}
});
});
})(this.clicks);
};

Counter.prototype.close = function() {
this.clearListeners();
var self = this;
chrome.storage.local.get('counters', function(data) {
delete data.counters[self.id];
chrome.storage.local.set(data);
});

// Remove self from global list.
var i = Counter.all.indexOf(this);
if (i != -1)
Counter.splice(i, 1);
};

function runApp(counter) {
chrome.app.window.create('main.html', {
id: counter.id + '',
width: 800,
height: 600
}, function(win) {
win.contentWindow.onload = function() {
win.contentWindow.setStartedBy(startedBy);
win.contentWindow.updateData(data);
counter.attachToWindow(win, win.contentWindow.document);
};
});
}

chrome.app.runtime.onLaunched.addListener(function() {
var data = {'clicks': 0};
chrome.storage.local.set(data, function() {
runApp('launched', data);
if (Counter.all.length == 0) {
// We might have left over state from a previous hard shutdown.
chrome.storage.local.clear();
}
chrome.storage.local.get('nextId', function(data) {
if (!data.nextId)
data.nextId = 0;
var id = data.nextId++;
var counter = new Counter(id, 0, 'launched');
runApp(counter);
chrome.storage.local.set(data);
});
});

chrome.app.runtime.onRestarted.addListener(function() {
chrome.storage.local.get('clicks', function(data) {
runApp('restarted', data);
chrome.storage.local.get(null, function(data) {
for (var id in data.counters) {
var clicks = data.counters[id].clicks;
var counter = new Counter(id, clicks, 'restarted');
runApp(counter);
}
});
});

function logLocalStorage() {
chrome.storage.local.get(null, function(data) {
console.log("local storage:", data);
});
}
4 changes: 2 additions & 2 deletions restarted-demo/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
<td>Clicks</td><td id='number'></td>
</tr>
</table>
<input id='button' type='submit' value='Click'></input>
<input id='clickButton' type='submit' value='Click'></input>
<input id='logLocalStorage' type='submit' value='Log local storage'></input>
</body>
<script src='main.js'></script>
22 changes: 0 additions & 22 deletions restarted-demo/main.js

This file was deleted.

0 comments on commit e11dc2a

Please sign in to comment.