refract-preact-xstream
Advanced tools
Comparing version 3.1.0 to 4.0.0
@@ -116,2 +116,17 @@ import xs from 'xstream'; | ||
}; | ||
var useEvent = function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var eventName = args[0]; | ||
var hasSeedValue = args.length > 1; | ||
var seedValue = args[2]; | ||
var events$ = fromEvent(eventName); | ||
var pushEventValue = pushEvent(eventName); | ||
return [ | ||
hasSeedValue ? events$ : events$.startWith(seedValue), | ||
pushEventValue | ||
]; | ||
}; | ||
return { | ||
@@ -122,17 +137,10 @@ mount: data.filter(isEvent(MOUNT_EVENT)).mapTo(undefined), | ||
pushEvent: pushEvent, | ||
useEvent: function (eventName, seedValue) { | ||
var events$ = fromEvent(eventName); | ||
var pushEventValue = pushEvent(eventName); | ||
return [ | ||
seedValue === undefined | ||
? events$ | ||
: events$.startWith(seedValue), | ||
pushEventValue | ||
]; | ||
} | ||
useEvent: useEvent | ||
}; | ||
}; | ||
var getObserve = function (getProp, data) { | ||
var getObserve = function (getProp, data, decoratedProps) { | ||
return function observe(propName, valueTransformer) { | ||
if (propName && typeof getProp(propName) === 'function') { | ||
if (decoratedProps && | ||
propName && | ||
typeof getProp(propName) === 'function') { | ||
return data() | ||
@@ -160,8 +168,8 @@ .filter(isCallback(propName)) | ||
}; | ||
var createComponent = function (getProp, dataObservable, pushEvent) { | ||
var createComponent = function (getProp, dataObservable, pushEvent, decoratedProps) { | ||
var data = function () { return xs.from(dataObservable); }; | ||
return __assign({ observe: getObserve(getProp, data) }, getComponentBase(data(), pushEvent)); | ||
return __assign({ observe: getObserve(getProp, data, decoratedProps) }, getComponentBase(data(), pushEvent)); | ||
}; | ||
var configureComponent = function (aperture, instance, isValidElement, isComponentClass, handler, errorHandler, mergeProps) { | ||
var configureComponent = function (aperture, instance, isValidElement, isComponentClass, handler, errorHandler, mergeProps, decorateProps) { | ||
if (isValidElement === void 0) { isValidElement = function () { return false; }; } | ||
@@ -237,7 +245,9 @@ if (isComponentClass === void 0) { isComponentClass = function () { return false; }; } | ||
}; | ||
Object.keys(instance.props).forEach(function (propName) { | ||
if (typeof instance.props[propName] === 'function') { | ||
decorateProp(decoratedProps, instance.props[propName], propName); | ||
} | ||
}); | ||
if (decorateProps) { | ||
Object.keys(instance.props).forEach(function (propName) { | ||
if (typeof instance.props[propName] === 'function') { | ||
decorateProp(decoratedProps, instance.props[propName], propName); | ||
} | ||
}); | ||
} | ||
var dataObservable = (_a = { | ||
@@ -254,3 +264,3 @@ subscribe: function (listener) { | ||
_a); | ||
var component = createComponent(function (propName) { return instance.props[propName]; }, dataObservable, pushEvent); | ||
var component = createComponent(function (propName) { return instance.props[propName]; }, dataObservable, pushEvent, decorateProps); | ||
var sinkObservable = aperture(component, instance.props, instance.context); | ||
@@ -261,8 +271,10 @@ var sinkSubscription = subscribeToSink(sinkObservable, finalHandler(instance.props, instance.context), errorHandler | ||
instance.reDecorateProps = function (nextProps) { | ||
Object.keys(nextProps).forEach(function (propName) { | ||
if (typeof instance.props[propName] === 'function' && | ||
nextProps[propName] !== instance.props[propName]) { | ||
decorateProp(decoratedProps, nextProps[propName], propName); | ||
} | ||
}); | ||
if (decorateProps) { | ||
Object.keys(nextProps).forEach(function (propName) { | ||
if (typeof instance.props[propName] === 'function' && | ||
nextProps[propName] !== instance.props[propName]) { | ||
decorateProp(decoratedProps, nextProps[propName], propName); | ||
} | ||
}); | ||
} | ||
}; | ||
@@ -275,6 +287,6 @@ instance.pushProps = function (props) { | ||
instance.triggerMount = function () { | ||
pushEvent(MOUNT_EVENT)(undefined); | ||
pushEvent(MOUNT_EVENT)(); | ||
}; | ||
instance.triggerUnmount = function () { | ||
pushEvent(UNMOUNT_EVENT)(undefined); | ||
pushEvent(UNMOUNT_EVENT)(); | ||
sinkSubscription.unsubscribe(); | ||
@@ -290,3 +302,3 @@ }; | ||
if (state.replace === false) { | ||
return __assign({}, instance.props, stateProps, additionalProps); | ||
return __assign({}, instance.props, additionalProps, stateProps); | ||
} | ||
@@ -337,3 +349,3 @@ return __assign({}, instance.props, additionalProps); | ||
_this.unmounted = false; | ||
configureComponent(aperture, _this, isValidElement, isComponentClass, config.handler, config.errorHandler, config.mergeProps); | ||
configureComponent(aperture, _this, isValidElement, isComponentClass, config.handler, config.errorHandler, config.mergeProps, config.decorateProps !== false); | ||
return _this; | ||
@@ -340,0 +352,0 @@ } |
74
index.js
@@ -122,2 +122,17 @@ 'use strict'; | ||
}; | ||
var useEvent = function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var eventName = args[0]; | ||
var hasSeedValue = args.length > 1; | ||
var seedValue = args[2]; | ||
var events$ = fromEvent(eventName); | ||
var pushEventValue = pushEvent(eventName); | ||
return [ | ||
hasSeedValue ? events$ : events$.startWith(seedValue), | ||
pushEventValue | ||
]; | ||
}; | ||
return { | ||
@@ -128,17 +143,10 @@ mount: data.filter(isEvent(MOUNT_EVENT)).mapTo(undefined), | ||
pushEvent: pushEvent, | ||
useEvent: function (eventName, seedValue) { | ||
var events$ = fromEvent(eventName); | ||
var pushEventValue = pushEvent(eventName); | ||
return [ | ||
seedValue === undefined | ||
? events$ | ||
: events$.startWith(seedValue), | ||
pushEventValue | ||
]; | ||
} | ||
useEvent: useEvent | ||
}; | ||
}; | ||
var getObserve = function (getProp, data) { | ||
var getObserve = function (getProp, data, decoratedProps) { | ||
return function observe(propName, valueTransformer) { | ||
if (propName && typeof getProp(propName) === 'function') { | ||
if (decoratedProps && | ||
propName && | ||
typeof getProp(propName) === 'function') { | ||
return data() | ||
@@ -166,8 +174,8 @@ .filter(isCallback(propName)) | ||
}; | ||
var createComponent = function (getProp, dataObservable, pushEvent) { | ||
var createComponent = function (getProp, dataObservable, pushEvent, decoratedProps) { | ||
var data = function () { return xs.from(dataObservable); }; | ||
return __assign({ observe: getObserve(getProp, data) }, getComponentBase(data(), pushEvent)); | ||
return __assign({ observe: getObserve(getProp, data, decoratedProps) }, getComponentBase(data(), pushEvent)); | ||
}; | ||
var configureComponent = function (aperture, instance, isValidElement, isComponentClass, handler, errorHandler, mergeProps) { | ||
var configureComponent = function (aperture, instance, isValidElement, isComponentClass, handler, errorHandler, mergeProps, decorateProps) { | ||
if (isValidElement === void 0) { isValidElement = function () { return false; }; } | ||
@@ -243,7 +251,9 @@ if (isComponentClass === void 0) { isComponentClass = function () { return false; }; } | ||
}; | ||
Object.keys(instance.props).forEach(function (propName) { | ||
if (typeof instance.props[propName] === 'function') { | ||
decorateProp(decoratedProps, instance.props[propName], propName); | ||
} | ||
}); | ||
if (decorateProps) { | ||
Object.keys(instance.props).forEach(function (propName) { | ||
if (typeof instance.props[propName] === 'function') { | ||
decorateProp(decoratedProps, instance.props[propName], propName); | ||
} | ||
}); | ||
} | ||
var dataObservable = (_a = { | ||
@@ -260,3 +270,3 @@ subscribe: function (listener) { | ||
_a); | ||
var component = createComponent(function (propName) { return instance.props[propName]; }, dataObservable, pushEvent); | ||
var component = createComponent(function (propName) { return instance.props[propName]; }, dataObservable, pushEvent, decorateProps); | ||
var sinkObservable = aperture(component, instance.props, instance.context); | ||
@@ -267,8 +277,10 @@ var sinkSubscription = subscribeToSink(sinkObservable, finalHandler(instance.props, instance.context), errorHandler | ||
instance.reDecorateProps = function (nextProps) { | ||
Object.keys(nextProps).forEach(function (propName) { | ||
if (typeof instance.props[propName] === 'function' && | ||
nextProps[propName] !== instance.props[propName]) { | ||
decorateProp(decoratedProps, nextProps[propName], propName); | ||
} | ||
}); | ||
if (decorateProps) { | ||
Object.keys(nextProps).forEach(function (propName) { | ||
if (typeof instance.props[propName] === 'function' && | ||
nextProps[propName] !== instance.props[propName]) { | ||
decorateProp(decoratedProps, nextProps[propName], propName); | ||
} | ||
}); | ||
} | ||
}; | ||
@@ -281,6 +293,6 @@ instance.pushProps = function (props) { | ||
instance.triggerMount = function () { | ||
pushEvent(MOUNT_EVENT)(undefined); | ||
pushEvent(MOUNT_EVENT)(); | ||
}; | ||
instance.triggerUnmount = function () { | ||
pushEvent(UNMOUNT_EVENT)(undefined); | ||
pushEvent(UNMOUNT_EVENT)(); | ||
sinkSubscription.unsubscribe(); | ||
@@ -296,3 +308,3 @@ }; | ||
if (state.replace === false) { | ||
return __assign({}, instance.props, stateProps, additionalProps); | ||
return __assign({}, instance.props, additionalProps, stateProps); | ||
} | ||
@@ -343,3 +355,3 @@ return __assign({}, instance.props, additionalProps); | ||
_this.unmounted = false; | ||
configureComponent(aperture, _this, isValidElement, isComponentClass, config.handler, config.errorHandler, config.mergeProps); | ||
configureComponent(aperture, _this, isValidElement, isComponentClass, config.handler, config.errorHandler, config.mergeProps, config.decorateProps !== false); | ||
return _this; | ||
@@ -346,0 +358,0 @@ } |
{ | ||
"name": "refract-preact-xstream", | ||
"description": "Refract bindings for Preact with xstream: harness the power of reactive programming to supercharge your components!", | ||
"version": "3.1.0", | ||
"version": "4.0.0", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "jsnext:main": "index.es.js", |
@@ -9,2 +9,5 @@ export declare type Handler<P, E, C = any> = ( | ||
) => (error: any) => void | ||
export declare type PushEvent = (eventName: string) => <T>(val: T) => void | ||
export interface PushEvent { | ||
(eventName: string): () => void | ||
<T = any>(eventName: string): (val: T) => void | ||
} |
@@ -10,4 +10,5 @@ import { Handler, ErrorHandler } from './baseTypes' | ||
errorHandler?: ErrorHandler<P, any>, | ||
mergeProps?: boolean | ||
mergeProps?: boolean, | ||
decorateProps?: boolean | ||
) => void | ||
export default configureComponent |
@@ -5,3 +5,5 @@ import { withEffects } from './withEffects' | ||
Aperture, | ||
ObservableComponentBase | ||
ObservableComponentBase, | ||
UseEvent, | ||
FromEvent | ||
} from './observable' | ||
@@ -27,2 +29,4 @@ import { ErrorHandler, Handler, PushEvent } from './baseTypes' | ||
PushEvent, | ||
FromEvent, | ||
UseEvent, | ||
compose, | ||
@@ -29,0 +33,0 @@ Compose, |
import { Stream, Listener, Subscription } from 'xstream' | ||
import { PushEvent } from './baseTypes' | ||
export { Listener, Subscription } | ||
export interface UseEvent { | ||
(eventName: string): [Stream<void>, () => any] | ||
<T = any>(eventName: string, seedValue?: T): [Stream<T>, (val: T) => any] | ||
} | ||
export interface FromEvent { | ||
(eventName: string): Stream<void> | ||
<T>(eventName: string, valueTransformer?: (val: any) => T): Stream<T> | ||
} | ||
export interface ObservableComponentBase { | ||
mount: Stream<any> | ||
unmount: Stream<any> | ||
fromEvent: <T>( | ||
eventName: string, | ||
valueTransformer?: (val: any) => T | ||
) => Stream<T> | ||
fromEvent: FromEvent | ||
pushEvent: PushEvent | ||
useEvent: <T>( | ||
eventName: string, | ||
seedValue?: T | ||
) => [Stream<T>, (val: T) => any] | ||
useEvent: UseEvent | ||
} | ||
@@ -36,3 +38,4 @@ export interface Observe { | ||
getProp: any, | ||
data: any | ||
data: any, | ||
decoratedProps: any | ||
) => <T>(propName?: any, valueTransformer?: any) => any | ||
@@ -42,3 +45,4 @@ export declare const createComponent: <P>( | ||
dataObservable: any, | ||
pushEvent: PushEvent | ||
pushEvent: PushEvent, | ||
decoratedProps: boolean | ||
) => ObservableComponent |
@@ -14,2 +14,3 @@ import { ComponentFactory, VNode } from 'preact' | ||
mergeProps?: boolean | ||
decorateProps?: boolean | ||
} | ||
@@ -16,0 +17,0 @@ export declare const withEffects: <P, E, CP = P>( |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
45172
1007