piral-search
Advanced tools
Comparing version 0.10.0-pre.837 to 0.10.0-pre.838
@@ -1,11 +0,10 @@ | ||
import { Atom } from '@dbeining/react-atom'; | ||
import { ReactChild } from 'react'; | ||
import { GlobalState, Disposable } from 'piral-core'; | ||
import { Disposable, GlobalStateContext } from 'piral-core'; | ||
import { SearchProviderRegistration } from './types'; | ||
export declare function setSearchInput(ctx: Atom<GlobalState>, input: string): void; | ||
export declare function triggerSearch(ctx: Atom<GlobalState>, query?: string, immediate?: boolean): Disposable; | ||
export declare function resetSearchResults(ctx: Atom<GlobalState>, input: string, loading: boolean): void; | ||
export declare function appendSearchResults(ctx: Atom<GlobalState>, items: Array<ReactChild>, done: boolean): void; | ||
export declare function prependSearchResults(ctx: Atom<GlobalState>, items: Array<ReactChild>, done: boolean): void; | ||
export declare function registerSearchProvider(ctx: Atom<GlobalState>, name: string, value: SearchProviderRegistration): void; | ||
export declare function unregisterSearchProvider(ctx: Atom<GlobalState>, name: string): void; | ||
export declare function setSearchInput(ctx: GlobalStateContext, input: string): void; | ||
export declare function triggerSearch(ctx: GlobalStateContext, query?: string, immediate?: boolean): Disposable; | ||
export declare function resetSearchResults(ctx: GlobalStateContext, input: string, loading: boolean): void; | ||
export declare function appendSearchResults(ctx: GlobalStateContext, items: Array<ReactChild>, done: boolean): void; | ||
export declare function prependSearchResults(ctx: GlobalStateContext, items: Array<ReactChild>, done: boolean): void; | ||
export declare function registerSearchProvider(ctx: GlobalStateContext, name: string, value: SearchProviderRegistration): void; | ||
export declare function unregisterSearchProvider(ctx: GlobalStateContext, name: string): void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const react_atom_1 = require("@dbeining/react-atom"); | ||
const piral_core_1 = require("piral-core"); | ||
function setSearchInput(ctx, input) { | ||
react_atom_1.swap(ctx, state => (Object.assign(Object.assign({}, state), { search: Object.assign(Object.assign({}, state.search), { input }) }))); | ||
ctx.dispatch(state => (Object.assign(Object.assign({}, state), { search: Object.assign(Object.assign({}, state.search), { input }) }))); | ||
} | ||
exports.setSearchInput = setSearchInput; | ||
function triggerSearch(ctx, query, immediate = false) { | ||
const state = react_atom_1.deref(ctx); | ||
const providers = state.registry.searchProviders; | ||
const { input, results } = state.search; | ||
const providers = ctx.readState(state => state.registry.searchProviders); | ||
const { input, results } = ctx.readState(state => state.search); | ||
const { loading } = results; | ||
@@ -52,3 +50,3 @@ if (query === undefined) { | ||
function resetSearchResults(ctx, input, loading) { | ||
react_atom_1.swap(ctx, state => (Object.assign(Object.assign({}, state), { search: { | ||
ctx.dispatch(state => (Object.assign(Object.assign({}, state), { search: { | ||
input, | ||
@@ -63,3 +61,3 @@ results: { | ||
function appendSearchResults(ctx, items, done) { | ||
react_atom_1.swap(ctx, state => (Object.assign(Object.assign({}, state), { search: Object.assign(Object.assign({}, state.search), { results: { | ||
ctx.dispatch(state => (Object.assign(Object.assign({}, state), { search: Object.assign(Object.assign({}, state.search), { results: { | ||
loading: !done, | ||
@@ -71,3 +69,3 @@ items: piral_core_1.appendItems(state.search.results.items, items), | ||
function prependSearchResults(ctx, items, done) { | ||
react_atom_1.swap(ctx, state => (Object.assign(Object.assign({}, state), { search: Object.assign(Object.assign({}, state.search), { results: { | ||
ctx.dispatch(state => (Object.assign(Object.assign({}, state), { search: Object.assign(Object.assign({}, state.search), { results: { | ||
loading: !done, | ||
@@ -79,9 +77,9 @@ items: piral_core_1.prependItems(state.search.results.items, items), | ||
function registerSearchProvider(ctx, name, value) { | ||
react_atom_1.swap(ctx, state => (Object.assign(Object.assign({}, state), { registry: Object.assign(Object.assign({}, state.registry), { searchProviders: piral_core_1.withKey(state.registry.searchProviders, name, value) }) }))); | ||
ctx.dispatch(state => (Object.assign(Object.assign({}, state), { registry: Object.assign(Object.assign({}, state.registry), { searchProviders: piral_core_1.withKey(state.registry.searchProviders, name, value) }) }))); | ||
} | ||
exports.registerSearchProvider = registerSearchProvider; | ||
function unregisterSearchProvider(ctx, name) { | ||
react_atom_1.swap(ctx, state => (Object.assign(Object.assign({}, state), { registry: Object.assign(Object.assign({}, state.registry), { searchProviders: piral_core_1.withoutKey(state.registry.searchProviders, name) }) }))); | ||
ctx.dispatch(state => (Object.assign(Object.assign({}, state), { registry: Object.assign(Object.assign({}, state.registry), { searchProviders: piral_core_1.withoutKey(state.registry.searchProviders, name) }) }))); | ||
} | ||
exports.unregisterSearchProvider = unregisterSearchProvider; | ||
//# sourceMappingURL=actions.js.map |
@@ -6,3 +6,2 @@ "use strict"; | ||
const react_1 = require("react"); | ||
const react_atom_1 = require("@dbeining/react-atom"); | ||
const piral_core_1 = require("piral-core"); | ||
@@ -49,3 +48,3 @@ const default_1 = require("./default"); | ||
context.defineActions(actions); | ||
react_atom_1.swap(context.state, state => (Object.assign(Object.assign({}, state), { components: Object.assign(Object.assign({}, state.components), { SearchContainer: default_1.DefaultContainer, SearchInput: default_1.DefaultInput, SearchResult: default_1.DefaultResult }), registry: Object.assign(Object.assign({}, state.registry), { searchProviders: getSearchProviders(providers) }), search: { | ||
context.dispatch(state => (Object.assign(Object.assign({}, state), { components: Object.assign(Object.assign({}, state.components), { SearchContainer: default_1.DefaultContainer, SearchInput: default_1.DefaultInput, SearchResult: default_1.DefaultResult }), registry: Object.assign(Object.assign({}, state.registry), { searchProviders: getSearchProviders(providers) }), search: { | ||
input: query, | ||
@@ -52,0 +51,0 @@ results: { |
{ | ||
"name": "piral-search", | ||
"version": "0.10.0-pre.837", | ||
"version": "0.10.0-pre.838", | ||
"description": "Plugin for centralizing search in Piral.", | ||
@@ -40,3 +40,3 @@ "keywords": [ | ||
"devDependencies": { | ||
"piral-core": "^0.10.0-pre.837" | ||
"piral-core": "^0.10.0-pre.838" | ||
}, | ||
@@ -46,3 +46,3 @@ "peerDependencies": { | ||
}, | ||
"gitHead": "5e8fd9a6f76c1505c459b18a5a06fcbeaa77f1a8" | ||
"gitHead": "5d2f55dafff3ceb751877958c5691dfbf995ae84" | ||
} |
@@ -11,2 +11,3 @@ import { Atom, deref } from '@dbeining/react-atom'; | ||
} from './actions'; | ||
import { createActions, createListener } from 'piral-core'; | ||
@@ -35,3 +36,4 @@ const state = { | ||
}); | ||
appendSearchResults(state, ['a', 'b'], false); | ||
const ctx = createActions(state, createListener({})); | ||
appendSearchResults(ctx, ['a', 'b'], false); | ||
expect(deref(state)).toEqual({ | ||
@@ -60,3 +62,4 @@ foo: 5, | ||
}); | ||
appendSearchResults(state, ['a'], true); | ||
const ctx = createActions(state, createListener({})); | ||
appendSearchResults(ctx, ['a'], true); | ||
expect(deref(state)).toEqual({ | ||
@@ -85,3 +88,4 @@ foo: [1, 2], | ||
}); | ||
prependSearchResults(state, ['a', 'b'], false); | ||
const ctx = createActions(state, createListener({})); | ||
prependSearchResults(ctx, ['a', 'b'], false); | ||
expect(deref(state)).toEqual({ | ||
@@ -110,3 +114,4 @@ foo: 5, | ||
}); | ||
prependSearchResults(state, ['a'], true); | ||
const ctx = createActions(state, createListener({})); | ||
prependSearchResults(ctx, ['a'], true); | ||
expect(deref(state)).toEqual({ | ||
@@ -135,3 +140,4 @@ foo: [1, 2], | ||
}); | ||
resetSearchResults(state, 'yo', true); | ||
const ctx = createActions(state, createListener({})); | ||
resetSearchResults(ctx, 'yo', true); | ||
expect(deref(state)).toEqual({ | ||
@@ -160,3 +166,4 @@ foo: [1, 2], | ||
}); | ||
resetSearchResults(state, 'yo y', false); | ||
const ctx = createActions(state, createListener({})); | ||
resetSearchResults(ctx, 'yo y', false); | ||
expect(deref(state)).toEqual({ | ||
@@ -185,3 +192,4 @@ foo: 5, | ||
}); | ||
setSearchInput(state, 'test input'); | ||
const ctx = createActions(state, createListener({})); | ||
setSearchInput(ctx, 'test input'); | ||
expect(deref(state)).toEqual({ | ||
@@ -201,5 +209,6 @@ foo: 5, | ||
state.search.input = 'foo'; | ||
const ctx = Atom.of(state as any); | ||
const globalState = Atom.of(state as any); | ||
const ctx = createActions(globalState, createListener({})); | ||
triggerSearch(ctx); | ||
expect(deref(ctx).search.results.loading).toBe(false); | ||
expect(deref(globalState).search.results.loading).toBe(false); | ||
}); | ||
@@ -216,5 +225,6 @@ | ||
}; | ||
const ctx = Atom.of(state as any); | ||
const globalState = Atom.of(state as any); | ||
const ctx = createActions(globalState, createListener({})); | ||
triggerSearch(ctx); | ||
expect(deref(ctx).search.results.loading).toBe(true); | ||
expect(deref(globalState).search.results.loading).toBe(true); | ||
}); | ||
@@ -224,19 +234,22 @@ | ||
state.search.input = ''; | ||
const ctx = Atom.of(state as any); | ||
const globalState = Atom.of(state as any); | ||
const ctx = createActions(globalState, createListener({})); | ||
triggerSearch(ctx); | ||
expect(deref(ctx).search.results.loading).toBe(false); | ||
expect(deref(globalState).search.results.loading).toBe(false); | ||
}); | ||
it('immediately resets with loading false if no value is given explicitly', () => { | ||
const ctx = Atom.of(state as any); | ||
const gs = Atom.of(state as any); | ||
const ctx = createActions(gs, createListener({})); | ||
const dispose = triggerSearch(ctx, ''); | ||
dispose(); | ||
expect(deref(ctx).search.results.loading).toBe(false); | ||
expect(deref(gs).search.results.loading).toBe(false); | ||
}); | ||
it('immediately resets with loading false if no value is given explicitly though immediate', () => { | ||
const ctx = Atom.of(state as any); | ||
const gs = Atom.of(state as any); | ||
const ctx = createActions(gs, createListener({})); | ||
const dispose = triggerSearch(ctx, '', true); | ||
dispose(); | ||
expect(deref(ctx).search.results.loading).toBe(false); | ||
expect(deref(gs).search.results.loading).toBe(false); | ||
}); | ||
@@ -253,5 +266,6 @@ | ||
}; | ||
const ctx = Atom.of(state as any); | ||
const gs = Atom.of(state as any); | ||
const ctx = createActions(gs, createListener({})); | ||
triggerSearch(ctx, 'foo'); | ||
expect(deref(ctx).search.results.loading).toBe(true); | ||
expect(deref(gs).search.results.loading).toBe(true); | ||
}); | ||
@@ -268,3 +282,5 @@ | ||
}; | ||
triggerSearch(Atom.of(state)); | ||
const gs = Atom.of(state as any); | ||
const ctx = createActions(gs, createListener({})); | ||
triggerSearch(ctx); | ||
expect(search).toHaveBeenCalledTimes(2); | ||
@@ -284,3 +300,5 @@ }); | ||
}; | ||
triggerSearch(Atom.of(state)); | ||
const gs = Atom.of(state as any); | ||
const ctx = createActions(gs, createListener({})); | ||
triggerSearch(ctx); | ||
await (state.registry.searchProviders as any).foo.search().catch(m => m); | ||
@@ -301,3 +319,5 @@ }); | ||
}; | ||
triggerSearch(Atom.of(state)); | ||
const gs = Atom.of(state as any); | ||
const ctx = createActions(gs, createListener({})); | ||
triggerSearch(ctx); | ||
await (state.registry.searchProviders as any).foo.search().catch(m => m); | ||
@@ -319,3 +339,5 @@ expect(cancel).toHaveBeenCalledTimes(0); | ||
}; | ||
const dispose = triggerSearch(Atom.of(state)); | ||
const gs = Atom.of(state as any); | ||
const ctx = createActions(gs, createListener({})); | ||
const dispose = triggerSearch(ctx); | ||
dispose(); | ||
@@ -338,3 +360,5 @@ await (state.registry.searchProviders as any).foo.search().catch(m => m); | ||
}; | ||
triggerSearch(Atom.of(state)); | ||
const gs = Atom.of(state as any); | ||
const ctx = createActions(gs, createListener({})); | ||
triggerSearch(ctx); | ||
await (state.registry.searchProviders as any).foo.search().catch(m => m); | ||
@@ -352,3 +376,4 @@ expect(console.warn).toHaveBeenCalled(); | ||
}); | ||
registerSearchProvider(state, 'foo', 10); | ||
const ctx = createActions(state, createListener({})); | ||
registerSearchProvider(ctx, 'foo', 10 as any); | ||
expect(deref(state)).toEqual({ | ||
@@ -363,3 +388,3 @@ foo: 5, | ||
}); | ||
unregisterSearchProvider(state, 'foo'); | ||
unregisterSearchProvider(ctx, 'foo'); | ||
expect(deref(state)).toEqual({ | ||
@@ -366,0 +391,0 @@ foo: 5, |
@@ -1,8 +0,7 @@ | ||
import { swap, Atom, deref } from '@dbeining/react-atom'; | ||
import { ReactChild } from 'react'; | ||
import { GlobalState, Disposable, appendItems, prependItems, withKey, withoutKey } from 'piral-core'; | ||
import { Disposable, appendItems, prependItems, withKey, withoutKey, GlobalStateContext } from 'piral-core'; | ||
import { SearchOptions, SearchProviderRegistration } from './types'; | ||
export function setSearchInput(ctx: Atom<GlobalState>, input: string) { | ||
swap(ctx, state => ({ | ||
export function setSearchInput(ctx: GlobalStateContext, input: string) { | ||
ctx.dispatch(state => ({ | ||
...state, | ||
@@ -16,6 +15,5 @@ search: { | ||
export function triggerSearch(ctx: Atom<GlobalState>, query?: string, immediate = false): Disposable { | ||
const state = deref(ctx); | ||
const providers = state.registry.searchProviders; | ||
const { input, results } = state.search; | ||
export function triggerSearch(ctx: GlobalStateContext, query?: string, immediate = false): Disposable { | ||
const providers = ctx.readState(state => state.registry.searchProviders); | ||
const { input, results } = ctx.readState(state => state.search); | ||
const { loading } = results; | ||
@@ -67,4 +65,4 @@ | ||
export function resetSearchResults(ctx: Atom<GlobalState>, input: string, loading: boolean) { | ||
swap(ctx, state => ({ | ||
export function resetSearchResults(ctx: GlobalStateContext, input: string, loading: boolean) { | ||
ctx.dispatch(state => ({ | ||
...state, | ||
@@ -81,4 +79,4 @@ search: { | ||
export function appendSearchResults(ctx: Atom<GlobalState>, items: Array<ReactChild>, done: boolean) { | ||
swap(ctx, state => ({ | ||
export function appendSearchResults(ctx: GlobalStateContext, items: Array<ReactChild>, done: boolean) { | ||
ctx.dispatch(state => ({ | ||
...state, | ||
@@ -95,4 +93,4 @@ search: { | ||
export function prependSearchResults(ctx: Atom<GlobalState>, items: Array<ReactChild>, done: boolean) { | ||
swap(ctx, state => ({ | ||
export function prependSearchResults(ctx: GlobalStateContext, items: Array<ReactChild>, done: boolean) { | ||
ctx.dispatch(state => ({ | ||
...state, | ||
@@ -109,4 +107,4 @@ search: { | ||
export function registerSearchProvider(ctx: Atom<GlobalState>, name: string, value: SearchProviderRegistration) { | ||
swap(ctx, state => ({ | ||
export function registerSearchProvider(ctx: GlobalStateContext, name: string, value: SearchProviderRegistration) { | ||
ctx.dispatch(state => ({ | ||
...state, | ||
@@ -120,4 +118,4 @@ registry: { | ||
export function unregisterSearchProvider(ctx: Atom<GlobalState>, name: string) { | ||
swap(ctx, state => ({ | ||
export function unregisterSearchProvider(ctx: GlobalStateContext, name: string) { | ||
ctx.dispatch(state => ({ | ||
...state, | ||
@@ -124,0 +122,0 @@ registry: { |
@@ -1,5 +0,6 @@ | ||
import { Atom } from '@dbeining/react-atom'; | ||
import { Atom, swap } from '@dbeining/react-atom'; | ||
import { createSearchApi } from './create'; | ||
function createMockContainer() { | ||
const state = Atom.of({}); | ||
return { | ||
@@ -11,3 +12,6 @@ context: { | ||
defineActions() {}, | ||
state: Atom.of({}), | ||
state, | ||
dispatch(update) { | ||
swap(state, update); | ||
}, | ||
} as any, | ||
@@ -14,0 +18,0 @@ api: {} as any, |
import * as actions from './actions'; | ||
import { isfunc } from 'piral-base'; | ||
import { ReactChild, isValidElement, createElement } from 'react'; | ||
import { swap } from '@dbeining/react-atom'; | ||
import { buildName, Extend, Dict, withApi, PiletApi, GlobalStateContext } from 'piral-core'; | ||
@@ -96,3 +95,3 @@ import { DefaultContainer, DefaultInput, DefaultResult } from './default'; | ||
swap(context.state, state => ({ | ||
context.dispatch(state => ({ | ||
...state, | ||
@@ -135,3 +134,7 @@ components: { | ||
pilet, | ||
q => Promise.resolve(provider(q, api)).then(results => wrapResults(results, api, context), () => []), | ||
q => | ||
Promise.resolve(provider(q, api)).then( | ||
results => wrapResults(results, api, context), | ||
() => [], | ||
), | ||
settings, | ||
@@ -138,0 +141,0 @@ ), |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
61918
1409