mobx-websocket-store
Advanced tools
Comparing version 0.1.2 to 0.2.0
@@ -12,3 +12,3 @@ "use strict"; | ||
this.closeWebsocket = closeWebsocket; | ||
this.atom = new mobx_1.Atom('MobXWebsocketAtom', this.startListening.bind(this), this.stopListening.bind(this)); | ||
this.atom = mobx_1.createAtom("MobXWebsocketAtom", this.startListening.bind(this), this.stopListening.bind(this)); | ||
if (opts) { | ||
@@ -15,0 +15,0 @@ this.opts = Object.assign({}, this.opts, opts); |
@@ -0,0 +0,0 @@ |
@@ -1,4 +0,4 @@ | ||
import { Atom } from 'mobx'; | ||
import { IAtom } from "mobx"; | ||
export default class MobxWebsocketStore<T> { | ||
atom: Atom; | ||
atom: IAtom; | ||
opts: StoreOpts; | ||
@@ -5,0 +5,0 @@ private __data; |
{ | ||
"name": "mobx-websocket-store", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": | ||
@@ -34,4 +34,4 @@ "MobX store generator to create stores that use atoms to open and close websocket connections based on the observed state of the store", | ||
"dependencies": { | ||
"mobx": "^3.3.0" | ||
"mobx": "^4.1.1" | ||
} | ||
} |
@@ -5,10 +5,18 @@ # mobx-websocket-store | ||
## A note on Mobx 4.x | ||
mobx-websocket-store 0.2.0 and above is compatible with MobX 4.x, however I strongly advise using Mobx 4's `onBecomeObserved` / `onBecomeUnobserved` hooks instead. See [here](https://medium.com/@sampsonjoliver/mobx-4-firebase-websockets-and-you-87fa9682e994) for more info. | ||
If you are using Mobx 3.x, you can use mobx-websocket-store 0.1.2 and below to achieve similar functionality. | ||
## Installation | ||
Install with your package manager of choice | ||
``` | ||
yarn add mobx-websocket-store | ||
yarn add mobx-websocket-store | ||
// or | ||
npm install mobx-websocket-store | ||
npm install mobx-websocket-store | ||
``` | ||
## Motivation | ||
@@ -20,7 +28,35 @@ | ||
## Usage | ||
Create a store instance for your websocket using the constructor, and passing in an onOpenWebsocket and onCloseWebsocket callback: | ||
### Constructor | ||
``` | ||
constructor( | ||
openWebsocket: (store: MobxWebsocketStore<T>) => void, | ||
closeWebsocket: (store: MobxWebsocketStore<T>) => void, | ||
opts?: StoreOpts | ||
); | ||
``` | ||
### Options | ||
``` | ||
{ | ||
id?: string; // An identifier that can be accessed by 'instance.id' | ||
resetDataOnOpen?: boolean; // Whether the store's cached data is reset when the socket is closed and reopened | ||
} | ||
``` | ||
### Instance Properties | ||
``` | ||
const store = new MobxWebsocketStore( ... ) | ||
store.id // Gets the id passed in via opts | ||
const data = store.data // Gets the store data and notifies the store it is being observed | ||
store.data = ... // Sets the store data and notifies observers of update | ||
``` | ||
### Example | ||
Create a store instance for your websocket using the constructor, and passing in an onOpenWebsocket and onCloseWebsocket callback and options: | ||
``` | ||
import MobxWebsocketStore from 'mobx-websocket-store'; | ||
@@ -38,2 +74,6 @@ import { autorun } from 'mobx'; | ||
socket.close(); | ||
}, | ||
{ | ||
id: 'MyStore', | ||
resetDataOnOpen: false | ||
} | ||
@@ -47,3 +87,3 @@ ); | ||
## Example Usage With React and Firebase | ||
### Example With React and Firebase | ||
Here's how you could set up a simple chat room in about 5 minutes, using the very excellent `mobx-react` bindings and the `firebase` package. | ||
@@ -61,3 +101,3 @@ | ||
const refListener = (snapshot: firebase.database.DataSnapshot) => { | ||
this.data = snapshot.val(); | ||
this.data = snapshot.val(); // 'this' will be bound later to the MobxWebsocketStore context | ||
this.atom.reportChanged(); | ||
@@ -64,0 +104,0 @@ }; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9251
8
128
+ Addedmobx@4.15.7(transitive)
- Removedmobx@3.6.2(transitive)
Updatedmobx@^4.1.1