@lvce-editor/json-rpc 0.1.0 → 0.1.1

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.
package/dist/index.d.ts CHANGED
@@ -1,3 +1,16 @@
1
- export const invoke: (method: string, ...params: any[]) => Promise<any>
1
+ export const send: (ipc: any, method: string, ...params: any[]) => void
2
+
3
+ export const invoke: (
4
+ ipc: any,
5
+ method: string,
6
+ ...params: any[]
7
+ ) => Promise<any>
8
+
9
+ export const invokeAndTransfer: (
10
+ ipc: any,
11
+ transfer: any,
12
+ method: string,
13
+ ...params: any[]
14
+ ) => Promise<any>
2
15
 
3
16
  export const resolve: (id: number, message: any) => void
package/dist/index.js CHANGED
@@ -8,6 +8,43 @@ const create$2 = (method, params) => {
8
8
  }
9
9
  };
10
10
 
11
+ class AssertionError extends Error {
12
+ constructor(message) {
13
+ super(message);
14
+ this.name = 'AssertionError';
15
+ }
16
+ }
17
+
18
+ const getType = (value) => {
19
+ switch (typeof value) {
20
+ case 'number':
21
+ return 'number'
22
+ case 'function':
23
+ return 'function'
24
+ case 'string':
25
+ return 'string'
26
+ case 'object':
27
+ if (value === null) {
28
+ return 'null'
29
+ }
30
+ if (Array.isArray(value)) {
31
+ return 'array'
32
+ }
33
+ return 'object'
34
+ case 'boolean':
35
+ return 'boolean'
36
+ default:
37
+ return 'unknown'
38
+ }
39
+ };
40
+
41
+ const number = (value) => {
42
+ const type = getType(value);
43
+ if (type !== 'number') {
44
+ throw new AssertionError('expected value to be of type number')
45
+ }
46
+ };
47
+
11
48
  const state$1 = {
12
49
  id: 0,
13
50
  };
@@ -16,6 +53,10 @@ const create$1 = () => {
16
53
  return ++state$1.id
17
54
  };
18
55
 
56
+ const warn = (...args) => {
57
+ console.warn(...args);
58
+ };
59
+
19
60
  const withResolvers = () => {
20
61
  /**
21
62
  * @type {any}
@@ -47,6 +88,17 @@ const registerPromise = () => {
47
88
  return { id, promise }
48
89
  };
49
90
 
91
+ const resolve = (id, args) => {
92
+ number(id);
93
+ if (!(id in state.callbacks)) {
94
+ console.log(args);
95
+ warn(`callback ${id} may already be disposed`);
96
+ return
97
+ }
98
+ state.callbacks[id](args);
99
+ delete state.callbacks[id];
100
+ };
101
+
50
102
  const create = (method, params) => {
51
103
  const { id, promise } = registerPromise();
52
104
  const message = {
@@ -231,4 +283,4 @@ const invokeAndTransfer = async (ipc, handle, method, ...params) => {
231
283
  return result
232
284
  };
233
285
 
234
- export { invoke, invokeAndTransfer, send };
286
+ export { invoke, invokeAndTransfer, resolve, send };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/json-rpc",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "JsonRpc implementation",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",