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

Default stateKey property for StoreProvider #273

Merged
merged 1 commit into from
Feb 26, 2019
Merged
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
15 changes: 11 additions & 4 deletions src/stores/StoreProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface GetPaths<S = any> {

export interface StoreProviderProperties<S = any> {
renderer: (store: Store<S>) => DNode | DNode[];
stateKey: string;
stateKey?: string;
paths?: GetPaths<S>;
}

Expand Down Expand Up @@ -41,10 +41,14 @@ export class StoreProvider<S = any> extends WidgetBase<StoreProviderProperties<S
}
}

private _getProperties() {
return { stateKey: 'state', ...this.properties };
}

@diffProperty('stateKey')
@diffProperty('paths', pathDiff)
protected onChange(previousProperties: StoreProviderProperties, currentProperties: StoreProviderProperties) {
const { stateKey, paths } = currentProperties;
protected onChange(previousProperties: any, currentProperties: StoreProviderProperties) {
const { stateKey = 'state', paths } = currentProperties;
if (this._handle) {
this._handle.destroy();
this._handle = undefined;
Expand All @@ -68,8 +72,11 @@ export class StoreProvider<S = any> extends WidgetBase<StoreProviderProperties<S
}

protected render(): DNode | DNode[] {
const { stateKey, renderer } = this.properties;
const { stateKey, renderer } = this._getProperties();
const store = this._getStore(stateKey);
if (!this._handle) {
this.onChange({}, this._getProperties());
}
if (store) {
return renderer(store);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/stores/unit/StoreProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ describe('StoreProvider', () => {
const container = new TestContainer();
container.registry.base = registry;
container.__setProperties__({
stateKey: 'state',
renderer(injectedStore) {
assert.strictEqual<any>(injectedStore, store);
return v('div');
}
});
container.__render__();
invalidateCount = 0;
fooProcess(store)({});
assert.strictEqual(invalidateCount, 1);
Expand Down