@lvce-editor/status-bar-worker 1.2.0 → 1.4.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.
@@ -54,6 +54,49 @@ class VError extends Error {
54
54
  }
55
55
  }
56
56
 
57
+ class AssertionError extends Error {
58
+ constructor(message) {
59
+ super(message);
60
+ this.name = 'AssertionError';
61
+ }
62
+ }
63
+ const Object$1 = 1;
64
+ const Number$1 = 2;
65
+ const Array$1 = 3;
66
+ const String = 4;
67
+ const Boolean = 5;
68
+ const Function = 6;
69
+ const Null = 7;
70
+ const Unknown = 8;
71
+ const getType = value => {
72
+ switch (typeof value) {
73
+ case 'number':
74
+ return Number$1;
75
+ case 'function':
76
+ return Function;
77
+ case 'string':
78
+ return String;
79
+ case 'object':
80
+ if (value === null) {
81
+ return Null;
82
+ }
83
+ if (Array.isArray(value)) {
84
+ return Array$1;
85
+ }
86
+ return Object$1;
87
+ case 'boolean':
88
+ return Boolean;
89
+ default:
90
+ return Unknown;
91
+ }
92
+ };
93
+ const number = value => {
94
+ const type = getType(value);
95
+ if (type !== Number$1) {
96
+ throw new AssertionError('expected value to be of type number');
97
+ }
98
+ };
99
+
57
100
  const isMessagePort = value => {
58
101
  return value && value instanceof MessagePort;
59
102
  };
@@ -378,26 +421,26 @@ const create$4 = (method, params) => {
378
421
  };
379
422
  };
380
423
  const callbacks = Object.create(null);
