Skip to content

Commit

Permalink
Push: update getNotificationsStore to use Apollo-like Result store
Browse files Browse the repository at this point in the history
  • Loading branch information
darrylyeo committed Apr 16, 2023
1 parent 68f3f63 commit ec05ab5
Showing 1 changed file with 33 additions and 15 deletions.
48 changes: 33 additions & 15 deletions src/api/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,35 +120,38 @@ export const getNotificationsStore = ({
}: {
network: Ethereum.Network,
address: Ethereum.Address,
}) => readable({}, set => {
}) => readable({
loading: true,
}, set => {
const state = {
isConnected: false,
notifications: [],
toggle: undefined,
loading: true,
data: {
isConnected: false,
notifications: [],
toggle: undefined,
},
error: undefined,
}

const socket = createSocketConnection({
user: `eip155:5:${address}`, // CAIP-10 format
user: `eip155:${network.chainId}:${address}`,
env: ENV.PROD,
socketOptions: { autoConnect: true }
})

socket?.on(EVENTS.CONNECT, () => {
Object.assign(state, {
isConnnected: true
})
state.data.isConnnected = true
set(state)
})

socket?.on(EVENTS.DISCONNECT, () => {
Object.assign(state, {
isConnnected: false
})
state.data.isConnnected = false
set(state)
})

socket?.on(EVENTS.USER_FEEDS, (feedItem) => {
state.notifications.push(feedItem)
state.data.notifications.push(feedItem)
state.loading = false
set(state)
})

Expand All @@ -160,13 +163,28 @@ export const getNotificationsStore = ({
}
}

Object.assign(state, {
toggle,
})
state.data.toggle = toggle
set(state)

toggle()

return () => {
socket?.off(EVENTS.CONNECT);
socket?.off(EVENTS.DISCONNECT);
}
})



// import type { Action } from 'svelte/action'
// import { readable } from 'svelte/store';

// export const pushAction: Action = () => {


// return {
// destroy() {

// }
// }
// }

1 comment on commit ec05ab5

@vercel
Copy link

@vercel vercel bot commented on ec05ab5 Apr 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

blockhead – ./

blockhead-phi.vercel.app
blockhead-darrylyeo.vercel.app
blockhead-git-main-darrylyeo.vercel.app

Please sign in to comment.