Skip to content

Commit

Permalink
fix: remove persist
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <tukon479@gmail.com>
  • Loading branch information
Innei committed Mar 16, 2023
1 parent 6e175c0 commit fd56422
Showing 1 changed file with 84 additions and 83 deletions.
167 changes: 84 additions & 83 deletions src/atoms/collections/utils/base.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { enableMapSet, immerable } from 'immer'
import { create } from 'zustand'
import { persist, subscribeWithSelector } from 'zustand/middleware'
import { subscribeWithSelector } from 'zustand/middleware'
import { immer } from 'zustand/middleware/immer'

type Id = string
Expand Down Expand Up @@ -39,98 +39,99 @@ export const createCollection = <T extends { id: Id }, A extends object>(
data[immerable] = true

return create(
persist(
immer(
// @ts-ignore
subscribeWithSelector<BaseStore<T> & A>((set: Setter<T, A>, get) => ({
data,

...(typeof actions === 'function' ? actions(set, get) : actions),

softDelete(key) {
const data = get().data.get(key)
if (!data) {
return false
}

set((state) => {
const data = state.data.get(key)
if (data) data.isDeleted = true
})

return true
},
add(...args: any[]) {
const addFn = get().add

const add = (id: string, data: T | T[]) => {
if (Array.isArray(data)) {
data.forEach((d) => {
addFn(d)
})

return
}

set((state) => {
state.data.set(id, { ...data })
})
}
// persist(
immer(
// @ts-ignore
subscribeWithSelector<BaseStore<T> & A>((set: Setter<T, A>, get) => ({
data,

...(typeof actions === 'function' ? actions(set, get) : actions),

softDelete(key) {
const data = get().data.get(key)
if (!data) {
return false
}

set((state) => {
const data = state.data.get(key)
if (data) data.isDeleted = true
})

return true
},
add(...args: any[]) {
const addFn = get().add

if (typeof args[0] === 'string') {
const id = args[0]
const data = args[1]
add(id, data)
} else {
const data = args[0]
add(data.id, data)
}
},
addAndPatch(data: T | T[]) {
const add = (id: string, data: T | T[]) => {
if (Array.isArray(data)) {
const patch = get().addAndPatch
data.forEach((d) => {
patch(d)
addFn(d)
})

return
}

set((state) => {
const collection = state.data
if (collection.has(data.id)) {
const exist = collection.get(data.id)

collection.set(data.id, { ...exist, ...data })
} else {
collection.set(data.id, data)
}
state.data.set(id, { ...data })
})
},
remove(id: Id) {
set((state) => {
state.data.delete(id)
}

if (typeof args[0] === 'string') {
const id = args[0]
const data = args[1]
add(id, data)
} else {
const data = args[0]
add(data.id, data)
}
},
addAndPatch(data: T | T[]) {
if (Array.isArray(data)) {
const patch = get().addAndPatch
data.forEach((d) => {
patch(d)
})
},
})),
),
{
name,
// serialize: (data) => {
// return JSON.stringify({
// ...data,
// state: {
// ...data.state,
// data: Array.from(data.state.data as Set<unknown>),
// },
// })
// },
deserialize: (value) => {
const data = JSON.parse(value)

data.state.data = new Map(Object.entries(data.state.data))

return data
return
}
set((state) => {
const collection = state.data
if (collection.has(data.id)) {
const exist = collection.get(data.id)

collection.set(data.id, { ...exist, ...data })
} else {
collection.set(data.id, data)
}
})
},
remove(id: Id) {
set((state) => {
state.data.delete(id)
})
},
},
})),
),
// {
// name,
// storage: {}
// serialize: (data) => {
// return JSON.stringify({
// ...data,
// state: {
// ...data.state,
// data: Array.from(data.state.data as Set<unknown>),
// },
// })
// },
// deserialize: (value) => {
// const data = JSON.parse(value)

// data.state.data = new Map(Object.entries(data.state.data))

// return data
// },
// },
// ),
)
}

0 comments on commit fd56422

Please sign in to comment.