@logux/client
Advanced tools
Comparing version 0.13.3 to 0.14.0
@@ -1,2 +0,2 @@ | ||
import { MapStore } from 'nanostores' | ||
import { ReadableAtom } from 'nanostores' | ||
@@ -8,6 +8,12 @@ import { Client } from '../client/index.js' | ||
*/ | ||
export type Auth = MapStore<{ | ||
userId: string | ||
isAuthenticated: boolean | ||
}> | ||
export interface AuthStore | ||
extends ReadableAtom<{ | ||
userId: string | ||
isAuthenticated: boolean | ||
}> { | ||
/** | ||
* While store is loading initial state. | ||
*/ | ||
readonly loading: Promise<void> | ||
} | ||
@@ -18,9 +24,7 @@ /** | ||
* ```js | ||
* import { createAuth } from '@logux/client' | ||
* import { getValue } from 'nanostores' | ||
* import { createAuth } from '@logux/client'\ | ||
* | ||
* let auth = createAuth(client) | ||
* auth.subscribe(({ isAuthenticated, userId }) => { | ||
* console.log(isAuthenticated, userId) | ||
* }) | ||
* await auth.loading | ||
* console.log(auth.get()) | ||
* ``` | ||
@@ -30,2 +34,2 @@ * | ||
*/ | ||
export function createAuth(client: Client): Auth | ||
export function createAuth(client: Client): AuthStore |
@@ -1,8 +0,25 @@ | ||
import { createMap } from 'nanostores' | ||
import { atom, onMount } from 'nanostores' | ||
export function createAuth(client) { | ||
let auth = createMap(() => { | ||
auth.setKey('userId', client.options.userId) | ||
auth.setKey('isAuthenticated', client.node.state === 'synchronized') | ||
let auth = atom({ | ||
userId: client.options.userId, | ||
isAuthenticated: client.node.state === 'synchronized' | ||
}) | ||
onMount(auth, () => { | ||
auth.set({ | ||
userId: client.options.userId, | ||
isAuthenticated: client.node.state === 'synchronized' | ||
}) | ||
let load | ||
let loaded = auth.value.isAuthenticated | ||
auth.loading = new Promise(resolve => { | ||
if (loaded) resolve() | ||
load = () => { | ||
loaded = true | ||
resolve() | ||
} | ||
}) | ||
let stateBinded = false | ||
@@ -15,3 +32,7 @@ let unbindState | ||
if (client.node.state === 'synchronized') { | ||
auth.setKey('isAuthenticated', true) | ||
auth.set({ | ||
userId: auth.value.userId, | ||
isAuthenticated: true | ||
}) | ||
if (!loaded) load() | ||
unbindState() | ||
@@ -28,7 +49,14 @@ stateBinded = false | ||
if (!stateBinded) bindState() | ||
auth.setKey('isAuthenticated', false) | ||
auth.set({ | ||
userId: auth.value.userId, | ||
isAuthenticated: false | ||
}) | ||
if (!loaded) load() | ||
} | ||
}) | ||
let unbindUser = client.on('user', newUserId => { | ||
auth.setKey('userId', newUserId) | ||
let unbindUser = client.on('user', userId => { | ||
auth.set({ | ||
userId, | ||
isAuthenticated: auth.value.isAuthenticated | ||
}) | ||
}) | ||
@@ -35,0 +63,0 @@ |
@@ -6,3 +6,3 @@ import { SyncMapValues } from '@logux/actions' | ||
LoadedSyncMapValue, | ||
SyncMapBuilder, | ||
SyncMapTemplate, | ||
SyncMapStore | ||
@@ -42,3 +42,2 @@ } from '../define-sync-map/index.js' | ||
* import { createFilter } from '@logux/client' | ||
* import { getValue } from 'nanostores' | ||
* | ||
@@ -49,7 +48,7 @@ * import { User } from '../store' | ||
* await usersInProject.loading | ||
* console.log(getValue(usersInProject)) | ||
* console.log(usersInProject.get()) | ||
* ``` | ||
* | ||
* @param client Logux Client. | ||
* @param Builder Store class from {@link defineSyncMap}. | ||
* @param Template Store template from {@link syncMapTemplate}. | ||
* @param filter Key-value to filter stores. | ||
@@ -60,5 +59,5 @@ * @param opts Loading options. | ||
client: Client, | ||
Builder: SyncMapBuilder<Value>, | ||
Template: SyncMapTemplate<Value>, | ||
filter?: Filter<Value>, | ||
opts?: FilterOptions | ||
): FilterStore<Value> |
@@ -0,12 +1,14 @@ | ||
import { map, onMount, startTask } from 'nanostores' | ||
import { isFirstOlder } from '@logux/core' | ||
import { createMap, startEffect } from 'nanostores' | ||
import { track } from '../track/index.js' | ||
export function createFilter(client, Builder, filter = {}, opts = {}) { | ||
let id = Builder.plural + JSON.stringify(filter) + JSON.stringify(opts) | ||
if (!Builder.filters) Builder.filters = {} | ||
export function createFilter(client, Template, filter = {}, opts = {}) { | ||
let id = Template.plural + JSON.stringify(filter) + JSON.stringify(opts) | ||
if (!Template.filters) Template.filters = {} | ||
if (!Builder.filters[id]) { | ||
let filterStore = createMap(() => { | ||
if (!Template.filters[id]) { | ||
let filterStore = map() | ||
onMount(filterStore, () => { | ||
let listener | ||
@@ -16,4 +18,7 @@ if (opts.listChangesOnly) { | ||
} else { | ||
listener = (childValue, key) => { | ||
filterStore.notify(`${childValue.id}.${key}`) | ||
listener = () => { | ||
filterStore.setKey( | ||
'list', | ||
Array.from(stores.values()).map(i => i.value) | ||
) | ||
} | ||
@@ -31,10 +36,10 @@ } | ||
let channelPrefix = Builder.plural + '/' | ||
let channelPrefix = Template.plural + '/' | ||
let createdType = `${Builder.plural}/created` | ||
let createType = `${Builder.plural}/create` | ||
let changedType = `${Builder.plural}/changed` | ||
let changeType = `${Builder.plural}/change` | ||
let deletedType = `${Builder.plural}/deleted` | ||
let deleteType = `${Builder.plural}/delete` | ||
let createdType = `${Template.plural}/created` | ||
let createType = `${Template.plural}/create` | ||
let changedType = `${Template.plural}/changed` | ||
let changeType = `${Template.plural}/change` | ||
let deletedType = `${Template.plural}/deleted` | ||
let deleteType = `${Template.plural}/delete` | ||
@@ -53,3 +58,2 @@ let unbinds = [] | ||
stores.set(child.value.id, child) | ||
filterStore.notify('stores') | ||
filterStore.setKey( | ||
@@ -68,3 +72,2 @@ 'list', | ||
stores.delete(childId) | ||
filterStore.notify('stores') | ||
filterStore.setKey( | ||
@@ -103,3 +106,3 @@ 'list', | ||
let endEffect = startEffect() | ||
let endTask = startTask() | ||
filterStore.loading = new Promise((resolve, reject) => { | ||
@@ -115,4 +118,4 @@ async function loadAndCheck(child) { | ||
for (let i in Builder.cache) { | ||
loadAndCheck(Builder.cache[i]) | ||
for (let i in Template.cache) { | ||
loadAndCheck(Template.cache[i]) | ||
} | ||
@@ -122,6 +125,6 @@ | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (Builder.mocked) { | ||
if (Template.mocked) { | ||
load = false | ||
filterStore.setKey('isLoading', false) | ||
endEffect() | ||
endTask() | ||
resolve() | ||
@@ -134,5 +137,5 @@ } | ||
let checking = [] | ||
if (Builder.offline) { | ||
if (Template.offline) { | ||
client.log | ||
.each({ index: Builder.plural }, async action => { | ||
.each({ index: Template.plural }, async action => { | ||
if (action.id && !ignore.has(action.id)) { | ||
@@ -148,3 +151,3 @@ let type = action.type | ||
let check = async () => { | ||
loadAndCheck(Builder(action.id, client)) | ||
loadAndCheck(Template(action.id, client)) | ||
} | ||
@@ -161,6 +164,6 @@ checking.push(check()) | ||
await Promise.all(checking) | ||
if (!Builder.remote && isLoading) { | ||
if (!Template.remote && isLoading) { | ||
isLoading = false | ||
filterStore.setKey('isLoading', false) | ||
endEffect() | ||
endTask() | ||
resolve() | ||
@@ -171,7 +174,7 @@ } | ||
if (Builder.remote) { | ||
if (Template.remote) { | ||
client | ||
.sync({ | ||
type: 'logux/subscribe', | ||
channel: Builder.plural, | ||
channel: Template.plural, | ||
filter | ||
@@ -185,3 +188,3 @@ }) | ||
} | ||
endEffect() | ||
endTask() | ||
resolve() | ||
@@ -193,3 +196,3 @@ } | ||
reject(e) | ||
endEffect() | ||
endTask() | ||
}) | ||
@@ -206,7 +209,7 @@ } | ||
function createAt(childId) { | ||
return Builder.cache[childId].createdAt | ||
return Template.cache[childId].createdAt | ||
} | ||
let removeAndListen = (childId, actionId) => { | ||
let child = Builder(childId, client) | ||
let child = Template(childId, client) | ||
let clear = child.listen(() => {}) | ||
@@ -234,3 +237,3 @@ remove(childId) | ||
add( | ||
Builder( | ||
Template( | ||
action.id, | ||
@@ -247,3 +250,3 @@ client, | ||
if (checkAllFields(action.fields)) { | ||
let child = Builder(action.id, client, action, meta) | ||
let child = Template(action.id, client, action, meta) | ||
try { | ||
@@ -264,3 +267,3 @@ add(child) | ||
} else if (checkSomeFields(action.fields)) { | ||
loadAndCheck(Builder(action.id, client)) | ||
loadAndCheck(Template(action.id, client)) | ||
} | ||
@@ -275,3 +278,3 @@ }), | ||
} else if (checkSomeFields(action.fields)) { | ||
let child = Builder(action.id, client) | ||
let child = Template(action.id, client) | ||
let clear = child.listen(() => {}) | ||
@@ -315,3 +318,3 @@ if (child.value.isLoading) await child.loading | ||
for (let unbindChild of unbindIds.values()) unbindChild() | ||
if (Builder.remote) { | ||
if (Template.remote) { | ||
if (!subscriptionError) { | ||
@@ -321,3 +324,3 @@ client.log.add( | ||
type: 'logux/unsubscribe', | ||
channel: Builder.plural, | ||
channel: Template.plural, | ||
filter | ||
@@ -330,8 +333,8 @@ }, | ||
client.log.removeReason(id) | ||
delete Builder.filters[id] | ||
delete Template.filters[id] | ||
} | ||
}) | ||
Builder.filters[id] = filterStore | ||
Template.filters[id] = filterStore | ||
} | ||
return Builder.filters[id] | ||
return Template.filters[id] | ||
} |
@@ -1,2 +0,2 @@ | ||
import { MapBuilder, MapStore } from 'nanostores' | ||
import { MapTemplate, MapStore } from 'nanostores' | ||
import { SyncMapValues } from '@logux/actions' | ||
@@ -53,4 +53,4 @@ import { Action, Meta } from '@logux/core' | ||
export interface SyncMapBuilder<Value extends SyncMapValues = any> | ||
extends MapBuilder< | ||
export interface SyncMapTemplate<Value extends SyncMapValues = any> | ||
extends MapTemplate< | ||
SyncMapValue<Value>, | ||
@@ -72,5 +72,5 @@ [Client] | [Client, Action, Meta, boolean | undefined], | ||
* ```ts | ||
* import { defineSyncMap } from '@logux/client' | ||
* import { syncMapTemplate } from '@logux/client' | ||
* | ||
* export const User = defineSyncMap<{ | ||
* export const User = syncMapTemplate<{ | ||
* login: string, | ||
@@ -87,3 +87,3 @@ * name?: string, | ||
*/ | ||
export function defineSyncMap<Value extends SyncMapValues>( | ||
export function syncMapTemplate<Value extends SyncMapValues>( | ||
plural: string, | ||
@@ -94,3 +94,3 @@ opts?: { | ||
} | ||
): SyncMapBuilder<Value> | ||
): SyncMapTemplate<Value> | ||
@@ -115,3 +115,3 @@ /** | ||
* @param client Logux Client instance. | ||
* @param Builder Store class from {@link defineSyncMap}. | ||
* @param Template Store template from {@link syncMapTemplate}. | ||
* @param values Initial value. | ||
@@ -123,3 +123,3 @@ * @return Promise until server validation for remote classes | ||
client: Client, | ||
Builder: SyncMapBuilder<Value>, | ||
Template: SyncMapTemplate<Value>, | ||
values: Value & { id: string } | ||
@@ -141,3 +141,3 @@ ): Promise<void> | ||
* @param client Logux Client instance. | ||
* @param Builder Store class from {@link defineSyncMap}. | ||
* @param Template Store template from {@link syncMapTemplate}. | ||
* @param values Initial value. | ||
@@ -148,3 +148,3 @@ * @return Promise with store instance. | ||
client: Client, | ||
Builder: SyncMapBuilder<Value>, | ||
Template: SyncMapTemplate<Value>, | ||
values: Value & { id: string } | ||
@@ -165,3 +165,3 @@ ): Promise<SyncMapStore<Value>> | ||
* @param client Logux Client instance. | ||
* @param Builder Store class from {@link defineSyncMap}. | ||
* @param Template Store template from {@link syncMapTemplate}. | ||
* @param id Store’s ID. | ||
@@ -174,3 +174,3 @@ * @param diff Store’s changes. | ||
client: Client, | ||
Builder: SyncMapBuilder<Value>, | ||
Template: SyncMapTemplate<Value>, | ||
id: string | { id: string }, | ||
@@ -184,3 +184,3 @@ diff: Partial<Value> | ||
client: Client, | ||
Builder: SyncMapBuilder<Value>, | ||
Template: SyncMapTemplate<Value>, | ||
id: string | { id: string }, | ||
@@ -231,3 +231,3 @@ key: ValueKey, | ||
* @param client Logux Client instance. | ||
* @param Builder Store class from {@link defineSyncMap}. | ||
* @param Template Store template from {@link syncMapTemplate}. | ||
* @param id Store’s ID. | ||
@@ -239,3 +239,3 @@ * @return Promise until server validation for remote classes | ||
client: Client, | ||
Builder: SyncMapBuilder, | ||
Template: SyncMapTemplate, | ||
id: string | { id: string } | ||
@@ -242,0 +242,0 @@ ): Promise<void> |
@@ -1,2 +0,2 @@ | ||
import { defineMap, getValue, clean, startEffect, effect } from 'nanostores' | ||
import { mapTemplate, clean, startTask, task } from 'nanostores' | ||
import { isFirstOlder } from '@logux/core' | ||
@@ -24,4 +24,4 @@ | ||
export function defineSyncMap(plural, opts = {}) { | ||
let Builder = defineMap( | ||
export function syncMapTemplate(plural, opts = {}) { | ||
let Template = mapTemplate( | ||
(store, id, client, createAction, createMeta, alreadySubscribed) => { | ||
@@ -45,4 +45,4 @@ if (!client) { | ||
store.client = client | ||
store.offline = Builder.offline | ||
store.remote = Builder.remote | ||
store.offline = Template.offline | ||
store.remote = Template.remote | ||
@@ -84,3 +84,3 @@ store.lastChanged = {} | ||
} else { | ||
let endEffect = startEffect() | ||
let endTask = startTask() | ||
let loadingResolve, loadingReject | ||
@@ -90,7 +90,7 @@ store.loading = new Promise((resolve, reject) => { | ||
resolve() | ||
endEffect() | ||
endTask() | ||
} | ||
loadingReject = e => { | ||
reject(e) | ||
endEffect() | ||
endTask() | ||
} | ||
@@ -187,3 +187,3 @@ }) | ||
async (action, meta) => { | ||
await effect(async () => { | ||
await task(async () => { | ||
try { | ||
@@ -204,3 +204,3 @@ await track(client, meta.id) | ||
async (action, meta) => { | ||
let endEffect = startEffect() | ||
let endTask = startTask() | ||
changeIfLast(store, action.fields, meta) | ||
@@ -216,3 +216,3 @@ try { | ||
} | ||
endEffect() | ||
endTask() | ||
} catch { | ||
@@ -254,3 +254,3 @@ client.log.changeMeta(meta.id, { reasons: [] }) | ||
} | ||
endEffect() | ||
endTask() | ||
}) | ||
@@ -285,15 +285,15 @@ } | ||
Builder.plural = plural | ||
Builder.offline = !!opts.offline | ||
Builder.remote = opts.remote !== false | ||
Template.plural = plural | ||
Template.offline = !!opts.offline | ||
Template.remote = opts.remote !== false | ||
if (process.env.NODE_ENV !== 'production') { | ||
let oldClean = Builder[clean] | ||
Builder[clean] = () => { | ||
let oldClean = Template[clean] | ||
Template[clean] = () => { | ||
oldClean() | ||
if (Builder.filters) { | ||
for (let id in Builder.filters) { | ||
Builder.filters[id][clean]() | ||
if (Template.filters) { | ||
for (let id in Template.filters) { | ||
Template.filters[id][clean]() | ||
} | ||
delete Builder.filters | ||
delete Template.filters | ||
} | ||
@@ -303,22 +303,22 @@ } | ||
return Builder | ||
return Template | ||
} | ||
function addSyncAction(client, Builder, action) { | ||
let meta = { indexes: getIndexes(Builder.plural, action.id) } | ||
if (!Builder.remote) { | ||
function addSyncAction(client, Template, action) { | ||
let meta = { indexes: getIndexes(Template.plural, action.id) } | ||
if (!Template.remote) { | ||
action.type += 'd' | ||
} | ||
if (Builder.remote) { | ||
return effect(() => client.sync(action, meta)) | ||
if (Template.remote) { | ||
return task(() => client.sync(action, meta)) | ||
} else { | ||
return effect(() => client.log.add(action, meta)) | ||
return task(() => client.log.add(action, meta)) | ||
} | ||
} | ||
export function createSyncMap(client, Builder, fields) { | ||
export function createSyncMap(client, Template, fields) { | ||
let id = fields.id | ||
delete fields.id | ||
return addSyncAction(client, Builder, { | ||
type: `${Builder.plural}/create`, | ||
return addSyncAction(client, Template, { | ||
type: `${Template.plural}/create`, | ||
id, | ||
@@ -329,3 +329,3 @@ fields | ||
export async function buildNewSyncMap(client, Builder, fields) { | ||
export async function buildNewSyncMap(client, Template, fields) { | ||
let id = fields.id | ||
@@ -335,4 +335,4 @@ delete fields.id | ||
let verb = Builder.remote ? 'create' : 'created' | ||
let type = `${Builder.plural}/${verb}` | ||
let verb = Template.remote ? 'create' : 'created' | ||
let type = `${Template.plural}/${verb}` | ||
let action = { type, id, fields } | ||
@@ -342,15 +342,15 @@ let meta = { | ||
time: parseInt(actionId), | ||
indexes: getIndexes(Builder.plural, id) | ||
indexes: getIndexes(Template.plural, id) | ||
} | ||
if (Builder.remote) meta.sync = true | ||
await effect(() => client.log.add(action, meta)) | ||
if (Template.remote) meta.sync = true | ||
await task(() => client.log.add(action, meta)) | ||
let store = Builder(id, client, action, meta) | ||
let store = Template(id, client, action, meta) | ||
return store | ||
} | ||
export function changeSyncMapById(client, Builder, id, fields, value) { | ||
export function changeSyncMapById(client, Template, id, fields, value) { | ||
if (value) fields = { [fields]: value } | ||
return addSyncAction(client, Builder, { | ||
type: `${Builder.plural}/change`, | ||
return addSyncAction(client, Template, { | ||
type: `${Template.plural}/change`, | ||
id, | ||
@@ -364,8 +364,8 @@ fields | ||
changeIfLast(store, fields) | ||
return changeSyncMapById(store.client, store, getValue(store).id, fields) | ||
return changeSyncMapById(store.client, store, store.get().id, fields) | ||
} | ||
export function deleteSyncMapById(client, Builder, id) { | ||
return addSyncAction(client, Builder, { | ||
type: `${Builder.plural}/delete`, | ||
export function deleteSyncMapById(client, Template, id) { | ||
return addSyncAction(client, Template, { | ||
type: `${Template.plural}/delete`, | ||
id | ||
@@ -376,3 +376,3 @@ }) | ||
export function deleteSyncMap(store) { | ||
return deleteSyncMapById(store.client, store, getValue(store).id) | ||
return deleteSyncMapById(store.client, store, store.get().id) | ||
} |
@@ -6,4 +6,4 @@ export { | ||
buildNewSyncMap, | ||
SyncMapBuilder, | ||
defineSyncMap, | ||
SyncMapTemplate, | ||
syncMapTemplate, | ||
createSyncMap, | ||
@@ -39,3 +39,3 @@ changeSyncMap, | ||
export { request, RequestOptions } from './request/index.js' | ||
export { createAuth, Auth } from './create-auth/index.js' | ||
export { createAuth, AuthStore } from './create-auth/index.js' | ||
export { encryptActions } from './encrypt-actions/index.js' | ||
@@ -42,0 +42,0 @@ export { CrossTabClient } from './cross-tab-client/index.js' |
export { | ||
deleteSyncMapById, | ||
changeSyncMapById, | ||
syncMapTemplate, | ||
buildNewSyncMap, | ||
defineSyncMap, | ||
createSyncMap, | ||
@@ -7,0 +7,0 @@ changeSyncMap, |
@@ -13,10 +13,2 @@ import { LogStore } from '@logux/core' | ||
* ``` | ||
* | ||
* ```js | ||
* import IndexedStore from '@logux/client/indexed-store' | ||
* const createStore = createLoguxCreator({ | ||
* …, | ||
* store: new IndexedStore() | ||
* }) | ||
* ``` | ||
*/ | ||
@@ -23,0 +15,0 @@ export class IndexedStore extends LogStore { |
{ | ||
"name": "@logux/client", | ||
"version": "0.13.3", | ||
"version": "0.14.0", | ||
"description": "Logux base components to build web client", | ||
@@ -32,3 +32,6 @@ "keywords": [ | ||
"@logux/core": "^0.7.0", | ||
"nanostores": "^0.4.3", | ||
"@nanostores/preact": "^0.0.0", | ||
"@nanostores/react": "^0.0.0", | ||
"@nanostores/vue": "^0.0.0", | ||
"nanostores": "^0.5.0", | ||
"preact": ">=10.0.0", | ||
@@ -40,2 +43,11 @@ "react": ">=16.8.0", | ||
"peerDependenciesMeta": { | ||
"@nanostores/preact": { | ||
"optional": true | ||
}, | ||
"@nanostores/react": { | ||
"optional": true | ||
}, | ||
"@nanostores/vue": { | ||
"optional": true | ||
}, | ||
"preact": { | ||
@@ -57,6 +69,6 @@ "optional": true | ||
"fast-json-stable-stringify": "^2.1.0", | ||
"nanodelay": "^2.0.0", | ||
"nanoevents": "^6.0.0", | ||
"nanoid": "^3.1.24" | ||
"nanodelay": "^2.0.2", | ||
"nanoevents": "^6.0.2", | ||
"nanoid": "^3.1.30" | ||
} | ||
} |
import { Context as PreactContext, Component, ComponentType } from 'preact' | ||
import { SyncMapValues, LoguxNotFoundError } from '@logux/actions' | ||
import { StoreValue, MapBuilder } from 'nanostores' | ||
import { StoreValue, MapTemplate } from 'nanostores' | ||
@@ -11,5 +11,5 @@ import { | ||
import { FilterOptions, FilterStore, Filter } from '../create-filter/index.js' | ||
import { SyncMapBuilder, SyncMapValue } from '../define-sync-map/index.js' | ||
import { SyncMapTemplate, SyncMapValue } from '../define-sync-map/index.js' | ||
import { Client } from '../client/index.js' | ||
import { Auth } from '../create-auth/index.js' | ||
import { AuthStore } from '../create-auth/index.js' | ||
@@ -100,3 +100,3 @@ /** | ||
* | ||
* @param Builder Store builder. | ||
* @param Template Store template. | ||
* @param id Store ID. | ||
@@ -107,7 +107,7 @@ * @param args Other store arguments. | ||
export function useSync<Value extends SyncMapValues>( | ||
Builder: SyncMapBuilder<Value>, | ||
Template: SyncMapTemplate<Value>, | ||
id: string | ||
): SyncMapValue<Value> | ||
export function useSync<Value extends object, Args extends any[]>( | ||
Builder: MapBuilder<Value, [Client, ...Args]>, | ||
Template: MapTemplate<Value, [Client, ...Args]>, | ||
id: string, | ||
@@ -134,3 +134,3 @@ ...args: Args | ||
* | ||
* @param Builder Store class. | ||
* @param Template Store class. | ||
* @param filter Key-value filter for stores. | ||
@@ -141,3 +141,3 @@ * @param opts Filter options. | ||
export function useFilter<Value extends SyncMapValues>( | ||
Builder: SyncMapBuilder<Value>, | ||
Template: SyncMapTemplate<Value>, | ||
filter?: Filter<Value>, | ||
@@ -163,2 +163,2 @@ opts?: FilterOptions | ||
*/ | ||
export function useAuth(): StoreValue<Auth> | ||
export function useAuth(): StoreValue<AuthStore> |
import { useState, useContext, useEffect, useRef } from 'preact/hooks' | ||
import { createContext, h, Component } from 'preact' | ||
import { useStore } from 'nanostores/preact' | ||
import { getValue } from 'nanostores' | ||
import { useStore } from '@nanostores/preact' | ||
@@ -24,5 +23,4 @@ import { createFilter } from '../create-filter/index.js' | ||
let [, forceRender] = useState({}) | ||
let value = store.get() | ||
let value = getValue(store) | ||
if (process.env.NODE_ENV !== 'production') { | ||
@@ -65,6 +63,6 @@ let errorProcessors = useContext(ErrorsContext) || {} | ||
export function useSync(Builder, id, ...builderArgs) { | ||
export function useSync(Template, id, ...builderArgs) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (typeof Builder !== 'function') { | ||
throw new Error('Use useStore() from nanostores/react for stores') | ||
if (typeof Template !== 'function') { | ||
throw new Error('Use useStore() from @nanostores/preact for stores') | ||
} | ||
@@ -74,3 +72,3 @@ } | ||
let client = useClient() | ||
let store = Builder(id, client, ...builderArgs) | ||
let store = Template(id, client, ...builderArgs) | ||
@@ -77,0 +75,0 @@ return useSyncStore(store) |
@@ -6,3 +6,3 @@ import ReactTesting from '@testing-library/preact' | ||
import { useSync, ClientContext, ChannelErrors } from './index.js' | ||
import { defineSyncMap, TestClient } from '../index.js' | ||
import { syncMapTemplate, TestClient } from '../index.js' | ||
@@ -12,3 +12,3 @@ let { render, screen } = ReactTesting | ||
let Store = defineSyncMap('test') | ||
let Store = syncMapTemplate('test') | ||
@@ -33,3 +33,3 @@ let IdTest = () => { | ||
it('does not have ChannelErrors check in production mode', async () => { | ||
expect(getText(h(ChannelErrors, {}, h(IdTest)))).toEqual('loading') | ||
expect(getText(h(ChannelErrors, {}, h(IdTest)))).toBe('loading') | ||
}) |
@@ -1,5 +0,5 @@ | ||
import { MapBuilder, MapStore } from 'nanostores' | ||
import { MapTemplate, MapStore } from 'nanostores' | ||
import { SyncMapValues } from '@logux/actions' | ||
import { SyncMapBuilder, SyncMapStore } from '../define-sync-map/index.js' | ||
import { SyncMapTemplate, SyncMapStore } from '../define-sync-map/index.js' | ||
import { Client } from '../client/index.js' | ||
@@ -10,3 +10,3 @@ | ||
client: Client, | ||
Builder: SyncMapBuilder<Value>, | ||
Template: SyncMapTemplate<Value>, | ||
value: Omit<Value, 'id'> & { id?: string } | ||
@@ -16,3 +16,3 @@ ): SyncMapStore<Value> | ||
client: Client, | ||
Builder: MapBuilder<Value>, | ||
Template: MapTemplate<Value>, | ||
value: Omit<Value, 'id'> & { id?: string } | ||
@@ -44,3 +44,3 @@ ): MapStore<Value> | ||
* @param client `TestClient` instance. | ||
* @param Builder Store builder. | ||
* @param Template Store builder. | ||
* @param value Store values. | ||
@@ -67,4 +67,4 @@ * @returns The mocked store. | ||
* | ||
* @param Builder Store builder. | ||
* @param Template Store builder. | ||
*/ | ||
export function emptyInTest(Builder: SyncMapBuilder): void | ||
export function emptyInTest(Template: SyncMapTemplate): void |
let lastId = 0 | ||
export function emptyInTest(Builder) { | ||
if (!Builder.mocks) Builder.mocked = true | ||
export function emptyInTest(Template) { | ||
if (!Template.mocks) Template.mocked = true | ||
} | ||
export function prepareForTest(client, Builder, value) { | ||
if (!Builder.mocks) Builder.mocked = true | ||
export function prepareForTest(client, Template, value) { | ||
if (!Template.mocks) Template.mocked = true | ||
let { id, ...keys } = value | ||
if (!id) { | ||
if (Builder.plural) { | ||
id = `${Builder.plural}:${Object.keys(Builder.cache).length + 1}` | ||
if (Template.plural) { | ||
id = `${Template.plural}:${Object.keys(Template.cache).length + 1}` | ||
} else { | ||
@@ -19,3 +19,3 @@ id = `${lastId++}` | ||
let store = Builder(id, client) | ||
let store = Template(id, client) | ||
store.listen(() => {}) | ||
@@ -22,0 +22,0 @@ |
import { Context as ReactContext, Component, ComponentType } from 'react' | ||
import { SyncMapValues, LoguxNotFoundError } from '@logux/actions' | ||
import { StoreValue, MapBuilder } from 'nanostores' | ||
import { StoreValue, MapTemplate } from 'nanostores' | ||
@@ -11,5 +11,5 @@ import { | ||
import { FilterOptions, FilterStore, Filter } from '../create-filter/index.js' | ||
import { SyncMapBuilder, SyncMapValue } from '../define-sync-map/index.js' | ||
import { SyncMapTemplate, SyncMapValue } from '../define-sync-map/index.js' | ||
import { AuthStore } from '../create-auth/index.js' | ||
import { Client } from '../client/index.js' | ||
import { Auth } from '../create-auth/index.js' | ||
@@ -100,3 +100,3 @@ /** | ||
* | ||
* @param Builder Store builder. | ||
* @param Template Store builder. | ||
* @param id Store ID. | ||
@@ -107,7 +107,7 @@ * @param args Other store arguments. | ||
export function useSync<Value extends SyncMapValues>( | ||
Builder: SyncMapBuilder<Value>, | ||
Template: SyncMapTemplate<Value>, | ||
id: string | ||
): SyncMapValue<Value> | ||
export function useSync<Value extends object, Args extends any[]>( | ||
Builder: MapBuilder<Value, [Client, ...Args]>, | ||
Template: MapTemplate<Value, [Client, ...Args]>, | ||
id: string, | ||
@@ -134,3 +134,3 @@ ...args: Args | ||
* | ||
* @param Builder Store class. | ||
* @param Template Store template. | ||
* @param filter Key-value filter for stores. | ||
@@ -141,3 +141,3 @@ * @param opts Filter options. | ||
export function useFilter<Value extends SyncMapValues>( | ||
Builder: SyncMapBuilder<Value>, | ||
Template: SyncMapTemplate<Value>, | ||
filter?: Filter<Value>, | ||
@@ -163,2 +163,2 @@ opts?: FilterOptions | ||
*/ | ||
export function useAuth(): StoreValue<Auth> | ||
export function useAuth(): StoreValue<AuthStore> |
@@ -1,3 +0,2 @@ | ||
import { useStore, batch } from 'nanostores/react' | ||
import { getValue } from 'nanostores' | ||
import { useStore, batch } from '@nanostores/react' | ||
import React from 'react' | ||
@@ -23,3 +22,3 @@ | ||
let [, forceRender] = React.useState({}) | ||
let value = getValue(store) | ||
let value = store.get() | ||
@@ -59,6 +58,6 @@ if (process.env.NODE_ENV !== 'production') { | ||
export function useSync(Builder, id, ...builderArgs) { | ||
export function useSync(Template, id, ...builderArgs) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (typeof Builder !== 'function') { | ||
throw new Error('Use useStore() from nanostores/react for stores') | ||
if (typeof Template !== 'function') { | ||
throw new Error('Use useStore() from @nanostores/react for stores') | ||
} | ||
@@ -68,3 +67,3 @@ } | ||
let client = useClient() | ||
let store = Builder(id, client, ...builderArgs) | ||
let store = Template(id, client, ...builderArgs) | ||
@@ -71,0 +70,0 @@ return useSyncStore(store) |
@@ -6,3 +6,3 @@ import ReactTesting from '@testing-library/react' | ||
import { useSync, ClientContext, ChannelErrors } from './index.js' | ||
import { defineSyncMap, TestClient } from '../index.js' | ||
import { syncMapTemplate, TestClient } from '../index.js' | ||
@@ -12,3 +12,3 @@ let { render, screen } = ReactTesting | ||
let Store = defineSyncMap('test') | ||
let Store = syncMapTemplate('test') | ||
@@ -33,3 +33,3 @@ let IdTest = () => { | ||
it('does not have ChannelErrors check in production mode', async () => { | ||
expect(getText(h(ChannelErrors, {}, h(IdTest)))).toEqual('loading') | ||
expect(getText(h(ChannelErrors, {}, h(IdTest)))).toBe('loading') | ||
}) |
@@ -11,6 +11,6 @@ import { | ||
import { SyncMapValues, LoguxNotFoundError } from '@logux/actions' | ||
import { StoreValue, MapBuilder } from 'nanostores' | ||
import { StoreValue, MapTemplate } from 'nanostores' | ||
import { FilterOptions, FilterStore, Filter } from '../create-filter/index.js' | ||
import { SyncMapBuilder, SyncMapValue } from '../define-sync-map/index.js' | ||
import { SyncMapTemplate, SyncMapValue } from '../define-sync-map/index.js' | ||
import { ChannelError } from '../logux-undo-error/index.js' | ||
@@ -83,3 +83,3 @@ import { Client } from '../client/index.js' | ||
* | ||
* @param Builder Store builder. | ||
* @param Template Store template. | ||
* @param id Store ID. | ||
@@ -90,7 +90,7 @@ * @param args Other store arguments. | ||
export function useSync<Value extends SyncMapValues>( | ||
Builder: SyncMapBuilder<Value>, | ||
Template: SyncMapTemplate<Value>, | ||
id: Refable<string> | ||
): ReadonlyRef<SyncMapValue<Value>> | ||
export function useSync<Value extends object, Args extends any[]>( | ||
Builder: MapBuilder<Value, [Client, ...Args]>, | ||
Template: MapTemplate<Value, [Client, ...Args]>, | ||
id: Refable<string>, | ||
@@ -124,3 +124,3 @@ ...args: Args | ||
* | ||
* @param Builder Store class. | ||
* @param Template Store class. | ||
* @param filter Key-value filter for stores. | ||
@@ -131,3 +131,3 @@ * @param opts Filter options. | ||
export function useFilter<Value extends SyncMapValues>( | ||
Builder: SyncMapBuilder<Value>, | ||
Template: SyncMapTemplate<Value>, | ||
filter?: Refable<Filter<Value>>, | ||
@@ -134,0 +134,0 @@ opts?: Refable<FilterOptions> |
@@ -5,3 +5,2 @@ import { | ||
onErrorCaptured, | ||
triggerRef, | ||
reactive, | ||
@@ -17,3 +16,3 @@ readonly, | ||
} from 'vue' | ||
import { useStore } from 'nanostores/vue' | ||
import { useStore } from '@nanostores/vue' | ||
@@ -58,11 +57,7 @@ import { createFilter } from '../create-filter/index.js' | ||
let state = ref() | ||
let unsubscribe | ||
let listener = newState => { | ||
state.value = newState | ||
triggerRef(state) | ||
} | ||
let unsubscribe = store.subscribe(value => { | ||
state.value = value | ||
}) | ||
unsubscribe = store.subscribe(listener) | ||
if (store.loading) { | ||
@@ -82,6 +77,6 @@ watch(error, () => { | ||
export function useSync(Builder, id, ...builderArgs) { | ||
export function useSync(Template, id, ...builderArgs) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (typeof Builder !== 'function') { | ||
throw new Error('Use useStore() from nanostores/vue for stores') | ||
if (typeof Template !== 'function') { | ||
throw new Error('Use useStore() from @nanostores/vue for stores') | ||
} | ||
@@ -104,3 +99,3 @@ } | ||
() => { | ||
state.value = useSyncStore(Builder(id.value, client, ...builderArgs)) | ||
state.value = useSyncStore(Template(id.value, client, ...builderArgs)) | ||
}, | ||
@@ -116,3 +111,3 @@ { immediate: true } | ||
export function useFilter(Builder, filter = {}, opts = {}) { | ||
export function useFilter(Template, filter = {}, opts = {}) { | ||
if (!isRef(filter)) filter = ref(filter) | ||
@@ -132,3 +127,3 @@ if (!isRef(opts)) opts = ref(opts) | ||
state.value = useSyncStore( | ||
createFilter(client, Builder, filter.value, opts.value) | ||
createFilter(client, Template, filter.value, opts.value) | ||
) | ||
@@ -135,0 +130,0 @@ }, |
@@ -6,3 +6,3 @@ import VueTesting from '@testing-library/vue' | ||
import { loguxPlugin, useSync, ChannelErrors, useFilter } from './index.js' | ||
import { defineSyncMap, TestClient } from '../index.js' | ||
import { syncMapTemplate, TestClient } from '../index.js' | ||
@@ -12,3 +12,3 @@ let { defineComponent, h, isReadonly, nextTick } = Vue | ||
let Store = defineSyncMap('test') | ||
let Store = syncMapTemplate('test') | ||
@@ -42,4 +42,4 @@ let IdTest = defineComponent(() => { | ||
let list = useFilter(Store) | ||
expect(isReadonly(state)).toEqual(false) | ||
expect(isReadonly(list)).toEqual(false) | ||
expect(isReadonly(state)).toBe(false) | ||
expect(isReadonly(list)).toBe(false) | ||
return () => null | ||
@@ -65,3 +65,3 @@ }), | ||
) | ||
).toEqual('loading') | ||
).toBe('loading') | ||
}) |
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
147095
4915
14
+ Addednanostores@0.5.13(transitive)
- Removednanostores@0.4.9(transitive)
Updatednanodelay@^2.0.2
Updatednanoevents@^6.0.2
Updatednanoid@^3.1.30