-
Notifications
You must be signed in to change notification settings - Fork 166
LG-11285: Add helper function to clean up observable properties #10809
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
zachmargolis
merged 12 commits into
LG-11285-handle-extra-acuant-callback
from
margolis-observable-property
Jun 13, 2024
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
eca7785
Extract defineObservableProperty into its own file
zachmargolis 462b794
Add removeObservableProperty
zachmargolis 44ec239
Use removeObservableProperty in acuant-capture-canvas
zachmargolis 2209562
Merge remote-tracking branch 'origin/LG-11285-handle-extra-acuant-cal…
zachmargolis 1ef54a4
Rename to stopObservingProperty
zachmargolis 1e56c24
Fix issues due to merge, lint issues
zachmargolis 39a11a9
Another lint fix
zachmargolis 3041309
Remove unused
zachmargolis 3c3ad3b
remove diff noise
zachmargolis fa1eda1
Typescript convert so we dont have to add any exceptions
zachmargolis 1866d4c
optional property
zachmargolis 8615c21
another lint
zachmargolis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
39 changes: 39 additions & 0 deletions
39
app/javascript/packages/document-capture/higher-order/observable-property.tsx
This file contains hidden or 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,39 @@ | ||
| /** | ||
| * Defines a property on the given object, calling the change callback when that property is set to | ||
| * a new value. | ||
| * | ||
| * @param object Object on which to define property. | ||
| * @param property Property name to observe. | ||
| * @param onChangeCallback Callback to trigger on change. | ||
| */ | ||
| export function defineObservableProperty( | ||
| object: any, | ||
| property: string, | ||
| onChangeCallback: (nextValue: any) => void, | ||
| ) { | ||
| let currentValue: any; | ||
|
|
||
| Object.defineProperty(object, property, { | ||
| get() { | ||
| return currentValue; | ||
| }, | ||
| set(nextValue) { | ||
| currentValue = nextValue; | ||
| onChangeCallback(nextValue); | ||
| }, | ||
| configurable: true, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Removes an observable property by removing the defined getter/setter methods | ||
| * and replaces the value with the most recent value. | ||
| * | ||
| * @param object Object on which to remove defined property. | ||
| * @param property Property name to remove observer for | ||
| */ | ||
| export function stopObservingProperty(object: any, property: string) { | ||
| const currentValue = object[property]; | ||
|
|
||
| Object.defineProperty(object, property, { value: currentValue, writable: true }); | ||
| } |
23 changes: 1 addition & 22 deletions
23
spec/javascript/packages/document-capture/components/acuant-capture-canvas-spec.jsx
This file contains hidden or 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
44 changes: 44 additions & 0 deletions
44
spec/javascript/packages/document-capture/higher-order/observable-property-spec.tsx
This file contains hidden or 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,44 @@ | ||
| import sinon from 'sinon'; | ||
| import { | ||
| defineObservableProperty, | ||
| stopObservingProperty, | ||
| } from '@18f/identity-document-capture/higher-order/observable-property'; | ||
|
|
||
| describe('document-capture/higher-order/observable-property', () => { | ||
| describe('defineObservableProperty', () => { | ||
| it('behaves like an object', () => { | ||
| const object = {} as { key?: string }; | ||
| defineObservableProperty(object, 'key', () => {}); | ||
| object.key = 'value'; | ||
|
|
||
| expect(object.key).to.equal('value'); | ||
| }); | ||
|
|
||
| it('calls the callback on changes, with the changed value', () => { | ||
| const callback = sinon.spy(); | ||
| const object = {} as { key?: string }; | ||
| defineObservableProperty(object, 'key', callback); | ||
| object.key = 'value'; | ||
|
|
||
| expect(callback).to.have.been.calledOnceWithExactly('value'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('stopObservingProperty', () => { | ||
| it('removes the defined property and set the last value as a plain value', () => { | ||
| const object = {} as { key?: string }; | ||
| const callback = sinon.spy(); | ||
| defineObservableProperty(object, 'key', callback); | ||
|
|
||
| object.key = 'value'; | ||
|
|
||
| stopObservingProperty(object, 'key'); | ||
| expect(object.key).to.equal('value'); | ||
|
|
||
| object.key = 'second_value'; | ||
| expect(object.key).to.equal('second_value'); | ||
|
|
||
| expect(callback).to.have.been.calledOnceWithExactly('value'); | ||
| }); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd expect this folder to contain higher-order React components, which this isn't. I might have suggested this be implemented as a hook instead, like
useDefinedProperty.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok! I can clean it up to be a hook, I was just guessing what to call it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cleanup PR: #10838