@@ -3,12 +3,16 @@ import {API} from '@/js/msg-api';
33import { closeCurrentTab } from '@/js/util-webext' ;
44import editor from './editor' ;
55
6+ let replacing , replaceQueue ;
7+
68onMessage . set ( request => {
9+ if ( ! request . broadcast ) // ignore duplicate message from broadcast() to this tab
10+ return ;
711 const { style} = request ;
812 switch ( request . method ) {
913 case 'styleUpdated' :
1014 if ( editor . style . id === style . id ) {
11- handleExternalUpdate ( request ) ;
15+ handleExternalUpdate ( style , request . reason , request . editorId ) ;
1216 }
1317 break ;
1418 case 'styleDeleted' :
@@ -19,7 +23,7 @@ onMessage.set(request => {
1923 }
2024} ) ;
2125
22- async function handleExternalUpdate ( { style, reason, editorId} ) {
26+ function handleExternalUpdate ( style , reason , editorId ) {
2327 if ( reason === 'editPreview' ||
2428 reason === 'editPreviewEnd' ) {
2529 return ;
@@ -38,6 +42,14 @@ async function handleExternalUpdate({style, reason, editorId}) {
3842 editor . updateMeta ( ) ;
3943 return ;
4044 }
45+ ( replaceQueue ??= [ ] ) . push ( [ style , reason ] ) ;
46+ replacing = replacing
47+ ? replacing . then ( onReplaced , onReplaced )
48+ : onReplaced ( ) ;
49+ }
50+
51+ async function onReplaced ( ) {
52+ let [ style , reason ] = replaceQueue . shift ( ) ;
4153 style = await API . styles . getCore ( { id : style . id , src : true , vars : true } ) ;
4254 if ( reason === 'config' ) {
4355 for ( const key in editor . style )
0 commit comments