@talex-touch/utils 1.0.3 → 1.0.4
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/package.json +29 -29
- package/permission/index.ts +50 -0
- package/plugin/preload.ts +1 -0
- package/plugin/sdk/hooks/index.ts +1 -0
- package/plugin/sdk/hooks/life-cycle.ts +91 -0
- package/plugin/sdk/index.ts +13 -0
package/package.json
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
{
|
|
2
|
-
"main": "./index.ts",
|
|
3
|
-
"name": "@talex-touch/utils",
|
|
4
|
-
"author": "TalexDreamSoul",
|
|
5
|
-
"module": "./index.ts",
|
|
6
|
-
"license": "MPL-2.0",
|
|
7
|
-
"private": false,
|
|
8
|
-
"version": "1.0.
|
|
9
|
-
"scripts": {
|
|
10
|
-
"publish": "npm publish --access public"
|
|
11
|
-
},
|
|
12
|
-
"keywords": [
|
|
13
|
-
"vue",
|
|
14
|
-
"electron",
|
|
15
|
-
"talex-touch"
|
|
16
|
-
],
|
|
17
|
-
"repository": {
|
|
18
|
-
"url": "https://github.com/talex-touch/talex-touch.git",
|
|
19
|
-
"type": "git"
|
|
20
|
-
},
|
|
21
|
-
"description": "TalexTouch series utils",
|
|
22
|
-
"dependencies": {
|
|
23
|
-
"vue": "^3.
|
|
24
|
-
},
|
|
25
|
-
"peerDependencies": {
|
|
26
|
-
"vue": "^3.2.47",
|
|
27
|
-
"electron": "^24.4.0"
|
|
28
|
-
}
|
|
29
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"main": "./index.ts",
|
|
3
|
+
"name": "@talex-touch/utils",
|
|
4
|
+
"author": "TalexDreamSoul",
|
|
5
|
+
"module": "./index.ts",
|
|
6
|
+
"license": "MPL-2.0",
|
|
7
|
+
"private": false,
|
|
8
|
+
"version": "1.0.4",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"publish": "npm publish --access public"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"vue",
|
|
14
|
+
"electron",
|
|
15
|
+
"talex-touch"
|
|
16
|
+
],
|
|
17
|
+
"repository": {
|
|
18
|
+
"url": "https://github.com/talex-touch/talex-touch.git",
|
|
19
|
+
"type": "git"
|
|
20
|
+
},
|
|
21
|
+
"description": "TalexTouch series utils",
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"vue": "^3.3.4"
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"vue": "^3.2.47",
|
|
27
|
+
"electron": "^24.4.0"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export interface Permission {
|
|
2
|
+
/**
|
|
3
|
+
* permission id
|
|
4
|
+
*/
|
|
5
|
+
id: symbol;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* permission name
|
|
9
|
+
*/
|
|
10
|
+
name: string;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* permission description
|
|
14
|
+
*/
|
|
15
|
+
description: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// generate a permission manager interface
|
|
19
|
+
export interface IPermissionCenter {
|
|
20
|
+
/**
|
|
21
|
+
* add a permission
|
|
22
|
+
* @param pluginScope plugin name
|
|
23
|
+
* @param permission permission
|
|
24
|
+
* @throws if permission already exists
|
|
25
|
+
*/
|
|
26
|
+
addPermission(pluginScope: string, permission: Permission): void;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* remove a permission
|
|
30
|
+
* @param pluginScope plugin name
|
|
31
|
+
* @param permission permission
|
|
32
|
+
* @throws if permission not exists
|
|
33
|
+
*/
|
|
34
|
+
delPermission(pluginScope: string, permission: Permission): void;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* if pluginScope has permission
|
|
38
|
+
* @param pluginScope plugin name
|
|
39
|
+
* @param permission permission
|
|
40
|
+
*/
|
|
41
|
+
hasPermission(pluginScope: string, permission: Permission): boolean;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* get permission
|
|
45
|
+
* @param pluginScope plugin name
|
|
46
|
+
* @param permission permission id
|
|
47
|
+
* @returns permission
|
|
48
|
+
*/
|
|
49
|
+
getPermission(pluginScope: string, permission: symbol): Permission;
|
|
50
|
+
}
|
package/plugin/preload.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * as LifeCycle from './life-cycle'
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { touchChannel } from './../../channel';
|
|
2
|
+
|
|
3
|
+
export enum LifecycleHooks {
|
|
4
|
+
ENABLE = 'en',
|
|
5
|
+
DISABLE = 'di',
|
|
6
|
+
ACTIVE = 'ac',
|
|
7
|
+
INACTIVE = 'in',
|
|
8
|
+
// FORE_PAUSED = 'fp',
|
|
9
|
+
CRASH = 'cr'
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function injectHook(type: LifecycleHooks, hook: Function, processFunc = ({ data, reply }) => {
|
|
13
|
+
const hooks: Array<Function> = window.$touchSDK.__hooks[type]
|
|
14
|
+
if (hooks) {
|
|
15
|
+
hooks.forEach(hook => hook(data))
|
|
16
|
+
}
|
|
17
|
+
reply(true)
|
|
18
|
+
}) {
|
|
19
|
+
const __hooks = window.$touchSDK.__hooks
|
|
20
|
+
const hooks: Array<Function> = __hooks[type] || (__hooks[type] = [])
|
|
21
|
+
|
|
22
|
+
if (hooks.length === 0) {
|
|
23
|
+
|
|
24
|
+
touchChannel.regChannel("@lifecycle:" + type, obj => {
|
|
25
|
+
|
|
26
|
+
processFunc(obj)
|
|
27
|
+
|
|
28
|
+
delete window.$touchSDK.__hooks[type]
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const wrappedHook = (data: any) => {
|
|
34
|
+
|
|
35
|
+
try {
|
|
36
|
+
|
|
37
|
+
hook(data)
|
|
38
|
+
|
|
39
|
+
} catch (e) {
|
|
40
|
+
console.error(`[TouchSDK] ${type} hook error: `, e)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
hooks.push(wrappedHook)
|
|
46
|
+
|
|
47
|
+
return wrappedHook
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export const createHook = <T extends Function = (data: any) => any>(type: LifecycleHooks) => (hook: T) => injectHook(type, hook)
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The plugin is enabled
|
|
54
|
+
* When the plugin is enabled, the plugin can be used
|
|
55
|
+
* @returns void
|
|
56
|
+
*/
|
|
57
|
+
export const onPluginEnable = createHook(LifecycleHooks.ENABLE)
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The plugin is disabled
|
|
61
|
+
* When the plugin is disabled, the plugin can not be used
|
|
62
|
+
* @returns void
|
|
63
|
+
*/
|
|
64
|
+
export const onPluginDisable = createHook(LifecycleHooks.DISABLE)
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The plugin is activated
|
|
68
|
+
* @returns boolean If return false, the plugin will not be activated (User can force to activate the plugin)
|
|
69
|
+
*/
|
|
70
|
+
export const onPluginActive = createHook(LifecycleHooks.ACTIVE)
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The plugin is inactivated
|
|
74
|
+
* @returns boolean If return false, the plugin will not be inactivated (User can force to inactivate the plugin)
|
|
75
|
+
*/
|
|
76
|
+
export const onPluginInactive = createHook(LifecycleHooks.INACTIVE)
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* When plugin is in foreground (e.g. plugin is using media, camera, microphone, etc.) But paused by user
|
|
80
|
+
* For a detail example: User force to stop music playing
|
|
81
|
+
* @returns void
|
|
82
|
+
*/
|
|
83
|
+
// export const onForePaused = createHook(LifecycleHooks.FORE_PAUSED)
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* When plugin is crashed
|
|
87
|
+
* data.message Crash message
|
|
88
|
+
* data.extraData Crash data
|
|
89
|
+
* @returns void
|
|
90
|
+
*/
|
|
91
|
+
export const onCrash = createHook(LifecycleHooks.CRASH)
|