77 */
88
99import PluginImmer from './plugin-immer' ;
10+ import PluginRandom from './plugin-random' ;
1011import { GameState , State , GameConfig , Plugin , Ctx } from '../types' ;
1112
1213interface PluginOpts {
@@ -17,7 +18,7 @@ interface PluginOpts {
1718/**
1819 * List of plugins that are always added.
1920 */
20- const DEFAULT_PLUGINS = [ PluginImmer ] ;
21+ const DEFAULT_PLUGINS = [ PluginImmer , PluginRandom ] ;
2122
2223/**
2324 * The API's created by various plugins are stored in the plugins
@@ -97,20 +98,22 @@ export const Enhance = (state: State, opts: PluginOpts): State => {
9798 const name = plugin . name ;
9899 const pluginState = state . plugins [ name ] ;
99100
100- const api = plugin . api ( {
101- G : state . G ,
102- ctx : state . ctx ,
103- data : pluginState . data ,
104- game : opts . game ,
105- } ) ;
101+ if ( pluginState ) {
102+ const api = plugin . api ( {
103+ G : state . G ,
104+ ctx : state . ctx ,
105+ data : pluginState . data ,
106+ game : opts . game ,
107+ } ) ;
106108
107- state = {
108- ...state ,
109- plugins : {
110- ...state . plugins ,
111- [ name ] : { ...pluginState , api } ,
112- } ,
113- } ;
109+ state = {
110+ ...state ,
111+ plugins : {
112+ ...state . plugins ,
113+ [ name ] : { ...pluginState , api } ,
114+ } ,
115+ } ;
116+ }
114117 } ) ;
115118 return state ;
116119} ;
@@ -123,22 +126,50 @@ export const Flush = (state: State, opts: PluginOpts): State => {
123126 . filter ( plugin => plugin . flush !== undefined )
124127 . forEach ( plugin => {
125128 const name = plugin . name ;
126- const { api, data } = state . plugins [ name ] ;
127- const newData = plugin . flush ( {
128- G : state . G ,
129- ctx : state . ctx ,
130- game : opts . game ,
131- api,
132- data,
133- } ) ;
129+ const pluginState = state . plugins [ name ] ;
134130
135- state = {
136- ...state ,
137- plugins : {
138- ...state . plugins ,
139- [ plugin . name ] : { data : newData } ,
140- } ,
141- } ;
131+ if ( pluginState ) {
132+ const newData = plugin . flush ( {
133+ G : state . G ,
134+ ctx : state . ctx ,
135+ game : opts . game ,
136+ api : pluginState . api ,
137+ data : pluginState . data ,
138+ } ) ;
139+
140+ state = {
141+ ...state ,
142+ plugins : {
143+ ...state . plugins ,
144+ [ plugin . name ] : { data : newData } ,
145+ } ,
146+ } ;
147+ }
142148 } ) ;
143149 return state ;
144150} ;
151+
152+ /**
153+ * Allows plugins to indicate if they should not be materialized on the client.
154+ * This will cause the client to discard the state update and wait for the
155+ * master instead.
156+ */
157+ export const NoClient = ( state : State , opts : PluginOpts ) : boolean => {
158+ return [ ...DEFAULT_PLUGINS , ...opts . game . plugins ]
159+ . filter ( plugin => plugin . noClient !== undefined )
160+ . map ( plugin => {
161+ const name = plugin . name ;
162+ const pluginState = state . plugins [ name ] ;
163+
164+ if ( pluginState ) {
165+ return plugin . noClient ( {
166+ G : state . G ,
167+ ctx : state . ctx ,
168+ game : opts . game ,
169+ api : pluginState . api ,
170+ data : pluginState . data ,
171+ } ) ;
172+ }
173+ } )
174+ . some ( value => value === true ) ;
175+ } ;
0 commit comments