fixed the context problem + docs...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2018-08-01 00:25:49 +03:00
parent c7ca8ee53e
commit 2fc533cc1c
2 changed files with 6 additions and 12 deletions

View File

@ -340,8 +340,7 @@ ExtendedDiff.types.delete('Text')
The [source code](./diff.js#L1098) is a good example for this as the base `Diff(..)` is built using this API, but note that we are registering types on the `Types` object rather than on the `Diff` itself, there is no functional difference other than how the main code is structured internally. The [source code](./diff.js#L1098) is a good example for this as the base `Diff(..)` is built using this API, but note that we are registering types on the `Types` object rather than on the `Diff` itself, there is no functional difference other than how the main code is structured internally.
*XXX handler context may change in the near future* The handler methods are all called in the context of the `Diff.types` instance, this instance is created per `Diff`'s method call and is destroyed right after the method is done, thus it is save to use the context for caching.
The handler methods are all called in the context of the `Diff.types` object.
To call a different type handler's methods use: To call a different type handler's methods use:
```javascript ```javascript

15
diff.js
View File

@ -1493,12 +1493,10 @@ Types.set('Text', {
}, },
// XXX this is not efficient... // XXX this is not efficient...
// ...find a way to do all the changes in one go... // ...need a way to .join(..) the end result only once (now it
// would be nice to cache this but then we would need a finalize // is done once per change)...
// stage to apply the results...
// XXX add object compatibility checks... // XXX add object compatibility checks...
patch: function(obj, key, change){ patch: function(obj, key, change){
/* XXX this needs to be in the context of the diff/pathc instance...
var cache = this._text_cache = this._text_cache || {} var cache = this._text_cache = this._text_cache || {}
var lines = cache[obj] || obj.split(/\n/) var lines = cache[obj] || obj.split(/\n/)
@ -1506,9 +1504,6 @@ Types.set('Text', {
// XXX do this on the finalize stage... // XXX do this on the finalize stage...
return res.join('\n') return res.join('\n')
//*/
return this.typeCall(Array, 'patch', obj.split(/\n/), key, change)
.join('\n')
}, },
}) })
@ -1725,14 +1720,14 @@ var DiffPrototype = {
// NOTE: this will not mutate this... // NOTE: this will not mutate this...
reverse: function(obj){ reverse: function(obj){
var res = this.clone() var res = this.clone()
res.diff = this.constructor.types.reverse(this.diff) res.diff = Object.create(this.constructor.types).reverse(this.diff)
return res return res
}, },
check: function(obj){ check: function(obj){
return this.constructor.types.check(this.diff, obj) }, return Object.create(this.constructor.types).check(this.diff, obj) },
patch: function(obj){ patch: function(obj){
return this.constructor.types.patch(this, obj) }, return Object.create(this.constructor.types).patch(this, obj) },
unpatch: function(obj){ unpatch: function(obj){
return this.reverse().patch(obj) }, return this.reverse().patch(obj) },