@@ -69,21 +69,26 @@ export function toContentScript(message, serializeState, serializeAction) {
6969 post ( message ) ;
7070}
7171
72- export function sendMessage ( action , state , shouldStringify , id , name ) {
72+ export function sendMessage ( action , state , config ) {
73+ if ( typeof config !== 'object' ) config = { } ; // eslint-disable-line no-param-reassign
7374 const message = {
7475 payload : state ,
7576 source,
76- name : name || '' ,
77- instanceId : id
77+ name : config . name ,
78+ instanceId : config . instanceId || 1
7879 } ;
7980 if ( action ) {
8081 message . type = 'ACTION' ;
81- message . action = action . action ? action :
82- { action : typeof action === 'object' ? action : { type : action } } ;
82+ if ( config . getActionType ) message . action = config . getActionType ( action ) ;
83+ else {
84+ if ( typeof action === 'string' ) message . action = { type : action } ;
85+ else if ( ! action . type ) message . action = { type : 'update' } ;
86+ else if ( action . action ) message . action = action ;
87+ else message . action = { action } ;
88+ }
8389 } else {
8490 message . type = 'STATE' ;
8591 }
86-
8792 toContentScript ( message ) ;
8893}
8994
@@ -122,9 +127,11 @@ export function disconnect() {
122127 post ( { type : 'DISCONNECT' , source } ) ;
123128}
124129
125- export function connect ( config = { } ) {
130+ export function connect ( preConfig ) {
131+ const config = preConfig || { } ;
126132 const id = generateId ( config . instanceId ) ;
127- const name = config . name || document . title || id ;
133+ if ( ! config . instanceId ) config . instanceId = id ;
134+ if ( ! config . name ) config . name = document . title && id === 1 ? document . title : `Instance ${ id } ` ;
128135
129136 const subscribe = ( listener ) => {
130137 if ( ! listener ) return undefined ;
@@ -143,15 +150,18 @@ export function connect(config = {}) {
143150 } ;
144151
145152 const send = ( action , state ) => {
146- sendMessage ( action , state , true , id , name ) ;
153+ sendMessage ( action , state , config ) ;
147154 } ;
148155
149156 const init = ( state , action ) => {
150157 post (
151158 {
152- type : 'INIT' , payload : stringify ( state ) ,
159+ type : 'INIT' ,
160+ payload : stringify ( state ) ,
153161 action : stringify ( action || { } ) ,
154- instanceId : id , name, source
162+ instanceId : id ,
163+ name : config . name ,
164+ source
155165 }
156166 ) ;
157167 } ;
0 commit comments