@lvce-editor/preview-worker 1.0.0 → 1.1.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
@@ -877,7 +877,7 @@ const remove = id => {
877
877
  };
878
878
 
879
879
  /* eslint-disable @typescript-eslint/explicit-function-return-type */
880
- const create$1 = rpcId => {
880
+ const create$2 = rpcId => {
881
881
  return {
882
882
  async dispose() {
883
883
  const rpc = get$1(rpcId);
@@ -915,49 +915,106 @@ const create$1 = rpcId => {
915
915
 
916
916
  const {
917
917
  set: set$1
918
- } = create$1(RendererWorker);
918
+ } = create$2(RendererWorker);
919
919
 
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
- });
933
- };
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;
920
+ const toCommandId = key => {
921
+ const dotIndex = key.indexOf('.');
922
+ return key.slice(dotIndex + 1);
940
923
  };
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);
924
+ const create$1 = () => {
925
+ const states = Object.create(null);
926
+ const commandMapRef = {};
927
+ return {
928
+ clear() {
929
+ for (const key of Object.keys(states)) {
930
+ delete states[key];
931
+ }
932
+ },
933
+ diff(uid, modules, numbers) {
934
+ const {
935
+ newState,
936
+ oldState
937
+ } = states[uid];
938
+ const diffResult = [];
939
+ for (let i = 0; i < modules.length; i++) {
940
+ const fn = modules[i];
941
+ if (!fn(oldState, newState)) {
942
+ diffResult.push(numbers[i]);
943
+ }
944
+ }
945
+ return diffResult;
946
+ },
947
+ dispose(uid) {
948
+ delete states[uid];
949
+ },
950
+ get(uid) {
951
+ return states[uid];
952
+ },
953
+ getCommandIds() {
954
+ const keys = Object.keys(commandMapRef);
955
+ const ids = keys.map(toCommandId);
956
+ return ids;
957
+ },
958
+ getKeys() {
959
+ return Object.keys(states).map(key => {
960
+ return Number.parseInt(key);
961
+ });
962
+ },
963
+ registerCommands(commandMap) {
964
+ Object.assign(commandMapRef, commandMap);
965
+ },
966
+ set(uid, oldState, newState) {
967
+ states[uid] = {
968
+ newState,
969
+ oldState
970
+ };
971
+ },
972
+ wrapCommand(fn) {
973
+ const wrapped = async (uid, ...args) => {
974
+ const {
975
+ newState,
976
+ oldState
977
+ } = states[uid];
978
+ const newerState = await fn(newState, ...args);
979
+ if (oldState === newerState || newState === newerState) {
980
+ return;
981
+ }
982
+ const latestOld = states[uid];
983
+ const latestNew = {
984
+ ...latestOld.newState,
985
+ ...newerState
986
+ };
987
+ states[uid] = {
988
+ newState: latestNew,
989
+ oldState: latestOld.oldState
990
+ };
991
+ };
992
+ return wrapped;
993
+ },
994
+ wrapGetter(fn) {
995
+ const wrapped = (uid, ...args) => {
996
+ const {
997
+ newState
998
+ } = states[uid];
999
+ return fn(newState, ...args);
1000
+ };
1001
+ return wrapped;
1002
+ }
950
1003
  };
951
1004
  };
952
- const wrapGetter = fn => {
953
- return uid => {
954
- const {
955
- newState
956
- } = get(uid);
957
- return fn(newState);
958
- };
1005
+ const terminate = () => {
1006
+ globalThis.close();
959
1007
  };
960
1008
 
1009
+ const {
1010
+ get,
1011
+ getCommandIds,
1012
+ registerCommands,
1013
+ set,
1014
+ wrapCommand,
1015
+ wrapGetter
1016
+ } = create$1();
1017
+
961
1018
  const create = (uid, uri, x, y, width, height, platform, assetDir) => {
962
1019
  const state = {
963
1020
  assetDir,
@@ -1378,18 +1435,19 @@ const saveState = state => {
1378
1435
  };
1379
1436
 
1380
1437
  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
1438
+ 'Preview.create': create,
1439
+ 'Preview.diff2': diff2,
1440
+ 'Preview.getCommandIds': getCommandIds,
1441
+ 'Preview.loadContent': wrapCommand(loadContent),
1442
+ 'Preview.render2': render2,
1443
+ 'Preview.renderEventListeners': renderEventListeners,
1444
+ 'Preview.resize': wrapCommand(resize),
1445
+ 'Preview.saveState': wrapGetter(saveState),
1446
+ 'Preview.terminate': terminate
1390
1447
  };
1391
1448
 
1392
1449
  const listen = async () => {
1450
+ registerCommands(commandMap);
1393
1451
  const rpc = await WebWorkerRpcClient.create({
1394
1452
  commandMap: commandMap
1395
1453
  });
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.1.0",
4
4
  "description": "Preview Worker",
5
5
  "repository": {
6
6
  "type": "git",