在哪里存储 Class 实例以在 Redux 中重用?
Where to store Class instance for reusability in Redux?
我正在尝试在我的 React/Redux/redux saga 应用程序中通过 Pusher 实现一个消息传递库 Chatkit,我是 Redux 的新手。连接到 chatkit 的代码如下所示:
const chatManager = new ChatManager({
instanceLocator: 'v1:us1:80215247-1df3-4956-8ba8-9744ffd12161',
userId: 'sarah',
tokenProvider: new TokenProvider({ url: 'your.auth.url' })
})
chatManager.connect()
.then(currentUser => {
console.log('Successful connection', currentUser)
})
.catch(err => {
console.log('Error on connection', err)
})
我需要全局存储 chatManager 和 currentUser 对象(它们是具有附加功能的复杂 类 的实例),以便我以后可以再次使用它们来加入房间、订阅事件等。
我的第一个想法是我应该将它存储在 Redux 中,因为它现在是我的 "global store" 但后来我意识到它可能无法工作,因为它不是我从中取出的原始对象商店,它可能是一个克隆。然后我读到显然只有普通对象应该存储在 Redux 中。
那么,不是普通对象但需要全局存储的东西在哪里?我不想将它们放在 window 对象上,因为我可能会将其转换为 React Native 应用程序,而且它看起来很乱。
根据 Redux FAQ entry on storing "connection"-type objects:
Middleware are the right place for persistent connections like websockets in a Redux app, for several reasons:
- Middleware exist for the lifetime of the application
- Like with the store itself, you probably only need a single instance of a given connection that the whole app can use
- Middleware can see all dispatched actions and dispatch actions themselves. This means a middleware can take dispatched actions and turn those into messages sent over the websocket, and dispatch new actions when a message is received over the websocket.
- A websocket connection instance isn't serializable, so it doesn't belong in the store state itself
编辑
这是我的 "sample socket middleware" 示例,已更新为延迟创建套接字直到它看到登录操作:
const createMySocketMiddleware = (url) => {
let socket;
return storeAPI => next => action => {
switch(action.type) {
case "LOGIN" : {
socket = createMyWebsocket(url);
socket.on("message", (message) => {
storeAPI.dispatch({
type : "SOCKET_MESSAGE_RECEIVED",
payload : message
});
});
break;
}
case "SEND_WEBSOCKET_MESSAGE": {
socket.send(action.payload);
return;
}
}
return next(action);
}
}
我正在尝试在我的 React/Redux/redux saga 应用程序中通过 Pusher 实现一个消息传递库 Chatkit,我是 Redux 的新手。连接到 chatkit 的代码如下所示:
const chatManager = new ChatManager({
instanceLocator: 'v1:us1:80215247-1df3-4956-8ba8-9744ffd12161',
userId: 'sarah',
tokenProvider: new TokenProvider({ url: 'your.auth.url' })
})
chatManager.connect()
.then(currentUser => {
console.log('Successful connection', currentUser)
})
.catch(err => {
console.log('Error on connection', err)
})
我需要全局存储 chatManager 和 currentUser 对象(它们是具有附加功能的复杂 类 的实例),以便我以后可以再次使用它们来加入房间、订阅事件等。
我的第一个想法是我应该将它存储在 Redux 中,因为它现在是我的 "global store" 但后来我意识到它可能无法工作,因为它不是我从中取出的原始对象商店,它可能是一个克隆。然后我读到显然只有普通对象应该存储在 Redux 中。
那么,不是普通对象但需要全局存储的东西在哪里?我不想将它们放在 window 对象上,因为我可能会将其转换为 React Native 应用程序,而且它看起来很乱。
根据 Redux FAQ entry on storing "connection"-type objects:
Middleware are the right place for persistent connections like websockets in a Redux app, for several reasons:
- Middleware exist for the lifetime of the application
- Like with the store itself, you probably only need a single instance of a given connection that the whole app can use
- Middleware can see all dispatched actions and dispatch actions themselves. This means a middleware can take dispatched actions and turn those into messages sent over the websocket, and dispatch new actions when a message is received over the websocket.
- A websocket connection instance isn't serializable, so it doesn't belong in the store state itself
编辑
这是我的 "sample socket middleware" 示例,已更新为延迟创建套接字直到它看到登录操作:
const createMySocketMiddleware = (url) => {
let socket;
return storeAPI => next => action => {
switch(action.type) {
case "LOGIN" : {
socket = createMyWebsocket(url);
socket.on("message", (message) => {
storeAPI.dispatch({
type : "SOCKET_MESSAGE_RECEIVED",
payload : message
});
});
break;
}
case "SEND_WEBSOCKET_MESSAGE": {
socket.send(action.payload);
return;
}
}
return next(action);
}
}