-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(watch): watch setters on an object
- Loading branch information
Showing
3 changed files
with
105 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,3 @@ | ||
export interface IWatchHandler { | ||
(value?: any): void; | ||
} |
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,64 @@ | ||
import { watch } from './watch'; | ||
|
||
describe('watch', () => { | ||
const prop = 'PROPERTY'; | ||
const value = 'VALUE'; | ||
let o; | ||
|
||
beforeEach(() => { | ||
o = {}; | ||
}); | ||
|
||
it('watches for change', () => { | ||
let counter = 0; | ||
watch(o, prop, () => { | ||
counter++; | ||
}); | ||
|
||
o[prop] = value; | ||
o[prop] = value; | ||
|
||
expect(counter).toBe(2); | ||
}); | ||
|
||
it('watches for change once', () => { | ||
let counter = 0; | ||
watch( | ||
o, | ||
prop, | ||
() => { | ||
counter++; | ||
}, | ||
true | ||
); | ||
|
||
o[prop] = value; | ||
o[prop] = value; | ||
|
||
expect(counter).toBe(1); | ||
}); | ||
|
||
it('unwatches and keeps set value', () => { | ||
const unwatch = watch(o, prop, () => {}); | ||
|
||
o[prop] = value; | ||
unwatch(); | ||
|
||
expect(o[prop]).toBe(value); | ||
}); | ||
|
||
it('can nest watches', () => { | ||
let counter = 0; | ||
watch(o, prop, () => { | ||
counter++; | ||
}); | ||
watch(o, prop, () => { | ||
counter++; | ||
}); | ||
|
||
o[prop] = value; | ||
o[prop] = value; | ||
|
||
expect(counter).toBe(4); | ||
}); | ||
}); |
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,38 @@ | ||
import { IWatchHandler } from './IWatchHandler'; | ||
|
||
export function watch( | ||
o: any, | ||
property: string, | ||
handler: IWatchHandler, | ||
once = false | ||
): () => void { | ||
|
||
const descriptor = Object.getOwnPropertyDescriptor(o, property) || {}; | ||
|
||
let getValue = descriptor.get || (() => descriptor.value); | ||
let setValue = descriptor.set || (value => (descriptor.value = value)); | ||
|
||
const unwatch = () => { | ||
delete o[property]; | ||
o[property] = getValue(); | ||
}; | ||
|
||
Object.defineProperty(o, property, { | ||
get: getValue, | ||
set(value) { | ||
if (once && getValue() === undefined) { | ||
setValue(value); | ||
unwatch(); | ||
handler(value); | ||
|
||
return; | ||
} | ||
|
||
setValue(value); | ||
handler(value); | ||
}, | ||
configurable: true | ||
}); | ||
|
||
return unwatch; | ||
} |