phecda-web 1.0.1-alpha.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.
- package/LICENSE +21 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +161 -0
- package/dist/index.mjs +123 -0
- package/package.json +25 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 fgsreally
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Events, Construct } from 'phecda-core';
|
|
2
|
+
export * from 'phecda-core';
|
|
3
|
+
|
|
4
|
+
interface ActiveInstance {
|
|
5
|
+
state: Record<string, any>;
|
|
6
|
+
_v: WeakMap<any, any>;
|
|
7
|
+
_r: WeakMap<any, any>;
|
|
8
|
+
_f: WeakMap<any, any>;
|
|
9
|
+
_c: WeakMap<any, any>;
|
|
10
|
+
[key: string]: any;
|
|
11
|
+
}
|
|
12
|
+
interface PhecdaEmitter {
|
|
13
|
+
on<N extends keyof Events>(eventName: N, cb: (args: Events[N]) => void): void;
|
|
14
|
+
off<N extends keyof Events>(eventName: N, cb?: (args: Events[N]) => void): void;
|
|
15
|
+
emit<N extends keyof Events>(eventName: N, param: Events[N]): void;
|
|
16
|
+
}
|
|
17
|
+
interface Plugin {
|
|
18
|
+
setup(instance: ActiveInstance): void;
|
|
19
|
+
unmount?(instance: ActiveInstance): void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
declare const emitter: PhecdaEmitter;
|
|
23
|
+
declare const storagePlugin: () => Plugin;
|
|
24
|
+
declare const watchPlugin: () => Plugin;
|
|
25
|
+
|
|
26
|
+
declare function waitUntilInit(...instances: InstanceType<Construct>[]): Promise<void>;
|
|
27
|
+
declare function resetActiveInstance(instance?: ActiveInstance): void;
|
|
28
|
+
declare function getActiveInstance(): ActiveInstance;
|
|
29
|
+
declare function serializeState(): any;
|
|
30
|
+
|
|
31
|
+
declare function wrapError(target: any, key: PropertyKey, errorHandler: Function): (...args: any) => any;
|
|
32
|
+
declare function isAsyncFunc(fn: Function): boolean;
|
|
33
|
+
|
|
34
|
+
export { ActiveInstance, PhecdaEmitter, Plugin, emitter, getActiveInstance, isAsyncFunc, resetActiveInstance, serializeState, storagePlugin, waitUntilInit, watchPlugin, wrapError };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
23
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
24
|
+
mod
|
|
25
|
+
));
|
|
26
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
27
|
+
|
|
28
|
+
// src/index.ts
|
|
29
|
+
var src_exports = {};
|
|
30
|
+
__export(src_exports, {
|
|
31
|
+
emitter: () => emitter,
|
|
32
|
+
getActiveInstance: () => getActiveInstance,
|
|
33
|
+
isAsyncFunc: () => isAsyncFunc,
|
|
34
|
+
resetActiveInstance: () => resetActiveInstance,
|
|
35
|
+
serializeState: () => serializeState,
|
|
36
|
+
storagePlugin: () => storagePlugin,
|
|
37
|
+
waitUntilInit: () => waitUntilInit,
|
|
38
|
+
watchPlugin: () => watchPlugin,
|
|
39
|
+
wrapError: () => wrapError
|
|
40
|
+
});
|
|
41
|
+
module.exports = __toCommonJS(src_exports);
|
|
42
|
+
__reExport(src_exports, require("phecda-core"), module.exports);
|
|
43
|
+
|
|
44
|
+
// src/plugin.ts
|
|
45
|
+
var import_phecda_core = require("phecda-core");
|
|
46
|
+
var import_mitt = __toESM(require("mitt"));
|
|
47
|
+
var emitter = (0, import_mitt.default)();
|
|
48
|
+
var storagePlugin = /* @__PURE__ */ __name(() => {
|
|
49
|
+
return {
|
|
50
|
+
setup() {
|
|
51
|
+
(0, import_phecda_core.injectProperty)("storage", ({ tag, key, instance }) => {
|
|
52
|
+
if (!tag)
|
|
53
|
+
return;
|
|
54
|
+
const initstr = localStorage.getItem(tag);
|
|
55
|
+
if (initstr) {
|
|
56
|
+
const data = JSON.parse(initstr);
|
|
57
|
+
if (key) {
|
|
58
|
+
instance[key] = data;
|
|
59
|
+
} else {
|
|
60
|
+
for (const i in data) {
|
|
61
|
+
if (i)
|
|
62
|
+
instance[i] = data[i];
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
globalThis.addEventListener("beforeunload", () => {
|
|
67
|
+
localStorage.setItem(tag, JSON.stringify(key ? instance[key] : instance));
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
}, "storagePlugin");
|
|
73
|
+
var watchPlugin = /* @__PURE__ */ __name(() => {
|
|
74
|
+
const eventRecord = [];
|
|
75
|
+
return {
|
|
76
|
+
setup() {
|
|
77
|
+
(0, import_phecda_core.injectProperty)("watcher", ({ eventName, instance, key, options }) => {
|
|
78
|
+
const fn = typeof instance[key] === "function" ? instance[key].bind(instance) : (v) => instance[key] = v;
|
|
79
|
+
if (options?.once) {
|
|
80
|
+
const handler = /* @__PURE__ */ __name((...args) => {
|
|
81
|
+
fn(...args);
|
|
82
|
+
emitter.off(eventName, handler);
|
|
83
|
+
}, "handler");
|
|
84
|
+
emitter.on(eventName, handler);
|
|
85
|
+
eventRecord.push([
|
|
86
|
+
eventName,
|
|
87
|
+
handler
|
|
88
|
+
]);
|
|
89
|
+
} else {
|
|
90
|
+
eventRecord.push([
|
|
91
|
+
eventName,
|
|
92
|
+
fn
|
|
93
|
+
]);
|
|
94
|
+
emitter.on(eventName, fn);
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
},
|
|
98
|
+
unmount() {
|
|
99
|
+
eventRecord.forEach(([eventName, handler]) => emitter.off(eventName, handler));
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
}, "watchPlugin");
|
|
103
|
+
|
|
104
|
+
// src/core.ts
|
|
105
|
+
async function waitUntilInit(...instances) {
|
|
106
|
+
await Promise.all(instances.map((i) => i._promise));
|
|
107
|
+
}
|
|
108
|
+
__name(waitUntilInit, "waitUntilInit");
|
|
109
|
+
var activeInstance;
|
|
110
|
+
function resetActiveInstance(instance) {
|
|
111
|
+
activeInstance = instance || {
|
|
112
|
+
state: {},
|
|
113
|
+
_v: /* @__PURE__ */ new WeakMap(),
|
|
114
|
+
_r: /* @__PURE__ */ new WeakMap(),
|
|
115
|
+
_f: /* @__PURE__ */ new WeakMap(),
|
|
116
|
+
_c: /* @__PURE__ */ new WeakMap()
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
__name(resetActiveInstance, "resetActiveInstance");
|
|
120
|
+
function getActiveInstance() {
|
|
121
|
+
return activeInstance;
|
|
122
|
+
}
|
|
123
|
+
__name(getActiveInstance, "getActiveInstance");
|
|
124
|
+
function serializeState() {
|
|
125
|
+
return JSON.parse(JSON.stringify(activeInstance.state));
|
|
126
|
+
}
|
|
127
|
+
__name(serializeState, "serializeState");
|
|
128
|
+
|
|
129
|
+
// src/utils.ts
|
|
130
|
+
function wrapError(target, key, errorHandler) {
|
|
131
|
+
if (isAsyncFunc(target[key])) {
|
|
132
|
+
return (...args) => {
|
|
133
|
+
return target[key].apply(target, args).catch(errorHandler);
|
|
134
|
+
};
|
|
135
|
+
} else {
|
|
136
|
+
return (...args) => {
|
|
137
|
+
try {
|
|
138
|
+
return target[key].apply(target, args);
|
|
139
|
+
} catch (e) {
|
|
140
|
+
return errorHandler(e);
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
__name(wrapError, "wrapError");
|
|
146
|
+
function isAsyncFunc(fn) {
|
|
147
|
+
return fn[Symbol.toStringTag] === "AsyncFunction";
|
|
148
|
+
}
|
|
149
|
+
__name(isAsyncFunc, "isAsyncFunc");
|
|
150
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
151
|
+
0 && (module.exports = {
|
|
152
|
+
emitter,
|
|
153
|
+
getActiveInstance,
|
|
154
|
+
isAsyncFunc,
|
|
155
|
+
resetActiveInstance,
|
|
156
|
+
serializeState,
|
|
157
|
+
storagePlugin,
|
|
158
|
+
waitUntilInit,
|
|
159
|
+
watchPlugin,
|
|
160
|
+
wrapError
|
|
161
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// src/index.ts
|
|
5
|
+
export * from "phecda-core";
|
|
6
|
+
|
|
7
|
+
// src/plugin.ts
|
|
8
|
+
import { injectProperty } from "phecda-core";
|
|
9
|
+
import mitt from "mitt";
|
|
10
|
+
var emitter = mitt();
|
|
11
|
+
var storagePlugin = /* @__PURE__ */ __name(() => {
|
|
12
|
+
return {
|
|
13
|
+
setup() {
|
|
14
|
+
injectProperty("storage", ({ tag, key, instance }) => {
|
|
15
|
+
if (!tag)
|
|
16
|
+
return;
|
|
17
|
+
const initstr = localStorage.getItem(tag);
|
|
18
|
+
if (initstr) {
|
|
19
|
+
const data = JSON.parse(initstr);
|
|
20
|
+
if (key) {
|
|
21
|
+
instance[key] = data;
|
|
22
|
+
} else {
|
|
23
|
+
for (const i in data) {
|
|
24
|
+
if (i)
|
|
25
|
+
instance[i] = data[i];
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
globalThis.addEventListener("beforeunload", () => {
|
|
30
|
+
localStorage.setItem(tag, JSON.stringify(key ? instance[key] : instance));
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}, "storagePlugin");
|
|
36
|
+
var watchPlugin = /* @__PURE__ */ __name(() => {
|
|
37
|
+
const eventRecord = [];
|
|
38
|
+
return {
|
|
39
|
+
setup() {
|
|
40
|
+
injectProperty("watcher", ({ eventName, instance, key, options }) => {
|
|
41
|
+
const fn = typeof instance[key] === "function" ? instance[key].bind(instance) : (v) => instance[key] = v;
|
|
42
|
+
if (options?.once) {
|
|
43
|
+
const handler = /* @__PURE__ */ __name((...args) => {
|
|
44
|
+
fn(...args);
|
|
45
|
+
emitter.off(eventName, handler);
|
|
46
|
+
}, "handler");
|
|
47
|
+
emitter.on(eventName, handler);
|
|
48
|
+
eventRecord.push([
|
|
49
|
+
eventName,
|
|
50
|
+
handler
|
|
51
|
+
]);
|
|
52
|
+
} else {
|
|
53
|
+
eventRecord.push([
|
|
54
|
+
eventName,
|
|
55
|
+
fn
|
|
56
|
+
]);
|
|
57
|
+
emitter.on(eventName, fn);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
},
|
|
61
|
+
unmount() {
|
|
62
|
+
eventRecord.forEach(([eventName, handler]) => emitter.off(eventName, handler));
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}, "watchPlugin");
|
|
66
|
+
|
|
67
|
+
// src/core.ts
|
|
68
|
+
async function waitUntilInit(...instances) {
|
|
69
|
+
await Promise.all(instances.map((i) => i._promise));
|
|
70
|
+
}
|
|
71
|
+
__name(waitUntilInit, "waitUntilInit");
|
|
72
|
+
var activeInstance;
|
|
73
|
+
function resetActiveInstance(instance) {
|
|
74
|
+
activeInstance = instance || {
|
|
75
|
+
state: {},
|
|
76
|
+
_v: /* @__PURE__ */ new WeakMap(),
|
|
77
|
+
_r: /* @__PURE__ */ new WeakMap(),
|
|
78
|
+
_f: /* @__PURE__ */ new WeakMap(),
|
|
79
|
+
_c: /* @__PURE__ */ new WeakMap()
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
__name(resetActiveInstance, "resetActiveInstance");
|
|
83
|
+
function getActiveInstance() {
|
|
84
|
+
return activeInstance;
|
|
85
|
+
}
|
|
86
|
+
__name(getActiveInstance, "getActiveInstance");
|
|
87
|
+
function serializeState() {
|
|
88
|
+
return JSON.parse(JSON.stringify(activeInstance.state));
|
|
89
|
+
}
|
|
90
|
+
__name(serializeState, "serializeState");
|
|
91
|
+
|
|
92
|
+
// src/utils.ts
|
|
93
|
+
function wrapError(target, key, errorHandler) {
|
|
94
|
+
if (isAsyncFunc(target[key])) {
|
|
95
|
+
return (...args) => {
|
|
96
|
+
return target[key].apply(target, args).catch(errorHandler);
|
|
97
|
+
};
|
|
98
|
+
} else {
|
|
99
|
+
return (...args) => {
|
|
100
|
+
try {
|
|
101
|
+
return target[key].apply(target, args);
|
|
102
|
+
} catch (e) {
|
|
103
|
+
return errorHandler(e);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
__name(wrapError, "wrapError");
|
|
109
|
+
function isAsyncFunc(fn) {
|
|
110
|
+
return fn[Symbol.toStringTag] === "AsyncFunction";
|
|
111
|
+
}
|
|
112
|
+
__name(isAsyncFunc, "isAsyncFunc");
|
|
113
|
+
export {
|
|
114
|
+
emitter,
|
|
115
|
+
getActiveInstance,
|
|
116
|
+
isAsyncFunc,
|
|
117
|
+
resetActiveInstance,
|
|
118
|
+
serializeState,
|
|
119
|
+
storagePlugin,
|
|
120
|
+
waitUntilInit,
|
|
121
|
+
watchPlugin,
|
|
122
|
+
wrapError
|
|
123
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "phecda-web",
|
|
3
|
+
"version": "1.0.1-alpha.0",
|
|
4
|
+
"description": "provide web function for phecda",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"keywords": [],
|
|
9
|
+
"author": "",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"mitt": "^3.0.0",
|
|
16
|
+
"phecda-core": "3.0.0-alpha.3"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"tsup": "^6.5.0"
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsup",
|
|
23
|
+
"dev": "tsup --watch"
|
|
24
|
+
}
|
|
25
|
+
}
|