-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core/change_detection): implement ngOnChanges life cycle hook
Closes #48
- Loading branch information
Showing
11 changed files
with
258 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { SimpleChange } from './change_detection/change_detection_util'; |
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,35 @@ | ||
import { isPresent, isBlank } from '../../facade/lang'; | ||
/** | ||
* Represents a basic change from a previous to a new value. | ||
*/ | ||
export class SimpleChange { | ||
constructor(public previousValue: any, public currentValue: any) {} | ||
|
||
/** | ||
* Check whether the new value is the first value assigned. | ||
*/ | ||
isFirstChange(): boolean { return this.previousValue === ChangeDetectionUtil.uninitialized; } | ||
} | ||
|
||
export class UninitializedValue {} | ||
|
||
function _simpleChange(previousValue, currentValue): SimpleChange { | ||
return new SimpleChange(previousValue, currentValue); | ||
} | ||
|
||
function _uninitializedValue(){ | ||
return new UninitializedValue(); | ||
} | ||
|
||
/* tslint:disable:requireParameterType */ | ||
export class ChangeDetectionUtil { | ||
static uninitialized: UninitializedValue = _uninitializedValue(); | ||
|
||
static simpleChange(previousValue: any, currentValue: any): SimpleChange { | ||
return _simpleChange(previousValue, currentValue); | ||
} | ||
|
||
static isValueBlank(value: any): boolean { return isBlank(value); } | ||
|
||
static s(value: any): string { return isPresent(value) ? `${value}` : ''; } | ||
} |
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,63 @@ | ||
// @TODO this needs to be in a singleton | ||
import { isFunction } from '../../facade/lang'; | ||
|
||
const TTL = 10; | ||
let onChangesTtl = TTL; | ||
|
||
|
||
export class ChangesQueue{ | ||
|
||
// The onChanges hooks should all be run together in a single digest | ||
// When changes occur, the call to trigger their hooks will be added to this queue | ||
onChangesQueue: Function[]; | ||
|
||
flushOnChangesQueue: () => void; | ||
|
||
buildFlushOnChanges( $rootScope: ng.IRootScopeService ){ | ||
|
||
const _context = this; | ||
|
||
buildFlushOnChangesCb( $rootScope ); | ||
|
||
function buildFlushOnChangesCb( $rootScope: ng.IRootScopeService ): () => void { | ||
|
||
if(isFunction(_context.flushOnChangesQueue)){ | ||
return _context.flushOnChangesQueue; | ||
} | ||
_context.flushOnChangesQueue = getFlushOnChangesQueueCb($rootScope); | ||
return _context.flushOnChangesQueue; | ||
|
||
} | ||
|
||
function getFlushOnChangesQueueCb( $rootScope: ng.IRootScopeService ): () => void { | ||
|
||
// This function is called in a $$postDigest to trigger all the onChanges hooks in a single digest | ||
return function _flushOnChangesQueue() { | ||
try { | ||
if (!(--onChangesTtl)) { | ||
// We have hit the TTL limit so reset everything | ||
_context.onChangesQueue = undefined; | ||
throw new Error(`infchng, ${TTL} ngOnChanges() iterations reached. Aborting!\n`); | ||
} | ||
// We must run this hook in an apply since the $$postDigest runs outside apply | ||
$rootScope.$apply(function() { | ||
for (var i = 0, ii = _context.onChangesQueue.length; i < ii; ++i) { | ||
_context.onChangesQueue[i](); | ||
} | ||
// Reset the queue to trigger a new schedule next time there is a change | ||
_context.onChangesQueue = undefined; | ||
}); | ||
} finally { | ||
onChangesTtl++; | ||
} | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
} | ||
|
||
} | ||
|
||
export const changesQueueService = new ChangesQueue(); |
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
Oops, something went wrong.