@livestore/livestore 0.0.58-dev.9 → 0.1.0-dev.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (191) hide show
  1. package/README.md +1 -117
  2. package/dist/.tsbuildinfo +1 -1
  3. package/dist/effect/LiveStore.d.ts +6 -6
  4. package/dist/effect/LiveStore.d.ts.map +1 -1
  5. package/dist/effect/LiveStore.js +1 -1
  6. package/dist/effect/LiveStore.js.map +1 -1
  7. package/dist/global-state.d.ts.map +1 -1
  8. package/dist/global-state.js +2 -1
  9. package/dist/global-state.js.map +1 -1
  10. package/dist/index.d.ts +11 -8
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +7 -4
  13. package/dist/index.js.map +1 -1
  14. package/dist/reactiveQueries/base-class.d.ts +5 -4
  15. package/dist/reactiveQueries/base-class.d.ts.map +1 -1
  16. package/dist/reactiveQueries/base-class.js.map +1 -1
  17. package/dist/reactiveQueries/computed.d.ts +35 -0
  18. package/dist/reactiveQueries/computed.d.ts.map +1 -0
  19. package/dist/reactiveQueries/computed.js +57 -0
  20. package/dist/reactiveQueries/computed.js.map +1 -0
  21. package/dist/reactiveQueries/graphql.d.ts +2 -1
  22. package/dist/reactiveQueries/graphql.d.ts.map +1 -1
  23. package/dist/reactiveQueries/graphql.js.map +1 -1
  24. package/dist/reactiveQueries/sql.d.ts +2 -2
  25. package/dist/reactiveQueries/sql.d.ts.map +1 -1
  26. package/dist/reactiveQueries/sql.js +3 -3
  27. package/dist/reactiveQueries/sql.js.map +1 -1
  28. package/dist/reactiveQueries/sql.test.js +2 -2
  29. package/dist/reactiveQueries/sql.test.js.map +1 -1
  30. package/dist/row-query.d.ts +3 -2
  31. package/dist/row-query.d.ts.map +1 -1
  32. package/dist/row-query.js +14 -10
  33. package/dist/row-query.js.map +1 -1
  34. package/dist/store/create-store.d.ts +28 -0
  35. package/dist/store/create-store.d.ts.map +1 -0
  36. package/dist/store/create-store.js +85 -0
  37. package/dist/store/create-store.js.map +1 -0
  38. package/dist/store/devtools.d.ts +19 -0
  39. package/dist/store/devtools.d.ts.map +1 -0
  40. package/dist/store/devtools.js +141 -0
  41. package/dist/store/devtools.js.map +1 -0
  42. package/dist/store/store-context.d.ts +26 -0
  43. package/dist/store/store-context.d.ts.map +1 -0
  44. package/dist/store/store-context.js +6 -0
  45. package/dist/store/store-context.js.map +1 -0
  46. package/dist/store/store-types.d.ts +98 -0
  47. package/dist/store/store-types.d.ts.map +1 -0
  48. package/dist/store/store-types.js +6 -0
  49. package/dist/store/store-types.js.map +1 -0
  50. package/dist/store/store.d.ts +88 -0
  51. package/dist/store/store.d.ts.map +1 -0
  52. package/dist/store/store.js +367 -0
  53. package/dist/store/store.js.map +1 -0
  54. package/dist/store-devtools.d.ts +2 -2
  55. package/dist/store-devtools.d.ts.map +1 -1
  56. package/dist/store-devtools.js +2 -2
  57. package/dist/store-devtools.js.map +1 -1
  58. package/dist/store.d.ts +8 -8
  59. package/dist/store.d.ts.map +1 -1
  60. package/dist/store.js +54 -54
  61. package/dist/store.js.map +1 -1
  62. package/dist/utils/dev.d.ts +1 -0
  63. package/dist/utils/dev.d.ts.map +1 -1
  64. package/dist/utils/dev.js +5 -0
  65. package/dist/utils/dev.js.map +1 -1
  66. package/dist/{react/utils → utils}/stack-info.d.ts +1 -2
  67. package/dist/utils/stack-info.d.ts.map +1 -0
  68. package/dist/{react/utils → utils}/stack-info.js +1 -9
  69. package/dist/utils/stack-info.js.map +1 -0
  70. package/dist/utils/stack-info.test.d.ts.map +1 -0
  71. package/dist/{__tests__/react/utils → utils}/stack-info.test.js +1 -1
  72. package/dist/utils/stack-info.test.js.map +1 -0
  73. package/dist/utils/tests/fixture.d.ts +259 -0
  74. package/dist/utils/tests/fixture.d.ts.map +1 -0
  75. package/dist/utils/tests/fixture.js +32 -0
  76. package/dist/utils/tests/fixture.js.map +1 -0
  77. package/dist/utils/tests/mod.d.ts +3 -0
  78. package/dist/utils/tests/mod.d.ts.map +1 -0
  79. package/dist/utils/tests/mod.js +3 -0
  80. package/dist/utils/tests/mod.js.map +1 -0
  81. package/dist/utils/tests/otel.d.ts.map +1 -0
  82. package/dist/{__tests__/react/utils → utils/tests}/otel.js +3 -3
  83. package/dist/utils/tests/otel.js.map +1 -0
  84. package/package.json +13 -20
  85. package/src/ambient.d.ts +3 -1
  86. package/src/effect/LiveStore.ts +7 -7
  87. package/src/global-state.ts +5 -1
  88. package/src/index.ts +19 -7
  89. package/src/reactiveQueries/base-class.ts +5 -4
  90. package/src/reactiveQueries/{js.ts → computed.ts} +3 -3
  91. package/src/reactiveQueries/graphql.ts +2 -1
  92. package/src/reactiveQueries/sql.test.ts +2 -2
  93. package/src/reactiveQueries/sql.ts +5 -5
  94. package/src/row-query.ts +33 -17
  95. package/src/store/create-store.ts +214 -0
  96. package/src/{store-devtools.ts → store/devtools.ts} +9 -9
  97. package/src/store/store-types.ts +110 -0
  98. package/src/{store.ts → store/store.ts} +56 -415
  99. package/src/utils/dev.ts +6 -0
  100. package/src/{__tests__/react/utils → utils}/stack-info.test.ts +1 -1
  101. package/src/{react/utils → utils}/stack-info.ts +2 -12
  102. package/src/utils/tests/fixture.ts +73 -0
  103. package/src/utils/tests/mod.ts +2 -0
  104. package/src/{__tests__/react/utils → utils/tests}/otel.ts +4 -4
  105. package/tsconfig.json +1 -2
  106. package/vitest.config.js +0 -8
  107. package/dist/__tests__/react/fixture.d.ts +0 -461
  108. package/dist/__tests__/react/fixture.d.ts.map +0 -1
  109. package/dist/__tests__/react/fixture.js +0 -68
  110. package/dist/__tests__/react/fixture.js.map +0 -1
  111. package/dist/__tests__/react/utils/otel.d.ts.map +0 -1
  112. package/dist/__tests__/react/utils/otel.js.map +0 -1
  113. package/dist/__tests__/react/utils/stack-info.test.d.ts.map +0 -1
  114. package/dist/__tests__/react/utils/stack-info.test.js.map +0 -1
  115. package/dist/react/LiveStoreContext.d.ts +0 -7
  116. package/dist/react/LiveStoreContext.d.ts.map +0 -1
  117. package/dist/react/LiveStoreContext.js +0 -13
  118. package/dist/react/LiveStoreContext.js.map +0 -1
  119. package/dist/react/LiveStoreProvider.d.ts +0 -49
  120. package/dist/react/LiveStoreProvider.d.ts.map +0 -1
  121. package/dist/react/LiveStoreProvider.js +0 -169
  122. package/dist/react/LiveStoreProvider.js.map +0 -1
  123. package/dist/react/LiveStoreProvider.test.d.ts +0 -2
  124. package/dist/react/LiveStoreProvider.test.d.ts.map +0 -1
  125. package/dist/react/LiveStoreProvider.test.js +0 -62
  126. package/dist/react/LiveStoreProvider.test.js.map +0 -1
  127. package/dist/react/components/LiveList.d.ts +0 -21
  128. package/dist/react/components/LiveList.d.ts.map +0 -1
  129. package/dist/react/components/LiveList.js +0 -31
  130. package/dist/react/components/LiveList.js.map +0 -1
  131. package/dist/react/index.d.ts +0 -11
  132. package/dist/react/index.d.ts.map +0 -1
  133. package/dist/react/index.js +0 -10
  134. package/dist/react/index.js.map +0 -1
  135. package/dist/react/useAtom.d.ts +0 -10
  136. package/dist/react/useAtom.d.ts.map +0 -1
  137. package/dist/react/useAtom.js +0 -37
  138. package/dist/react/useAtom.js.map +0 -1
  139. package/dist/react/useLocalId.d.ts +0 -10
  140. package/dist/react/useLocalId.d.ts.map +0 -1
  141. package/dist/react/useLocalId.js +0 -22
  142. package/dist/react/useLocalId.js.map +0 -1
  143. package/dist/react/useQuery.d.ts +0 -9
  144. package/dist/react/useQuery.d.ts.map +0 -1
  145. package/dist/react/useQuery.js +0 -70
  146. package/dist/react/useQuery.js.map +0 -1
  147. package/dist/react/useQuery.test.d.ts +0 -2
  148. package/dist/react/useQuery.test.d.ts.map +0 -1
  149. package/dist/react/useQuery.test.js +0 -51
  150. package/dist/react/useQuery.test.js.map +0 -1
  151. package/dist/react/useRow.d.ts +0 -46
  152. package/dist/react/useRow.d.ts.map +0 -1
  153. package/dist/react/useRow.js +0 -94
  154. package/dist/react/useRow.js.map +0 -1
  155. package/dist/react/useRow.test.d.ts +0 -2
  156. package/dist/react/useRow.test.d.ts.map +0 -1
  157. package/dist/react/useRow.test.js +0 -208
  158. package/dist/react/useRow.test.js.map +0 -1
  159. package/dist/react/useTemporaryQuery.d.ts +0 -22
  160. package/dist/react/useTemporaryQuery.d.ts.map +0 -1
  161. package/dist/react/useTemporaryQuery.js +0 -75
  162. package/dist/react/useTemporaryQuery.js.map +0 -1
  163. package/dist/react/useTemporaryQuery.test.d.ts +0 -2
  164. package/dist/react/useTemporaryQuery.test.d.ts.map +0 -1
  165. package/dist/react/useTemporaryQuery.test.js +0 -59
  166. package/dist/react/useTemporaryQuery.test.js.map +0 -1
  167. package/dist/react/utils/stack-info.d.ts.map +0 -1
  168. package/dist/react/utils/stack-info.js.map +0 -1
  169. package/dist/react/utils/useStateRefWithReactiveInput.d.ts +0 -13
  170. package/dist/react/utils/useStateRefWithReactiveInput.d.ts.map +0 -1
  171. package/dist/react/utils/useStateRefWithReactiveInput.js +0 -38
  172. package/dist/react/utils/useStateRefWithReactiveInput.js.map +0 -1
  173. package/src/__tests__/react/fixture.tsx +0 -126
  174. package/src/react/LiveStoreContext.ts +0 -20
  175. package/src/react/LiveStoreProvider.test.tsx +0 -109
  176. package/src/react/LiveStoreProvider.tsx +0 -291
  177. package/src/react/__snapshots__/useRow.test.tsx.snap +0 -359
  178. package/src/react/components/LiveList.tsx +0 -84
  179. package/src/react/index.ts +0 -19
  180. package/src/react/useAtom.ts +0 -55
  181. package/src/react/useLocalId.ts +0 -34
  182. package/src/react/useQuery.test.tsx +0 -82
  183. package/src/react/useQuery.ts +0 -106
  184. package/src/react/useRow.test.tsx +0 -345
  185. package/src/react/useRow.ts +0 -180
  186. package/src/react/useTemporaryQuery.test.tsx +0 -98
  187. package/src/react/useTemporaryQuery.ts +0 -131
  188. package/src/react/utils/useStateRefWithReactiveInput.ts +0 -51
  189. package/src/store-context.ts +0 -23
  190. /package/dist/{__tests__/react/utils → utils}/stack-info.test.d.ts +0 -0
  191. /package/dist/{__tests__/react/utils → utils/tests}/otel.d.ts +0 -0
