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

feat(ValueConverter): enable signal #626

Merged
merged 4 commits into from
Oct 8, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 15 additions & 0 deletions src/ast.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Unparser} from './unparser';
import {getContextFor} from './scope';
import {connectBindingToSignal} from './signals';

export class Expression {
constructor() {
Expand Down Expand Up @@ -148,6 +149,20 @@ export class ValueConverter extends Expression {
while (i--) {
expressions[i].connect(binding, scope);
}
let converter = binding.lookupFunctions.valueConverters(this.name);
if (!converter) {
throw new Error(`No ValueConverter named "${this.name}" was found!`);
}
let signals = converter.signals;
if (signals === undefined) {
return;
}
// support both input type 'signal' & ['signal-1', 'signal-2']
signals = Array.isArray(signals) ? signals : [signals];
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a really hot path. Let's remove this line entirely to ensure the connect overhead remains minimal.

I don't think supporting string & array is necessary, just more to document & maintain. If we were to do that here's a good spot to perform the normalization, but again, I would prefer to keep the API surface small and strict:

initialize(container, target) {
this.instance = container.get(target);
}

Copy link
Member Author

Choose a reason for hiding this comment

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

@jdanyow All done as suggested. Learnt to be more careful with connect. About the API, 99% of usage will be a single signal, I do think we need to support normalisation, but I'll let you make the call after this got merged. What do you think ?

i = signals.length;
while (i--) {
connectBindingToSignal(binding, signals[i]);
}
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/signals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const signals = {};

export function connectBindingToSignal(binding, name) {
if (!signals.hasOwnProperty(name)) {
signals[name] = 0;
}
binding.observeProperty(signals, name);
}

export function signalBindings(name) {
if (signals.hasOwnProperty(name)) {
signals[name]++;
}
}
167 changes: 110 additions & 57 deletions test/binding-expression.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {BindingEngine} from '../src/binding-engine';
import {checkDelay} from './shared';
import {createScopeForTest} from '../src/scope';
import {sourceContext} from '../src/connectable-binding';
import {signalBindings} from '../src/signals';

describe('BindingExpression', () => {
let bindingEngine;
Expand Down Expand Up @@ -67,77 +68,129 @@ describe('BindingExpression', () => {
}, checkDelay * 2);
});

it('handles ValueConverter', done => {
let valueConverters = {
numberToString: { toView: value => value.toString(), fromView: value => parseInt(value) },
multiply: { toView: (value, arg) => value * arg, fromView: (value, arg) => value / arg }
};
spyOn(valueConverters.numberToString, 'toView').and.callThrough();
spyOn(valueConverters.numberToString, 'fromView').and.callThrough();
spyOn(valueConverters.multiply, 'toView').and.callThrough();
spyOn(valueConverters.multiply, 'fromView').and.callThrough();
let lookupFunctions = { valueConverters: name => valueConverters[name] };
let source = { foo: { bar: 1 }, arg: 2 };
let target = document.createElement('input');
let bindingExpression = bindingEngine.createBindingExpression('value', 'foo.bar | multiply:arg | numberToString', bindingMode.twoWay, lookupFunctions);
let binding = bindingExpression.createBinding(target);
binding.bind(createScopeForTest(source));
expect(target.value).toBe('2');
expect(valueConverters.numberToString.toView).toHaveBeenCalledWith(2);
expect(valueConverters.multiply.toView).toHaveBeenCalledWith(1, 2);
let sourceObserver = bindingEngine.observerLocator.getObserver(source.foo, 'bar');
expect(sourceObserver.hasSubscribers()).toBe(true);
let argObserver = bindingEngine.observerLocator.getObserver(source, 'arg');
expect(argObserver.hasSubscribers()).toBe(true);
expect(binding.targetObserver.hasSubscribers()).toBe(true);
source.foo.bar = 2;
setTimeout(() => {
expect(target.value).toBe('4');
expect(valueConverters.numberToString.toView).toHaveBeenCalledWith(4);
expect(valueConverters.multiply.toView).toHaveBeenCalledWith(2, 2);
valueConverters.numberToString.toView.calls.reset();
valueConverters.numberToString.fromView.calls.reset();
valueConverters.multiply.toView.calls.reset();
valueConverters.multiply.fromView.calls.reset();
source.arg = 4;
describe('ValueConverter', () => {
it('handles ValueConverter without signals', done => {
let valueConverters = {
numberToString: { toView: value => value.toString(), fromView: value => parseInt(value) },
multiply: { toView: (value, arg) => value * arg, fromView: (value, arg) => value / arg }
};
spyOn(valueConverters.numberToString, 'toView').and.callThrough();
spyOn(valueConverters.numberToString, 'fromView').and.callThrough();
spyOn(valueConverters.multiply, 'toView').and.callThrough();
spyOn(valueConverters.multiply, 'fromView').and.callThrough();
let lookupFunctions = { valueConverters: name => valueConverters[name] };
let source = { foo: { bar: 1 }, arg: 2 };
let target = document.createElement('input');
let bindingExpression = bindingEngine.createBindingExpression('value', 'foo.bar | multiply:arg | numberToString', bindingMode.twoWay, lookupFunctions);
let binding = bindingExpression.createBinding(target);
binding.bind(createScopeForTest(source));
expect(target.value).toBe('2');
expect(valueConverters.numberToString.toView).toHaveBeenCalledWith(2);
expect(valueConverters.multiply.toView).toHaveBeenCalledWith(1, 2);
let sourceObserver = bindingEngine.observerLocator.getObserver(source.foo, 'bar');
expect(sourceObserver.hasSubscribers()).toBe(true);
let argObserver = bindingEngine.observerLocator.getObserver(source, 'arg');
expect(argObserver.hasSubscribers()).toBe(true);
expect(binding.targetObserver.hasSubscribers()).toBe(true);
source.foo.bar = 2;
setTimeout(() => {
expect(target.value).toBe('8');
expect(valueConverters.numberToString.toView).toHaveBeenCalledWith(8);
expect(valueConverters.numberToString.fromView).not.toHaveBeenCalled();
expect(valueConverters.multiply.toView).toHaveBeenCalledWith(2, 4);
expect(valueConverters.multiply.fromView).not.toHaveBeenCalled();
expect(target.value).toBe('4');
expect(valueConverters.numberToString.toView).toHaveBeenCalledWith(4);
expect(valueConverters.multiply.toView).toHaveBeenCalledWith(2, 2);
valueConverters.numberToString.toView.calls.reset();
valueConverters.numberToString.fromView.calls.reset();
valueConverters.multiply.toView.calls.reset();
valueConverters.multiply.fromView.calls.reset();
target.value = '24';
target.dispatchEvent(DOM.createCustomEvent('change'));
source.arg = 4;
setTimeout(() => {
expect(valueConverters.numberToString.toView).toHaveBeenCalledWith(24);
expect(valueConverters.numberToString.fromView).toHaveBeenCalledWith('24');
expect(valueConverters.multiply.toView).toHaveBeenCalledWith(6, 4);
expect(valueConverters.multiply.fromView).toHaveBeenCalledWith(24, 4);
expect(target.value).toBe('8');
expect(valueConverters.numberToString.toView).toHaveBeenCalledWith(8);
expect(valueConverters.numberToString.fromView).not.toHaveBeenCalled();
expect(valueConverters.multiply.toView).toHaveBeenCalledWith(2, 4);
expect(valueConverters.multiply.fromView).not.toHaveBeenCalled();
valueConverters.numberToString.toView.calls.reset();
valueConverters.numberToString.fromView.calls.reset();
valueConverters.multiply.toView.calls.reset();
valueConverters.multiply.fromView.calls.reset();
expect(source.foo.bar).toBe(6);
binding.unbind();
expect(sourceObserver.hasSubscribers()).toBe(false);
expect(argObserver.hasSubscribers()).toBe(false);
expect(binding.targetObserver.hasSubscribers()).toBe(false);
source.foo.bar = 4;
target.value = '24';
target.dispatchEvent(DOM.createCustomEvent('change'));
setTimeout(() => {
expect(valueConverters.numberToString.toView).not.toHaveBeenCalled();
expect(valueConverters.numberToString.fromView).not.toHaveBeenCalled();
expect(valueConverters.multiply.toView).not.toHaveBeenCalled();
expect(valueConverters.multiply.fromView).not.toHaveBeenCalled();
expect(target.value).toBe('24');
done();
expect(valueConverters.numberToString.toView).toHaveBeenCalledWith(24);
expect(valueConverters.numberToString.fromView).toHaveBeenCalledWith('24');
expect(valueConverters.multiply.toView).toHaveBeenCalledWith(6, 4);
expect(valueConverters.multiply.fromView).toHaveBeenCalledWith(24, 4);
valueConverters.numberToString.toView.calls.reset();
valueConverters.numberToString.fromView.calls.reset();
valueConverters.multiply.toView.calls.reset();
valueConverters.multiply.fromView.calls.reset();
expect(source.foo.bar).toBe(6);
binding.unbind();
expect(sourceObserver.hasSubscribers()).toBe(false);
expect(argObserver.hasSubscribers()).toBe(false);
expect(binding.targetObserver.hasSubscribers()).toBe(false);
source.foo.bar = 4;
setTimeout(() => {
expect(valueConverters.numberToString.toView).not.toHaveBeenCalled();
expect(valueConverters.numberToString.fromView).not.toHaveBeenCalled();
expect(valueConverters.multiply.toView).not.toHaveBeenCalled();
expect(valueConverters.multiply.fromView).not.toHaveBeenCalled();
expect(target.value).toBe('24');
done();
}, checkDelay * 2);
}, checkDelay * 2);
}, checkDelay * 2);
}, checkDelay * 2);
}, checkDelay * 2);
});

it('handles ValueConverter with signals', done => {
let prefix = '_';
let valueConverters = {
withSingleSignals: {
signals: 'hello',
toView: val => prefix + val
},
withMultipleSignals: {
signals: ['hello', 'world'],
toView: val => prefix + val
}
};
let lookupFunctions = { valueConverters: name => valueConverters[name] };
let source = { foo: { bar: 1 }, arg: 2 };
let target1 = document.createElement('input');
let bindingExpression1 = bindingEngine.createBindingExpression(
'value',
'foo.bar | withSingleSignals',
bindingMode.oneWay,
lookupFunctions
);
let binding1 = bindingExpression1.createBinding(target1);
let target2 = document.createElement('input');
let bindingExpression2 = bindingEngine.createBindingExpression(
'value',
'foo.bar | withMultipleSignals',
bindingMode.oneWay,
lookupFunctions
);
let binding2 = bindingExpression2.createBinding(target2);
var scope = createScopeForTest(source);
binding1.bind(scope);
binding2.bind(scope);
expect(target1.value).toBe('_1');
expect(target2.value).toBe('_1');
prefix = '';
signalBindings('hello');
setTimeout(() => {
expect(target1.value).toBe('1');
expect(target2.value).toBe('1');
prefix = '_';
signalBindings('world');
setTimeout(() => {
expect(target1.value).toBe('1');
expect(target2.value).toBe('_1');
done();
}, checkDelay * 2);
}, checkDelay * 2);
});
});

it('handles BindingBehavior', done => {
Expand Down