381
- const set$1 = (id, fn) => {
424
+ const set$2 = (id, fn) => {
382
425
  callbacks[id] = fn;
383
426
  };
384
- const get$2 = id => {
427
+ const get$3 = id => {
385
428
  return callbacks[id];
386
429
  };
387
430
  const remove = id => {
388
431
  delete callbacks[id];
389
432
  };
390
433
  let id = 0;
391
- const create$3 = () => {
434
+ const create$3$1 = () => {
392
435
  return ++id;
393
436
  };
394
437
  const registerPromise = () => {
395
- const id = create$3();
438
+ const id = create$3$1();
396
439
  const {
397
440
  resolve,
398
441
  promise
399
442
  } = Promise.withResolvers();
400
- set$1(id, resolve);
443
+ set$2(id, resolve);
401
444
  return {
402
445
  id,
403
446
  promise
@@ -562,7 +605,7 @@ const warn = (...args) => {
562
605
  console.warn(...args);
563
606
  };
564
607
  const resolve = (id, response) => {
565
- const fn = get$2(id);
608
+ const fn = get$3(id);
566
609
  if (!fn) {
567
610
  console.log(response);
568
611
  warn(`callback ${id} may already be disposed`);
@@ -822,7 +865,7 @@ const listen$1 = async (module, options) => {
822
865
  const ipc = module.wrap(rawIpc);
823
866
  return ipc;
824
867
  };
825
- const create$2 = async ({
868
+ const create$3 = async ({
826
869
  commandMap
827
870
  }) => {
828
871
  // TODO create a commandMap per rpc instance
@@ -834,19 +877,135 @@ const create$2 = async ({
834
877
  };
835
878
  const WebWorkerRpcClient = {
836
879
  __proto__: null,
837
- create: create$2
880
+ create: create$3
838
881
  };
839
882
 
883
+ const toCommandId = key => {
884
+ const dotIndex = key.indexOf('.');
885
+ return key.slice(dotIndex + 1);
886
+ };
887
+ const create$2 = () => {
888
+ const states = Object.create(null);
889
+ const commandMapRef = {};
890
+ return {
891
+ get(uid) {
892
+ return states[uid];
893
+ },
894
+ set(uid, oldState, newState) {
895
+ states[uid] = {
896
+ oldState,
897
+ newState
898
+ };
899
+ },
900
+ dispose(uid) {
901
+ delete states[uid];
902
+ },
903
+ getKeys() {
904
+ return Object.keys(states).map(key => {
905
+ return Number.parseInt(key);
906
+ });
907
+ },
908
+ clear() {
909
+ for (const key of Object.keys(states)) {
910
+ delete states[key];
911
+ }
912
+ },
913
+ wrapCommand(fn) {
914
+ const wrapped = async (uid, ...args) => {
915
+ const {
916
+ oldState,
917
+ newState
918
+ } = states[uid];
919
+ const newerState = await fn(newState, ...args);
920
+ if (oldState === newerState || newState === newerState) {
921
+ return;
922
+ }
923
+ const latest = states[uid];
924
+ states[uid] = {
925
+ oldState: latest.oldState,
926
+ newState: newerState
927
+ };
928
+ };
929
+ return wrapped;
930
+ },
931
+ wrapGetter(fn) {
932
+ const wrapped = (uid, ...args) => {
933
+ const {
934
+ newState
935
+ } = states[uid];
936
+ return fn(newState, ...args);
937
+ };
938
+ return wrapped;
939
+ },
940
+ diff(uid, modules, numbers) {
941
+ const {
942
+ oldState,
943
+ newState
944
+ } = states[uid];
945
+ const diffResult = [];
946
+ for (let i = 0; i < modules.length; i++) {
947
+ const fn = modules[i];
948
+ if (!fn(oldState, newState)) {
949
+ diffResult.push(numbers[i]);
950
+ }
951
+ }
952
+ return diffResult;
953
+ },
954
+ getCommandIds() {
955
+ const keys = Object.keys(commandMapRef);
956
+ const ids = keys.map(toCommandId);
957
+ return ids;
958
+ },
959
+ registerCommands(commandMap) {
960
+ Object.assign(commandMapRef, commandMap);
961
+ }
962
+ };
963
+ };
840
964
  const terminate = () => {
841
965
  globalThis.close();
842
966
  };
843
967
 
968
+ const {
969
+ get: get$2,
970
+ set: set$1,
971
+ wrapCommand} = create$2();
972
+
844
973
  const create$1 = uid => {
845
- return {
974
+ const state = {
846
975
  statusBarItemsLeft: [],
847
976
  statusBarItemsRight: [],
848
977
  uid
849
978
  };
979
+ set$1(uid, state, state);
980
+ };
981
+
982
+ const isEqual = (oldState, newState) => {
983
+ return oldState.statusBarItemsLeft === newState.statusBarItemsLeft && oldState.statusBarItemsRight === newState.statusBarItemsRight;
984
+ };
985
+
986
+ const RenderItems = 4;
987
+
988
+ const modules = [isEqual];
989
+ const numbers = [RenderItems];
990
+
991
+ const diff = (oldState, newState) => {
992
+ const diffResult = [];
993
+ for (let i = 0; i < modules.length; i++) {
994
+ const fn = modules[i];
995
+ if (!fn(oldState, newState)) {
996
+ diffResult.push(numbers[i]);
997
+ }
998
+ }
999
+ return diffResult;
1000
+ };
1001
+
1002
+ const diff2 = uid => {
1003
+ const {
1004
+ newState,
1005
+ oldState
1006
+ } = get$2(uid);
1007
+ const result = diff(oldState, newState);
1008
+ return result;
850
1009
  };
851
1010
 
852
1011
  const handleClick = (state, name) => {
@@ -903,9 +1062,24 @@ const OnStatusBarItem = 'onStatusBarItem';
903
1062
 
904
1063
  const GetStatusBarItems = 'ExtensionHost.getStatusBarItems';
905
1064
 
1065
+ const Button$1 = 'button';
1066
+
1067
+ const Div = 4;
1068
+ const Text = 12;
1069
+
1070
+ const Button = 'event.button';
1071
+ const ClientX = 'event.clientX';
1072
+ const ClientY = 'event.clientY';
1073
+ const DeltaMode = 'event.deltaMode';
1074
+ const DeltaY = 'event.deltaY';
1075
+ const TargetName = 'event.target.name';
1076
+ const TargetValue = 'event.target.value';
1077
+
906
1078
  const ExtensionHostWorker = 44;
907
1079
  const RendererWorker = 1;
908
1080
 
1081
+ const SetDom2 = 'Viewlet.setDom2';
1082
+
909
1083
  const rpcs = Object.create(null);
910
1084
  const set = (id, rpc) => {
911
1085
  rpcs[id] = rpc;
@@ -1025,13 +1199,166 @@ const loadContent = async state => {
1025
1199
  };
1026
1200
  };
1027
1201
 
1202
+ const StatusBarItem = 'StatusBarItem';
1203
+ const StatusBarItemsLeft = 'StatusBarItemsLeft';
1204
+ const StatusBarItemsRight = 'StatusBarItemsRight';
1205
+
1206
+ const text = data => {
1207
+ return {
1208
+ type: Text,
1209
+ text: data,
1210
+ childCount: 0
1211
+ };
1212
+ };
1213
+
1214
+ const getStatusBarItemVirtualDom = statusBarItem => {
1215
+ const {
1216
+ tooltip
1217
+ } = statusBarItem;
1218
+ return [{
1219
+ childCount: 1,
1220
+ className: StatusBarItem,
1221
+ role: Button$1,
1222
+ tabIndex: -1,
1223
+ title: tooltip,
1224
+ type: Div
1225
+ }, text(statusBarItem.text)];
1226
+ };
1227
+
1228
+ const getStatusBarItemsVirtualDom = (items, className) => {
1229
+ return [{
1230
+ childCount: items.length,
1231
+ className,
1232
+ type: Div
1233
+ }, ...items.flatMap(getStatusBarItemVirtualDom)];
1234
+ };
1235
+
1236
+ const getStatusBarVirtualDom = (statusBarItemsLeft, statusBarItemsRight) => {
1237
+ const dom = [];
1238
+ if (statusBarItemsLeft.length > 0) {
1239
+ dom.push(...getStatusBarItemsVirtualDom(statusBarItemsLeft, StatusBarItemsLeft));
1240
+ }
1241
+ if (statusBarItemsRight.length > 0) {
1242
+ dom.push(...getStatusBarItemsVirtualDom(statusBarItemsRight, StatusBarItemsRight));
1243
+ }
1244
+ return dom;
1245
+ };
1246
+
1247
+ const renderItems = (oldState, newState) => {
1248
+ const {
1249
+ statusBarItemsLeft,
1250
+ statusBarItemsRight,
1251
+ uid
1252
+ } = newState;
1253
+ const dom = getStatusBarVirtualDom(statusBarItemsLeft, statusBarItemsRight);
1254
+ return [SetDom2, uid, dom];
1255
+ };
1256
+
1257
+ const getRenderer = diffType => {
1258
+ switch (diffType) {
1259
+ case RenderItems:
1260
+ return renderItems;
1261
+ default:
1262
+ throw new Error('unknown renderer');
1263
+ }
1264
+ };
1265
+
1266
+ const applyRender = (oldState, newState, diffResult) => {
1267
+ const commands = [];
1268
+ for (const item of diffResult) {
1269
+ const fn = getRenderer(item);
1270
+ const result = fn(oldState, newState);
1271
+ if (result.length > 0) {
1272
+ commands.push(result);
1273
+ }
1274
+ }
1275
+ return commands;
1276
+ };
1277
+
1278
+ const render2 = (uid, diffResult) => {
1279
+ const {
1280
+ newState,
1281
+ oldState
1282
+ } = get$2(uid);
1283
+ set$1(uid, newState, newState);
1284
+ const commands = applyRender(oldState, newState, diffResult);
1285
+ return commands;
1286
+ };
1287
+
1288
+ const HandleClickAt = 1;
1289
+ const HandleContextMenu = 2;
1290
+ const HandleFocus = 3;
1291
+ const HandleInput = 4;
1292
+ const HandleMouseOutAt = 6;
1293
+ const HandleMouseOver = 7;
1294
+ const HandleMouseOverAt = 8;
1295
+ const HandleWheel = 9;
1296
+ const HandleClickAction = 10;
1297
+
1298
+ const renderEventListeners = () => {
1299
+ return [{
1300
+ name: HandleWheel,
1301
+ params: ['handleWheel', DeltaMode, DeltaY],
1302
+ passive: true
1303
+ }, {
1304
+ name: HandleFocus,
1305
+ params: ['handleFocus']
1306
+ }, {
1307
+ name: HandleClickAt,
1308
+ params: ['handleClickAt', ClientX, ClientY, TargetName]
1309
+ }, {
1310
+ name: HandleMouseOverAt,
1311
+ params: ['handleMouseOverAt', ClientX, ClientY]
1312
+ }, {
1313
+ name: HandleMouseOver,
1314
+ params: ['handleMouseOver', ClientX, ClientY]
1315
+ }, {
1316
+ name: HandleMouseOutAt,
1317
+ params: ['handleMouseOutAt', ClientX, ClientY]
1318
+ }, {
1319
+ name: HandleInput,
1320
+ params: ['handleInput', TargetValue]
1321
+ }, {
1322
+ name: HandleContextMenu,
1323
+ params: ['handleContextMenu', Button, ClientX, ClientY],
1324
+ preventDefault: true
1325
+ }, {
1326
+ name: HandleWheel,
1327
+ params: ['handleWheel', DeltaMode, DeltaY],
1328
+ passive: true
1329
+ }, {
1330
+ name: HandleClickAction,
1331
+ params: ['handleActionClick', TargetName]
1332
+ }];
1333
+ };
1334
+
1335
+ const saveState = uid => {
1336
+ number(uid);
1337
+ const value = get$2(uid);
1338
+ const {
1339
+ newState
1340
+ } = value;
1341
+ const {
1342
+ statusBarItemsLeft,
1343
+ statusBarItemsRight
1344
+ } = newState;
1345
+ return {
1346
+ itemsLeft: statusBarItemsLeft,
1347
+ itemsRight: statusBarItemsRight
1348
+ };
1349
+ };
1350
+
1028
1351
  const commandMap = {
1029
1352
  'StatusBar.create': create$1,
1030
- 'StatusBar.handleClick': handleClick,
1031
- 'StatusBar.itemLeftUpdate': itemLeftUpdate,
1032
- 'StatusBar.itemRightCreate': itemRightCreate,
1033
- 'StatusBar.itemRightUpdate': itemRightUpdate,
1034
- 'StatusBar.loadContent': loadContent,
1353
+ 'StatusBar.diff2': diff2,
1354
+ 'StatusBar.handleClick': wrapCommand(handleClick),
1355
+ 'StatusBar.itemLeftUpdate': wrapCommand(itemLeftUpdate),
1356
+ 'StatusBar.itemRightCreate': wrapCommand(itemRightCreate),
1357
+ 'StatusBar.itemRightUpdate': wrapCommand(itemRightUpdate),
1358
+ 'StatusBar.loadContent': wrapCommand(loadContent),
1359
+ 'StatusBar.render2': render2,
1360
+ 'StatusBar.renderEventListeners': renderEventListeners,
1361
+ 'StatusBar.saveState': saveState,
1035
1362
  'StatusBar.terminate': terminate
1036
1363
  };
1037
1364
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/status-bar-worker",
3
- "version": "1.2.0",
3
+ "version": "1.4.0",
4
4
  "description": "Status Bar Worker",
5
5
  "repository": {
6
6
  "type": "git",