Skip to content

Commit

Permalink
Merge pull request #52 from canjs/major
Browse files Browse the repository at this point in the history
DON'T MERGE: uses can-queues
  • Loading branch information
justinbmeyer authored Jan 27, 2018
2 parents 9fa7f42 + 92bb2ac commit bf7c585
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 96 deletions.
4 changes: 3 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
"start": true,
"stop": true,
"global": true,
"Promise": true
"Promise": true,
"Map": true,
"Set": true
},
"strict": false,
"curly": true,
Expand Down
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
18 changes: 0 additions & 18 deletions build.js

This file was deleted.

147 changes: 104 additions & 43 deletions can-simple-map.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
var Construct = require("can-construct");
var canEvent = require("can-event");
var canBatch = require("can-event/batch/batch");
var assign = require("can-util/js/assign/assign");
var eventQueue = require("can-event-queue/map/map");
var queues = require("can-queues");
var each = require("can-util/js/each/each");
var types = require("can-types");
var Observation = require("can-observation");
var ObservationRecorder = require("can-observation-recorder");
var canReflect = require("can-reflect");
var singleReference = require("can-util/js/single-reference/single-reference");
var CIDMap = require("can-util/js/cid-map/cid-map");
var dev = require("can-log/dev/dev");
var canSymbol = require("can-symbol");

// Ensure the "obj" passed as an argument has an object on @@can.meta
var ensureMeta = function ensureMeta(obj) {
var metaSymbol = canSymbol.for("can.meta");
var meta = obj[metaSymbol];

if (!meta) {
meta = {};
canReflect.setKeyValue(obj, metaSymbol, meta);
}

return meta;
};

// this is a very simple can-map like object
var SimpleMap = Construct.extend(
var SimpleMap = Construct.extend("SimpleMap",
{
// ### setup
// A setup function for the instantiation of a simple-map.
setup: function(initialData){
this._data = {};
this.attr(initialData);
if(initialData && typeof initialData === "object") {
this.attr(initialData);
}
},
// ### attr
// The main get/set interface simple-map.
Expand All @@ -25,10 +38,10 @@ var SimpleMap = Construct.extend(
var self = this;

if(arguments.length === 0 ) {
Observation.add(this,"__keys");
ObservationRecorder.add(this,"can.keys");
var data = {};
each(this._data, function(value, prop){
Observation.add(this, prop);
ObservationRecorder.add(this, prop);
data[prop] = value;
}, this);
return data;
Expand All @@ -37,45 +50,83 @@ var SimpleMap = Construct.extend(
var had = this._data.hasOwnProperty(prop);
var old = this._data[prop];
this._data[prop] = value;
canBatch.start();
if(!had) {
canEvent.dispatch.call(this, "__keys", []);
if(old !== value) {


//!steal-remove-start
if (typeof this._log === "function") {
this._log(prop, value, old);
}
//!steal-remove-end

this.dispatch({
keyChanged: !had ? prop : undefined,
type: prop,
//!steal-remove-start
reasonLog: [ canReflect.getName(this) + "'s", prop, "changed to", value, "from", old ],
//!steal-remove-end
}, [value, old]);
}
canEvent.dispatch.call(this, prop, [value, old]);
canBatch.stop();

}
// 1 argument
else if(typeof prop === 'object') {
queues.batch.start();
canReflect.eachKey(prop, function(value, key) {
self.attr(key, value);
});
queues.batch.stop();
}
else {
if(prop !== "constructor") {
Observation.add(this, prop);
ObservationRecorder.add(this, prop);
return this._data[prop];
}

return this.constructor;
}
},
serialize: function(){
return canReflect.serialize(this, CIDMap);
return canReflect.serialize(this, Map);
},
get: function(){
return this.attr.apply(this, arguments);
},
set: function(){
return this.attr.apply(this, arguments);
}
});
},
// call `.log()` to log all property changes
// pass a single property to only get logs for said property, e.g: `.log("foo")`
log: function(key) {
//!steal-remove-start
var quoteString = function quoteString(x) {
return typeof x === "string" ? JSON.stringify(x) : x;
};

assign(SimpleMap.prototype, canEvent);
var meta = ensureMeta(this);
meta.allowedLogKeysSet = meta.allowedLogKeysSet || new Set();

if(!types.DefaultMap) {
types.DefaultMap = SimpleMap;
}
if (key) {
meta.allowedLogKeysSet.add(key);
}

this._log = function(prop, current, previous, log) {
if (key && !meta.allowedLogKeysSet.has(prop)) {
return;
}
dev.log(
canReflect.getName(this),
"\n key ", quoteString(prop),
"\n is ", quoteString(current),
"\n was ", quoteString(previous)
);
};
//!steal-remove-end
}
}
);

eventQueue(SimpleMap.prototype);

canReflect.assignSymbols(SimpleMap.prototype,{
// -type-
Expand All @@ -87,47 +138,57 @@ canReflect.assignSymbols(SimpleMap.prototype,{
"can.getKeyValue": SimpleMap.prototype.get,
"can.setKeyValue": SimpleMap.prototype.set,
"can.deleteKeyValue": function(prop) {
return this.attr(prop, undefined);
if( this._data.hasOwnProperty(prop) ) {
var old = this._data[prop];
delete this._data[prop];

//!steal-remove-start
if (typeof this._log === "function") {
this._log(prop, undefined, old);
}
//!steal-remove-end
this.dispatch({
keyChanged: prop,
type: prop,
//!steal-remove-start
reasonLog: [ canReflect.getName(this) + "'s", prop, "deleted", old ],
//!steal-remove-end
}, [undefined, old]);
}
},


// -shape
"can.getOwnEnumerableKeys": function(){
Observation.add(this, '__keys');
ObservationRecorder.add(this, 'can.keys');
return Object.keys(this._data);
},

// -shape get/set-
"can.assignDeep": function(source){
canBatch.start();
queues.batch.start();
// TODO: we should probably just throw an error instead of cleaning
canReflect.assignMap(this, source);
canBatch.stop();
queues.batch.stop();
},
"can.updateDeep": function(source){
canBatch.start();
queues.batch.start();
// TODO: we should probably just throw an error instead of cleaning
canReflect.updateMap(this, source);
canBatch.stop();
},
// observable
"can.onKeyValue": function(key, handler){
var translationHandler = function(ev, newValue, oldValue){
handler.call(this, newValue, oldValue);
};
singleReference.set(handler, this, translationHandler, key);

this.addEventListener(key, translationHandler);
},
"can.offKeyValue": function(key, handler){
this.removeEventListener(key, singleReference.getAndDelete(handler, this, key) );
queues.batch.stop();
},
"can.keyHasDependencies": function(key) {
return false;
},
"can.getKeyDependencies": function(key) {
return undefined;
}
},

//!steal-remove-start
"can.getName": function() {
return canReflect.getName(this.constructor) + "{}";
},
//!steal-remove-end
});

// Setup other symbols
Expand Down
Loading

0 comments on commit bf7c585

Please sign in to comment.