-
Notifications
You must be signed in to change notification settings - Fork 38
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
Adding operator attachEventWithElement #124
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`attachEventWithElement should attach a click handler 1`] = `"[{\\"type\\":\\"click\\"},{\\"type\\":\\"click\\"},{\\"type\\":\\"click\\"}]"`; | ||
|
||
exports[`attachEventWithElement should attach multiple handlers 1`] = `"[{\\"type\\":\\"click\\"},{\\"type\\":\\"click\\"},{\\"type\\":\\"click\\"},{\\"type\\":\\"mouseenter\\"},{\\"type\\":\\"mouseenter\\"},{\\"type\\":\\"mouseenter\\"}]"`; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* Copyright 2019 trivago N.V. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { attachEventWithElement } from '../src'; | ||
import { applyGradualyAndComplete } from './util/testHelpers'; | ||
import { createMouseEvent } from './util/mouseEvent'; | ||
|
||
const dispatchClick = createMouseEvent('click'); | ||
const dispatchMouseEnter = createMouseEvent('mouseenter'); | ||
|
||
describe('attachEventWithElement', () => { | ||
it('should attach a click handler', async () => { | ||
const el = document.createElement('div'); | ||
const [refHandler, subj, originEl] = attachEventWithElement('click'); | ||
refHandler(el); | ||
applyGradualyAndComplete(subj, dispatchClick(el), [ | ||
undefined, | ||
undefined, | ||
undefined, | ||
]).then(handlers => { | ||
expect(JSON.stringify(handlers)).toMatchSnapshot(); | ||
expect(el).toBe(originEl.value); | ||
}); | ||
}); | ||
|
||
it('should attach multiple handlers', async () => { | ||
const el = document.createElement('div'); | ||
const [refHandler, subj, originEl] = attachEventWithElement( | ||
'click', | ||
'mouseenter' | ||
); | ||
refHandler(el); | ||
applyGradualyAndComplete( | ||
subj, | ||
[dispatchClick(el), dispatchMouseEnter(el)], | ||
[undefined, undefined, undefined] | ||
).then(handlers => { | ||
expect(JSON.stringify(handlers)).toMatchSnapshot(); | ||
expect(el).toBe(originEl.value); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* Copyright 2019 trivago N.V. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { attachEvent } from './attachEvent'; | ||
import { withElement } from './withElement'; | ||
import { combineRefs } from './combineRefs'; | ||
import { of } from 'rxjs'; | ||
|
||
export const attachEventWithElement = (...events) => { | ||
const eventsAndElement = [ | ||
attachEvent(...events), | ||
withElement(el => of(el), null), | ||
]; | ||
const refHandler = combineRefs( | ||
...eventsAndElement.map(([handler]) => handler) | ||
); | ||
const streams = eventsAndElement.map(([_, stream]) => stream); | ||
return [refHandler, ...streams]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we keep this API design we could use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same goes for the other usages |
||
}; |
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'm not sure about the ergonomics of this operator. What I don't like is that it produces only a single stream for all of the registered events, forcing the user of the API to do the splitting manually. I have the feeling that it'd be more valuable if it returned one stream per defined event.
The other part of the question is whether we should add this at all. Its a userland solution for an underlying problem: You can only define a single
ref
per element. So if we find a way to add more than oneref
, that might solve the problem already.What do you think?