Skip to content

Commit f0e2ec8

Browse files
committed
Support for custom action types via API
Related to elmish/elmish#18 (comment) 1423290
1 parent 7fa587e commit f0e2ec8

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

src/app/api/index.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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
};

test/app/inject/api.spec.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ describe('API', () => {
3333
});
3434
expect(message).toInclude({
3535
type: 'ACTION',
36-
action: '{"action":{"type":"hi"}}',
36+
action: '{"type":"hi"}',
3737
payload: undefined,
38-
id: undefined,
39-
name: '',
38+
instanceId: 1,
39+
name: undefined,
4040
source: '@devtools-page'
4141
});
4242

@@ -47,8 +47,8 @@ describe('API', () => {
4747
type: 'ACTION',
4848
action: '{"action":{"type":"hi"}}',
4949
payload: '{"counter":1}',
50-
instanceId: undefined,
51-
name: '',
50+
instanceId: 1,
51+
name: undefined,
5252
source: '@devtools-page'
5353
});
5454

@@ -58,8 +58,8 @@ describe('API', () => {
5858
expect(message).toInclude({
5959
type: 'ACTION',
6060
payload: '{"counter":1}',
61-
instanceId: undefined,
62-
name: '',
61+
instanceId: 1,
62+
name: undefined,
6363
source: '@devtools-page'
6464
});
6565
expect(message.action).toBe('{"action":{"type":"hi"}}');
@@ -73,8 +73,8 @@ describe('API', () => {
7373
actionsById: undefined,
7474
computedStates: undefined,
7575
committedState: false,
76-
instanceId: undefined,
77-
name: '',
76+
instanceId: 1,
77+
name: undefined,
7878
source: '@devtools-page'
7979
});
8080
});

0 commit comments

Comments
 (0)