@lvce-editor/preview-worker 1.0.0 → 1.2.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.
@@ -820,7 +820,7 @@ const listen$1 = async (module, options) => {
820
820
  const ipc = module.wrap(rawIpc);
821
821
  return ipc;
822
822
  };
823
- const create$2 = async ({
823
+ const create$2$1 = async ({
824
824
  commandMap
825
825
  }) => {
826
826
  // TODO create a commandMap per rpc instance
@@ -832,7 +832,7 @@ const create$2 = async ({
832
832
  };
833
833
  const WebWorkerRpcClient = {
834
834
  __proto__: null,
835
- create: create$2
835
+ create: create$2$1
836
836
  };
837
837
  const createMockRpc = ({
838
838
  commandMap
@@ -854,6 +854,7 @@ const createMockRpc = ({
854
854
  return mockRpc;
855
855
  };
856
856
 
857
+ const Div = 4;
857
858
  const H1 = 5;
858
859
  const Text = 12;
859
860
  const Reference = 100;
@@ -877,7 +878,7 @@ const remove = id => {
877
878
  };
878
879
 
879
880
  /* eslint-disable @typescript-eslint/explicit-function-return-type */
880
- const create$1 = rpcId => {
881
+ const create$2 = rpcId => {
881
882
  return {
882
883
  async dispose() {
883
884
  const rpc = get$1(rpcId);
@@ -915,63 +916,123 @@ const create$1 = rpcId => {
915
916
 
916
917
  const {
917
918
  set: set$1
918
- } = create$1(RendererWorker);
919
+ } = create$2(RendererWorker);
919
920
 
920
- const terminate = () => {
921
- globalThis.close();
922
- };
923
-
924
- /* eslint-disable @typescript-eslint/explicit-function-return-type */
925
- /* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */
926
-
927
- const states = new Map();
928
- const set = (uid, newState, oldState) => {
929
- states.set(uid, {
930
- newState,
931
- oldState
932
- });
921
+ const toCommandId = key => {
922
+ const dotIndex = key.indexOf('.');
923
+ return key.slice(dotIndex + 1);
933
924
  };
934
- const get = uid => {
935
- const state = states.get(uid);
936
- if (!state) {
937
- throw new Error(`State not found for uid ${uid}`);
938
- }
939
- return state;
940
- };
941
- const getCommandIds = () => {
942
- return [];
943
- };
944
- const wrapCommand = fn => {
945
- return (uid, ...args) => {
946
- const {
947
- newState
948
- } = get(uid);
949
- return fn(newState, ...args);
925
+ const create$1 = () => {
926
+ const states = Object.create(null);
927
+ const commandMapRef = {};
928
+ return {
929
+ clear() {
930
+ for (const key of Object.keys(states)) {
931
+ delete states[key];
932
+ }
933
+ },
934
+ diff(uid, modules, numbers) {
935
+ const {
936
+ newState,
937
+ oldState
938
+ } = states[uid];
939
+ const diffResult = [];
940
+ for (let i = 0; i < modules.length; i++) {
941
+ const fn = modules[i];
942
+ if (!fn(oldState, newState)) {
943
+ diffResult.push(numbers[i]);
944
+ }
945
+ }
946
+ return diffResult;
947
+ },
948
+ dispose(uid) {
949
+ delete states[uid];
950
+ },
951
+ get(uid) {
952
+ return states[uid];
953
+ },
954
+ getCommandIds() {
955
+ const keys = Object.keys(commandMapRef);
956
+ const ids = keys.map(toCommandId);
957
+ return ids;
958
+ },
959
+ getKeys() {
960
+ return Object.keys(states).map(key => {
961
+ return Number.parseInt(key);
962
+ });
963
+ },
964
+ registerCommands(commandMap) {
965
+ Object.assign(commandMapRef, commandMap);
966
+ },
967
+ set(uid, oldState, newState) {
968
+ states[uid] = {
969
+ newState,
970
+ oldState
971
+ };
972
+ },
973
+ wrapCommand(fn) {
974
+ const wrapped = async (uid, ...args) => {
975
+ const {
976
+ newState,
977
+ oldState
978
+ } = states[uid];
979
+ const newerState = await fn(newState, ...args);
980
+ if (oldState === newerState || newState === newerState) {
981
+ return;
982
+ }
983
+ const latestOld = states[uid];
984
+ const latestNew = {
985
+ ...latestOld.newState,
986
+ ...newerState
987
+ };
988
+ states[uid] = {
989
+ newState: latestNew,
990
+ oldState: latestOld.oldState
991
+ };
992
+ };
993
+ return wrapped;
994
+ },
995
+ wrapGetter(fn) {
996
+ const wrapped = (uid, ...args) => {
997
+ const {
998
+ newState
999
+ } = states[uid];
1000
+ return fn(newState, ...args);
1001
+ };
1002
+ return wrapped;
1003
+ }
950
1004
  };
951
1005
  };
952
- const wrapGetter = fn => {
953
- return uid => {
954
- const {
955
- newState
956
- } = get(uid);
957
- return fn(newState);
958
- };
1006
+ const terminate = () => {
1007
+ globalThis.close();
959
1008
  };
960
1009
 
1010
+ const {
1011
+ get,
1012
+ getCommandIds,
1013
+ registerCommands,
1014
+ set,
1015
+ wrapCommand,
1016
+ wrapGetter
1017
+ } = create$1();
1018
+
961
1019
  const create = (uid, uri, x, y, width, height, platform, assetDir) => {
962
1020
  const state = {
963
1021
  assetDir,
1022
+ content: '',
964
1023
  errorCount: 0,
965
1024
  initial: true,
1025
+ parsedDom: [],
966
1026
  platform,
967
1027
  uid,
1028
+ uri,
968
1029
  warningCount: 0
969
1030
  };
970
1031
  set(uid, state, state);
971
1032
  };
972
1033
 
973
1034
  const isEqual = (oldState, newState) => {
974
- return oldState.warningCount === newState.warningCount;
1035
+ return oldState.warningCount === newState.warningCount && oldState.initial === newState.initial;
975
1036
  };
976
1037
 
977
1038
  const RenderItems = 4;
@@ -1005,7 +1066,7 @@ const loadContent = async state => {
1005
1066
  ...state,
1006
1067
  errorCount: 0,
1007
1068
  initial: false,
1008
- warningCount: 0
1069
+ warningCount: 1
1009
1070
  };
1010
1071
  };
1011
1072
 
@@ -1297,8 +1358,28 @@ const diffTree = (oldNodes, newNodes) => {
1297
1358
  return removeTrailingNavigationPatches(patches);
1298
1359
  };
1299
1360
 
1300
- const getPreviewDom = () => {
1361
+ const getEmptyPreviewDom = () => {
1362
+ return [{
1363
+ childCount: 1,
1364
+ className: 'Viewlet Preview',
1365
+ type: Div
1366
+ }, {
1367
+ childCount: 1,
1368
+ type: H1
1369
+ }, {
1370
+ text: 'No URI has been specified',
1371
+ type: Text
1372
+ }];
1373
+ };
1374
+ const getPreviewDom = state => {
1375
+ if (!state.uri) {
1376
+ return getEmptyPreviewDom();
1377
+ }
1301
1378
  return [{
1379
+ childCount: 1,
1380
+ className: 'Viewlet Preview',
1381
+ type: Div
1382
+ }, {
1302
1383
  childCount: 1,
1303
1384
  type: H1
1304
1385
  }, {
@@ -1309,9 +1390,13 @@ const getPreviewDom = () => {
1309
1390
 
1310
1391
  const renderItems = (oldState, newState) => {
1311
1392
  const {
1393
+ initial,
1312
1394
  uid
1313
1395
  } = newState;
1314
- const dom = getPreviewDom();
1396
+ if (initial) {
1397
+ return [SetDom2, uid, []];
1398
+ }
1399
+ const dom = getPreviewDom(newState);
1315
1400
  return [SetDom2, uid, dom];
1316
1401
  };
1317
1402
 
@@ -1377,19 +1462,28 @@ const saveState = state => {
1377
1462
  };
1378
1463
  };
1379
1464
 
1465
+ const setUri = (state, uri) => {
1466
+ return {
1467
+ ...state,
1468
+ uri
1469
+ };
1470
+ };
1471
+
1380
1472
  const commandMap = {
1381
- 'StatusBar.create': create,
1382
- 'StatusBar.diff2': diff2,
1383
- 'StatusBar.getCommandIds': getCommandIds,
1384
- 'StatusBar.loadContent': wrapCommand(loadContent),
1385
- 'StatusBar.render2': render2,
1386
- 'StatusBar.renderEventListeners': renderEventListeners,
1387
- 'StatusBar.resize': wrapCommand(resize),
1388
- 'StatusBar.saveState': wrapGetter(saveState),
1389
- 'StatusBar.terminate': terminate
1473
+ 'Preview.create': create,
1474
+ 'Preview.diff2': diff2,
1475
+ 'Preview.getCommandIds': getCommandIds,
1476
+ 'Preview.loadContent': wrapCommand(loadContent),
1477
+ 'Preview.render2': render2,
1478
+ 'Preview.renderEventListeners': renderEventListeners,
1479
+ 'Preview.resize': wrapCommand(resize),
1480
+ 'Preview.saveState': wrapGetter(saveState),
1481
+ 'Preview.setUri': wrapCommand(setUri),
1482
+ 'Preview.terminate': terminate
1390
1483
  };
1391
1484
 
1392
1485
  const listen = async () => {
1486
+ registerCommands(commandMap);
1393
1487
  const rpc = await WebWorkerRpcClient.create({
1394
1488
  commandMap: commandMap
1395
1489
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/preview-worker",
3
- "version": "1.0.0",
3
+ "version": "1.2.0",
4
4
  "description": "Preview Worker",
5
5
  "repository": {
6
6
  "type": "git",