Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DON'T MERGE: uses can-queues #52

Merged
merged 39 commits into from
Jan 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
3758a0b
simple map
justinbmeyer Oct 7, 2017
cf1733c
Update dist for release
justinbmeyer Oct 7, 2017
f222c2c
fixing release
justinbmeyer Oct 7, 2017
fa12ad3
4.0.0-pre.2
justinbmeyer Oct 7, 2017
a770bce
points at right versions and includes queues
justinbmeyer Oct 17, 2017
d233fde
4.0.0-pre.3
justinbmeyer Oct 17, 2017
9f20e30
provide can.getName, makeMeta, and reasonLog
Oct 18, 2017
0a73efe
reasonLog logs actual values
Oct 19, 2017
2a176b0
Implement `.log` to log map propert changes
m-mujica Oct 19, 2017
e61ae1f
4.0.0-pre.4
Oct 19, 2017
988bdbe
Merge pull request #53 from canjs/log
m-mujica Oct 19, 2017
acbee03
4.0.0-pre.5
m-mujica Oct 19, 2017
99074a3
remove 'called because'
Oct 23, 2017
768ece9
4.0.0-pre.6
Oct 23, 2017
d92aa96
cleanup dependencies
Oct 26, 2017
d213ec2
4.0.0-pre.7
Oct 27, 2017
08396cd
prevents Observation.add during init
justinbmeyer Nov 5, 2017
ebdf215
4.0.0-pre.4
justinbmeyer Nov 5, 2017
6d34c14
merging
justinbmeyer Nov 5, 2017
7b94779
linting
justinbmeyer Nov 5, 2017
becbf81
4.0.0-pre.8
justinbmeyer Nov 5, 2017
ff6d611
prevent dispatching events if there is no change
justinbmeyer Nov 5, 2017
fb77db7
linting
justinbmeyer Nov 5, 2017
51fa195
4.0.0-pre.9
justinbmeyer Nov 5, 2017
7be5368
Merge branch 'master' into merge-master-to-major
chasenlehara Nov 10, 2017
eddd9ea
Merge pull request #56 from canjs/merge-master-to-major
chasenlehara Nov 10, 2017
0f30be2
4.0.0-pre.10
chasenlehara Nov 10, 2017
b2cb2d0
delete keys
justinbmeyer Nov 13, 2017
b990425
4.0.0-pre.10
justinbmeyer Nov 13, 2017
be6c3ed
Merge branch 'major' of github.com:canjs/can-simple-map into major
justinbmeyer Nov 13, 2017
51fe649
4.0.0-pre.11
justinbmeyer Nov 13, 2017
5274cf1
4.0.0-pre.12
justinbmeyer Nov 13, 2017
a704adf
Use new legacy module path introduced in can-event-queue 0.9.0
chasenlehara Nov 21, 2017
9432c11
4.0.0-pre.13
chasenlehara Nov 21, 2017
f191ad6
import non legacy map bindings
justinbmeyer Dec 13, 2017
098bb29
4.0.0-pre.14
justinbmeyer Dec 13, 2017
8407bea
using can.keys
justinbmeyer Dec 13, 2017
565d40f
4.0.0-pre.15
justinbmeyer Dec 13, 2017
92bb2ac
point deps at latest
justinbmeyer Jan 27, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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