@@ -1,131 +0,0 @@
1
- import type { QueryInfo } from '@livestore/common'
2
- import * as otel from '@opentelemetry/api'
3
- import React from 'react'
4
-
5
- import type { LiveQuery } from '../reactiveQueries/base-class.js'
6
- import { useStore } from './LiveStoreContext.js'
7
- import { useQueryRef } from './useQuery.js'
8
-
9
- // NOTE Given `useMemo` will be called multiple times (e.g. when using React Strict mode or Fast Refresh),
10
- // we are using this cache to avoid starting multiple queries/spans for the same component.
11
- // This is somewhat against some recommended React best practices, but it should be fine in our case below.
12
- // Please definitely open an issue if you see or run into any problems with this approach!
13
- const cache = new Map<
14
- string,
15
- | {
16
- _tag: 'active'
17
- rc: number
18
- query$: LiveQuery<any, any>
19
- span: otel.Span
20
- otelContext: otel.Context
21
- }
22
- | {
23
- _tag: 'destroyed'
24
- }
25
- >()
26
-
27
- export type DepKey = string | number | ReadonlyArray<string | number>
28
-
29
- /**
30
- * Creates a query, subscribes and destroys it when the component unmounts.
31
- *
32
- * The `key` is used to determine whether the a new query should be created or if the existing one should be reused.
33
- */
34
- export const useTemporaryQuery = <TResult>(makeQuery: () => LiveQuery<TResult>, key: DepKey): TResult =>
35
- useTemporaryQueryRef(makeQuery, key).current
36
-
37
- export const useTemporaryQueryRef = <TResult>(
38
- makeQuery: () => LiveQuery<TResult>,
39
- key: DepKey,
40
- ): React.MutableRefObject<TResult> => {
41
- const { query$ } = useMakeTemporaryQuery(makeQuery, key)
42
-
43
- return useQueryRef(query$)
44
- }
45
-
46
- export const useMakeTemporaryQuery = <TResult, TQueryInfo extends QueryInfo>(
47
- makeQuery: (otelContext: otel.Context) => LiveQuery<TResult, TQueryInfo>,
48
- key: DepKey,
49
- options?: {
50
- otel?: {
51
- spanName?: string
52
- attributes?: otel.Attributes
53
- }
54
- },
55
- ): { query$: LiveQuery<TResult, TQueryInfo>; otelContext: otel.Context } => {
56
- const { store } = useStore()
57
- const fullKey = React.useMemo(
58
- // NOTE We're using the `makeQuery` function body string to make sure the key is unique across the app
59
- // TODO we should figure out whether this could cause some problems and/or if there's a better way to do this
60
- () => (Array.isArray(key) ? key.join('-') : key) + '-' + store.reactivityGraph.id + '-' + makeQuery.toString(),
61
- [key, makeQuery, store.reactivityGraph.id],
62
- )
63
- const fullKeyRef = React.useRef<string>()
64
-
65
- const { query$, otelContext } = React.useMemo(() => {
66
- if (fullKeyRef.current !== undefined && fullKeyRef.current !== fullKey) {
67
- // console.debug('fullKey changed', 'prev', fullKeyRef.current.split('-')[0]!, '-> new', fullKey.split('-')[0]!)
68
-
69
- const cachedItem = cache.get(fullKeyRef.current)
70
- if (cachedItem !== undefined && cachedItem._tag === 'active') {
71
- cachedItem.rc--
72
-
73
- if (cachedItem.rc === 0) {
74
- // console.debug('rc=0-changed', cachedItem.query$.id, cachedItem.query$.label)
75
- cachedItem.query$.destroy()
76
- cachedItem.span.end()
77
- cache.set(fullKeyRef.current, { _tag: 'destroyed' })
78
- }
79
- }
80
- }
81
-
82
- const cachedItem = cache.get(fullKey)
83
- if (cachedItem !== undefined && cachedItem._tag === 'active') {
84
- // console.debug('rc++', cachedItem.query$.id, cachedItem.query$.label)
85
- cachedItem.rc++
86
-
87
- return cachedItem
88
- }
89
-
90
- const spanName = options?.otel?.spanName ?? `LiveStore:useTemporaryQuery:${key}`
91
-
92
- const span = store.otel.tracer.startSpan(
93
- spanName,
94
- { attributes: options?.otel?.attributes },
95
- store.otel.queriesSpanContext,
96
- )
97
-
98
- const otelContext = otel.trace.setSpan(otel.context.active(), span)
99
-
100
- const query$ = makeQuery(otelContext)
101
-
102
- cache.set(fullKey, { _tag: 'active', rc: 1, query$, span, otelContext })
103
-
104
- return { query$, otelContext }
105
- // eslint-disable-next-line react-hooks/exhaustive-deps
106
- }, [fullKey])
107
-
108
- fullKeyRef.current = fullKey
109
-
110
- React.useEffect(() => {
111
- return () => {
112
- const fullKey = fullKeyRef.current!
113
- const cachedItem = cache.get(fullKey)
114
- // NOTE in case the fullKey changed then the query was already destroyed in the useMemo above
115
- if (cachedItem === undefined || cachedItem._tag === 'destroyed') return
116
-
117
- // console.debug('rc--', cachedItem.query$.id, cachedItem.query$.label)
118
-
119
- cachedItem.rc--
120
-
121
- if (cachedItem.rc === 0) {
122
- // console.debug('rc=0', cachedItem.query$.id, cachedItem.query$.label)
123
- cachedItem.query$.destroy()
124
- cachedItem.span.end()
125
- cache.delete(fullKey)
126
- }
127
- }
128
- }, [])
129
-
130
- return { query$, otelContext }
131
- }
@@ -1,51 +0,0 @@
1
- import React from 'react'
2
-
3
- /**
4
- * A variant of `React.useState` which allows the `inputState` to change over time as well.
5
- * Important: This hook is synchronous / single-render-pass (i.e. doesn't use `useEffect` or `setState` directly).
6
- *
7
- * Notes:
8
- * - The output state is always reset to the input state in case the input state changes (i.e. the previous "external" `setStateAndRerender` call is forgotten)
9
- * - This hook might not work properly with React Suspense
10
- * - Also see this Tweet for more potential problems: https://twitter.com/schickling/status/1677317711104278528
11
- *
12
- */
13
- export const useStateRefWithReactiveInput = <T>(
14
- inputState: T,
15
- ): [React.MutableRefObject<T>, (newState: T | ((prev: T) => T)) => void] => {
16
- const [_, rerender] = React.useState(0)
17
-
18
- const lastKnownInputStateRef = React.useRef<T>(inputState)
19
- const stateRef = React.useRef<T>(inputState)
20
-
21
- if (lastKnownInputStateRef.current !== inputState) {
22
- lastKnownInputStateRef.current = inputState
23
-
24
- // NOTE we don't need to re-render here, because the component is already re-rendering due to the `inputState` change
25
- stateRef.current = inputState
26
- }
27
-
28
- const setStateAndRerender = React.useCallback(
29
- (newState: ((prev: T) => T) | T) => {
30
- // @ts-expect-error https://github.com/microsoft/TypeScript/issues/37663
31
- const val = typeof newState === 'function' ? newState(stateRef.current) : newState
32
- stateRef.current = val
33
- rerender((c) => c + 1)
34
- },
35
- [rerender],
36
- )
37
-
38
- return [stateRef, setStateAndRerender]
39
- }
40
-
41
- // Down-side of this implementation: Double render pass due to `setState` call (which forces a re-render)
42
- // Keeping around for now in case `useStateRefWithReactiveInput` doesn't work out
43
- // const _useStateWithReactiveInput = <T>(inputState: T): [T, (newState: T | ((prev: T) => T)) => void] => {
44
- // const [externalState, setExternalState] = React.useState(inputState)
45
-
46
- // if (externalState !== inputState) {
47
- // setExternalState(inputState)
48
- // }
49
-
50
- // return [externalState, setExternalState]
51
- // }
@@ -1,23 +0,0 @@
1
- import type { IntentionalShutdownCause, UnexpectedError } from '@livestore/common'
2
- import { Schema } from '@livestore/utils/effect'
3
-
4
- import type { Store } from './store.js'
5
-
6
- export type LiveStoreContext =
7
- | LiveStoreContextRunning
8
- | {
9
- stage: 'error'
10
- error: UnexpectedError | unknown
11
- }
12
- | {
13
- stage: 'shutdown'
14
- cause: IntentionalShutdownCause | StoreAbort
15
- }
16
-
17
- export class StoreAbort extends Schema.TaggedError<StoreAbort>()('LiveStore.StoreAbort', {}) {}
18
- export class StoreInterrupted extends Schema.TaggedError<StoreInterrupted>()('LiveStore.StoreInterrupted', {}) {}
19
-
20
- export type LiveStoreContextRunning = {
21
- stage: 'running'
22
- store: Store
23
- }