@lvce-editor/about-view 7.9.0 → 7.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,9 +3,18 @@ const toCommandId = key => {
3
3
  return key.slice(dotIndex + 1);
4
4
  };
5
5
  const create$a = () => {
6
+ const commandQueues = new Map();
7
+ const generations = Object.create(null);
6
8
  const states = Object.create(null);
7
9
  const commandMapRef = {};
8
- const updateState = (uid, updater) => {
10
+ const getGeneration = uid => generations[uid] || 0;
11
+ const isCurrentGeneration = (uid, generation) => {
12
+ return states[uid] !== undefined && getGeneration(uid) === generation;
13
+ };
14
+ const updateState = (uid, generation, fallbackState, updater) => {
15
+ if (!isCurrentGeneration(uid, generation)) {
16
+ return Promise.resolve(fallbackState);
17
+ }
9
18
  const current = states[uid];
10
19
  const updatedState = updater(current.newState);
11
20
  if (updatedState !== current.newState) {
@@ -17,8 +26,44 @@ const create$a = () => {
17
26
  }
18
27
  return Promise.resolve(updatedState);
19
28
  };
29
+ const createAsyncCommandContext = (uid, generation) => {
30
+ let latestState = states[uid].newState;
31
+ return {
32
+ getState: () => {
33
+ if (isCurrentGeneration(uid, generation)) {
34
+ latestState = states[uid].newState;
35
+ }
36
+ return latestState;
37
+ },
38
+ updateState: async updater => {
39
+ latestState = await updateState(uid, generation, latestState, updater);
40
+ return latestState;
41
+ }
42
+ };
43
+ };
44
+ const enqueueCommand = async (uid, command) => {
45
+ const previous = commandQueues.get(uid) || Promise.resolve();
46
+ const run = async () => {
47
+ try {
48
+ await previous;
49
+ } catch {
50
+ // The previous caller receives its error; later commands must still run.
51
+ }
52
+ await command();
53
+ };
54
+ const current = run();
55
+ commandQueues.set(uid, current);
56
+ try {
57
+ await current;
58
+ } finally {
59
+ if (commandQueues.get(uid) === current) {
60
+ commandQueues.delete(uid);
61
+ }
62
+ }
63
+ };
20
64
  return {
21
65
  clear() {
66
+ commandQueues.clear();
22
67
  for (const key of Object.keys(states)) {
23
68
  delete states[key];
24
69
  }
@@ -38,6 +83,7 @@ const create$a = () => {
38
83
  return diffResult;
39
84
  },
40
85
  dispose(uid) {
86
+ commandQueues.delete(uid);
41
87
  delete states[uid];
42
88
  },
43
89
  get(uid) {
@@ -55,6 +101,10 @@ const create$a = () => {
55
101
  Object.assign(commandMapRef, commandMap);
56
102
  },
57
103
  set(uid, oldState, newState, scheduledState) {
104
+ const current = states[uid];
105
+ if (!current || oldState === newState && newState !== current.newState) {
106
+ generations[uid] = getGeneration(uid) + 1;
107
+ }
58
108
  states[uid] = {
59
109
  newState,
60
110
  oldState,
@@ -63,16 +113,15 @@ const create$a = () => {
63
113
  },
64
114
  wrapAsyncCommand(fn) {
65
115
  const wrapped = async (uid, ...args) => {
66
- const context = {
67
- getState: () => states[uid].newState,
68
- updateState: updater => updateState(uid, updater)
69
- };
116
+ const generation = getGeneration(uid);
117
+ const context = createAsyncCommandContext(uid, generation);
70
118
  await fn(context, ...args);
71
119
  };
72
120
  return wrapped;
73
121
  },
74
122
  wrapCommand(fn) {
75
123
  const wrapped = async (uid, ...args) => {
124
+ const generation = getGeneration(uid);
76
125
  const {
77
126
  newState,
78
127
  oldState
@@ -81,6 +130,9 @@ const create$a = () => {
81
130
  if (oldState === newerState || newState === newerState) {
82
131
  return;
83
132
  }
133
+ if (!isCurrentGeneration(uid, generation)) {
134
+ return;
135
+ }
84
136
  const latestOld = states[uid];
85
137
  const latestNew = {
86
138
  ...latestOld.newState,
@@ -105,6 +157,7 @@ const create$a = () => {
105
157
  },
106
158
  wrapLoadContent(fn) {
107
159
  const wrapped = async (uid, ...args) => {
160
+ const generation = getGeneration(uid);
108
161
  const {
109
162
  newState,
110
163
  oldState
@@ -119,6 +172,11 @@ const create$a = () => {
119
172
  error
120
173
  };
121
174
  }
175
+ if (!isCurrentGeneration(uid, generation)) {
176
+ return {
177
+ error
178
+ };
179
+ }
122
180
  const latestOld = states[uid];
123
181
  const latestNew = {
124
182
  ...latestOld.newState,
@@ -134,6 +192,51 @@ const create$a = () => {
134
192
  };
135
193
  };
136
194
  return wrapped;
195
+ },
196
+ wrapSerialAsyncCommand(fn) {
197
+ const wrapped = async (uid, ...args) => {
198
+ await enqueueCommand(uid, async () => {
199
+ if (!states[uid]) {
200
+ return;
201
+ }
202
+ const generation = getGeneration(uid);
203
+ const context = createAsyncCommandContext(uid, generation);
204
+ await fn(context, ...args);
205
+ });
206
+ };
207
+ return wrapped;
208
+ },
209
+ wrapSerialCommand(fn) {
210
+ const wrapped = async (uid, ...args) => {
211
+ await enqueueCommand(uid, async () => {
212
+ if (!states[uid]) {
213
+ return;
214
+ }
215
+ const generation = getGeneration(uid);
216
+ const {
217
+ newState,
218
+ oldState
219
+ } = states[uid];
220
+ const newerState = await fn(newState, ...args);
221
+ if (oldState === newerState || newState === newerState) {
222
+ return;
223
+ }
224
+ if (!isCurrentGeneration(uid, generation)) {
225
+ return;
226
+ }
227
+ const latestOld = states[uid];
228
+ const latestNew = {
229
+ ...latestOld.newState,
230
+ ...newerState
231
+ };
232
+ states[uid] = {
233
+ newState: latestNew,
234
+ oldState: latestOld.oldState,
235
+ scheduledState: latestNew
236
+ };
237
+ });
238
+ };
239
+ return wrapped;
137
240
  }
138
241
  };
139
242
  };
@@ -2262,7 +2365,7 @@ const renderFocus = (oldState, newState) => {
2262
2365
  };
2263
2366
 
2264
2367
  const renderFocusContext = (oldState, newState) => {
2265
- return ['Viewlet.setFocusContext', FocusAbout];
2368
+ return ['Viewlet.setFocusContext', newState.uid, FocusAbout];
2266
2369
  };
2267
2370
 
2268
2371
  const getRenderer = diffType => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/about-view",
3
- "version": "7.9.0",
3
+ "version": "7.10.0",
4
4
  "description": "About View Worker",
5
5
  "keywords": [
6
6
  "about-view"