Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

cell types

  • Loading branch information...
commit 781b7862ab190dc43b8b896e4f8f46c81b724685 1 parent 54e7c96
@rranshous authored
View
60 scripts/cell.coffee
@@ -5,8 +5,6 @@ define ['spine'], (spine) ->
@extend Spine.Events
constructor: (@id) ->
- # note our data
- @data = {}
# store a lookup of when it was set
@tokens = {}
@@ -17,33 +15,42 @@ define ['spine'], (spine) ->
# sets the cell's value
# if token is set only sets if given token
# is greater than the stored token
- set: (key, value, token, fire = true) ->
+ set: (key, value, token, fire = true, callback= ->) ->
console.log "cell [set] #{key} #{value} #{token} #{fire}"
- # set the new value
- @data[key] = value
-
# if token is given and older, we're done
- return unless token? or token > @tokens[key]
+ unless token? or token > @tokens[key]
+ # call back w/ success being false
+ callback false, key, value
+ return
# generate a new token for this set op
token = @tokens[key] = @_new_token_id()
- # let the world know
- if fire
- console.log "cells firing set_cell_value"
- @trigger 'set_cell_value',
- key: key,
- value: value,
- token: token
+ # actually set the data
+ @_set key, value, ->
+
+ # let the world know
+ if fire
+ console.log "cells firing set_cell_value"
+ @fire 'set_cell_value',
+ key: key,
+ value: value,
+ token: token
+
+ # call back with much success
+ callback true, key, value
+
+ _set: (key, value, callback= ->) ->
+ callback true, key, value
# gets a value from the
get: (key) ->
@data[key]
# sets multiple values
- set_data: (data) ->
+ set_data: (data, callback= ->) ->
# this can't be used for clearning
unless data?.length
@@ -52,14 +59,23 @@ define ['spine'], (spine) ->
# generate token
token = @_new_token_id()
- # update ourself, don't emit
- @set k, v, token, false for k, v of data
+ # keep track of how many set's we've done
+ total = data.length
+ done = 0
- # let the world know
- @trigger 'set_cell_data',
- id: @id,
- data: data,
- token: token
+ # update ourself, don't emit
+ @set k, v, token, false, (success, key, value) ->
+ done += 1
+
+ # did we finish ?
+ if total == done
+ callback true, data
+
+ # let the world know
+ @fire 'set_cell_data',
+ id: @id,
+ data: data,
+ token: token
# clears all the cell's values
clear: () ->
View
59 scripts/cell.js
@@ -13,7 +13,6 @@
function Cell(id) {
this.id = id;
- this.data = {};
this.tokens = {};
}
@@ -21,44 +20,64 @@
return new Date().getTime();
};
- Cell.prototype.set = function(key, value, token, fire) {
+ Cell.prototype.set = function(key, value, token, fire, callback) {
if (fire == null) {
fire = true;
}
+ if (callback == null) {
+ callback = function() {};
+ }
console.log("cell [set] " + key + " " + value + " " + token + " " + fire);
- this.data[key] = value;
if (!((token != null) || token > this.tokens[key])) {
+ callback(false, key, value);
return;
}
token = this.tokens[key] = this._new_token_id();
- if (fire) {
- console.log("cells firing set_cell_value");
- return this.trigger('set_cell_value', {
- key: key,
- value: value,
- token: token
- });
+ return this._set(key, value, function() {
+ if (fire) {
+ console.log("cells firing set_cell_value");
+ this.fire('set_cell_value', {
+ key: key,
+ value: value,
+ token: token
+ });
+ }
+ return callback(true, key, value);
+ });
+ };
+
+ Cell.prototype._set = function(key, value, callback) {
+ if (callback == null) {
+ callback = function() {};
}
+ return callback(true, key, value);
};
Cell.prototype.get = function(key) {
return this.data[key];
};
- Cell.prototype.set_data = function(data) {
- var k, token, v;
+ Cell.prototype.set_data = function(data, callback) {
+ var done, token, total;
+ if (callback == null) {
+ callback = function() {};
+ }
if (!(data != null ? data.length : void 0)) {
return;
}
token = this._new_token_id();
- for (k in data) {
- v = data[k];
- this.set(k, v, token, false);
- }
- return this.trigger('set_cell_data', {
- id: this.id,
- data: data,
- token: token
+ total = data.length;
+ done = 0;
+ return this.set(k, v, token, false, function(success, key, value) {
+ done += 1;
+ if (total === done) {
+ callback(true, data);
+ return this.fire('set_cell_data', {
+ id: this.id,
+ data: data,
+ token: token
+ });
+ }
});
};
View
13 scripts/memory_cell.coffee
@@ -0,0 +1,13 @@
+
+define ['cell'], (Cell) ->
+
+ class MemoryCell extends Cell
+
+ constructor: () ->
+ # store our cell's values
+ @data = {}
+ super arguments
+
+ _set: (key, value, callback= ->) ->
+ @data[key] = value
+ callback true, key, value
View
16 simpleserver.coffee
@@ -1,6 +1,11 @@
+
+requirejs = require('requirejs')
+
+requirejs ['spine', 'socket.io', 'express','mediator', 'cell' ],
+(spine, io, express, mediator, cell) ->
+
+#### HOSTING #####
# Web server
-io = require 'socket.io'
-express = require 'express'
app = express()
server = app.listen 8080
# host our statics
@@ -8,6 +13,7 @@ server = app.listen 8080
app.use express.static __dirname + '/'
# socket IO layer
io = io.listen server
+#### END HOSTING ####
# helper method for cell ids
last_cell_id = 0
@@ -15,12 +21,6 @@ next_cell_id = ->
last_cell_id += 1
last_cell_id
-Spine = require 'spine'
-class CellsLookup extends Spine.Module
- @include Spine.Events
-cells = new CellsLookup()
-
-
# set single cell value
# callback: id, key, value, old_value
set_cell_value = (id, key, value, emit, callback) ->
View
28 simpleserver.js
@@ -1,12 +1,10 @@
// Generated by CoffeeScript 1.3.3
(function() {
- var CellsLookup, Spine, app, cells, express, get_cell_data, get_cell_value, io, last_cell_id, next_cell_id, server, set_cell_data, set_cell_value,
- __hasProp = {}.hasOwnProperty,
- __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
+ var app, get_cell_data, get_cell_value, io, last_cell_id, next_cell_id, requirejs, server, set_cell_data, set_cell_value;
- io = require('socket.io');
+ requirejs = require('requirejs');
- express = require('express');
+ requirejs(['spine', 'socket.io', 'express', 'mediator', 'cell'], function(spine, io, express, mediator, cell) {});
app = express();
@@ -23,24 +21,6 @@
return last_cell_id;
};
- Spine = require('spine');
-
- CellsLookup = (function(_super) {
-
- __extends(CellsLookup, _super);
-
- function CellsLookup() {
- return CellsLookup.__super__.constructor.apply(this, arguments);
- }
-
- CellsLookup.include(Spine.Events);
-
- return CellsLookup;
-
- })(Spine.Module);
-
- cells = new CellsLookup();
-
set_cell_value = function(id, key, value, emit, callback) {
var cell, old_value;
console.log("setting cell value " + id + " " + key + " " + value + " " + emit);
@@ -100,7 +80,7 @@
get_cell_value = function(id, key, callback) {
var _ref;
- return callback(id, cells != null ? (_ref = cells.id) != null ? _ref.key : void 0 : void 0);
+ return callback(id, typeof cells !== "undefined" && cells !== null ? (_ref = cells.id) != null ? _ref.key : void 0 : void 0);
};
io.sockets.on('connection', function(socket) {
Please sign in to comment.
Something went wrong with that request. Please try again.