Comparing version 0.6.28 to 0.6.29
{ | ||
"name": "aurumjs", | ||
"version": "0.6.28", | ||
"version": "0.6.29", | ||
"description": "Stream based declarative DOM rendering library for javascript", | ||
@@ -5,0 +5,0 @@ "main": "prebuilt/cjs/aurumjs.js", |
import { CancellationToken } from '../utilities/cancellation_token'; | ||
import { Callback } from '../utilities/common'; | ||
import { ArrayDataSource, ReadOnlyArrayDataSource } from './data_source'; | ||
@@ -6,6 +7,19 @@ export declare type GenericTree<T, K extends keyof T> = { | ||
}; | ||
export interface TreeChange<T> { | ||
parentNode: T; | ||
changedNode: T; | ||
index: number; | ||
operation: 'added' | 'deleted'; | ||
} | ||
export declare class TreeDataSource<T, K extends keyof T> { | ||
private childrenKey; | ||
private roots; | ||
private updateEvent; | ||
private watchCount; | ||
private watchToken; | ||
constructor(childrenKey: K, roots: Array<GenericTree<T, K>> | ArrayDataSource<GenericTree<T, K>>); | ||
private watch; | ||
private watchHandleChange; | ||
listen(callback: Callback<TreeChange<T>>, cancellationToken: CancellationToken): Callback<void>; | ||
listenAndRepeat(callback: Callback<TreeChange<T>>, cancellationToken: CancellationToken): Callback<void>; | ||
private adaptNodeList; | ||
@@ -18,4 +32,5 @@ private adaptNodeTree; | ||
[Symbol.iterator](): IterableIterator<T>; | ||
private iterateLevelWithParentRef; | ||
private iterateLevel; | ||
} | ||
//# sourceMappingURL=tree_data_source.d.ts.map |
@@ -5,8 +5,107 @@ "use strict"; | ||
const cancellation_token_1 = require("../utilities/cancellation_token"); | ||
const event_emitter_1 = require("../utilities/event_emitter"); | ||
const data_source_1 = require("./data_source"); | ||
class TreeDataSource { | ||
constructor(childrenKey, roots) { | ||
this.watchCount = 0; | ||
this.childrenKey = childrenKey; | ||
this.roots = data_source_1.ArrayDataSource.toArrayDataSource(roots); | ||
this.updateEvent = new event_emitter_1.EventEmitter(); | ||
} | ||
watch(cancellationToken) { | ||
this.watchCount++; | ||
cancellationToken.addCancelable(() => { | ||
this.watchCount--; | ||
if (this.watchCount === 0) { | ||
this.watchToken.cancel(); | ||
this.watchToken = undefined; | ||
} | ||
}); | ||
if (!this.watchToken) { | ||
this.watchToken = new cancellation_token_1.CancellationToken(); | ||
const watchMap = new Map(); | ||
if (this.roots instanceof data_source_1.ArrayDataSource) { | ||
this.roots.listen((change) => { | ||
this.watchHandleChange(change, undefined, watchMap); | ||
}, this.watchToken); | ||
} | ||
for (const root of this.roots) { | ||
for (const { node } of this.iterateLevelWithParentRef(root)) { | ||
if (node[this.childrenKey] instanceof data_source_1.ArrayDataSource) { | ||
watchMap.set(node, new cancellation_token_1.CancellationToken()); | ||
this.watchToken.chain(watchMap.get(node)); | ||
node[this.childrenKey].listenAndRepeat((change) => { | ||
this.watchHandleChange(change, node, watchMap); | ||
}, watchMap.get(node)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
watchHandleChange(change, parent, watchMap) { | ||
var _a; | ||
switch (change.operation) { | ||
case 'add': | ||
let i = 0; | ||
for (const item of change.items) { | ||
this.updateEvent.fire({ | ||
changedNode: item, | ||
index: change.index + i++, | ||
parentNode: parent, | ||
operation: 'added' | ||
}); | ||
if (item[this.childrenKey] instanceof data_source_1.ArrayDataSource) { | ||
watchMap.set(item, new cancellation_token_1.CancellationToken()); | ||
this.watchToken.chain(watchMap.get(item)); | ||
item[this.childrenKey].listenAndRepeat((change) => { | ||
this.watchHandleChange(change, item, watchMap); | ||
}, watchMap.get(item)); | ||
} | ||
} | ||
break; | ||
case 'remove': | ||
let j = 0; | ||
for (const item of change.items) { | ||
(_a = watchMap.get(item)) === null || _a === void 0 ? void 0 : _a.cancel(); | ||
this.updateEvent.fire({ | ||
changedNode: item, | ||
index: change.index + j++, | ||
parentNode: parent, | ||
operation: 'deleted' | ||
}); | ||
} | ||
break; | ||
case 'merge': | ||
throw new Error('Not implemented'); | ||
case 'replace': | ||
this.updateEvent.fire({ | ||
changedNode: change.target, | ||
index: change.index, | ||
parentNode: parent, | ||
operation: 'deleted' | ||
}); | ||
this.updateEvent.fire({ | ||
changedNode: change.items[0], | ||
index: change.index, | ||
parentNode: parent, | ||
operation: 'added' | ||
}); | ||
break; | ||
} | ||
} | ||
listen(callback, cancellationToken) { | ||
this.watch(cancellationToken); | ||
return this.updateEvent.subscribe(callback, cancellationToken).cancel; | ||
} | ||
listenAndRepeat(callback, cancellationToken) { | ||
for (const { parent, node, index } of this.iterateLevelWithParentRef(this.roots)) { | ||
callback({ | ||
changedNode: node, | ||
index, | ||
parentNode: parent, | ||
operation: 'added' | ||
}); | ||
} | ||
return this.listen(callback, cancellationToken); | ||
} | ||
adaptNodeList(nodes, token, nodeList = new data_source_1.ArrayDataSource()) { | ||
@@ -18,3 +117,3 @@ nodes.listenAndRepeat((change) => { | ||
for (const item of change.items) { | ||
this.addItem(adaptMap, item, nodeList); | ||
this.addItem(adaptMap, token, item, nodeList); | ||
} | ||
@@ -31,3 +130,3 @@ break; | ||
this.removeItem(nodeList, adaptMap, change.target); | ||
this.addItem(adaptMap, change.items[0], nodeList); | ||
this.addItem(adaptMap, token, change.items[0], nodeList); | ||
break; | ||
@@ -64,5 +163,6 @@ } | ||
} | ||
addItem(adaptMap, item, nodeList) { | ||
addItem(adaptMap, parentToken, item, nodeList) { | ||
nodeList.push(item); | ||
adaptMap.set(item, new cancellation_token_1.CancellationToken()); | ||
parentToken.chain(adaptMap.get(item)); | ||
const list = data_source_1.ArrayDataSource.toArrayDataSource(item[this.childrenKey]); | ||
@@ -84,2 +184,9 @@ this.adaptNodeList(list, adaptMap.get(item), nodeList); | ||
} | ||
*iterateLevelWithParentRef(level, parent, index = 0) { | ||
yield { node: level, parent, index }; | ||
let i = 0; | ||
for (const child of level[this.childrenKey]) { | ||
yield* this.iterateLevelWithParentRef(child, level, i++); | ||
} | ||
} | ||
*iterateLevel(level) { | ||
@@ -86,0 +193,0 @@ yield level; |
@@ -71,6 +71,14 @@ "use strict"; | ||
chain(target, twoWays = false) { | ||
const cancelable = () => target.cancel(); | ||
if (twoWays) { | ||
target.chain(this, false); | ||
} | ||
this.addCancelable(() => target.cancel()); | ||
else { | ||
target.addCancelable(() => { | ||
if (!this.isCanceled) { | ||
this.removeCancelable(cancelable); | ||
} | ||
}); | ||
} | ||
this.addCancelable(cancelable); | ||
return this; | ||
@@ -77,0 +85,0 @@ } |
import { CancellationToken } from '../utilities/cancellation_token'; | ||
import { Callback } from '../utilities/common'; | ||
import { ArrayDataSource, ReadOnlyArrayDataSource } from './data_source'; | ||
@@ -6,6 +7,19 @@ export declare type GenericTree<T, K extends keyof T> = { | ||
}; | ||
export interface TreeChange<T> { | ||
parentNode: T; | ||
changedNode: T; | ||
index: number; | ||
operation: 'added' | 'deleted'; | ||
} | ||
export declare class TreeDataSource<T, K extends keyof T> { | ||
private childrenKey; | ||
private roots; | ||
private updateEvent; | ||
private watchCount; | ||
private watchToken; | ||
constructor(childrenKey: K, roots: Array<GenericTree<T, K>> | ArrayDataSource<GenericTree<T, K>>); | ||
private watch; | ||
private watchHandleChange; | ||
listen(callback: Callback<TreeChange<T>>, cancellationToken: CancellationToken): Callback<void>; | ||
listenAndRepeat(callback: Callback<TreeChange<T>>, cancellationToken: CancellationToken): Callback<void>; | ||
private adaptNodeList; | ||
@@ -18,4 +32,5 @@ private adaptNodeTree; | ||
[Symbol.iterator](): IterableIterator<T>; | ||
private iterateLevelWithParentRef; | ||
private iterateLevel; | ||
} | ||
//# sourceMappingURL=tree_data_source.d.ts.map |
import { CancellationToken } from '../utilities/cancellation_token'; | ||
import { EventEmitter } from '../utilities/event_emitter'; | ||
import { ArrayDataSource } from './data_source'; | ||
@@ -6,6 +7,105 @@ export class TreeDataSource { | ||
roots; | ||
updateEvent; | ||
watchCount = 0; | ||
watchToken; | ||
constructor(childrenKey, roots) { | ||
this.childrenKey = childrenKey; | ||
this.roots = ArrayDataSource.toArrayDataSource(roots); | ||
this.updateEvent = new EventEmitter(); | ||
} | ||
watch(cancellationToken) { | ||
this.watchCount++; | ||
cancellationToken.addCancelable(() => { | ||
this.watchCount--; | ||
if (this.watchCount === 0) { | ||
this.watchToken.cancel(); | ||
this.watchToken = undefined; | ||
} | ||
}); | ||
if (!this.watchToken) { | ||
this.watchToken = new CancellationToken(); | ||
const watchMap = new Map(); | ||
if (this.roots instanceof ArrayDataSource) { | ||
this.roots.listen((change) => { | ||
this.watchHandleChange(change, undefined, watchMap); | ||
}, this.watchToken); | ||
} | ||
for (const root of this.roots) { | ||
for (const { node } of this.iterateLevelWithParentRef(root)) { | ||
if (node[this.childrenKey] instanceof ArrayDataSource) { | ||
watchMap.set(node, new CancellationToken()); | ||
this.watchToken.chain(watchMap.get(node)); | ||
node[this.childrenKey].listenAndRepeat((change) => { | ||
this.watchHandleChange(change, node, watchMap); | ||
}, watchMap.get(node)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
watchHandleChange(change, parent, watchMap) { | ||
switch (change.operation) { | ||
case 'add': | ||
let i = 0; | ||
for (const item of change.items) { | ||
this.updateEvent.fire({ | ||
changedNode: item, | ||
index: change.index + i++, | ||
parentNode: parent, | ||
operation: 'added' | ||
}); | ||
if (item[this.childrenKey] instanceof ArrayDataSource) { | ||
watchMap.set(item, new CancellationToken()); | ||
this.watchToken.chain(watchMap.get(item)); | ||
item[this.childrenKey].listenAndRepeat((change) => { | ||
this.watchHandleChange(change, item, watchMap); | ||
}, watchMap.get(item)); | ||
} | ||
} | ||
break; | ||
case 'remove': | ||
let j = 0; | ||
for (const item of change.items) { | ||
watchMap.get(item)?.cancel(); | ||
this.updateEvent.fire({ | ||
changedNode: item, | ||
index: change.index + j++, | ||
parentNode: parent, | ||
operation: 'deleted' | ||
}); | ||
} | ||
break; | ||
case 'merge': | ||
throw new Error('Not implemented'); | ||
case 'replace': | ||
this.updateEvent.fire({ | ||
changedNode: change.target, | ||
index: change.index, | ||
parentNode: parent, | ||
operation: 'deleted' | ||
}); | ||
this.updateEvent.fire({ | ||
changedNode: change.items[0], | ||
index: change.index, | ||
parentNode: parent, | ||
operation: 'added' | ||
}); | ||
break; | ||
} | ||
} | ||
listen(callback, cancellationToken) { | ||
this.watch(cancellationToken); | ||
return this.updateEvent.subscribe(callback, cancellationToken).cancel; | ||
} | ||
listenAndRepeat(callback, cancellationToken) { | ||
for (const { parent, node, index } of this.iterateLevelWithParentRef(this.roots)) { | ||
callback({ | ||
changedNode: node, | ||
index, | ||
parentNode: parent, | ||
operation: 'added' | ||
}); | ||
} | ||
return this.listen(callback, cancellationToken); | ||
} | ||
adaptNodeList(nodes, token, nodeList = new ArrayDataSource()) { | ||
@@ -17,3 +117,3 @@ nodes.listenAndRepeat((change) => { | ||
for (const item of change.items) { | ||
this.addItem(adaptMap, item, nodeList); | ||
this.addItem(adaptMap, token, item, nodeList); | ||
} | ||
@@ -30,3 +130,3 @@ break; | ||
this.removeItem(nodeList, adaptMap, change.target); | ||
this.addItem(adaptMap, change.items[0], nodeList); | ||
this.addItem(adaptMap, token, change.items[0], nodeList); | ||
break; | ||
@@ -63,5 +163,6 @@ } | ||
} | ||
addItem(adaptMap, item, nodeList) { | ||
addItem(adaptMap, parentToken, item, nodeList) { | ||
nodeList.push(item); | ||
adaptMap.set(item, new CancellationToken()); | ||
parentToken.chain(adaptMap.get(item)); | ||
const list = ArrayDataSource.toArrayDataSource(item[this.childrenKey]); | ||
@@ -83,2 +184,9 @@ this.adaptNodeList(list, adaptMap.get(item), nodeList); | ||
} | ||
*iterateLevelWithParentRef(level, parent, index = 0) { | ||
yield { node: level, parent, index }; | ||
let i = 0; | ||
for (const child of level[this.childrenKey]) { | ||
yield* this.iterateLevelWithParentRef(child, level, i++); | ||
} | ||
} | ||
*iterateLevel(level) { | ||
@@ -85,0 +193,0 @@ yield level; |
@@ -70,6 +70,14 @@ export class CancellationToken { | ||
chain(target, twoWays = false) { | ||
const cancelable = () => target.cancel(); | ||
if (twoWays) { | ||
target.chain(this, false); | ||
} | ||
this.addCancelable(() => target.cancel()); | ||
else { | ||
target.addCancelable(() => { | ||
if (!this.isCanceled) { | ||
this.removeCancelable(cancelable); | ||
} | ||
}); | ||
} | ||
this.addCancelable(cancelable); | ||
return this; | ||
@@ -76,0 +84,0 @@ } |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4305620
40543