Skip to content

Commit 808cb80

Browse files
authored
Merge pull request #19 from nanostores/feat/update-to-0.10
feat: bump `nanostores` to 0.10
2 parents 3dc7bd2 + 0149e60 commit 808cb80

File tree

15 files changed

+1354
-1426
lines changed

15 files changed

+1354
-1426
lines changed

build-creator-logger/errors.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let $atom = atom()
88
buildCreatorLogger($atom, 'Atom', {})
99

1010
buildCreatorLogger($atom, 'Atom', {
11-
// THROWS Argument of type '{ mount: () => void; }' is not assignable to
11+
// THROWS 'mount' does not exist in typ
1212
mount: () => {}
1313
})
1414

@@ -20,7 +20,7 @@ buildCreatorLogger(
2020
},
2121
{
2222
messages: {
23-
// THROWS Type '{ mount: false; }' is not assignable to
23+
// THROWS 'mount' does not exist in type
2424
mount: false
2525
}
2626
}

build-logger/index.d.ts

-38
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import type { AnyStore, Store, StoreValue } from 'nanostores'
22

33
interface LoggerOptionsMessages {
4-
/**
5-
* Disable action logs.
6-
*/
7-
action?: boolean
8-
94
/**
105
* Disable change logs.
116
*/
@@ -23,11 +18,6 @@ interface LoggerOptionsMessages {
2318
}
2419

2520
export interface LoggerOptions {
26-
/**
27-
* Disable logs of actions with a specific name.
28-
*/
29-
ignoreActions?: string[]
30-
3121
/**
3222
* Disable specific types of logs.
3323
*/
@@ -39,19 +29,12 @@ interface EventPayloadBase {
3929
}
4030

4131
interface EventChangePayload extends EventPayloadBase {
42-
actionId?: number
43-
actionName?: string
4432
changed?: keyof StoreValue<Store>
4533
newValue: any
4634
oldValue?: any
4735
valueMessage?: string
4836
}
4937

50-
interface EventActionPayload extends EventPayloadBase {
51-
actionId: number
52-
actionName: string
53-
}
54-
5538
interface EventActionStartPayload extends EventActionPayload {
5639
args: any[]
5740
}
@@ -61,11 +44,6 @@ interface EventActionErrorPayload extends EventActionPayload {
6144
}
6245

6346
interface BuildLoggerEvents {
64-
action?: {
65-
end?: (payload: EventActionPayload) => void
66-
error?: (payload: EventActionErrorPayload) => void
67-
start?: (payload: EventActionStartPayload) => void
68-
}
6947
change?: (payload: EventChangePayload) => void
7048
mount?: (payload: EventPayloadBase) => void
7149
unmount?: (payload: EventPayloadBase) => void
@@ -94,22 +72,6 @@ interface BuildLoggerEvents {
9472
* message += `to ${newValue}`
9573
* if (actionName) message += `by action ${actionName}`
9674
* console.log(message, valueMessage)
97-
* },
98-
*
99-
* action: {
100-
* start: ({ actionName, args }) => {
101-
* let message = `${actionName} was started`
102-
* if (args.length) message += 'with arguments'
103-
* console.log(message, args)
104-
* },
105-
*
106-
* error: ({ actionName, error }) => {
107-
* console.log(`${actionName} was failed`, error)
108-
* },
109-
*
110-
* end: ({ actionName }) => {
111-
* console.log(`${actionName} was ended`)
112-
* }
11375
* }
11476
* ```
11577
*

build-logger/index.js

+3-39
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import {
2-
actionId,
3-
lastAction,
4-
onAction,
5-
onMount,
6-
onNotify,
7-
onSet
8-
} from 'nanostores'
1+
import { onMount, onNotify, onSet } from 'nanostores'
92

103
const isAtom = store => store.setKey === undefined
114
const isDeepMapKey = key => /.+(\..+|\[\d+\.*])/.test(key)
@@ -23,30 +16,8 @@ function handleMount(store, storeName, messages, events) {
2316
})
2417
}
2518

26-
function handleAction(store, storeName, ignoreActions, events) {
27-
return onAction(store, ({ actionName, args, id, onEnd, onError }) => {
28-
if (ignoreActions && ignoreActions.includes(actionName)) return
29-
30-
events.action.start({ actionId: id, actionName, args, storeName })
31-
32-
onError(({ error }) => {
33-
events.action.error({ actionId: id, actionName, error, storeName })
34-
})
35-
36-
onEnd(() => {
37-
events.action.end({ actionId: id, actionName, storeName })
38-
})
39-
})
40-
}
41-
42-
function handleSet(store, storeName, messages, ignoreActions, events) {
19+
function handleSet(store, storeName, events) {
4320
return onSet(store, ({ changed }) => {
44-
let currentActionId = store[actionId]
45-
let currentActionName = store[lastAction]
46-
47-
if (messages.action === false && currentActionId) return
48-
if (ignoreActions && ignoreActions.includes(currentActionName)) return
49-
5021
let oldValue = isAtom(store) ? store.value : { ...store.value }
5122
oldValue = isDeepMapKey(changed) ? structuredClone(oldValue) : oldValue
5223
let unbindNotify = onNotify(store, () => {
@@ -57,8 +28,6 @@ function handleSet(store, storeName, messages, ignoreActions, events) {
5728
}
5829

5930
events.change({
60-
actionId: currentActionId,
61-
actionName: currentActionName,
6231
changed,
6332
newValue,
6433
oldValue,
@@ -72,20 +41,15 @@ function handleSet(store, storeName, messages, ignoreActions, events) {
7241
}
7342

7443
export function buildLogger(store, storeName, events, opts = {}) {
75-
let ignoreActions = opts.ignoreActions
7644
let messages = opts.messages || {}
7745
let unbind = []
7846

7947
if (messages.mount !== false || messages.unmount !== false) {
8048
unbind.push(handleMount(store, storeName, messages, events))
8149
}
8250

83-
if (messages.action !== false) {
84-
unbind.push(handleAction(store, storeName, ignoreActions, events))
85-
}
86-
8751
if (messages.change !== false) {
88-
unbind.push(handleSet(store, storeName, messages, ignoreActions, events))
52+
unbind.push(handleSet(store, storeName, events))
8953
}
9054

9155
return () => {

creator-logger/__snapshots__/index.test.ts.snap

+6-51
Original file line numberDiff line numberDiff line change
@@ -8,63 +8,18 @@ Nano Stores build: Map Creator:3 store was built by Map Creator creator
88
Nano Stores mount: Map Creator:1 store was mounted
99
Nano Stores change: Map Creator:1 store was changed in the value key
1010
value: null → 0
11-
new: {\\"id\\":\\"1\\",\\"value\\":0}
12-
old: {\\"id\\":\\"1\\",\\"value\\":null}
13-
Nano Stores action: Map Creator:1 store was changed by action Increase Value
14-
change: Map Creator:1 store was changed in the value key
15-
value: 0 → 1
16-
new: {\\"id\\":\\"1\\",\\"value\\":1}
17-
old: {\\"id\\":\\"1\\",\\"value\\":0}
18-
Nano Stores action: Map Creator:1 store was changed by action Increase Value
19-
change: Map Creator:1 store was changed in the value key
20-
value: 1 → 2
21-
new: {\\"id\\":\\"1\\",\\"value\\":2}
22-
old: {\\"id\\":\\"1\\",\\"value\\":1}
23-
Nano Stores action: Map Creator:1 store was changed by action Increase Value
24-
change: Map Creator:1 store was changed in the value key
25-
value: 2 → 3
26-
new: {\\"id\\":\\"1\\",\\"value\\":3}
27-
old: {\\"id\\":\\"1\\",\\"value\\":2}
11+
new: {"id":"1","value":0}
12+
old: {"id":"1","value":null}
2813
Nano Stores mount: Map Creator:2 store was mounted
2914
Nano Stores change: Map Creator:2 store was changed in the value key
3015
value: null → 0
31-
new: {\\"id\\":\\"2\\",\\"value\\":0}
32-
old: {\\"id\\":\\"2\\",\\"value\\":null}
33-
Nano Stores action: Map Creator:2 store was changed by action Increase Value
34-
change: Map Creator:2 store was changed in the value key
35-
value: 0 → 1
36-
new: {\\"id\\":\\"2\\",\\"value\\":1}
37-
old: {\\"id\\":\\"2\\",\\"value\\":0}
38-
Nano Stores action: Map Creator:2 store was changed by action Increase Value
39-
change: Map Creator:2 store was changed in the value key
40-
value: 1 → 2
41-
new: {\\"id\\":\\"2\\",\\"value\\":2}
42-
old: {\\"id\\":\\"2\\",\\"value\\":1}
43-
Nano Stores action: Map Creator:2 store was changed by action Increase Value
44-
change: Map Creator:2 store was changed in the value key
45-
value: 2 → 3
46-
new: {\\"id\\":\\"2\\",\\"value\\":3}
47-
old: {\\"id\\":\\"2\\",\\"value\\":2}
16+
new: {"id":"2","value":0}
17+
old: {"id":"2","value":null}
4818
Nano Stores mount: Map Creator:3 store was mounted
4919
Nano Stores change: Map Creator:3 store was changed in the value key
5020
value: null → 0
51-
new: {\\"id\\":\\"3\\",\\"value\\":0}
52-
old: {\\"id\\":\\"3\\",\\"value\\":null}
53-
Nano Stores action: Map Creator:3 store was changed by action Increase Value
54-
change: Map Creator:3 store was changed in the value key
55-
value: 0 → 1
56-
new: {\\"id\\":\\"3\\",\\"value\\":1}
57-
old: {\\"id\\":\\"3\\",\\"value\\":0}
58-
Nano Stores action: Map Creator:3 store was changed by action Increase Value
59-
change: Map Creator:3 store was changed in the value key
60-
value: 1 → 2
61-
new: {\\"id\\":\\"3\\",\\"value\\":2}
62-
old: {\\"id\\":\\"3\\",\\"value\\":1}
63-
Nano Stores action: Map Creator:3 store was changed by action Increase Value
64-
change: Map Creator:3 store was changed in the value key
65-
value: 2 → 3
66-
new: {\\"id\\":\\"3\\",\\"value\\":3}
67-
old: {\\"id\\":\\"3\\",\\"value\\":2}
21+
new: {"id":"3","value":0}
22+
old: {"id":"3","value":null}
6823
Nano Stores unmount: Map Creator:1 store was unmounted
6924
Nano Stores unmount: Map Creator:2 store was unmounted
7025
Nano Stores unmount: Map Creator:3 store was unmounted

creator-logger/index.test.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { delay } from 'nanodelay'
22
import type { MapCreator, MapStore } from 'nanostores'
3-
import { action, map, onMount, STORE_UNMOUNT_DELAY } from 'nanostores'
3+
import { map, onMount, STORE_UNMOUNT_DELAY } from 'nanostores'
44
import { afterEach, beforeAll, expect, it, vi } from 'vitest'
55

66
import { format } from '../test/index.js'
@@ -57,12 +57,6 @@ afterEach(() => {
5757
it('prints logs from store builder', async () => {
5858
let creator = createMapCreator($store => {
5959
$store.setKey('value', 0)
60-
let increaseValue = action($store, 'Increase Value', store => {
61-
store.setKey('value', Number(store.get().value) + 1)
62-
})
63-
increaseValue()
64-
increaseValue()
65-
increaseValue()
6660
})
6761
let destroy = creatorLogger({ 'Map Creator': creator })
6862

creator-logger/types.ts

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ let $creator = createCreator()
1313
let destroy = creatorLogger(
1414
{ $creator },
1515
{
16-
ignoreActions: ['Increase Value'],
1716
messages: {
1817
build: false,
1918
mount: false

demo/index.ts

+21-46
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { delay } from 'nanodelay'
2-
import { action, atom, deepMap, map, STORE_UNMOUNT_DELAY } from 'nanostores'
2+
import { atom, deepMap, map, STORE_UNMOUNT_DELAY } from 'nanostores'
33

44
import { logger } from '../index.js'
55

@@ -26,41 +26,6 @@ let $deepMap = deepMap<{
2626
}
2727
})
2828

29-
let increaseCounter = action($atom, 'Increase Counter', store => {
30-
store.set(Number(store.get()) + 1)
31-
})
32-
33-
let changeUserArtworks = action(
34-
$map,
35-
'Change User Artworks',
36-
(store, value) => {
37-
store.setKey('artworks', value)
38-
}
39-
)
40-
41-
let changeUser = action(
42-
$map,
43-
'Change User',
44-
async (store, username = 'chashnik', fullname = 'Ilya Chashnik') => {
45-
await delay(1000)
46-
store.setKey('username', username)
47-
store.setKey('fullname', fullname)
48-
}
49-
)
50-
51-
let addArtworks = action(
52-
$deepMap,
53-
'Add Artworks',
54-
async (store, artist: string, artworks: string[]) => {
55-
for (let item of artworks) {
56-
await delay(100)
57-
let index = store.get().artists.malevich.artworks.length
58-
store.setKey(`artists.${artist}.artworks[${index}]`, item)
59-
}
60-
throw Error('Something went wrong in action Add Artworks')
61-
}
62-
)
63-
6429
logger({
6530
Artist: $map,
6631
Counter: $atom,
@@ -69,22 +34,32 @@ logger({
6934

7035
async function run(): Promise<void> {
7136
let unbindAtom = $atom.listen(() => {})
72-
increaseCounter()
37+
$atom.set(100)
38+
$atom.set(101)
39+
$atom.set(Number($atom.get()) + 1)
40+
$atom.set(Number($atom.get()) + 1)
41+
$atom.set(Number($atom.get()) + 1)
7342
unbindAtom()
7443
await delay(STORE_UNMOUNT_DELAY + 10)
7544

76-
changeUserArtworks(303)
45+
$map.setKey('artworks', 303)
7746

78-
await changeUser('malevich', 'Kazimir Malevich')
47+
await delay(STORE_UNMOUNT_DELAY)
48+
$map.setKey('username', 'chashnik')
49+
$map.setKey('fullname', 'Ilya Chashnik')
50+
51+
await delay(STORE_UNMOUNT_DELAY)
52+
$map.setKey('username', 'malevich')
53+
$map.setKey('fullname', 'Kazimir Malevich')
7954

8055
$deepMap.setKey('artists.malevich.movement', 'Suprematism')
81-
try {
82-
await addArtworks('malevich', [
83-
'White on White',
84-
'Suprematist Composition',
85-
'Black Circle'
86-
])
87-
} catch {}
56+
57+
let artworks = ['White on White', 'Suprematist Composition', 'Black Circle']
58+
for (let item of artworks) {
59+
await delay(100)
60+
let index = $deepMap.get().artists.malevich.artworks.length
61+
$deepMap.setKey(`artists.malevich.artworks[${index}]`, item)
62+
}
8863
await delay(STORE_UNMOUNT_DELAY + 10)
8964
}
9065

0 commit comments

Comments
 (0)