-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update events, add tests * Fix docs modal
- Loading branch information
Showing
14 changed files
with
383 additions
and
178 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
__mocks__ | ||
dist/ | ||
www/ | ||
|
||
|
This file contains 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,85 @@ | ||
import { FunctionalComponent, HTMLStencilElement } from '@stencil/core'; | ||
import { SubscribeCallback, ConsumerRenderer, PropList } from '../declarations'; | ||
|
||
export const createProviderConsumer = <T extends { [key: string]: any }>( | ||
defaultState: T, | ||
consumerRender: ConsumerRenderer<T> | ||
) => { | ||
let listeners: Map<HTMLStencilElement, PropList<T>> = new Map(); | ||
let currentState: T = defaultState; | ||
|
||
const updateListener = (fields: PropList<T>, listener: HTMLStencilElement) => { | ||
if (Array.isArray(fields)) { | ||
[...fields].forEach(fieldName => { | ||
(listener as any)[fieldName] = currentState[fieldName]; | ||
}); | ||
} else { | ||
(listener as any)[fields] = { | ||
...(currentState as object), | ||
} as T; | ||
} | ||
listener.forceUpdate(); | ||
}; | ||
|
||
const subscribe: SubscribeCallback<T> = (el: HTMLStencilElement, propList: PropList<T>) => { | ||
if (listeners.has(el)) { | ||
return () => {}; | ||
} | ||
listeners.set(el, propList); | ||
updateListener(propList, el); | ||
|
||
return () => { | ||
listeners.delete(el); | ||
}; | ||
}; | ||
|
||
const Provider: FunctionalComponent<{ state: T }> = ({ state }, children) => { | ||
currentState = state; | ||
listeners.forEach(updateListener); | ||
return children; | ||
}; | ||
|
||
const Consumer: FunctionalComponent<{}> = (props, children) => { | ||
// The casting on subscribe is to allow for crossover through the stencil compiler | ||
// In the future we should allow for generics in components. | ||
return consumerRender(subscribe, children[0] as any); | ||
}; | ||
|
||
const injectProps = (childComponent: any, fieldList: PropList<T>) => { | ||
let unsubscribe: any = null; | ||
|
||
const elementRefName = Object.keys(childComponent.properties).find(propName => { | ||
return childComponent.properties[propName].elementRef == true; | ||
}); | ||
if (elementRefName == undefined) { | ||
throw new Error( | ||
`Please ensure that your Component ${ | ||
childComponent.is | ||
} has an attribute with an "@Element" decorator. ` + | ||
`This is required to be able to inject properties.` | ||
); | ||
} | ||
|
||
const prevComponentWillLoad = childComponent.prototype.componentWillLoad; | ||
childComponent.prototype.componentWillLoad = function() { | ||
unsubscribe = subscribe(this[elementRefName], fieldList); | ||
if (prevComponentWillLoad) { | ||
return prevComponentWillLoad.bind(this)(); | ||
} | ||
}; | ||
|
||
const prevComponentDidUnload = childComponent.prototype.componentDidUnload; | ||
childComponent.prototype.componentDidUnload = function() { | ||
unsubscribe(); | ||
if (prevComponentDidUnload) { | ||
return prevComponentDidUnload.bind(this)(); | ||
} | ||
}; | ||
}; | ||
|
||
return { | ||
Provider, | ||
Consumer, | ||
injectProps, | ||
}; | ||
}; |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.