-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Christopher Baker
committed
Feb 16, 2018
1 parent
e235aca
commit 002cf57
Showing
14 changed files
with
5,516 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/*can-observe@2.0.1#array/array*/ | ||
define([ | ||
'require', | ||
'exports', | ||
'module', | ||
'can-symbol', | ||
'../src/-make-array', | ||
'../src/-make-observe', | ||
'can-event-queue/map', | ||
'../src/-helpers', | ||
'../src/-observable-store', | ||
'../src/-getter-helpers' | ||
], function (require, exports, module) { | ||
var canSymbol = require('can-symbol'); | ||
var makeArray = require('../src/-make-array'); | ||
var makeObserve = require('../src/-make-observe'); | ||
var eventMixin = require('can-event-queue/map'); | ||
var helpers = require('../src/-helpers'); | ||
var observableStore = require('../src/-observable-store'); | ||
var getterHelpers = require('../src/-getter-helpers'); | ||
var definitionsSymbol = canSymbol.for('can.typeDefinitions'); | ||
var computedDefinitionsSymbol = canSymbol.for('can.computedDefinitions'); | ||
var proxyKeys = helpers.assignEverything({}, makeArray.proxyKeys()); | ||
getterHelpers.addMemoizedGetterBindings(proxyKeys); | ||
var ObserveArray; | ||
if (false) { | ||
} else { | ||
var ObserveArray = function (items) { | ||
var prototype = Object.getPrototypeOf(this); | ||
if (prototype[computedDefinitionsSymbol] === undefined) { | ||
prototype[computedDefinitionsSymbol] = getterHelpers.setupComputedProperties(prototype); | ||
} | ||
var instance = this; | ||
var definitions = prototype[definitionsSymbol] || {}; | ||
for (var key in definitions) { | ||
Object.defineProperty(instance, key, definitions[key]); | ||
} | ||
this.push.apply(this, items || []); | ||
var localProxyKeys = Object.create(proxyKeys); | ||
localProxyKeys.constructor = this.constructor; | ||
var observable = makeArray.observable(instance, { | ||
observe: makeObserve.observe, | ||
proxyKeys: localProxyKeys, | ||
shouldRecordObservation: getterHelpers.shouldRecordObservationOnAllKeysExceptFunctionsOnProto | ||
}); | ||
observableStore.proxiedObjects.set(instance, observable); | ||
observableStore.proxies.add(observable); | ||
return observable; | ||
}; | ||
ObserveArray.prototype = Object.create(Array.prototype); | ||
} | ||
eventMixin(ObserveArray.prototype); | ||
getterHelpers.addMethodsAndSymbols(ObserveArray); | ||
ObserveArray.extend = helpers.makeSimpleExtender(ObserveArray); | ||
module.exports = ObserveArray; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/*can-observe@2.0.1#can-observe*/ | ||
define([ | ||
'require', | ||
'exports', | ||
'module', | ||
'./src/-make-object', | ||
'./src/-make-array', | ||
'./src/-make-function', | ||
'./src/-make-observe', | ||
'./object/object', | ||
'./array/array' | ||
], function (require, exports, module) { | ||
var makeObject = require('./src/-make-object'); | ||
var makeArray = require('./src/-make-array'); | ||
var makeFunction = require('./src/-make-function'); | ||
var makeObserve = require('./src/-make-observe'); | ||
var ObserveObject = require('./object/object'); | ||
var ObserveArray = require('./array/array'); | ||
makeObserve.object = function (object) { | ||
return makeObject.observable(object, makeObserve); | ||
}; | ||
makeObserve.array = function (array) { | ||
return makeArray.observable(array, makeObserve); | ||
}; | ||
makeObserve.function = function (array) { | ||
return makeFunction.observable(array, makeObserve); | ||
}; | ||
makeObserve.observe.Object = ObserveObject; | ||
makeObserve.observe.Array = ObserveArray; | ||
module.exports = makeObserve.observe; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/*can-observe@2.0.1#object/object*/ | ||
define([ | ||
'require', | ||
'exports', | ||
'module', | ||
'can-reflect', | ||
'can-symbol', | ||
'../src/-make-observe', | ||
'can-event-queue/map', | ||
'../src/-helpers', | ||
'../src/-make-object', | ||
'../src/-observable-store', | ||
'../src/-getter-helpers' | ||
], function (require, exports, module) { | ||
var canReflect = require('can-reflect'); | ||
var canSymbol = require('can-symbol'); | ||
var makeObserve = require('../src/-make-observe'); | ||
var eventMixin = require('can-event-queue/map'); | ||
var helpers = require('../src/-helpers'); | ||
var makeObject = require('../src/-make-object'); | ||
var observableStore = require('../src/-observable-store'); | ||
var definitionsSymbol = canSymbol.for('can.typeDefinitions'); | ||
var getterHelpers = require('../src/-getter-helpers'); | ||
var computedDefinitionsSymbol = canSymbol.for('can.computedDefinitions'); | ||
var proxyKeys = helpers.assignEverything({}, makeObject.proxyKeys()); | ||
getterHelpers.addMemoizedGetterBindings(proxyKeys); | ||
var ObserveObject = function (props) { | ||
var prototype = Object.getPrototypeOf(this); | ||
if (prototype[computedDefinitionsSymbol] === undefined) { | ||
prototype[computedDefinitionsSymbol] = getterHelpers.setupComputedProperties(prototype); | ||
} | ||
var sourceInstance = this; | ||
var definitions = prototype[definitionsSymbol] || {}; | ||
for (var key in definitions) { | ||
Object.defineProperty(sourceInstance, key, definitions[key]); | ||
} | ||
if (props !== undefined) { | ||
canReflect.assign(sourceInstance, props); | ||
} | ||
var localProxyKeys = Object.create(proxyKeys); | ||
localProxyKeys.constructor = this.constructor; | ||
var observable = makeObject.observable(sourceInstance, { | ||
observe: makeObserve.observe, | ||
proxyKeys: localProxyKeys, | ||
shouldRecordObservation: getterHelpers.shouldRecordObservationOnAllKeysExceptFunctionsOnProto | ||
}); | ||
observableStore.proxiedObjects.set(sourceInstance, observable); | ||
observableStore.proxies.add(observable); | ||
return observable; | ||
}; | ||
eventMixin(ObserveObject.prototype); | ||
getterHelpers.addMethodsAndSymbols(ObserveObject); | ||
ObserveObject.extend = helpers.makeSimpleExtender(ObserveObject); | ||
module.exports = ObserveObject; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/*can-observe@2.0.1#src/-add-get-key-dependencies*/ | ||
define([ | ||
'require', | ||
'exports', | ||
'module', | ||
'can-symbol' | ||
], function (require, exports, module) { | ||
var canSymbol = require('can-symbol'); | ||
var getKeyDependenciesSymbol = canSymbol.for('can.getKeyDependencies'); | ||
var computedDefinitionsSymbol = canSymbol.for('can.computedDefinitions'); | ||
module.exports = function addGetterKeyDependencies(obj) { | ||
obj[getKeyDependenciesSymbol] = function getKeyDependencies(key) { | ||
var computed = this[computedDefinitionsSymbol]; | ||
if (computed) { | ||
var getObservation = computed[key]; | ||
var observationData = getObservation && getObservation(this); | ||
if (observationData != null) { | ||
return { valueDependencies: new Set([observationData.observation]) }; | ||
} | ||
} | ||
}; | ||
return obj; | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/*can-observe@2.0.1#src/-getter-helpers*/ | ||
define([ | ||
'require', | ||
'exports', | ||
'module', | ||
'can-symbol', | ||
'can-event-queue/type', | ||
'can-reflect', | ||
'./-memoize-getter', | ||
'can-queues', | ||
'can-event-queue/map' | ||
], function (require, exports, module) { | ||
var canSymbol = require('can-symbol'); | ||
var definitionsSymbol = canSymbol.for('can.typeDefinitions'); | ||
var typeEventMixin = require('can-event-queue/type'); | ||
var canReflect = require('can-reflect'); | ||
var memoizeGetter = require('./-memoize-getter'); | ||
var queues = require('can-queues'); | ||
var eventMixin = require('can-event-queue/map'); | ||
var computedDefinitionsSymbol = canSymbol.for('can.computedDefinitions'); | ||
var metaSymbol = canSymbol.for('can.meta'); | ||
var getterHelpers = { | ||
ensureTypeDefinition: function (obj) { | ||
var typeDefs = obj.prototype[definitionsSymbol]; | ||
if (!typeDefs) { | ||
typeDefs = obj.prototype[definitionsSymbol] = Object.create(null); | ||
} | ||
return typeDefs; | ||
}, | ||
shouldRecordObservationOnAllKeysExceptFunctionsOnProto: function (keyInfo, meta) { | ||
return meta.preventSideEffects === 0 && !keyInfo.isAccessor && (keyInfo.targetHasOwnKey || !keyInfo.protoHasKey && !Object.isSealed(meta.target) || keyInfo.protoHasKey && typeof targetValue !== 'function'); | ||
}, | ||
addMethodsAndSymbols: function (Type) { | ||
typeEventMixin(Type); | ||
canReflect.assignSymbols(Type, { | ||
'can.defineInstanceKey': function (prop, value) { | ||
getterHelpers.ensureTypeDefinition(this)[prop] = value; | ||
}, | ||
'can.dispatchInstanceBoundChange': function (obj, isBound) { | ||
var meta = this[metaSymbol]; | ||
if (meta) { | ||
var lifecycleHandlers = meta.lifecycleHandlers; | ||
if (lifecycleHandlers) { | ||
queues.enqueueByQueue(lifecycleHandlers.getNode([]), this, [ | ||
obj, | ||
isBound | ||
]); | ||
} | ||
} | ||
} | ||
}); | ||
Type.prototype.addEventListener = function (key, handler, queue) { | ||
var getObservation = this[computedDefinitionsSymbol][key]; | ||
if (getObservation !== undefined) { | ||
getObservation(this).bind(); | ||
} | ||
return eventMixin.addEventListener.call(this, key, handler, queue); | ||
}; | ||
Type.prototype.removeEventListener = function (key, handler, queue) { | ||
var getObservation = this[computedDefinitionsSymbol][key]; | ||
if (getObservation !== undefined) { | ||
getObservation(this).unbind(); | ||
} | ||
return eventMixin.removeEventListener.call(this, key, handler, queue); | ||
}; | ||
}, | ||
setupComputedProperties: function (prototype) { | ||
var computed = {}; | ||
Object.getOwnPropertyNames(prototype).forEach(function (prop) { | ||
var descriptor = Object.getOwnPropertyDescriptor(prototype, prop); | ||
if (descriptor.get !== undefined) { | ||
var getObservationData = memoizeGetter(prototype, prop, descriptor); | ||
computed[prop] = getObservationData; | ||
} | ||
}); | ||
return computed; | ||
}, | ||
addMemoizedGetterBindings: function (proxyKeys) { | ||
return canReflect.assignSymbols(proxyKeys, { | ||
'can.onKeyValue': function (key, handler, queue) { | ||
var getObservation = this[computedDefinitionsSymbol][key]; | ||
if (getObservation !== undefined) { | ||
getObservation(this).bind(); | ||
} | ||
var handlers = this[metaSymbol].handlers; | ||
handlers.add([ | ||
key, | ||
'onKeyValue', | ||
queue || 'notify', | ||
handler | ||
]); | ||
}, | ||
'can.offKeyValue': function (key, handler, queue) { | ||
var getObservation = this[computedDefinitionsSymbol][key]; | ||
if (getObservation !== undefined) { | ||
getObservation(this).unbind(); | ||
} | ||
var handlers = this[metaSymbol].handlers; | ||
handlers.delete([ | ||
key, | ||
'onKeyValue', | ||
queue || 'notify', | ||
handler | ||
]); | ||
} | ||
}); | ||
} | ||
}; | ||
module.exports = getterHelpers; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/*can-observe@2.0.1#src/-helpers*/ | ||
define([ | ||
'require', | ||
'exports', | ||
'module', | ||
'can-symbol' | ||
], function (require, exports, module) { | ||
var canSymbol = require('can-symbol'); | ||
var metaSymbol = canSymbol.for('can.meta'); | ||
var definitionsSymbol = canSymbol.for('can.typeDefinitions'); | ||
var classTest = /^\s*class\s+/; | ||
var helpers = { | ||
assignEverything: function (d, s) { | ||
Object.getOwnPropertyNames(s).concat(Object.getOwnPropertySymbols(s)).forEach(function (key) { | ||
Object.defineProperty(d, key, Object.getOwnPropertyDescriptor(s, key)); | ||
}); | ||
return d; | ||
}, | ||
isBuiltInButNotArrayOrPlainObject: function (obj) { | ||
if (Array.isArray(obj)) { | ||
return false; | ||
} | ||
if (typeof obj === 'function') { | ||
var fnCode = obj.toString(); | ||
if (fnCode.indexOf('[native code]') > 0) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} else { | ||
var toString = Object.prototype.toString.call(obj); | ||
return toString !== '[object Object]' && toString.indexOf('[object ') !== -1; | ||
} | ||
}, | ||
inheritsFromArray: function (obj) { | ||
var cur = obj; | ||
do { | ||
if (Array.isArray(cur)) { | ||
return true; | ||
} | ||
cur = Object.getPrototypeOf(cur); | ||
} while (cur); | ||
return false; | ||
}, | ||
isClass: function (obj) { | ||
return typeof obj === 'function' && classTest.test(obj.toString()); | ||
}, | ||
supportsClass: function () { | ||
try { | ||
eval('"use strict"; class A{};'); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
}(), | ||
makeSimpleExtender: function (BaseType) { | ||
return function etend(name, staticProps, prototypeProps) { | ||
var Type = function () { | ||
var source = this; | ||
var instance = BaseType.apply(this, arguments); | ||
if (source.init) { | ||
instance[metaSymbol].preventSideEffects++; | ||
source.init.apply(instance, arguments); | ||
instance[metaSymbol].preventSideEffects--; | ||
} | ||
return instance; | ||
}; | ||
helpers.assignEverything(Type, BaseType); | ||
helpers.assignEverything(Type, staticProps || {}); | ||
Type.prototype = Object.create(BaseType.prototype); | ||
helpers.assignEverything(Type.prototype, prototypeProps || {}); | ||
Type.prototype.constructor = Type; | ||
Type.prototype[definitionsSymbol] = Object.create(BaseType.prototype[definitionsSymbol] || null); | ||
return Type; | ||
}; | ||
} | ||
}; | ||
module.exports = helpers; | ||
}); |
Oops, something went wrong.