Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
},
{
"path": "./packages/melody-streams/lib/index.js",
"maxSize": "1.53 kB"
"maxSize": "1.59 kB"
}
],
"husky": {
Expand Down
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\\"}]"`;
55 changes: 55 additions & 0 deletions packages/melody-streams/__tests__/attachEventWithElementSpec.js
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);
});
});
});
1 change: 1 addition & 0 deletions packages/melody-streams/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export { createComponent } from './component';
export { render } from './render';
export { createState } from './operators/createState';
export { attachEvent } from './operators/attachEvent';
export { attachEventWithElement } from './operators/attachEventWithElement';
export { withElement } from './operators/withElement';
export { combine } from './operators/combine';
export { combineRefs } from './operators/combineRefs';
32 changes: 32 additions & 0 deletions packages/melody-streams/src/operators/attachEventWithElement.js
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) => {
Copy link
Contributor

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 one ref, that might solve the problem already.

What do you think?

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];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we keep this API design we could use streams[0], streams[1] here instead of the spread operator for more explicitness, smaller size and better performance.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same goes for the other usages

};