-
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.
Merge pull request #65 from gerich-home/fixing-diamond-perf
Fixing diamond perf
- Loading branch information
Showing
9 changed files
with
145 additions
and
59 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
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
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
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
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 |
---|---|---|
@@ -1,65 +1,67 @@ | ||
'use strict'; | ||
|
||
import { IHasValue } from './subscriptionList'; | ||
import { doChange } from './change'; | ||
|
||
interface IChange<T> { | ||
value: IHasValue<T>; | ||
notify(): void; | ||
oldValue: T; | ||
newValue?: T; | ||
notify(from: T, to: T): void; | ||
} | ||
|
||
interface IChanges { | ||
[id: number]: IChange<any>; | ||
ids: number[]; | ||
interface IHasChanges { | ||
[id: number]: boolean; | ||
} | ||
|
||
var bulkLevels: number = 0; | ||
var changes: IChanges; | ||
var changes: IChange<any>[]; | ||
var hasChange: IHasChanges; | ||
|
||
export function valueChanged<T>(id: number, value: IHasValue<T>, oldValue: T, notify: (from: T, to: T) => void): void { | ||
if (bulkLevels === 0) { | ||
notify(oldValue, value()); | ||
} else if (!changes[id]) { | ||
changes.ids.push(id); | ||
} else if (hasChange[id] === undefined) { | ||
changes.push({ | ||
notify: notify, | ||
value: value, | ||
oldValue: oldValue | ||
}); | ||
|
||
changes[id] = { | ||
notify: function(): void { | ||
var newValue = value(); | ||
if (oldValue !== newValue) { | ||
notify(oldValue, newValue); | ||
} | ||
}, | ||
value: value | ||
}; | ||
hasChange[id] = true; | ||
} | ||
} | ||
|
||
export default function(changeAction: () => void): void { | ||
var isFirstBulk = bulkLevels === 0; | ||
|
||
if (isFirstBulk) { | ||
changes = { | ||
ids: [] | ||
}; | ||
changes = []; | ||
hasChange = {}; | ||
} | ||
|
||
bulkLevels++; | ||
|
||
try { | ||
changeAction(); | ||
doChange(changeAction); | ||
} finally { | ||
if (isFirstBulk) { | ||
for (var id of changes.ids) { | ||
changes[id].value(); | ||
for (var change of changes) { | ||
change.newValue = change.value(); | ||
} | ||
} | ||
|
||
bulkLevels--; | ||
|
||
if (isFirstBulk) { | ||
for (var id of changes.ids) { | ||
changes[id].notify(); | ||
for (var change of changes) { | ||
if (change.oldValue !== change.newValue) { | ||
change.notify(change.oldValue, change.newValue); | ||
} | ||
} | ||
|
||
changes = undefined; | ||
hasChange = undefined; | ||
} | ||
} | ||
} |
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,36 @@ | ||
'use strict'; | ||
|
||
var changeLevels: number = 0; | ||
var actions: (() => void)[]; | ||
|
||
export function onChangeFinished(action: () => void): void { | ||
if (changeLevels === 0) { | ||
action(); | ||
} else { | ||
actions.push(action); | ||
} | ||
} | ||
|
||
export function doChange(changeAction: () => void): void { | ||
var isFirstChange = changeLevels === 0; | ||
|
||
if (isFirstChange) { | ||
actions = []; | ||
} | ||
|
||
changeLevels++; | ||
|
||
try { | ||
changeAction(); | ||
} finally { | ||
changeLevels--; | ||
|
||
if (isFirstChange) { | ||
for (var action of actions) { | ||
action(); | ||
} | ||
|
||
actions = undefined; | ||
} | ||
} | ||
} |
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
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
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