Compare commits
27
Commits
dev-Rachel
...
5b4842b261
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5b4842b261 | ||
|
|
691ac03cfd | ||
|
|
5e4c4fdd22 | ||
|
|
de67ab0d68 | ||
|
|
121b620312 | ||
|
|
49ed8e588a | ||
|
|
22af68428f | ||
|
|
4f49012bb6 | ||
|
|
73563de980 | ||
|
|
eb77678686 | ||
|
|
fc6fee74b0 | ||
|
|
9543fa8409 | ||
|
|
fadbc4dc72 | ||
|
|
ad4570dae0 | ||
|
|
8d248466eb | ||
|
|
bfa5ed0e3a | ||
|
|
582c8446e2 | ||
|
|
0fc0fa932a | ||
|
|
98f4236c35 | ||
|
|
b37977418a | ||
|
|
7a78cf0594 | ||
|
|
af68984caa | ||
|
|
623307ef2b | ||
|
|
14b03af584 | ||
|
|
c3773a3c71 | ||
|
|
0140136a0b | ||
|
|
41519b78e4 |
@@ -0,0 +1,674 @@
|
|||||||
|
GNU GENERAL PUBLIC LICENSE
|
||||||
|
Version 3, 29 June 2007
|
||||||
|
|
||||||
|
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||||
|
Everyone is permitted to copy and distribute verbatim copies
|
||||||
|
of this license document, but changing it is not allowed.
|
||||||
|
|
||||||
|
Preamble
|
||||||
|
|
||||||
|
The GNU General Public License is a free, copyleft license for
|
||||||
|
software and other kinds of works.
|
||||||
|
|
||||||
|
The licenses for most software and other practical works are designed
|
||||||
|
to take away your freedom to share and change the works. By contrast,
|
||||||
|
the GNU General Public License is intended to guarantee your freedom to
|
||||||
|
share and change all versions of a program--to make sure it remains free
|
||||||
|
software for all its users. We, the Free Software Foundation, use the
|
||||||
|
GNU General Public License for most of our software; it applies also to
|
||||||
|
any other work released this way by its authors. You can apply it to
|
||||||
|
your programs, too.
|
||||||
|
|
||||||
|
When we speak of free software, we are referring to freedom, not
|
||||||
|
price. Our General Public Licenses are designed to make sure that you
|
||||||
|
have the freedom to distribute copies of free software (and charge for
|
||||||
|
them if you wish), that you receive source code or can get it if you
|
||||||
|
want it, that you can change the software or use pieces of it in new
|
||||||
|
free programs, and that you know you can do these things.
|
||||||
|
|
||||||
|
To protect your rights, we need to prevent others from denying you
|
||||||
|
these rights or asking you to surrender the rights. Therefore, you have
|
||||||
|
certain responsibilities if you distribute copies of the software, or if
|
||||||
|
you modify it: responsibilities to respect the freedom of others.
|
||||||
|
|
||||||
|
For example, if you distribute copies of such a program, whether
|
||||||
|
gratis or for a fee, you must pass on to the recipients the same
|
||||||
|
freedoms that you received. You must make sure that they, too, receive
|
||||||
|
or can get the source code. And you must show them these terms so they
|
||||||
|
know their rights.
|
||||||
|
|
||||||
|
Developers that use the GNU GPL protect your rights with two steps:
|
||||||
|
(1) assert copyright on the software, and (2) offer you this License
|
||||||
|
giving you legal permission to copy, distribute and/or modify it.
|
||||||
|
|
||||||
|
For the developers' and authors' protection, the GPL clearly explains
|
||||||
|
that there is no warranty for this free software. For both users' and
|
||||||
|
authors' sake, the GPL requires that modified versions be marked as
|
||||||
|
changed, so that their problems will not be attributed erroneously to
|
||||||
|
authors of previous versions.
|
||||||
|
|
||||||
|
Some devices are designed to deny users access to install or run
|
||||||
|
modified versions of the software inside them, although the manufacturer
|
||||||
|
can do so. This is fundamentally incompatible with the aim of
|
||||||
|
protecting users' freedom to change the software. The systematic
|
||||||
|
pattern of such abuse occurs in the area of products for individuals to
|
||||||
|
use, which is precisely where it is most unacceptable. Therefore, we
|
||||||
|
have designed this version of the GPL to prohibit the practice for those
|
||||||
|
products. If such problems arise substantially in other domains, we
|
||||||
|
stand ready to extend this provision to those domains in future versions
|
||||||
|
of the GPL, as needed to protect the freedom of users.
|
||||||
|
|
||||||
|
Finally, every program is threatened constantly by software patents.
|
||||||
|
States should not allow patents to restrict development and use of
|
||||||
|
software on general-purpose computers, but in those that do, we wish to
|
||||||
|
avoid the special danger that patents applied to a free program could
|
||||||
|
make it effectively proprietary. To prevent this, the GPL assures that
|
||||||
|
patents cannot be used to render the program non-free.
|
||||||
|
|
||||||
|
The precise terms and conditions for copying, distribution and
|
||||||
|
modification follow.
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
0. Definitions.
|
||||||
|
|
||||||
|
"This License" refers to version 3 of the GNU General Public License.
|
||||||
|
|
||||||
|
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||||
|
works, such as semiconductor masks.
|
||||||
|
|
||||||
|
"The Program" refers to any copyrightable work licensed under this
|
||||||
|
License. Each licensee is addressed as "you". "Licensees" and
|
||||||
|
"recipients" may be individuals or organizations.
|
||||||
|
|
||||||
|
To "modify" a work means to copy from or adapt all or part of the work
|
||||||
|
in a fashion requiring copyright permission, other than the making of an
|
||||||
|
exact copy. The resulting work is called a "modified version" of the
|
||||||
|
earlier work or a work "based on" the earlier work.
|
||||||
|
|
||||||
|
A "covered work" means either the unmodified Program or a work based
|
||||||
|
on the Program.
|
||||||
|
|
||||||
|
To "propagate" a work means to do anything with it that, without
|
||||||
|
permission, would make you directly or secondarily liable for
|
||||||
|
infringement under applicable copyright law, except executing it on a
|
||||||
|
computer or modifying a private copy. Propagation includes copying,
|
||||||
|
distribution (with or without modification), making available to the
|
||||||
|
public, and in some countries other activities as well.
|
||||||
|
|
||||||
|
To "convey" a work means any kind of propagation that enables other
|
||||||
|
parties to make or receive copies. Mere interaction with a user through
|
||||||
|
a computer network, with no transfer of a copy, is not conveying.
|
||||||
|
|
||||||
|
An interactive user interface displays "Appropriate Legal Notices"
|
||||||
|
to the extent that it includes a convenient and prominently visible
|
||||||
|
feature that (1) displays an appropriate copyright notice, and (2)
|
||||||
|
tells the user that there is no warranty for the work (except to the
|
||||||
|
extent that warranties are provided), that licensees may convey the
|
||||||
|
work under this License, and how to view a copy of this License. If
|
||||||
|
the interface presents a list of user commands or options, such as a
|
||||||
|
menu, a prominent item in the list meets this criterion.
|
||||||
|
|
||||||
|
1. Source Code.
|
||||||
|
|
||||||
|
The "source code" for a work means the preferred form of the work
|
||||||
|
for making modifications to it. "Object code" means any non-source
|
||||||
|
form of a work.
|
||||||
|
|
||||||
|
A "Standard Interface" means an interface that either is an official
|
||||||
|
standard defined by a recognized standards body, or, in the case of
|
||||||
|
interfaces specified for a particular programming language, one that
|
||||||
|
is widely used among developers working in that language.
|
||||||
|
|
||||||
|
The "System Libraries" of an executable work include anything, other
|
||||||
|
than the work as a whole, that (a) is included in the normal form of
|
||||||
|
packaging a Major Component, but which is not part of that Major
|
||||||
|
Component, and (b) serves only to enable use of the work with that
|
||||||
|
Major Component, or to implement a Standard Interface for which an
|
||||||
|
implementation is available to the public in source code form. A
|
||||||
|
"Major Component", in this context, means a major essential component
|
||||||
|
(kernel, window system, and so on) of the specific operating system
|
||||||
|
(if any) on which the executable work runs, or a compiler used to
|
||||||
|
produce the work, or an object code interpreter used to run it.
|
||||||
|
|
||||||
|
The "Corresponding Source" for a work in object code form means all
|
||||||
|
the source code needed to generate, install, and (for an executable
|
||||||
|
work) run the object code and to modify the work, including scripts to
|
||||||
|
control those activities. However, it does not include the work's
|
||||||
|
System Libraries, or general-purpose tools or generally available free
|
||||||
|
programs which are used unmodified in performing those activities but
|
||||||
|
which are not part of the work. For example, Corresponding Source
|
||||||
|
includes interface definition files associated with source files for
|
||||||
|
the work, and the source code for shared libraries and dynamically
|
||||||
|
linked subprograms that the work is specifically designed to require,
|
||||||
|
such as by intimate data communication or control flow between those
|
||||||
|
subprograms and other parts of the work.
|
||||||
|
|
||||||
|
The Corresponding Source need not include anything that users
|
||||||
|
can regenerate automatically from other parts of the Corresponding
|
||||||
|
Source.
|
||||||
|
|
||||||
|
The Corresponding Source for a work in source code form is that
|
||||||
|
same work.
|
||||||
|
|
||||||
|
2. Basic Permissions.
|
||||||
|
|
||||||
|
All rights granted under this License are granted for the term of
|
||||||
|
copyright on the Program, and are irrevocable provided the stated
|
||||||
|
conditions are met. This License explicitly affirms your unlimited
|
||||||
|
permission to run the unmodified Program. The output from running a
|
||||||
|
covered work is covered by this License only if the output, given its
|
||||||
|
content, constitutes a covered work. This License acknowledges your
|
||||||
|
rights of fair use or other equivalent, as provided by copyright law.
|
||||||
|
|
||||||
|
You may make, run and propagate covered works that you do not
|
||||||
|
convey, without conditions so long as your license otherwise remains
|
||||||
|
in force. You may convey covered works to others for the sole purpose
|
||||||
|
of having them make modifications exclusively for you, or provide you
|
||||||
|
with facilities for running those works, provided that you comply with
|
||||||
|
the terms of this License in conveying all material for which you do
|
||||||
|
not control copyright. Those thus making or running the covered works
|
||||||
|
for you must do so exclusively on your behalf, under your direction
|
||||||
|
and control, on terms that prohibit them from making any copies of
|
||||||
|
your copyrighted material outside their relationship with you.
|
||||||
|
|
||||||
|
Conveying under any other circumstances is permitted solely under
|
||||||
|
the conditions stated below. Sublicensing is not allowed; section 10
|
||||||
|
makes it unnecessary.
|
||||||
|
|
||||||
|
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||||
|
|
||||||
|
No covered work shall be deemed part of an effective technological
|
||||||
|
measure under any applicable law fulfilling obligations under article
|
||||||
|
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||||
|
similar laws prohibiting or restricting circumvention of such
|
||||||
|
measures.
|
||||||
|
|
||||||
|
When you convey a covered work, you waive any legal power to forbid
|
||||||
|
circumvention of technological measures to the extent such circumvention
|
||||||
|
is effected by exercising rights under this License with respect to
|
||||||
|
the covered work, and you disclaim any intention to limit operation or
|
||||||
|
modification of the work as a means of enforcing, against the work's
|
||||||
|
users, your or third parties' legal rights to forbid circumvention of
|
||||||
|
technological measures.
|
||||||
|
|
||||||
|
4. Conveying Verbatim Copies.
|
||||||
|
|
||||||
|
You may convey verbatim copies of the Program's source code as you
|
||||||
|
receive it, in any medium, provided that you conspicuously and
|
||||||
|
appropriately publish on each copy an appropriate copyright notice;
|
||||||
|
keep intact all notices stating that this License and any
|
||||||
|
non-permissive terms added in accord with section 7 apply to the code;
|
||||||
|
keep intact all notices of the absence of any warranty; and give all
|
||||||
|
recipients a copy of this License along with the Program.
|
||||||
|
|
||||||
|
You may charge any price or no price for each copy that you convey,
|
||||||
|
and you may offer support or warranty protection for a fee.
|
||||||
|
|
||||||
|
5. Conveying Modified Source Versions.
|
||||||
|
|
||||||
|
You may convey a work based on the Program, or the modifications to
|
||||||
|
produce it from the Program, in the form of source code under the
|
||||||
|
terms of section 4, provided that you also meet all of these conditions:
|
||||||
|
|
||||||
|
a) The work must carry prominent notices stating that you modified
|
||||||
|
it, and giving a relevant date.
|
||||||
|
|
||||||
|
b) The work must carry prominent notices stating that it is
|
||||||
|
released under this License and any conditions added under section
|
||||||
|
7. This requirement modifies the requirement in section 4 to
|
||||||
|
"keep intact all notices".
|
||||||
|
|
||||||
|
c) You must license the entire work, as a whole, under this
|
||||||
|
License to anyone who comes into possession of a copy. This
|
||||||
|
License will therefore apply, along with any applicable section 7
|
||||||
|
additional terms, to the whole of the work, and all its parts,
|
||||||
|
regardless of how they are packaged. This License gives no
|
||||||
|
permission to license the work in any other way, but it does not
|
||||||
|
invalidate such permission if you have separately received it.
|
||||||
|
|
||||||
|
d) If the work has interactive user interfaces, each must display
|
||||||
|
Appropriate Legal Notices; however, if the Program has interactive
|
||||||
|
interfaces that do not display Appropriate Legal Notices, your
|
||||||
|
work need not make them do so.
|
||||||
|
|
||||||
|
A compilation of a covered work with other separate and independent
|
||||||
|
works, which are not by their nature extensions of the covered work,
|
||||||
|
and which are not combined with it such as to form a larger program,
|
||||||
|
in or on a volume of a storage or distribution medium, is called an
|
||||||
|
"aggregate" if the compilation and its resulting copyright are not
|
||||||
|
used to limit the access or legal rights of the compilation's users
|
||||||
|
beyond what the individual works permit. Inclusion of a covered work
|
||||||
|
in an aggregate does not cause this License to apply to the other
|
||||||
|
parts of the aggregate.
|
||||||
|
|
||||||
|
6. Conveying Non-Source Forms.
|
||||||
|
|
||||||
|
You may convey a covered work in object code form under the terms
|
||||||
|
of sections 4 and 5, provided that you also convey the
|
||||||
|
machine-readable Corresponding Source under the terms of this License,
|
||||||
|
in one of these ways:
|
||||||
|
|
||||||
|
a) Convey the object code in, or embodied in, a physical product
|
||||||
|
(including a physical distribution medium), accompanied by the
|
||||||
|
Corresponding Source fixed on a durable physical medium
|
||||||
|
customarily used for software interchange.
|
||||||
|
|
||||||
|
b) Convey the object code in, or embodied in, a physical product
|
||||||
|
(including a physical distribution medium), accompanied by a
|
||||||
|
written offer, valid for at least three years and valid for as
|
||||||
|
long as you offer spare parts or customer support for that product
|
||||||
|
model, to give anyone who possesses the object code either (1) a
|
||||||
|
copy of the Corresponding Source for all the software in the
|
||||||
|
product that is covered by this License, on a durable physical
|
||||||
|
medium customarily used for software interchange, for a price no
|
||||||
|
more than your reasonable cost of physically performing this
|
||||||
|
conveying of source, or (2) access to copy the
|
||||||
|
Corresponding Source from a network server at no charge.
|
||||||
|
|
||||||
|
c) Convey individual copies of the object code with a copy of the
|
||||||
|
written offer to provide the Corresponding Source. This
|
||||||
|
alternative is allowed only occasionally and noncommercially, and
|
||||||
|
only if you received the object code with such an offer, in accord
|
||||||
|
with subsection 6b.
|
||||||
|
|
||||||
|
d) Convey the object code by offering access from a designated
|
||||||
|
place (gratis or for a charge), and offer equivalent access to the
|
||||||
|
Corresponding Source in the same way through the same place at no
|
||||||
|
further charge. You need not require recipients to copy the
|
||||||
|
Corresponding Source along with the object code. If the place to
|
||||||
|
copy the object code is a network server, the Corresponding Source
|
||||||
|
may be on a different server (operated by you or a third party)
|
||||||
|
that supports equivalent copying facilities, provided you maintain
|
||||||
|
clear directions next to the object code saying where to find the
|
||||||
|
Corresponding Source. Regardless of what server hosts the
|
||||||
|
Corresponding Source, you remain obligated to ensure that it is
|
||||||
|
available for as long as needed to satisfy these requirements.
|
||||||
|
|
||||||
|
e) Convey the object code using peer-to-peer transmission, provided
|
||||||
|
you inform other peers where the object code and Corresponding
|
||||||
|
Source of the work are being offered to the general public at no
|
||||||
|
charge under subsection 6d.
|
||||||
|
|
||||||
|
A separable portion of the object code, whose source code is excluded
|
||||||
|
from the Corresponding Source as a System Library, need not be
|
||||||
|
included in conveying the object code work.
|
||||||
|
|
||||||
|
A "User Product" is either (1) a "consumer product", which means any
|
||||||
|
tangible personal property which is normally used for personal, family,
|
||||||
|
or household purposes, or (2) anything designed or sold for incorporation
|
||||||
|
into a dwelling. In determining whether a product is a consumer product,
|
||||||
|
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||||
|
product received by a particular user, "normally used" refers to a
|
||||||
|
typical or common use of that class of product, regardless of the status
|
||||||
|
of the particular user or of the way in which the particular user
|
||||||
|
actually uses, or expects or is expected to use, the product. A product
|
||||||
|
is a consumer product regardless of whether the product has substantial
|
||||||
|
commercial, industrial or non-consumer uses, unless such uses represent
|
||||||
|
the only significant mode of use of the product.
|
||||||
|
|
||||||
|
"Installation Information" for a User Product means any methods,
|
||||||
|
procedures, authorization keys, or other information required to install
|
||||||
|
and execute modified versions of a covered work in that User Product from
|
||||||
|
a modified version of its Corresponding Source. The information must
|
||||||
|
suffice to ensure that the continued functioning of the modified object
|
||||||
|
code is in no case prevented or interfered with solely because
|
||||||
|
modification has been made.
|
||||||
|
|
||||||
|
If you convey an object code work under this section in, or with, or
|
||||||
|
specifically for use in, a User Product, and the conveying occurs as
|
||||||
|
part of a transaction in which the right of possession and use of the
|
||||||
|
User Product is transferred to the recipient in perpetuity or for a
|
||||||
|
fixed term (regardless of how the transaction is characterized), the
|
||||||
|
Corresponding Source conveyed under this section must be accompanied
|
||||||
|
by the Installation Information. But this requirement does not apply
|
||||||
|
if neither you nor any third party retains the ability to install
|
||||||
|
modified object code on the User Product (for example, the work has
|
||||||
|
been installed in ROM).
|
||||||
|
|
||||||
|
The requirement to provide Installation Information does not include a
|
||||||
|
requirement to continue to provide support service, warranty, or updates
|
||||||
|
for a work that has been modified or installed by the recipient, or for
|
||||||
|
the User Product in which it has been modified or installed. Access to a
|
||||||
|
network may be denied when the modification itself materially and
|
||||||
|
adversely affects the operation of the network or violates the rules and
|
||||||
|
protocols for communication across the network.
|
||||||
|
|
||||||
|
Corresponding Source conveyed, and Installation Information provided,
|
||||||
|
in accord with this section must be in a format that is publicly
|
||||||
|
documented (and with an implementation available to the public in
|
||||||
|
source code form), and must require no special password or key for
|
||||||
|
unpacking, reading or copying.
|
||||||
|
|
||||||
|
7. Additional Terms.
|
||||||
|
|
||||||
|
"Additional permissions" are terms that supplement the terms of this
|
||||||
|
License by making exceptions from one or more of its conditions.
|
||||||
|
Additional permissions that are applicable to the entire Program shall
|
||||||
|
be treated as though they were included in this License, to the extent
|
||||||
|
that they are valid under applicable law. If additional permissions
|
||||||
|
apply only to part of the Program, that part may be used separately
|
||||||
|
under those permissions, but the entire Program remains governed by
|
||||||
|
this License without regard to the additional permissions.
|
||||||
|
|
||||||
|
When you convey a copy of a covered work, you may at your option
|
||||||
|
remove any additional permissions from that copy, or from any part of
|
||||||
|
it. (Additional permissions may be written to require their own
|
||||||
|
removal in certain cases when you modify the work.) You may place
|
||||||
|
additional permissions on material, added by you to a covered work,
|
||||||
|
for which you have or can give appropriate copyright permission.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, for material you
|
||||||
|
add to a covered work, you may (if authorized by the copyright holders of
|
||||||
|
that material) supplement the terms of this License with terms:
|
||||||
|
|
||||||
|
a) Disclaiming warranty or limiting liability differently from the
|
||||||
|
terms of sections 15 and 16 of this License; or
|
||||||
|
|
||||||
|
b) Requiring preservation of specified reasonable legal notices or
|
||||||
|
author attributions in that material or in the Appropriate Legal
|
||||||
|
Notices displayed by works containing it; or
|
||||||
|
|
||||||
|
c) Prohibiting misrepresentation of the origin of that material, or
|
||||||
|
requiring that modified versions of such material be marked in
|
||||||
|
reasonable ways as different from the original version; or
|
||||||
|
|
||||||
|
d) Limiting the use for publicity purposes of names of licensors or
|
||||||
|
authors of the material; or
|
||||||
|
|
||||||
|
e) Declining to grant rights under trademark law for use of some
|
||||||
|
trade names, trademarks, or service marks; or
|
||||||
|
|
||||||
|
f) Requiring indemnification of licensors and authors of that
|
||||||
|
material by anyone who conveys the material (or modified versions of
|
||||||
|
it) with contractual assumptions of liability to the recipient, for
|
||||||
|
any liability that these contractual assumptions directly impose on
|
||||||
|
those licensors and authors.
|
||||||
|
|
||||||
|
All other non-permissive additional terms are considered "further
|
||||||
|
restrictions" within the meaning of section 10. If the Program as you
|
||||||
|
received it, or any part of it, contains a notice stating that it is
|
||||||
|
governed by this License along with a term that is a further
|
||||||
|
restriction, you may remove that term. If a license document contains
|
||||||
|
a further restriction but permits relicensing or conveying under this
|
||||||
|
License, you may add to a covered work material governed by the terms
|
||||||
|
of that license document, provided that the further restriction does
|
||||||
|
not survive such relicensing or conveying.
|
||||||
|
|
||||||
|
If you add terms to a covered work in accord with this section, you
|
||||||
|
must place, in the relevant source files, a statement of the
|
||||||
|
additional terms that apply to those files, or a notice indicating
|
||||||
|
where to find the applicable terms.
|
||||||
|
|
||||||
|
Additional terms, permissive or non-permissive, may be stated in the
|
||||||
|
form of a separately written license, or stated as exceptions;
|
||||||
|
the above requirements apply either way.
|
||||||
|
|
||||||
|
8. Termination.
|
||||||
|
|
||||||
|
You may not propagate or modify a covered work except as expressly
|
||||||
|
provided under this License. Any attempt otherwise to propagate or
|
||||||
|
modify it is void, and will automatically terminate your rights under
|
||||||
|
this License (including any patent licenses granted under the third
|
||||||
|
paragraph of section 11).
|
||||||
|
|
||||||
|
However, if you cease all violation of this License, then your
|
||||||
|
license from a particular copyright holder is reinstated (a)
|
||||||
|
provisionally, unless and until the copyright holder explicitly and
|
||||||
|
finally terminates your license, and (b) permanently, if the copyright
|
||||||
|
holder fails to notify you of the violation by some reasonable means
|
||||||
|
prior to 60 days after the cessation.
|
||||||
|
|
||||||
|
Moreover, your license from a particular copyright holder is
|
||||||
|
reinstated permanently if the copyright holder notifies you of the
|
||||||
|
violation by some reasonable means, this is the first time you have
|
||||||
|
received notice of violation of this License (for any work) from that
|
||||||
|
copyright holder, and you cure the violation prior to 30 days after
|
||||||
|
your receipt of the notice.
|
||||||
|
|
||||||
|
Termination of your rights under this section does not terminate the
|
||||||
|
licenses of parties who have received copies or rights from you under
|
||||||
|
this License. If your rights have been terminated and not permanently
|
||||||
|
reinstated, you do not qualify to receive new licenses for the same
|
||||||
|
material under section 10.
|
||||||
|
|
||||||
|
9. Acceptance Not Required for Having Copies.
|
||||||
|
|
||||||
|
You are not required to accept this License in order to receive or
|
||||||
|
run a copy of the Program. Ancillary propagation of a covered work
|
||||||
|
occurring solely as a consequence of using peer-to-peer transmission
|
||||||
|
to receive a copy likewise does not require acceptance. However,
|
||||||
|
nothing other than this License grants you permission to propagate or
|
||||||
|
modify any covered work. These actions infringe copyright if you do
|
||||||
|
not accept this License. Therefore, by modifying or propagating a
|
||||||
|
covered work, you indicate your acceptance of this License to do so.
|
||||||
|
|
||||||
|
10. Automatic Licensing of Downstream Recipients.
|
||||||
|
|
||||||
|
Each time you convey a covered work, the recipient automatically
|
||||||
|
receives a license from the original licensors, to run, modify and
|
||||||
|
propagate that work, subject to this License. You are not responsible
|
||||||
|
for enforcing compliance by third parties with this License.
|
||||||
|
|
||||||
|
An "entity transaction" is a transaction transferring control of an
|
||||||
|
organization, or substantially all assets of one, or subdividing an
|
||||||
|
organization, or merging organizations. If propagation of a covered
|
||||||
|
work results from an entity transaction, each party to that
|
||||||
|
transaction who receives a copy of the work also receives whatever
|
||||||
|
licenses to the work the party's predecessor in interest had or could
|
||||||
|
give under the previous paragraph, plus a right to possession of the
|
||||||
|
Corresponding Source of the work from the predecessor in interest, if
|
||||||
|
the predecessor has it or can get it with reasonable efforts.
|
||||||
|
|
||||||
|
You may not impose any further restrictions on the exercise of the
|
||||||
|
rights granted or affirmed under this License. For example, you may
|
||||||
|
not impose a license fee, royalty, or other charge for exercise of
|
||||||
|
rights granted under this License, and you may not initiate litigation
|
||||||
|
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||||
|
any patent claim is infringed by making, using, selling, offering for
|
||||||
|
sale, or importing the Program or any portion of it.
|
||||||
|
|
||||||
|
11. Patents.
|
||||||
|
|
||||||
|
A "contributor" is a copyright holder who authorizes use under this
|
||||||
|
License of the Program or a work on which the Program is based. The
|
||||||
|
work thus licensed is called the contributor's "contributor version".
|
||||||
|
|
||||||
|
A contributor's "essential patent claims" are all patent claims
|
||||||
|
owned or controlled by the contributor, whether already acquired or
|
||||||
|
hereafter acquired, that would be infringed by some manner, permitted
|
||||||
|
by this License, of making, using, or selling its contributor version,
|
||||||
|
but do not include claims that would be infringed only as a
|
||||||
|
consequence of further modification of the contributor version. For
|
||||||
|
purposes of this definition, "control" includes the right to grant
|
||||||
|
patent sublicenses in a manner consistent with the requirements of
|
||||||
|
this License.
|
||||||
|
|
||||||
|
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||||
|
patent license under the contributor's essential patent claims, to
|
||||||
|
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||||
|
propagate the contents of its contributor version.
|
||||||
|
|
||||||
|
In the following three paragraphs, a "patent license" is any express
|
||||||
|
agreement or commitment, however denominated, not to enforce a patent
|
||||||
|
(such as an express permission to practice a patent or covenant not to
|
||||||
|
sue for patent infringement). To "grant" such a patent license to a
|
||||||
|
party means to make such an agreement or commitment not to enforce a
|
||||||
|
patent against the party.
|
||||||
|
|
||||||
|
If you convey a covered work, knowingly relying on a patent license,
|
||||||
|
and the Corresponding Source of the work is not available for anyone
|
||||||
|
to copy, free of charge and under the terms of this License, through a
|
||||||
|
publicly available network server or other readily accessible means,
|
||||||
|
then you must either (1) cause the Corresponding Source to be so
|
||||||
|
available, or (2) arrange to deprive yourself of the benefit of the
|
||||||
|
patent license for this particular work, or (3) arrange, in a manner
|
||||||
|
consistent with the requirements of this License, to extend the patent
|
||||||
|
license to downstream recipients. "Knowingly relying" means you have
|
||||||
|
actual knowledge that, but for the patent license, your conveying the
|
||||||
|
covered work in a country, or your recipient's use of the covered work
|
||||||
|
in a country, would infringe one or more identifiable patents in that
|
||||||
|
country that you have reason to believe are valid.
|
||||||
|
|
||||||
|
If, pursuant to or in connection with a single transaction or
|
||||||
|
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||||
|
covered work, and grant a patent license to some of the parties
|
||||||
|
receiving the covered work authorizing them to use, propagate, modify
|
||||||
|
or convey a specific copy of the covered work, then the patent license
|
||||||
|
you grant is automatically extended to all recipients of the covered
|
||||||
|
work and works based on it.
|
||||||
|
|
||||||
|
A patent license is "discriminatory" if it does not include within
|
||||||
|
the scope of its coverage, prohibits the exercise of, or is
|
||||||
|
conditioned on the non-exercise of one or more of the rights that are
|
||||||
|
specifically granted under this License. You may not convey a covered
|
||||||
|
work if you are a party to an arrangement with a third party that is
|
||||||
|
in the business of distributing software, under which you make payment
|
||||||
|
to the third party based on the extent of your activity of conveying
|
||||||
|
the work, and under which the third party grants, to any of the
|
||||||
|
parties who would receive the covered work from you, a discriminatory
|
||||||
|
patent license (a) in connection with copies of the covered work
|
||||||
|
conveyed by you (or copies made from those copies), or (b) primarily
|
||||||
|
for and in connection with specific products or compilations that
|
||||||
|
contain the covered work, unless you entered into that arrangement,
|
||||||
|
or that patent license was granted, prior to 28 March 2007.
|
||||||
|
|
||||||
|
Nothing in this License shall be construed as excluding or limiting
|
||||||
|
any implied license or other defenses to infringement that may
|
||||||
|
otherwise be available to you under applicable patent law.
|
||||||
|
|
||||||
|
12. No Surrender of Others' Freedom.
|
||||||
|
|
||||||
|
If conditions are imposed on you (whether by court order, agreement or
|
||||||
|
otherwise) that contradict the conditions of this License, they do not
|
||||||
|
excuse you from the conditions of this License. If you cannot convey a
|
||||||
|
covered work so as to satisfy simultaneously your obligations under this
|
||||||
|
License and any other pertinent obligations, then as a consequence you may
|
||||||
|
not convey it at all. For example, if you agree to terms that obligate you
|
||||||
|
to collect a royalty for further conveying from those to whom you convey
|
||||||
|
the Program, the only way you could satisfy both those terms and this
|
||||||
|
License would be to refrain entirely from conveying the Program.
|
||||||
|
|
||||||
|
13. Use with the GNU Affero General Public License.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, you have
|
||||||
|
permission to link or combine any covered work with a work licensed
|
||||||
|
under version 3 of the GNU Affero General Public License into a single
|
||||||
|
combined work, and to convey the resulting work. The terms of this
|
||||||
|
License will continue to apply to the part which is the covered work,
|
||||||
|
but the special requirements of the GNU Affero General Public License,
|
||||||
|
section 13, concerning interaction through a network will apply to the
|
||||||
|
combination as such.
|
||||||
|
|
||||||
|
14. Revised Versions of this License.
|
||||||
|
|
||||||
|
The Free Software Foundation may publish revised and/or new versions of
|
||||||
|
the GNU General Public License from time to time. Such new versions will
|
||||||
|
be similar in spirit to the present version, but may differ in detail to
|
||||||
|
address new problems or concerns.
|
||||||
|
|
||||||
|
Each version is given a distinguishing version number. If the
|
||||||
|
Program specifies that a certain numbered version of the GNU General
|
||||||
|
Public License "or any later version" applies to it, you have the
|
||||||
|
option of following the terms and conditions either of that numbered
|
||||||
|
version or of any later version published by the Free Software
|
||||||
|
Foundation. If the Program does not specify a version number of the
|
||||||
|
GNU General Public License, you may choose any version ever published
|
||||||
|
by the Free Software Foundation.
|
||||||
|
|
||||||
|
If the Program specifies that a proxy can decide which future
|
||||||
|
versions of the GNU General Public License can be used, that proxy's
|
||||||
|
public statement of acceptance of a version permanently authorizes you
|
||||||
|
to choose that version for the Program.
|
||||||
|
|
||||||
|
Later license versions may give you additional or different
|
||||||
|
permissions. However, no additional obligations are imposed on any
|
||||||
|
author or copyright holder as a result of your choosing to follow a
|
||||||
|
later version.
|
||||||
|
|
||||||
|
15. Disclaimer of Warranty.
|
||||||
|
|
||||||
|
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||||
|
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||||
|
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||||
|
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||||
|
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||||
|
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||||
|
|
||||||
|
16. Limitation of Liability.
|
||||||
|
|
||||||
|
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||||
|
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||||
|
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||||
|
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||||
|
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||||
|
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||||
|
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||||
|
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||||
|
SUCH DAMAGES.
|
||||||
|
|
||||||
|
17. Interpretation of Sections 15 and 16.
|
||||||
|
|
||||||
|
If the disclaimer of warranty and limitation of liability provided
|
||||||
|
above cannot be given local legal effect according to their terms,
|
||||||
|
reviewing courts shall apply local law that most closely approximates
|
||||||
|
an absolute waiver of all civil liability in connection with the
|
||||||
|
Program, unless a warranty or assumption of liability accompanies a
|
||||||
|
copy of the Program in return for a fee.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
How to Apply These Terms to Your New Programs
|
||||||
|
|
||||||
|
If you develop a new program, and you want it to be of the greatest
|
||||||
|
possible use to the public, the best way to achieve this is to make it
|
||||||
|
free software which everyone can redistribute and change under these terms.
|
||||||
|
|
||||||
|
To do so, attach the following notices to the program. It is safest
|
||||||
|
to attach them to the start of each source file to most effectively
|
||||||
|
state the exclusion of warranty; and each file should have at least
|
||||||
|
the "copyright" line and a pointer to where the full notice is found.
|
||||||
|
|
||||||
|
<one line to give the program's name and a brief idea of what it does.>
|
||||||
|
Copyright (C) <year> <name of author>
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
Also add information on how to contact you by electronic and paper mail.
|
||||||
|
|
||||||
|
If the program does terminal interaction, make it output a short
|
||||||
|
notice like this when it starts in an interactive mode:
|
||||||
|
|
||||||
|
<program> Copyright (C) <year> <name of author>
|
||||||
|
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||||
|
This is free software, and you are welcome to redistribute it
|
||||||
|
under certain conditions; type `show c' for details.
|
||||||
|
|
||||||
|
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||||
|
parts of the General Public License. Of course, your program's commands
|
||||||
|
might be different; for a GUI interface, you would use an "about box".
|
||||||
|
|
||||||
|
You should also get your employer (if you work as a programmer) or school,
|
||||||
|
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||||
|
For more information on this, and how to apply and follow the GNU GPL, see
|
||||||
|
<https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
The GNU General Public License does not permit incorporating your program
|
||||||
|
into proprietary programs. If your program is a subroutine library, you
|
||||||
|
may consider it more useful to permit linking proprietary applications with
|
||||||
|
the library. If this is what you want to do, use the GNU Lesser General
|
||||||
|
Public License instead of this License. But first, please read
|
||||||
|
<https://www.gnu.org/licenses/why-not-lgpl.html>.
|
||||||
@@ -2,6 +2,22 @@
|
|||||||
|
|
||||||
掌上教务处作为工业大学的社区开源项目,自从2017年开始已有近5年岁月,在无数同学的贡献之下,为大家打造便捷的校园服务。
|
掌上教务处作为工业大学的社区开源项目,自从2017年开始已有近5年岁月,在无数同学的贡献之下,为大家打造便捷的校园服务。
|
||||||
|
|
||||||
|
__*!!!警告!!!*__
|
||||||
|
|
||||||
|
请在主仓库提交代码,而非镜像仓库!在镜像仓库提交的代码将会在同步时被覆盖!
|
||||||
|
|
||||||
|
主仓库: http://git.mrkbear.com/MrKBear/mini-dlpu-v3
|
||||||
|
|
||||||
|
镜像仓库: https://github.com/Mr-k-bear/mini-dlpu-v3
|
||||||
|
|
||||||
|
## 目录
|
||||||
|
|
||||||
|
- [社区介绍](#小程序社区)
|
||||||
|
- [项目设计](#第三代小程序)
|
||||||
|
- [贡献规范](#社区贡献规范)
|
||||||
|
- [入门文档(等待撰写)](#第三代掌上教务处小程序)
|
||||||
|
- [设计架构(等待撰写)](#第三代掌上教务处小程序)
|
||||||
|
|
||||||
## 小程序社区
|
## 小程序社区
|
||||||
|
|
||||||
掌上教务处小程序诞生于2017年中旬,此时微信小程序刚刚公测。
|
掌上教务处小程序诞生于2017年中旬,此时微信小程序刚刚公测。
|
||||||
@@ -48,12 +64,39 @@
|
|||||||
|
|
||||||
3. 更好的拓展性,加入更多大家喜欢的功能
|
3. 更好的拓展性,加入更多大家喜欢的功能
|
||||||
|
|
||||||
|
新功能:
|
||||||
|
|
||||||
|
1. 正在讨论设计,等待你的建议...
|
||||||
|
|
||||||
## 社区贡献规范
|
## 社区贡献规范
|
||||||
|
|
||||||
|
请仔细阅读!
|
||||||
|
### 项目贡献流程
|
||||||
|
|
||||||
请先邮件联系 ```mrkbear@mrkbear.com``` 获得 Gitea 平台账号
|
请先邮件联系 ```mrkbear@mrkbear.com``` 获得 Gitea 平台账号
|
||||||
|
|
||||||
在仓库中创建自己的分支,分支命名规范为 ```dev-你的昵称```
|
在仓库中创建自己的分支,分支命名规范为 ```dev-你的昵称```,例如 ```dev-mrkbear```
|
||||||
|
|
||||||
克隆此储库,在本地 ```checkout``` 到自己的分支
|
克隆此储库,在本地 ```git checkout dev-你的昵称``` 到自己的分支,进行改动。
|
||||||
|
|
||||||
开发完成后 ```push``` 到自己的远程分支,并发起合并请求,等待 review
|
开发完成后 ```git push``` 到自己的远程分支,并发起合并请求到 ```master``` 分支
|
||||||
|
|
||||||
|
发起合并请求时,需要指派给 ```MrKBear``` 进行代码审核,审核通过后,代码将完成合并。
|
||||||
|
|
||||||
|
### 注意事项
|
||||||
|
|
||||||
|
1. ```master``` 分支处于保护状态,仅通过合并请求进行修改
|
||||||
|
|
||||||
|
2. 代码提交时,请使用清晰明确的 ```message```
|
||||||
|
|
||||||
|
正例: ```Add timetable page``` 反例: ```阿巴阿巴阿巴阿巴```
|
||||||
|
|
||||||
|
3. 请勿将任何个人隐私信息以任何方式,放入代码中
|
||||||
|
|
||||||
|
4. 为保证 CI/CD,提交代码前必须保证编译可以通过
|
||||||
|
|
||||||
|
5. 一个文件不要超过 1000 行代码,尽量保证代码可读性
|
||||||
|
|
||||||
|
## 贡献者
|
||||||
|
|
||||||
|
@MrKBear (熊鲜森)
|
||||||
+15
-16
@@ -1,30 +1,29 @@
|
|||||||
import { Logger } from "./core/Logger";
|
import { IAppAPIParam } from "./core/Api";
|
||||||
import { LevelLogLabel, LifeCycleLogLabel } from "./core/PresetLogLabel";
|
import { IAppStorageParam, Storage, IStorageData } from "./core/Storage";
|
||||||
|
import { Logger, LevelLogLabel, LifeCycleLogLabel } from "./core/Logger";
|
||||||
import { Storage } from "./core/Storage";
|
|
||||||
|
|
||||||
|
|
||||||
App({
|
App<IAppAPIParam & IAppStorageParam>({
|
||||||
|
|
||||||
|
/**
|
||||||
|
* API 模块需要的全局数据
|
||||||
|
* 参见 "/core/Api"
|
||||||
|
*/
|
||||||
|
api: {
|
||||||
|
nextId: 1,
|
||||||
|
pool: new Set()
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 存储缓存键值
|
* 存储缓存键值
|
||||||
*/
|
*/
|
||||||
storageCache: new Set<string>(),
|
storage: new Map<string, Storage<IStorageData>>(),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 小程序加载时
|
* 小程序加载时
|
||||||
*/
|
*/
|
||||||
onLaunch() {
|
onLaunch() {
|
||||||
Logger.log("小程序启动...",
|
Logger.log("小程序启动...",
|
||||||
LevelLogLabel.TraceLabel, LifeCycleLogLabel.OnLaunchLabel);
|
LevelLogLabel.InfoLabel, LifeCycleLogLabel.OnLaunchLabel);
|
||||||
|
|
||||||
let s = new Storage("test", {
|
|
||||||
a: new Date(),
|
|
||||||
be: 2
|
|
||||||
});
|
|
||||||
|
|
||||||
setTimeout(() => {
|
|
||||||
s.set("be", 12);
|
|
||||||
}, 1000)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -0,0 +1,556 @@
|
|||||||
|
import { Emitter } from "./Emitter";
|
||||||
|
import { Logger, LogLabel, LevelLogLabel, colorRadio, StatusLabel } from "./Logger";
|
||||||
|
interface IAppAPIParam {
|
||||||
|
api: {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* API 编号
|
||||||
|
*/
|
||||||
|
nextId: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 请求池
|
||||||
|
* 保存正在等待的 API 请求
|
||||||
|
*/
|
||||||
|
pool: Set<API<IAnyData, IAnyData>>;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface IAnyData {
|
||||||
|
[x:string]: any
|
||||||
|
}
|
||||||
|
|
||||||
|
type IWxRequestOption<O> = WechatMiniprogram.RequestOption<O>;
|
||||||
|
|
||||||
|
type DeepReadonly<T> = {
|
||||||
|
readonly [P in keyof T]: DeepReadonly<T[P]>;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 请求参数设置
|
||||||
|
*/
|
||||||
|
type IParamSetting<T extends IAnyData> = {
|
||||||
|
[P in keyof T]: {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认值
|
||||||
|
*/
|
||||||
|
defaultValue?: T[P],
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ### 数据测试
|
||||||
|
* 1、支持正则表达式测试 \
|
||||||
|
* 2、支持使用 string === string 测试 \
|
||||||
|
* 3、支持使用 number === number 测试 \
|
||||||
|
* 4、支持使用自定义函数测试
|
||||||
|
*/
|
||||||
|
tester?: RegExp | ((data:T[P]) => boolean) | string | number,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ### 预解析函数
|
||||||
|
* 1、此函数用来处理该键值 \
|
||||||
|
* 2、当返回 undefined 时此键值被遗弃 \
|
||||||
|
* 3、返回值时,此键值被覆盖
|
||||||
|
*
|
||||||
|
* @param data 当前给定数据
|
||||||
|
* @param key 当前给定数据键值
|
||||||
|
* @param all 全部输入数据
|
||||||
|
* @returns 解析结果
|
||||||
|
*/
|
||||||
|
parse?: ((data:T[P], key:string, all:DeepReadonly<Partial<T>>) => T[P] | undefined),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否为请求头数据
|
||||||
|
*/
|
||||||
|
isHeader?: boolean,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否为模板
|
||||||
|
*/
|
||||||
|
isTemplate?: boolean
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否可选
|
||||||
|
*/
|
||||||
|
Optional?: boolean
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type SuccessCallbackResult<O extends IAnyData> = WechatMiniprogram.RequestSuccessCallbackResult<O>;
|
||||||
|
type GeneralCallbackResult = WechatMiniprogram.GeneralCallbackResult;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* API 事件
|
||||||
|
*/
|
||||||
|
type IAPIEvent<I extends IAnyData, O extends IAnyData> = {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当数据初始化事件
|
||||||
|
*/
|
||||||
|
initData: Partial<I>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 请求数据解析完成后
|
||||||
|
*/
|
||||||
|
parseRequestData: Partial<I>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 请求发送前
|
||||||
|
*/
|
||||||
|
request: IWxRequestOption<O>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 成功回调
|
||||||
|
*/
|
||||||
|
success: SuccessCallbackResult<O>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 失败回调
|
||||||
|
*/
|
||||||
|
fail: GeneralCallbackResult;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 完成回调
|
||||||
|
*/
|
||||||
|
complete: GeneralCallbackResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 接口调用
|
||||||
|
*/
|
||||||
|
class API<I extends IAnyData, O extends IAnyData> extends Emitter<IAPIEvent<I, O>> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 基础 URL
|
||||||
|
* TODO: 这里可能涉及负载均衡
|
||||||
|
*/
|
||||||
|
public static get baseUrl():string {
|
||||||
|
return "https://blog.mrkbear.com";
|
||||||
|
}
|
||||||
|
|
||||||
|
public static defaultLogLabel:LogLabel = new LogLabel(
|
||||||
|
`API:API`, colorRadio(200, 120, 222)
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logger 使用的标签
|
||||||
|
*/
|
||||||
|
private LogLabel:LogLabel = API.defaultLogLabel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Api 唯一 ID
|
||||||
|
*/
|
||||||
|
public key:string = "API";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* API url
|
||||||
|
*/
|
||||||
|
public url:string = "/";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* API 需要的参数列表
|
||||||
|
*/
|
||||||
|
public params:IParamSetting<I> = {} as any;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* API 需要的数据
|
||||||
|
*/
|
||||||
|
public data?:Partial<I>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 请求数据
|
||||||
|
*/
|
||||||
|
public requestData?:IWxRequestOption<O>;
|
||||||
|
|
||||||
|
//#region wx.request
|
||||||
|
|
||||||
|
public timeout?:number;
|
||||||
|
public method:HTTPMethod = HTTPMethod.GET;
|
||||||
|
public enableHttp2:boolean = false;
|
||||||
|
public enableQuic:boolean = false;
|
||||||
|
public enableCache:boolean = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否自动解析返回的 json
|
||||||
|
* 对应 wx.request 的 dataType
|
||||||
|
*/
|
||||||
|
public jsonParse:boolean = true;
|
||||||
|
|
||||||
|
//#endregion wx.request
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化标签
|
||||||
|
*/
|
||||||
|
public initDebugLabel(key: string) {
|
||||||
|
this.key = key;
|
||||||
|
this.LogLabel = new LogLabel(
|
||||||
|
`API:${ this.key }`, colorRadio(200, 120, 222)
|
||||||
|
);
|
||||||
|
|
||||||
|
// 添加 API 生命周期调试事件
|
||||||
|
this.on("request", (data) => {
|
||||||
|
Logger.logMultiple(
|
||||||
|
[LevelLogLabel.InfoLabel, this.LogLabel, StatusLabel.Pending],
|
||||||
|
`请求发送中: `, data
|
||||||
|
);
|
||||||
|
})
|
||||||
|
|
||||||
|
this.on("success", (data) => {
|
||||||
|
Logger.logMultiple(
|
||||||
|
[LevelLogLabel.InfoLabel, this.LogLabel, StatusLabel.Success],
|
||||||
|
`请求成功: `, data
|
||||||
|
);
|
||||||
|
})
|
||||||
|
|
||||||
|
this.on("fail", (data) => {
|
||||||
|
Logger.logMultiple(
|
||||||
|
[LevelLogLabel.ErrorLabel, this.LogLabel, StatusLabel.Failed],
|
||||||
|
`请求失败: `, data.errMsg
|
||||||
|
);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化数据
|
||||||
|
* 注意:data 是不安全的,请传入数据副本
|
||||||
|
* @param data API需要的全部数据
|
||||||
|
*/
|
||||||
|
public param(data?: Partial<I>):this {
|
||||||
|
|
||||||
|
this.data = data;
|
||||||
|
|
||||||
|
if (this.data === void 0) {
|
||||||
|
Logger.log(`数据初始化异常: 没有输入 [data] 数据!`,
|
||||||
|
LevelLogLabel.ErrorLabel, this.LogLabel);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let key in this.params) {
|
||||||
|
|
||||||
|
let data = this.data[key];
|
||||||
|
let { defaultValue, Optional, tester } = this.params[key];
|
||||||
|
|
||||||
|
// 默认值赋予
|
||||||
|
if (data === void 0 && defaultValue !== void 0) {
|
||||||
|
this.data[key] = defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 数据存在测试
|
||||||
|
if (data === void 0 && !Optional) {
|
||||||
|
Logger.log(`数据校验异常: 数据 [${key}] 是必须的,但是并没有接收到!`,
|
||||||
|
LevelLogLabel.ErrorLabel, this.LogLabel);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 用户自定义测试
|
||||||
|
if (data !== void 0 && tester !== void 0) {
|
||||||
|
let testRes:boolean = false;
|
||||||
|
|
||||||
|
if (tester instanceof RegExp) {
|
||||||
|
testRes = tester.test(data!);
|
||||||
|
} else if (typeof tester === "string" || typeof tester === "number") {
|
||||||
|
testRes = tester === data;
|
||||||
|
} else if (tester instanceof Function) {
|
||||||
|
testRes = tester(data!);
|
||||||
|
} else {
|
||||||
|
Logger.logMultiple(
|
||||||
|
[LevelLogLabel.ErrorLabel, this.LogLabel],
|
||||||
|
`数据校验异常: [${ key }] 参数存在未知类型的 tester:`, tester
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!testRes) {
|
||||||
|
Logger.logMultiple(
|
||||||
|
[LevelLogLabel.ErrorLabel, this.LogLabel],
|
||||||
|
`数据校验异常: [${ key }] 参数数据未通过自定义的 tester:`, data
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 触发数据初始化事件
|
||||||
|
this.emit("initData", this.data);
|
||||||
|
|
||||||
|
// 重置请求数据
|
||||||
|
const requestData:IWxRequestOption<O> = this.requestData = {
|
||||||
|
url: API.baseUrl + this.url,
|
||||||
|
data: {}, header: {},
|
||||||
|
timeout: this.timeout,
|
||||||
|
method: this.method,
|
||||||
|
dataType: this.jsonParse ? "json" : undefined,
|
||||||
|
enableHttp2: this.enableHttp2,
|
||||||
|
enableQuic: this.enableQuic,
|
||||||
|
enableCache: this.enableCache
|
||||||
|
};
|
||||||
|
|
||||||
|
// 数据解析
|
||||||
|
for (let key in this.params) {
|
||||||
|
|
||||||
|
let data = this.data[key];
|
||||||
|
let { parse } = this.params[key];
|
||||||
|
|
||||||
|
// 数据预解析
|
||||||
|
if (parse !== void 0) {
|
||||||
|
this.data[key] = parse(data!, key, this.data as DeepReadonly<Partial<I>>);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 触发数据解析
|
||||||
|
this.emit("parseRequestData", this.data);
|
||||||
|
|
||||||
|
// 数据收集
|
||||||
|
for (let key in this.params) {
|
||||||
|
|
||||||
|
let data = this.data[key];
|
||||||
|
let { isHeader, isTemplate } = this.params[key];
|
||||||
|
|
||||||
|
// 加载数据
|
||||||
|
if (!isTemplate) {
|
||||||
|
if (isHeader) {
|
||||||
|
requestData.header![key] = data;
|
||||||
|
} else {
|
||||||
|
(requestData.data as IAnyData)[key] = data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 请求的唯一标识符
|
||||||
|
*/
|
||||||
|
protected id:number = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将此请求加入到请求池
|
||||||
|
*/
|
||||||
|
protected addPool():this {
|
||||||
|
|
||||||
|
let app = getApp<IAppAPIParam>();
|
||||||
|
if (app.api.pool.has(this as any)) {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取标识符
|
||||||
|
if (!this.id) {
|
||||||
|
this.id = app.api.nextId;
|
||||||
|
app.api.nextId ++;
|
||||||
|
}
|
||||||
|
|
||||||
|
app.api.pool.add(this as any);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从 pool 中移除
|
||||||
|
*/
|
||||||
|
protected removePool():this {
|
||||||
|
let app = getApp<IAppAPIParam>();
|
||||||
|
app.api.pool.delete(this as any);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 寻找相似的请求
|
||||||
|
*/
|
||||||
|
protected findSameAPI():API<IAnyData, IAnyData> | undefined {
|
||||||
|
|
||||||
|
if (this.requestData === void 0) {
|
||||||
|
Logger.log(`搜索相似请求异常: 没有收集 [requestData] 数据!`,
|
||||||
|
LevelLogLabel.ErrorLabel, this.LogLabel);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let app = getApp<IAppAPIParam>();
|
||||||
|
let sameAPI:API<IAnyData, IAnyData> | undefined;
|
||||||
|
|
||||||
|
// 判断 API 是否相似
|
||||||
|
app.api.pool.forEach((api) => {
|
||||||
|
if (api === this) return;
|
||||||
|
if (!api.requestData) return;
|
||||||
|
if (api.requestData!.url !== this.requestData!.url) return;
|
||||||
|
if (api.requestData!.method !== this.requestData!.method) return;
|
||||||
|
sameAPI = api;
|
||||||
|
});
|
||||||
|
|
||||||
|
return sameAPI;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 标记此实例是否已被使用
|
||||||
|
*/
|
||||||
|
private isUsed = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 请求策略
|
||||||
|
*/
|
||||||
|
public policy:RequestPolicy = RequestPolicy.RequestAnyway;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 运行 API
|
||||||
|
*/
|
||||||
|
public request():this {
|
||||||
|
|
||||||
|
if (this.requestData === void 0) {
|
||||||
|
Logger.log(`请求发送异常: 没有收集 [requestData] 数据!`,
|
||||||
|
LevelLogLabel.ErrorLabel, this.LogLabel);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.isUsed) {
|
||||||
|
Logger.log(`请求发送异常: 此实例已经使用过了,请使用新实例操作`,
|
||||||
|
LevelLogLabel.ErrorLabel, this.LogLabel);
|
||||||
|
return this;
|
||||||
|
} else {
|
||||||
|
this.isUsed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 加入请求池
|
||||||
|
this.addPool();
|
||||||
|
|
||||||
|
// 发起请求
|
||||||
|
let request = () => {
|
||||||
|
|
||||||
|
// 触发请求发送事件
|
||||||
|
this.emit("request", this.requestData!)
|
||||||
|
|
||||||
|
wx.request<O>({
|
||||||
|
...this.requestData!,
|
||||||
|
success: (e) => {
|
||||||
|
this.emit("success", e);
|
||||||
|
},
|
||||||
|
fail: (e) => {
|
||||||
|
this.emit("fail", e);
|
||||||
|
},
|
||||||
|
complete: (e) => {
|
||||||
|
this.emit("complete", e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.policy !== RequestPolicy.RequestAnyway) {
|
||||||
|
let lastAPI = this.findSameAPI();
|
||||||
|
|
||||||
|
if (lastAPI) {
|
||||||
|
|
||||||
|
// 被上次请求阻止
|
||||||
|
if (this.policy === RequestPolicy.BlockByLastRequest) {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用上次请求结果
|
||||||
|
if (this.policy === RequestPolicy.useLastRequest) {
|
||||||
|
lastAPI.on("success", (e) => {
|
||||||
|
this.emit("success", e as SuccessCallbackResult<O>);
|
||||||
|
this.emit("complete", {errMsg: e.errMsg});
|
||||||
|
});
|
||||||
|
lastAPI.on("fail", (e) => {
|
||||||
|
this.emit("fail", e);
|
||||||
|
this.emit("complete", {errMsg: e.errMsg});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 等待上次请求
|
||||||
|
if (this.policy === RequestPolicy.waitLastRequest) {
|
||||||
|
lastAPI.on("success", () => request());
|
||||||
|
lastAPI.on("fail", () => request());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
request();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 监听请求完成后,从 pool 中移除,释放内存
|
||||||
|
this.on("complete", () => {
|
||||||
|
this.removePool();
|
||||||
|
})
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 等待结果
|
||||||
|
*/
|
||||||
|
public wait(): Promise<IRespondData<O>>;
|
||||||
|
public wait(callBack?: ICallBack<O>): this;
|
||||||
|
public wait(callBack?: ICallBack<O>): Promise<IRespondData<O>> | this {
|
||||||
|
|
||||||
|
// 存在 callback 使用传统回调
|
||||||
|
if (callBack) {
|
||||||
|
callBack.success && this.on("success", callBack.success);
|
||||||
|
callBack.fail && this.on("fail", callBack.fail);
|
||||||
|
callBack.complete && this.on("complete", callBack.complete);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 不存在 callback 使用 Promise 对象
|
||||||
|
else {
|
||||||
|
return new Promise((r) => {
|
||||||
|
this.on("success", r);
|
||||||
|
this.on("fail", r);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自动响应数据
|
||||||
|
*/
|
||||||
|
type IRespondData<O extends IAnyData> = Partial<IAPIEvent<IAnyData, O>["success"]> & IAPIEvent<IAnyData, O>["fail"];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 回调对象
|
||||||
|
*/
|
||||||
|
interface ICallBack<O extends IAnyData> {
|
||||||
|
success?: (data: IAPIEvent<{}, O>["success"]) => any;
|
||||||
|
fail?: (data: IAPIEvent<{}, O>["fail"]) => any;
|
||||||
|
complete?: (data: IAPIEvent<{}, O>["complete"]) => any;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Request 请求策略
|
||||||
|
* 此策略用于节流
|
||||||
|
*/
|
||||||
|
enum RequestPolicy {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 什么都不管,发就完了
|
||||||
|
*/
|
||||||
|
RequestAnyway = 1,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 如果存在等待中的相似请求
|
||||||
|
* 等待相似请求结束后在发送
|
||||||
|
*/
|
||||||
|
waitLastRequest = 2,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 如果存在等待中的相似请求
|
||||||
|
* 阻止本次请求发送
|
||||||
|
* 将相似请求的结果作为本次请求结果
|
||||||
|
*/
|
||||||
|
useLastRequest = 3,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 如果存在等待中的相似请求
|
||||||
|
* 阻止本次请求发送
|
||||||
|
*/
|
||||||
|
BlockByLastRequest = 4
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HTTP 请求类型
|
||||||
|
*/
|
||||||
|
enum HTTPMethod {
|
||||||
|
OPTIONS = "OPTIONS",
|
||||||
|
GET = "GET",
|
||||||
|
HEAD = "HEAD",
|
||||||
|
POST = "POST",
|
||||||
|
PUT = "PUT",
|
||||||
|
DELETE = "DELETE",
|
||||||
|
TRACE = "TRACE",
|
||||||
|
CONNECT = "CONNECT"
|
||||||
|
}
|
||||||
|
|
||||||
|
export default API;
|
||||||
|
export { API, IParamSetting, IAppAPIParam, ICallBack, HTTPMethod, RequestPolicy }
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
export type EventType = string | symbol;
|
||||||
|
|
||||||
|
// An event handler can take an optional event argument
|
||||||
|
// and should not return a value
|
||||||
|
export type Handler<T = any> = (event: T) => void;
|
||||||
|
|
||||||
|
// An array of all currently registered event handlers for a type
|
||||||
|
export type EventHandlerList<T = any> = Array<Handler<T>>;
|
||||||
|
|
||||||
|
// A map of event types and their corresponding event handlers.
|
||||||
|
export type EventHandlerMap<Events extends Record<EventType, any>> = Map<
|
||||||
|
keyof Events,
|
||||||
|
EventHandlerList<Events[keyof Events]>
|
||||||
|
>;
|
||||||
|
|
||||||
|
export class Emitter<Events extends Record<EventType, any>> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Map of event names to registered handler functions.
|
||||||
|
*/
|
||||||
|
public all: EventHandlerMap<Events>;
|
||||||
|
|
||||||
|
public constructor() {
|
||||||
|
this.all = new Map();
|
||||||
|
}
|
||||||
|
|
||||||
|
public resetAll() {
|
||||||
|
this.all = new Map();
|
||||||
|
}
|
||||||
|
|
||||||
|
public reset<Key extends keyof Events>(type: Key) {
|
||||||
|
this.all!.set(type, [] as EventHandlerList<Events[keyof Events]>);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register an event handler for the given type.
|
||||||
|
* @param {string|symbol} type Type of event to listen for
|
||||||
|
* @param {Function} handler Function to call in response to given event
|
||||||
|
* @memberOf mitt
|
||||||
|
*/
|
||||||
|
public on<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>) {
|
||||||
|
const handlers: Array<Handler<Events[Key]>> | undefined = this.all!.get(type);
|
||||||
|
if (handlers) {
|
||||||
|
handlers.push(handler);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.all!.set(type, [handler] as EventHandlerList<Events[keyof Events]>);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove an event handler for the given type.
|
||||||
|
* If `handler` is omitted, all handlers of the given type are removed.
|
||||||
|
* @param {string|symbol} type Type of event to unregister `handler` from
|
||||||
|
* @param {Function} [handler] Handler function to remove
|
||||||
|
* @memberOf mitt
|
||||||
|
*/
|
||||||
|
public off<Key extends keyof Events>(type: Key, handler?: Handler<Events[Key]>) {
|
||||||
|
const handlers: Array<Handler<Events[Key]>> | undefined = this.all!.get(type);
|
||||||
|
if (handlers) {
|
||||||
|
if (handler) {
|
||||||
|
handlers.splice(handlers.indexOf(handler) >>> 0, 1);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.all!.set(type, []);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoke all handlers for the given type.
|
||||||
|
*
|
||||||
|
* @param {string|symbol} type The event type to invoke
|
||||||
|
* @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler
|
||||||
|
* @memberOf mitt
|
||||||
|
*/
|
||||||
|
emit<Key extends keyof Events>(type: Key, evt: Events[Key]) {
|
||||||
|
let handlers = this.all!.get(type);
|
||||||
|
if (handlers) {
|
||||||
|
(handlers as EventHandlerList<Events[keyof Events]>)
|
||||||
|
.slice()
|
||||||
|
.map((handler) => {
|
||||||
|
handler(evt!);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,119 +0,0 @@
|
|||||||
export type EventType = string | symbol;
|
|
||||||
|
|
||||||
// An event handler can take an optional event argument
|
|
||||||
// and should not return a value
|
|
||||||
export type Handler<T = unknown> = (event: T) => void;
|
|
||||||
export type WildcardHandler<T = Record<string, unknown>> = (
|
|
||||||
type: keyof T,
|
|
||||||
event: T[keyof T]
|
|
||||||
) => void;
|
|
||||||
|
|
||||||
// An array of all currently registered event handlers for a type
|
|
||||||
export type EventHandlerList<T = unknown> = Array<Handler<T>>;
|
|
||||||
export type WildCardEventHandlerList<T = Record<string, unknown>> = Array<WildcardHandler<T>>;
|
|
||||||
|
|
||||||
// A map of event types and their corresponding event handlers.
|
|
||||||
export type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<
|
|
||||||
keyof Events | '*',
|
|
||||||
EventHandlerList<Events[keyof Events]> | WildCardEventHandlerList<Events>
|
|
||||||
>;
|
|
||||||
|
|
||||||
export interface Emitter<Events extends Record<EventType, unknown>> {
|
|
||||||
all: EventHandlerMap<Events>;
|
|
||||||
|
|
||||||
on<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): void;
|
|
||||||
on(type: '*', handler: WildcardHandler<Events>): void;
|
|
||||||
|
|
||||||
off<Key extends keyof Events>(type: Key, handler?: Handler<Events[Key]>): void;
|
|
||||||
off(type: '*', handler: WildcardHandler<Events>): void;
|
|
||||||
|
|
||||||
emit<Key extends keyof Events>(type: Key, event: Events[Key]): void;
|
|
||||||
emit<Key extends keyof Events>(type: undefined extends Events[Key] ? Key : never): void;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Mitt: Tiny (~200b) functional event emitter / pubsub.
|
|
||||||
* @name mitt
|
|
||||||
* @returns {Mitt}
|
|
||||||
*/
|
|
||||||
export default function mitt<Events extends Record<EventType, unknown>>(
|
|
||||||
all?: EventHandlerMap<Events>
|
|
||||||
): Emitter<Events> {
|
|
||||||
type GenericEventHandler =
|
|
||||||
| Handler<Events[keyof Events]>
|
|
||||||
| WildcardHandler<Events>;
|
|
||||||
all = all || new Map();
|
|
||||||
|
|
||||||
return {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A Map of event names to registered handler functions.
|
|
||||||
*/
|
|
||||||
all,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Register an event handler for the given type.
|
|
||||||
* @param {string|symbol} type Type of event to listen for, or `'*'` for all events
|
|
||||||
* @param {Function} handler Function to call in response to given event
|
|
||||||
* @memberOf mitt
|
|
||||||
*/
|
|
||||||
on<Key extends keyof Events>(type: Key, handler: GenericEventHandler) {
|
|
||||||
const handlers: Array<GenericEventHandler> | undefined = all!.get(type);
|
|
||||||
if (handlers) {
|
|
||||||
handlers.push(handler);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
all!.set(type, [handler] as EventHandlerList<Events[keyof Events]>);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove an event handler for the given type.
|
|
||||||
* If `handler` is omitted, all handlers of the given type are removed.
|
|
||||||
* @param {string|symbol} type Type of event to unregister `handler` from, or `'*'`
|
|
||||||
* @param {Function} [handler] Handler function to remove
|
|
||||||
* @memberOf mitt
|
|
||||||
*/
|
|
||||||
off<Key extends keyof Events>(type: Key, handler?: GenericEventHandler) {
|
|
||||||
const handlers: Array<GenericEventHandler> | undefined = all!.get(type);
|
|
||||||
if (handlers) {
|
|
||||||
if (handler) {
|
|
||||||
handlers.splice(handlers.indexOf(handler) >>> 0, 1);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
all!.set(type, []);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Invoke all handlers for the given type.
|
|
||||||
* If present, `'*'` handlers are invoked after type-matched handlers.
|
|
||||||
*
|
|
||||||
* Note: Manually firing '*' handlers is not supported.
|
|
||||||
*
|
|
||||||
* @param {string|symbol} type The event type to invoke
|
|
||||||
* @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler
|
|
||||||
* @memberOf mitt
|
|
||||||
*/
|
|
||||||
emit<Key extends keyof Events>(type: Key, evt?: Events[Key]) {
|
|
||||||
let handlers = all!.get(type);
|
|
||||||
if (handlers) {
|
|
||||||
(handlers as EventHandlerList<Events[keyof Events]>)
|
|
||||||
.slice()
|
|
||||||
.map((handler) => {
|
|
||||||
handler(evt!);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
handlers = all!.get('*');
|
|
||||||
if (handlers) {
|
|
||||||
(handlers as WildCardEventHandlerList<Events>)
|
|
||||||
.slice()
|
|
||||||
.map((handler) => {
|
|
||||||
handler(type, evt!);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -1,227 +0,0 @@
|
|||||||
|
|
||||||
/**
|
|
||||||
* 调试输出样式
|
|
||||||
*/
|
|
||||||
class LogStyle {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 日志文字颜色
|
|
||||||
*/
|
|
||||||
private color:string | undefined;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 日志背景颜色
|
|
||||||
*/
|
|
||||||
private backgroundColor:string | undefined;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 日志文字粗细
|
|
||||||
*/
|
|
||||||
private weight:string | undefined;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 日志文字大小
|
|
||||||
*/
|
|
||||||
private size:string | undefined;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 日志文字字体
|
|
||||||
*/
|
|
||||||
private family:string | undefined;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 日志文字圆角
|
|
||||||
*/
|
|
||||||
private borderRadius:string | undefined;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 日志文字边框
|
|
||||||
*/
|
|
||||||
private border:string | undefined;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 日志文字外边距
|
|
||||||
*/
|
|
||||||
private margin:string | undefined;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 日志文字内边距
|
|
||||||
*/
|
|
||||||
private padding:string | undefined;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置颜色
|
|
||||||
* @param color 日志文字颜色
|
|
||||||
* @param backgroundColor 日志背景颜色
|
|
||||||
*/
|
|
||||||
public setColor(color?:string, backgroundColor?:string):LogStyle {
|
|
||||||
this.color = color ?? this.color;
|
|
||||||
this.backgroundColor = backgroundColor ?? this.backgroundColor;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置边框
|
|
||||||
* @param borderRadius 日志文字圆角
|
|
||||||
* @param border 日志文字边框
|
|
||||||
*/
|
|
||||||
public setBorder(borderRadius?:string, border?:string):LogStyle {
|
|
||||||
this.borderRadius = borderRadius ?? this.borderRadius;
|
|
||||||
this.border = border ?? this.border;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置文字
|
|
||||||
* @param weight 日志文字粗细
|
|
||||||
* @param family 日志文字字体
|
|
||||||
*/
|
|
||||||
public setFont(weight?:string, family?:string):LogStyle {
|
|
||||||
this.weight = weight ?? this.weight;
|
|
||||||
this.family = family ?? this.family;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置文字大小
|
|
||||||
* @param size 日志文字大小
|
|
||||||
*/
|
|
||||||
public setSize(size?:string):LogStyle {
|
|
||||||
this.size = size ?? this.size;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置内边距外边距
|
|
||||||
* @param padding 内边距
|
|
||||||
* @param margin 外边距
|
|
||||||
*/
|
|
||||||
public setBlank(padding?:string, margin?:string):LogStyle {
|
|
||||||
this.padding = padding ?? this.padding;
|
|
||||||
this.margin = margin ?? this.margin;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 字符化转义样式
|
|
||||||
*/
|
|
||||||
public stringify():string {
|
|
||||||
let stringArr:string[] = [];
|
|
||||||
|
|
||||||
this.color && stringArr.push(`color:${ this.color }`);
|
|
||||||
this.backgroundColor && stringArr.push(`background-color:${ this.backgroundColor }`);
|
|
||||||
this.weight && stringArr.push(`font-weight:${ this.weight }`);
|
|
||||||
this.family && stringArr.push(`font-family:${ this.family }`);
|
|
||||||
this.borderRadius && stringArr.push(`border-radius:${ this.borderRadius }`);
|
|
||||||
this.border && stringArr.push(`border:${ this.border }`);
|
|
||||||
this.size && stringArr.push(`font-size:${ this.size }`);
|
|
||||||
this.padding && stringArr.push(`padding:${ this.padding }`);
|
|
||||||
this.margin && stringArr.push(`margin:${ this.margin }`);
|
|
||||||
|
|
||||||
return stringArr.join(";");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 克隆一个新的 LogStyle
|
|
||||||
*/
|
|
||||||
public clone():LogStyle {
|
|
||||||
return new LogStyle()
|
|
||||||
.setColor(this.color, this.backgroundColor)
|
|
||||||
.setBorder(this.borderRadius, this.border)
|
|
||||||
.setFont(this.weight, this.family)
|
|
||||||
.setBlank(this.padding, this.margin)
|
|
||||||
.setSize(this.size)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 日志标签
|
|
||||||
*/
|
|
||||||
class LogLabel {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 关键字
|
|
||||||
* 用于标识这个类别
|
|
||||||
*/
|
|
||||||
public key:string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 文字样式
|
|
||||||
*/
|
|
||||||
public style:LogStyle;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 是否受到过滤器影响
|
|
||||||
*/
|
|
||||||
public checked:boolean;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 是否输出
|
|
||||||
*/
|
|
||||||
public display:boolean;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 是否为附件标签
|
|
||||||
* 例如回车、时间、代码位置
|
|
||||||
*/
|
|
||||||
public attach:boolean;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param key 关键字
|
|
||||||
* @param style 文字样式
|
|
||||||
*/
|
|
||||||
constructor(key:string, style:LogStyle,
|
|
||||||
checked?:boolean, display?:boolean, attach?:boolean) {
|
|
||||||
this.key = key;
|
|
||||||
this.style = style;
|
|
||||||
this.checked = checked ?? true;
|
|
||||||
this.display = display ?? true;
|
|
||||||
this.attach = attach ?? false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获得 Logger 输出使用的内容
|
|
||||||
*/
|
|
||||||
public getLoggerOutput():string {
|
|
||||||
if(!this.display) return "";
|
|
||||||
return `%c${ this.key }`;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获得 Text 输出内容
|
|
||||||
*/
|
|
||||||
public getTextOutput():string {
|
|
||||||
if(!this.display) return "";
|
|
||||||
return `[${ this.key }]`;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获得 style 格式化
|
|
||||||
*/
|
|
||||||
public getStyleOutput():string {
|
|
||||||
if(!this.display) return "";
|
|
||||||
return this.style.stringify();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 校验
|
|
||||||
*/
|
|
||||||
public checking(src:RegExp | string):boolean {
|
|
||||||
|
|
||||||
let pass = false;
|
|
||||||
|
|
||||||
// 关闭校验
|
|
||||||
if(!this.checked) return pass;
|
|
||||||
|
|
||||||
if(src instanceof RegExp) {
|
|
||||||
pass = (src as RegExp).test(this.key)
|
|
||||||
} else {
|
|
||||||
pass = (src as string) === this.key;
|
|
||||||
}
|
|
||||||
|
|
||||||
return pass;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default LogLabel;
|
|
||||||
export {LogLabel, LogStyle}
|
|
||||||
+563
-3
@@ -1,7 +1,228 @@
|
|||||||
import { LOGGER_FILTER, LOGGER_CONSOLE, LOGGER_STYLE } from "./Config";
|
import { LOGGER_FILTER, LOGGER_CONSOLE, LOGGER_STYLE } from "./Config";
|
||||||
import { StackLogLabel } from "./PresetLogLabel";
|
|
||||||
import { LogLabel } from "./LogLabel";
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 调试输出样式
|
||||||
|
*/
|
||||||
|
class LogStyle {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 日志文字颜色
|
||||||
|
*/
|
||||||
|
private color:string | undefined;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 日志背景颜色
|
||||||
|
*/
|
||||||
|
private backgroundColor:string | undefined;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 日志文字粗细
|
||||||
|
*/
|
||||||
|
private weight:string | undefined;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 日志文字大小
|
||||||
|
*/
|
||||||
|
private size:string | undefined;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 日志文字字体
|
||||||
|
*/
|
||||||
|
private family:string | undefined;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 日志文字圆角
|
||||||
|
*/
|
||||||
|
private borderRadius:string | undefined;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 日志文字边框
|
||||||
|
*/
|
||||||
|
private border:string | undefined;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 日志文字外边距
|
||||||
|
*/
|
||||||
|
private margin:string | undefined;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 日志文字内边距
|
||||||
|
*/
|
||||||
|
private padding:string | undefined;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置颜色
|
||||||
|
* @param color 日志文字颜色
|
||||||
|
* @param backgroundColor 日志背景颜色
|
||||||
|
*/
|
||||||
|
public setColor(color?:string, backgroundColor?:string):LogStyle {
|
||||||
|
this.color = color ?? this.color;
|
||||||
|
this.backgroundColor = backgroundColor ?? this.backgroundColor;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置边框
|
||||||
|
* @param borderRadius 日志文字圆角
|
||||||
|
* @param border 日志文字边框
|
||||||
|
*/
|
||||||
|
public setBorder(borderRadius?:string, border?:string):LogStyle {
|
||||||
|
this.borderRadius = borderRadius ?? this.borderRadius;
|
||||||
|
this.border = border ?? this.border;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置文字
|
||||||
|
* @param weight 日志文字粗细
|
||||||
|
* @param family 日志文字字体
|
||||||
|
*/
|
||||||
|
public setFont(weight?:string, family?:string):LogStyle {
|
||||||
|
this.weight = weight ?? this.weight;
|
||||||
|
this.family = family ?? this.family;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置文字大小
|
||||||
|
* @param size 日志文字大小
|
||||||
|
*/
|
||||||
|
public setSize(size?:string):LogStyle {
|
||||||
|
this.size = size ?? this.size;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置内边距外边距
|
||||||
|
* @param padding 内边距
|
||||||
|
* @param margin 外边距
|
||||||
|
*/
|
||||||
|
public setBlank(padding?:string, margin?:string):LogStyle {
|
||||||
|
this.padding = padding ?? this.padding;
|
||||||
|
this.margin = margin ?? this.margin;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字符化转义样式
|
||||||
|
*/
|
||||||
|
public stringify():string {
|
||||||
|
let stringArr:string[] = [];
|
||||||
|
|
||||||
|
this.color && stringArr.push(`color:${ this.color }`);
|
||||||
|
this.backgroundColor && stringArr.push(`background-color:${ this.backgroundColor }`);
|
||||||
|
this.weight && stringArr.push(`font-weight:${ this.weight }`);
|
||||||
|
this.family && stringArr.push(`font-family:${ this.family }`);
|
||||||
|
this.borderRadius && stringArr.push(`border-radius:${ this.borderRadius }`);
|
||||||
|
this.border && stringArr.push(`border:${ this.border }`);
|
||||||
|
this.size && stringArr.push(`font-size:${ this.size }`);
|
||||||
|
this.padding && stringArr.push(`padding:${ this.padding }`);
|
||||||
|
this.margin && stringArr.push(`margin:${ this.margin }`);
|
||||||
|
|
||||||
|
return stringArr.join(";");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 克隆一个新的 LogStyle
|
||||||
|
*/
|
||||||
|
public clone():LogStyle {
|
||||||
|
return new LogStyle()
|
||||||
|
.setColor(this.color, this.backgroundColor)
|
||||||
|
.setBorder(this.borderRadius, this.border)
|
||||||
|
.setFont(this.weight, this.family)
|
||||||
|
.setBlank(this.padding, this.margin)
|
||||||
|
.setSize(this.size)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 日志标签
|
||||||
|
*/
|
||||||
|
class LogLabel {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关键字
|
||||||
|
* 用于标识这个类别
|
||||||
|
*/
|
||||||
|
public key:string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文字样式
|
||||||
|
*/
|
||||||
|
public style:LogStyle;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否受到过滤器影响
|
||||||
|
*/
|
||||||
|
public checked:boolean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否输出
|
||||||
|
*/
|
||||||
|
public display:boolean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否为附件标签
|
||||||
|
* 例如回车、时间、代码位置
|
||||||
|
*/
|
||||||
|
public attach:boolean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key 关键字
|
||||||
|
* @param style 文字样式
|
||||||
|
*/
|
||||||
|
constructor(key:string, style:LogStyle,
|
||||||
|
checked?:boolean, display?:boolean, attach?:boolean) {
|
||||||
|
this.key = key;
|
||||||
|
this.style = style;
|
||||||
|
this.checked = checked ?? true;
|
||||||
|
this.display = display ?? true;
|
||||||
|
this.attach = attach ?? false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获得 Logger 输出使用的内容
|
||||||
|
*/
|
||||||
|
public getLoggerOutput():string {
|
||||||
|
if(!this.display) return "";
|
||||||
|
return `%c${ this.key }`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获得 Text 输出内容
|
||||||
|
*/
|
||||||
|
public getTextOutput():string {
|
||||||
|
if(!this.display) return "";
|
||||||
|
return `[${ this.key }]`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获得 style 格式化
|
||||||
|
*/
|
||||||
|
public getStyleOutput():string {
|
||||||
|
if(!this.display) return "";
|
||||||
|
return this.style.stringify();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 校验
|
||||||
|
*/
|
||||||
|
public checking(src:RegExp | string):boolean {
|
||||||
|
|
||||||
|
let pass = false;
|
||||||
|
|
||||||
|
// 关闭校验
|
||||||
|
if(!this.checked) return pass;
|
||||||
|
|
||||||
|
if(src instanceof RegExp) {
|
||||||
|
pass = (src as RegExp).test(this.key)
|
||||||
|
} else {
|
||||||
|
pass = (src as string) === this.key;
|
||||||
|
}
|
||||||
|
|
||||||
|
return pass;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 多重内容捆绑
|
* 多重内容捆绑
|
||||||
@@ -231,5 +452,344 @@ class Logger {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 栈信息
|
||||||
|
*/
|
||||||
|
class StackInfo {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 函数名
|
||||||
|
*/
|
||||||
|
public functionName:string | undefined;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件名
|
||||||
|
*/
|
||||||
|
public fileName:string | undefined;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件路径
|
||||||
|
*/
|
||||||
|
public url:string | undefined;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件名和行号
|
||||||
|
*/
|
||||||
|
public fileNameLine: string | undefined;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置信息
|
||||||
|
* @param functionName 函数名
|
||||||
|
* @param fileName 文件名
|
||||||
|
* @param url 文件路径
|
||||||
|
*/
|
||||||
|
public setInfo(functionName:string, fileNameLine:string, url:string):StackInfo {
|
||||||
|
this.functionName = functionName;
|
||||||
|
this.fileNameLine = fileNameLine;
|
||||||
|
this.url = url;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 计算文件名
|
||||||
|
*/
|
||||||
|
public calcFileName():string | undefined {
|
||||||
|
|
||||||
|
let replaceToTs = this.fileNameLine?.replace(".js", ".ts");
|
||||||
|
let matched = replaceToTs?.match(/^(.+\.(js|ts)):\d+:\d+$/);
|
||||||
|
|
||||||
|
return matched ? matched[1] : undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 计算路径名
|
||||||
|
*/
|
||||||
|
public calcPathName():string | undefined {
|
||||||
|
|
||||||
|
let replaceToTs = this.url?.replace(".js", ".ts");
|
||||||
|
let matched = replaceToTs?.match(/^https?:\/\/(\d+\.){3}\d+:\d+\/(.+):\d+:\d+$/);
|
||||||
|
|
||||||
|
return matched ? matched[2] : undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取函数调用栈列表
|
||||||
|
*/
|
||||||
|
public static getCallStack():StackInfo[] {
|
||||||
|
|
||||||
|
// 获取堆栈信息
|
||||||
|
let stack:string | undefined = new Error().stack;
|
||||||
|
|
||||||
|
if (stack === void 0) return [];
|
||||||
|
|
||||||
|
// 去除 Error
|
||||||
|
stack = stack.replace(/^(Error)\s/, "");
|
||||||
|
|
||||||
|
// 获取堆栈信息
|
||||||
|
let stackList:string[] = stack.split(/\n/);
|
||||||
|
|
||||||
|
let callStack:StackInfo[] = [];
|
||||||
|
|
||||||
|
for(let i = 0; i < stackList.length; i++) {
|
||||||
|
|
||||||
|
let matcher = stackList[i].match(/^\s+at\s+(.+)\s(\(.+\))/);
|
||||||
|
if (matcher === null || matcher.length < 3) continue;
|
||||||
|
|
||||||
|
let fileName = matcher[2].match(/.+\/(.+\..+:\d+:\d+)\)/);
|
||||||
|
if (fileName === null || matcher.length < 2) continue;
|
||||||
|
|
||||||
|
callStack.push(new StackInfo().setInfo(
|
||||||
|
matcher[1], fileName[1], matcher[2]?.replace(/(\(|\))/g, "")
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
// console.log(callStack);
|
||||||
|
|
||||||
|
return callStack;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 排除的
|
||||||
|
*/
|
||||||
|
public static readonly excludeFile:RegExp = /^.*(\\|\/)core(\\|\/)(.*Log.*).js:\d+:\d+/;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取第一个调用栈
|
||||||
|
*/
|
||||||
|
public static getFirstStack():StackInfo | undefined {
|
||||||
|
|
||||||
|
let callStack = this.getCallStack();
|
||||||
|
|
||||||
|
for(let i = 0; i < callStack.length; i++) {
|
||||||
|
|
||||||
|
if(!callStack[i].url) continue;
|
||||||
|
|
||||||
|
if(!StackInfo.excludeFile.test(callStack[i].url ?? "")) {
|
||||||
|
return callStack[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 内部预定义的 LogLabel
|
||||||
|
*/
|
||||||
|
class StackLogLabel {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 堆栈路径样式
|
||||||
|
*/
|
||||||
|
public static readonly normalStyle:LogStyle = new LogStyle()
|
||||||
|
.setColor("#979797").setBorder("4px", "1px solid #979797").setBlank("0 5px");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 一个回车
|
||||||
|
*/
|
||||||
|
public static readonly blankLabel = new LogLabel("\n\r",
|
||||||
|
new LogStyle(), false, true, true);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 包含文件名和行号的 label
|
||||||
|
*/
|
||||||
|
public static get fileNameLabel():LogLabel {
|
||||||
|
|
||||||
|
// 获得调用堆栈
|
||||||
|
let stack = StackInfo.getFirstStack();
|
||||||
|
|
||||||
|
return new LogLabel(stack?.calcFileName() ?? "Unknown file name",
|
||||||
|
StackLogLabel.normalStyle, false, true, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 包含 URL 链接的 label
|
||||||
|
*/
|
||||||
|
public static get urlLabel():LogLabel {
|
||||||
|
|
||||||
|
// 获得调用堆栈
|
||||||
|
let stack = StackInfo.getFirstStack();
|
||||||
|
|
||||||
|
return new LogLabel(stack?.calcPathName() ?? "Unknown url",
|
||||||
|
StackLogLabel.normalStyle, false, true, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 仅仅用来 filter 的 URL 链接的 label
|
||||||
|
*/
|
||||||
|
public static get filterUrlLabel():LogLabel {
|
||||||
|
|
||||||
|
// 获得调用堆栈
|
||||||
|
let stack = StackInfo.getFirstStack();
|
||||||
|
|
||||||
|
return new LogLabel(stack?.calcPathName() ?? "Unknown url",
|
||||||
|
StackLogLabel.normalStyle, true, false, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成圆角颜色标签样式
|
||||||
|
*/
|
||||||
|
const normalLevelStyleGen = (color:string):LogStyle => {
|
||||||
|
return new LogStyle().setBorder("4px", `1px solid ${color}`)
|
||||||
|
.setColor(color).setBlank("0 5px");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 调试等级标签
|
||||||
|
*/
|
||||||
|
class LevelLogLabel {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 致命
|
||||||
|
*/
|
||||||
|
static readonly FatalLabel = new LogLabel(
|
||||||
|
"FATAL", normalLevelStyleGen("#FF00CC")
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 错误
|
||||||
|
*/
|
||||||
|
static readonly ErrorLabel = new LogLabel(
|
||||||
|
"ERROR", normalLevelStyleGen("#FF0000")
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 警告
|
||||||
|
*/
|
||||||
|
static readonly WarnLabel = new LogLabel(
|
||||||
|
"WARN", normalLevelStyleGen("#FF9900")
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 消息
|
||||||
|
*/
|
||||||
|
static readonly InfoLabel = new LogLabel(
|
||||||
|
"INFO", normalLevelStyleGen("#99FF00")
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 调试
|
||||||
|
*/
|
||||||
|
static readonly DebugLabel = new LogLabel(
|
||||||
|
"DEBUG", normalLevelStyleGen("#00FF99")
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 追踪
|
||||||
|
*/
|
||||||
|
static readonly TraceLabel = new LogLabel(
|
||||||
|
"TRACE", normalLevelStyleGen("#00CCFF")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成圆角颜色标签样式
|
||||||
|
*/
|
||||||
|
const normalLifeStyleGen = (r:number, g:number, b:number):LogStyle => {
|
||||||
|
return new LogStyle().setBorder("4px", `1px solid rgb(${ r }, ${ g }, ${ b })`)
|
||||||
|
.setColor(`rgb(${ r }, ${ g }, ${ b })`, `rgba(${ r }, ${ g }, ${ b }, .1)`)
|
||||||
|
.setBlank("0 5px");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生命周期标签
|
||||||
|
*/
|
||||||
|
class LifeCycleLogLabel {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 小程序加载时
|
||||||
|
*/
|
||||||
|
static readonly OnLaunchLabel = new LogLabel(
|
||||||
|
"onLaunch", normalLifeStyleGen(160, 32, 240)
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生命周期函数--监听页面加载
|
||||||
|
*/
|
||||||
|
static readonly OnLoadLabel = new LogLabel(
|
||||||
|
"onLoad", normalLifeStyleGen(255, 140, 105)
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生命周期函数--监听页面初次渲染完成
|
||||||
|
*/
|
||||||
|
static readonly OnReadyLabel = new LogLabel(
|
||||||
|
"onReady", normalLifeStyleGen(255, 127, 36)
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生命周期函数--监听页面显示
|
||||||
|
*/
|
||||||
|
static readonly OnShowLabel = new LogLabel(
|
||||||
|
"onShow", normalLifeStyleGen(255, 215, 0)
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生命周期函数--监听页面隐藏
|
||||||
|
*/
|
||||||
|
static readonly OnHideLabel = new LogLabel(
|
||||||
|
"onHide", normalLifeStyleGen(173, 255, 47)
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生命周期函数--监听页面卸载
|
||||||
|
*/
|
||||||
|
static readonly OnUnloadLabel = new LogLabel(
|
||||||
|
"onUnload", normalLifeStyleGen(127, 255, 212)
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 页面相关事件处理函数--监听用户下拉动作
|
||||||
|
*/
|
||||||
|
static readonly OnPullDownRefreshLabel = new LogLabel(
|
||||||
|
"onPullDownRefresh", normalLifeStyleGen(0, 191, 255)
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 页面上拉触底事件的处理函数
|
||||||
|
*/
|
||||||
|
static readonly OnReachBottomLabel = new LogLabel(
|
||||||
|
"onReachBottom", normalLifeStyleGen(84, 255, 159)
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户点击右上角分享
|
||||||
|
*/
|
||||||
|
static readonly OnShareAppMessageLabel = new LogLabel(
|
||||||
|
"onShareAppMessage", normalLifeStyleGen(147, 112, 219)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
class StatusLabel {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 等待
|
||||||
|
*/
|
||||||
|
static readonly Pending = new LogLabel(
|
||||||
|
"◉", new LogStyle().setBlank("0 2px").setBorder("1000px", "1px solid lightblue").setColor("lightblue")
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 成功
|
||||||
|
*/
|
||||||
|
static readonly Success = new LogLabel(
|
||||||
|
"√", new LogStyle().setBlank("0 4px").setBorder("1000px", "1px solid lightgreen").setColor("lightgreen")
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 失败
|
||||||
|
*/
|
||||||
|
static readonly Failed = new LogLabel(
|
||||||
|
"x", new LogStyle().setBlank("0 4px").setBorder("1000px", "1px solid red").setColor("red")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const NormalStyle = StackLogLabel.normalStyle;
|
||||||
|
|
||||||
export default Logger;
|
export default Logger;
|
||||||
export { Logger };
|
export {
|
||||||
|
Logger, LogLabel, LevelLogLabel, StackLogLabel, LifeCycleLogLabel, LogStyle,
|
||||||
|
StackInfo, StatusLabel, NormalStyle, normalLifeStyleGen as colorRadio
|
||||||
|
};
|
||||||
+173
-139
@@ -1,7 +1,5 @@
|
|||||||
import mitt, { Emitter, EventHandlerMap, EventType, Handler, WildcardHandler } from "./EventEmitter";
|
import { Emitter } from "./Emitter";
|
||||||
import { LogLabel, LogStyle } from "./LogLabel";
|
import { Logger, LogLabel, colorRadio, LevelLogLabel } from "./Logger";
|
||||||
import { Logger } from "./Logger";
|
|
||||||
import { LevelLogLabel } from "./PresetLogLabel";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 自定义对象类型
|
* 自定义对象类型
|
||||||
@@ -52,6 +50,149 @@ type Depends<M extends Manager<AnyWXContext>> = {
|
|||||||
[x:string]: Modular<M, Depends<M>>;
|
[x:string]: Modular<M, Depends<M>>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 微信继承的函数
|
||||||
|
*/
|
||||||
|
class WXInstanceMethods<
|
||||||
|
E extends IAnyTypeObject = IAnyTypeObject,
|
||||||
|
W extends AnyWXContext = AnyWXContext
|
||||||
|
>
|
||||||
|
extends Emitter<E>
|
||||||
|
implements InstanceMethods<W["data"]> {
|
||||||
|
|
||||||
|
public superContext: W;
|
||||||
|
|
||||||
|
public constructor(context: W) {
|
||||||
|
super();
|
||||||
|
this.superContext = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
public setData(data: Partial<W["data"]> & WechatMiniprogram.IAnyObject, callback?: () => void): void {
|
||||||
|
return this.superContext.setData(data, callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
public hasBehavior(behavior: string): void {
|
||||||
|
return this.superContext.hasBehavior(behavior);
|
||||||
|
}
|
||||||
|
|
||||||
|
public triggerEvent<DetailType = any>(name: string, detail?: DetailType, options?: WechatMiniprogram.Component.TriggerEventOption): void {
|
||||||
|
return this.superContext.triggerEvent(name, detail, options)
|
||||||
|
}
|
||||||
|
|
||||||
|
public createSelectorQuery(): WechatMiniprogram.SelectorQuery {
|
||||||
|
return this.superContext.createSelectorQuery();
|
||||||
|
}
|
||||||
|
|
||||||
|
public createIntersectionObserver(options: WechatMiniprogram.CreateIntersectionObserverOption): WechatMiniprogram.IntersectionObserver {
|
||||||
|
return this.superContext.createIntersectionObserver(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
public selectComponent(selector: string): WechatMiniprogram.Component.TrivialInstance {
|
||||||
|
return this.superContext.selectComponent(selector);
|
||||||
|
}
|
||||||
|
|
||||||
|
public selectAllComponents(selector: string): WechatMiniprogram.Component.TrivialInstance[] {
|
||||||
|
return this.superContext.selectAllComponents(selector);
|
||||||
|
}
|
||||||
|
|
||||||
|
public selectOwnerComponent(): WechatMiniprogram.Component.TrivialInstance {
|
||||||
|
return this.superContext.selectOwnerComponent();
|
||||||
|
}
|
||||||
|
|
||||||
|
public getRelationNodes(relationKey: string): WechatMiniprogram.Component.TrivialInstance[] {
|
||||||
|
return this.superContext.getRelationNodes(relationKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
public groupSetData(callback?: () => void): void {
|
||||||
|
return this.superContext.groupSetData(callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
public getTabBar(): WechatMiniprogram.Component.TrivialInstance {
|
||||||
|
return this.superContext.getTabBar();
|
||||||
|
}
|
||||||
|
|
||||||
|
public getPageId(): string {
|
||||||
|
return this.superContext.getPageId();
|
||||||
|
}
|
||||||
|
|
||||||
|
public animate(selector: string, keyFrames: WechatMiniprogram.Component.KeyFrame[], duration: number, callback?: () => void): void;
|
||||||
|
public animate(selector: string, keyFrames: WechatMiniprogram.Component.ScrollTimelineKeyframe[], duration: number,
|
||||||
|
scrollTimeline: WechatMiniprogram.Component.ScrollTimelineOption): void;
|
||||||
|
public animate(selector: any, keyFrames: any, duration: any, scrollTimeline?: any): void {
|
||||||
|
return this.superContext.animate(selector, keyFrames, duration, scrollTimeline);
|
||||||
|
}
|
||||||
|
|
||||||
|
public clearAnimation(selector: string, callback: () => void): void;
|
||||||
|
public clearAnimation(selector: string, options?: WechatMiniprogram.Component.ClearAnimationOptions, callback?: () => void): void;
|
||||||
|
public clearAnimation(selector: any, options?: any, callback?: any): void {
|
||||||
|
return this.superContext.clearAnimation(selector, options, callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
public getOpenerEventChannel(): WechatMiniprogram.EventChannel {
|
||||||
|
return this.superContext.getOpenerEventChannel();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 微信继承的属性
|
||||||
|
*/
|
||||||
|
class WXInstanceProperties<
|
||||||
|
E extends IAnyTypeObject = IAnyTypeObject,
|
||||||
|
W extends AnyWXContext = AnyWXContext
|
||||||
|
>
|
||||||
|
extends WXInstanceMethods<E, W>
|
||||||
|
implements InstanceProperties {
|
||||||
|
|
||||||
|
public override superContext: W;
|
||||||
|
|
||||||
|
constructor(context: W) {
|
||||||
|
super(context);
|
||||||
|
this.superContext = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
public get is(): string { return this.superContext.is };
|
||||||
|
|
||||||
|
public get route(): string { return this.superContext.route };
|
||||||
|
|
||||||
|
public get options(): Record<string, string | undefined> { return this.superContext.options };
|
||||||
|
}
|
||||||
|
|
||||||
|
class WXILifetime<
|
||||||
|
E extends IAnyTypeObject = IAnyTypeObject,
|
||||||
|
W extends AnyWXContext = AnyWXContext
|
||||||
|
>
|
||||||
|
extends WXInstanceProperties<E, W>
|
||||||
|
implements ILifetime {
|
||||||
|
|
||||||
|
public onLoad(query: Record<string, string | undefined>): void | Promise<void> {};
|
||||||
|
|
||||||
|
public onShow(): void | Promise<void> {};
|
||||||
|
|
||||||
|
public onReady(): void | Promise<void> {};
|
||||||
|
|
||||||
|
public onHide(): void | Promise<void> {};
|
||||||
|
|
||||||
|
public onUnload(): void | Promise<void> {};
|
||||||
|
|
||||||
|
public onPullDownRefresh(): void | Promise<void> {};
|
||||||
|
|
||||||
|
public onReachBottom(): void | Promise<void> {};
|
||||||
|
|
||||||
|
public onShareAppMessage(options: WechatMiniprogram.Page.IShareAppMessageOption): void | WechatMiniprogram.Page.ICustomShareContent {};
|
||||||
|
|
||||||
|
public onShareTimeline(): void | WechatMiniprogram.Page.ICustomTimelineContent {};
|
||||||
|
|
||||||
|
public onPageScroll(options: WechatMiniprogram.Page.IPageScrollOption): void | Promise<void> {};
|
||||||
|
|
||||||
|
public onTabItemTap(options: WechatMiniprogram.Page.ITabItemTapOption): void | Promise<void> {};
|
||||||
|
|
||||||
|
public onResize(options: WechatMiniprogram.Page.IResizeOption): void | Promise<void> {};
|
||||||
|
|
||||||
|
public onAddToFavorites(options: WechatMiniprogram.Page.IAddToFavoritesOption): WechatMiniprogram.Page.IAddToFavoritesContent {
|
||||||
|
return {};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 页面模组
|
* 页面模组
|
||||||
* @template M 所属 Manager
|
* @template M 所属 Manager
|
||||||
@@ -62,9 +203,11 @@ type Depends<M extends Manager<AnyWXContext>> = {
|
|||||||
class Modular<
|
class Modular<
|
||||||
M extends Manager<AnyWXContext> = Manager<AnyWXContext>,
|
M extends Manager<AnyWXContext> = Manager<AnyWXContext>,
|
||||||
DEP extends Depends<M> = Depends<M>,
|
DEP extends Depends<M> = Depends<M>,
|
||||||
E extends Record<EventType, unknown> = Record<EventType, unknown>,
|
E extends IAnyTypeObject = IAnyTypeObject,
|
||||||
TD extends IAnyTypeObject = IAnyTypeObject>
|
TD extends IAnyTypeObject = IAnyTypeObject
|
||||||
implements WXContext<TD, IAnyTypeObject>, Emitter<E> {
|
>
|
||||||
|
extends WXILifetime<E, M["context"]>
|
||||||
|
implements WXContext<TD, IAnyTypeObject> {
|
||||||
|
|
||||||
// [x:string]: any;
|
// [x:string]: any;
|
||||||
|
|
||||||
@@ -96,7 +239,7 @@ implements WXContext<TD, IAnyTypeObject>, Emitter<E> {
|
|||||||
public functionList:Set<string>;
|
public functionList:Set<string>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 函数使用的参数列表
|
* 模组使用的参数列表
|
||||||
*/
|
*/
|
||||||
public paramList:Set<string>;
|
public paramList:Set<string>;
|
||||||
|
|
||||||
@@ -105,13 +248,6 @@ implements WXContext<TD, IAnyTypeObject>, Emitter<E> {
|
|||||||
*/
|
*/
|
||||||
public nameSpace:string;
|
public nameSpace:string;
|
||||||
|
|
||||||
// 映射主上下文属性
|
|
||||||
public get is():string { return this.context.is };
|
|
||||||
public get route():string { return this.context.route };
|
|
||||||
public get options():Record<string, string | undefined> {
|
|
||||||
return this.context.options;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 一旦被类被构造,整个页面的全部申明周期将
|
* 一旦被类被构造,整个页面的全部申明周期将
|
||||||
* 由该类实例控制,此后请勿修改任何生命周期函数
|
* 由该类实例控制,此后请勿修改任何生命周期函数
|
||||||
@@ -121,6 +257,8 @@ implements WXContext<TD, IAnyTypeObject>, Emitter<E> {
|
|||||||
*/
|
*/
|
||||||
public constructor(manager:M, nameSpace:string, depend?: DEP) {
|
public constructor(manager:M, nameSpace:string, depend?: DEP) {
|
||||||
|
|
||||||
|
super(manager.context);
|
||||||
|
|
||||||
// 保存微信上下文
|
// 保存微信上下文
|
||||||
this.manager = manager;
|
this.manager = manager;
|
||||||
|
|
||||||
@@ -131,37 +269,9 @@ implements WXContext<TD, IAnyTypeObject>, Emitter<E> {
|
|||||||
this.functionList = new Set<string>();
|
this.functionList = new Set<string>();
|
||||||
this.paramList = new Set<string>();
|
this.paramList = new Set<string>();
|
||||||
this.nameSpace = nameSpace;
|
this.nameSpace = nameSpace;
|
||||||
|
|
||||||
this.emitter = mitt<E>();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public override setData(data:Partial<TD>, callback?: () => void):void {
|
||||||
* 内部事件控制器
|
|
||||||
*/
|
|
||||||
private emitter:Emitter<E>;
|
|
||||||
|
|
||||||
public get all():EventHandlerMap<E> { return this.emitter.all };
|
|
||||||
|
|
||||||
on<Key extends keyof E>(type: Key, handler: Handler<E[Key]>): void;
|
|
||||||
on(type: "*", handler: WildcardHandler<E>): void;
|
|
||||||
on(type: any, handler: any): void {
|
|
||||||
return this.emitter.on(type, handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
off<Key extends keyof E>(type: Key, handler?: Handler<E[Key]>): void;
|
|
||||||
off(type: "*", handler: WildcardHandler<E>): void;
|
|
||||||
off(type: any, handler?: any): void {
|
|
||||||
return this.emitter.off(type, handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
emit<Key extends keyof E>(type: Key, event: E[Key]): void;
|
|
||||||
emit<Key extends keyof E>(type: undefined extends E[Key] ? Key : never): void;
|
|
||||||
emit(type: any, event?: any): void {
|
|
||||||
return this.emitter.emit(type, event);
|
|
||||||
}
|
|
||||||
|
|
||||||
public setData(data:Partial<TD>, callback?: () => void):void {
|
|
||||||
|
|
||||||
if(this.data === void 0) {
|
if(this.data === void 0) {
|
||||||
this.data = {} as TD;
|
this.data = {} as TD;
|
||||||
@@ -189,76 +299,6 @@ implements WXContext<TD, IAnyTypeObject>, Emitter<E> {
|
|||||||
(this.context as IAnyTypeObject)
|
(this.context as IAnyTypeObject)
|
||||||
[`${ this.nameSpace }$${ name }`] = fn.bind(this);
|
[`${ this.nameSpace }$${ name }`] = fn.bind(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
//#region 映射微信的继承函数
|
|
||||||
|
|
||||||
public hasBehavior(behavior: string): void {
|
|
||||||
return this.context.hasBehavior(behavior);
|
|
||||||
}
|
|
||||||
|
|
||||||
public triggerEvent<DetailType>(name: string, detail?: DetailType,
|
|
||||||
options?: WechatMiniprogram.Component.TriggerEventOption): void {
|
|
||||||
return this.context.triggerEvent<DetailType>(name, detail, options);
|
|
||||||
}
|
|
||||||
|
|
||||||
public createSelectorQuery(): WechatMiniprogram.SelectorQuery {
|
|
||||||
return this.context.createSelectorQuery();
|
|
||||||
}
|
|
||||||
|
|
||||||
public createIntersectionObserver(options: WechatMiniprogram.CreateIntersectionObserverOption):
|
|
||||||
WechatMiniprogram.IntersectionObserver {
|
|
||||||
return this.context.createIntersectionObserver(options);
|
|
||||||
}
|
|
||||||
|
|
||||||
public selectComponent(selector: string): WechatMiniprogram.Component.TrivialInstance {
|
|
||||||
return this.context.selectComponent(selector);
|
|
||||||
}
|
|
||||||
|
|
||||||
public selectAllComponents(selector: string): WechatMiniprogram.Component.TrivialInstance[] {
|
|
||||||
return this.context.selectAllComponents(selector);
|
|
||||||
}
|
|
||||||
|
|
||||||
public selectOwnerComponent(): WechatMiniprogram.Component.TrivialInstance {
|
|
||||||
return this.context.selectOwnerComponent();
|
|
||||||
}
|
|
||||||
|
|
||||||
public getRelationNodes(relationKey: string): WechatMiniprogram.Component.TrivialInstance[] {
|
|
||||||
return this.context.getRelationNodes(relationKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
public groupSetData(callback?: () => void): void {
|
|
||||||
return this.context.groupSetData(callback);
|
|
||||||
}
|
|
||||||
|
|
||||||
public getTabBar(): WechatMiniprogram.Component.TrivialInstance {
|
|
||||||
return this.context.getTabBar();
|
|
||||||
}
|
|
||||||
|
|
||||||
public getPageId(): string {
|
|
||||||
return this.context.getPageId();
|
|
||||||
}
|
|
||||||
|
|
||||||
public animate(selector: string, keyFrames: WechatMiniprogram.Component.KeyFrame[],
|
|
||||||
duration: number, callback?: () => void): void;
|
|
||||||
public animate(selector: string, keyFrames: WechatMiniprogram.Component.ScrollTimelineKeyframe[],
|
|
||||||
duration: number, scrollTimeline: WechatMiniprogram.Component.ScrollTimelineOption): void;
|
|
||||||
public animate(selector: any, keyFrames: any, duration: any, scrollTimeline?: any): void {
|
|
||||||
return this.context.animate(selector, keyFrames, duration, scrollTimeline);
|
|
||||||
}
|
|
||||||
|
|
||||||
public clearAnimation(selector: string, callback: () => void): void;
|
|
||||||
public clearAnimation(selector: string, options?: WechatMiniprogram.Component.ClearAnimationOptions,
|
|
||||||
callback?: () => void): void;
|
|
||||||
public clearAnimation(selector: any, options?: any, callback?: any): void {
|
|
||||||
return this.context.clearAnimation(selector, options, callback);
|
|
||||||
}
|
|
||||||
|
|
||||||
public getOpenerEventChannel(): WechatMiniprogram.EventChannel {
|
|
||||||
return this.context.getOpenerEventChannel();
|
|
||||||
}
|
|
||||||
|
|
||||||
//#endregion
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -271,19 +311,9 @@ class Manager<WXC extends AnyWXContext = AnyWXContext> {
|
|||||||
* 微信生命周期
|
* 微信生命周期
|
||||||
*/
|
*/
|
||||||
static readonly WxLifeCycle:(keyof ILifetime)[] = [
|
static readonly WxLifeCycle:(keyof ILifetime)[] = [
|
||||||
"onShow",
|
"onShow", "onReady", "onHide", "onUnload", "onPullDownRefresh", "onReachBottom",
|
||||||
"onReady",
|
"onShareAppMessage", "onShareTimeline","onAddToFavorites","onPageScroll", "onResize", "onTabItemTap"
|
||||||
"onHide",
|
];
|
||||||
"onUnload",
|
|
||||||
"onPullDownRefresh",
|
|
||||||
"onReachBottom",
|
|
||||||
"onShareAppMessage",
|
|
||||||
"onShareTimeline",
|
|
||||||
"onAddToFavorites",
|
|
||||||
"onPageScroll",
|
|
||||||
"onResize",
|
|
||||||
"onTabItemTap"
|
|
||||||
]
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 保存页面上下文
|
* 保存页面上下文
|
||||||
@@ -315,9 +345,10 @@ class Manager<WXC extends AnyWXContext = AnyWXContext> {
|
|||||||
* @param depend 模块依赖
|
* @param depend 模块依赖
|
||||||
* @returns 模块实例
|
* @returns 模块实例
|
||||||
*/
|
*/
|
||||||
public addModule<DEP extends Depends<this>, M extends Modular<this, DEP>>
|
public addModule<DEP extends Depends<this>, M extends Modular<this, DEP>> (
|
||||||
(mode: new (manager:Manager<WXC>, nameSpace:string, depend?:DEP) => M,
|
mode: new (manager:this, nameSpace:string, depend?:DEP) => M,
|
||||||
nameSpace:string, depend?:DEP):M {
|
nameSpace:string, depend?:DEP
|
||||||
|
):M {
|
||||||
let mod = new mode(this, nameSpace, depend);
|
let mod = new mode(this, nameSpace, depend);
|
||||||
this.modules.push(mod);
|
this.modules.push(mod);
|
||||||
return mod;
|
return mod;
|
||||||
@@ -327,14 +358,14 @@ class Manager<WXC extends AnyWXContext = AnyWXContext> {
|
|||||||
* 创建指定生命周期的钩子
|
* 创建指定生命周期的钩子
|
||||||
* @param key 生命周期键值
|
* @param key 生命周期键值
|
||||||
*/
|
*/
|
||||||
public creatHooks(key:keyof ILifetime):(...arg: any[]) => Promise<any> {
|
public creatHooks<Key extends keyof ILifetime>(key: Key): ILifetime[Key] {
|
||||||
return async (...arg: any[]) => {
|
let hook = (async (...arg: any[]) => {
|
||||||
|
|
||||||
let hooks:Promise<any>[] = [];
|
let hooks:Promise<any>[] = [];
|
||||||
|
|
||||||
for(let i = 0; i < this.modules.length; i++) {
|
for(let i = 0; i < this.modules.length; i++) {
|
||||||
|
|
||||||
let fn:Function = (this.modules[i] as IAnyTypeObject)[key];
|
let fn:Function = this.modules[i][key];
|
||||||
|
|
||||||
if(fn === void 0) continue;
|
if(fn === void 0) continue;
|
||||||
let res: Promise<any> | any = fn.apply(this.modules[i], arg);
|
let res: Promise<any> | any = fn.apply(this.modules[i], arg);
|
||||||
@@ -367,7 +398,10 @@ class Manager<WXC extends AnyWXContext = AnyWXContext> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return Promise.all(hooks);
|
return Promise.all(hooks);
|
||||||
}
|
});
|
||||||
|
|
||||||
|
// TODO: 此处为,关键位置,容易出错,请再次检查
|
||||||
|
return (hook as any);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -410,7 +444,7 @@ class Manager<WXC extends AnyWXContext = AnyWXContext> {
|
|||||||
this.context.setData(this.context.data);
|
this.context.setData(this.context.data);
|
||||||
|
|
||||||
// 调用全部模块的 onLoad 周期
|
// 调用全部模块的 onLoad 周期
|
||||||
let res = this.creatHooks("onLoad")(query as any);
|
let res = this.creatHooks("onLoad")(query);
|
||||||
|
|
||||||
// 打印每个模块的键值对使用情况
|
// 打印每个模块的键值对使用情况
|
||||||
for(let i = 0; i < this.modules.length; i++) {
|
for(let i = 0; i < this.modules.length; i++) {
|
||||||
@@ -429,7 +463,7 @@ class Manager<WXC extends AnyWXContext = AnyWXContext> {
|
|||||||
if(data.length > 0) log += `Using Props: ${ data.join(", ") }\n`;
|
if(data.length > 0) log += `Using Props: ${ data.join(", ") }\n`;
|
||||||
if(func.length > 0) log += `Using Function: ${ func.join(", ") }\n`;
|
if(func.length > 0) log += `Using Function: ${ func.join(", ") }\n`;
|
||||||
|
|
||||||
Logger.log(log, LevelLogLabel.TraceLabel, Manager.AddModuleLabel);
|
Logger.log(log, LevelLogLabel.InfoLabel, Manager.AddModuleLabel);
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
@@ -438,9 +472,9 @@ class Manager<WXC extends AnyWXContext = AnyWXContext> {
|
|||||||
/**
|
/**
|
||||||
* 模块被添加时的标签
|
* 模块被添加时的标签
|
||||||
*/
|
*/
|
||||||
public static readonly AddModuleLabel = new LogLabel("addModule",
|
public static readonly AddModuleLabel = new LogLabel(
|
||||||
new LogStyle().setBorder("4px", `1px solid #8600FF`)
|
"addModule",
|
||||||
.setColor("#FF00FF", "rgba(54, 0, 255, .2)").setBlank("0 5px")
|
colorRadio(54, 0, 255)
|
||||||
)
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,322 +0,0 @@
|
|||||||
import { LogLabel, LogStyle } from "./LogLabel";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 栈信息
|
|
||||||
*/
|
|
||||||
class StackInfo {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 函数名
|
|
||||||
*/
|
|
||||||
public functionName:string | undefined;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 文件名
|
|
||||||
*/
|
|
||||||
public fileName:string | undefined;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 文件路径
|
|
||||||
*/
|
|
||||||
public url:string | undefined;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 文件名和行号
|
|
||||||
*/
|
|
||||||
public fileNameLine: string | undefined;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置信息
|
|
||||||
* @param functionName 函数名
|
|
||||||
* @param fileName 文件名
|
|
||||||
* @param url 文件路径
|
|
||||||
*/
|
|
||||||
public setInfo(functionName:string, fileNameLine:string, url:string):StackInfo {
|
|
||||||
this.functionName = functionName;
|
|
||||||
this.fileNameLine = fileNameLine;
|
|
||||||
this.url = url;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 计算文件名
|
|
||||||
*/
|
|
||||||
public calcFileName():string | undefined {
|
|
||||||
|
|
||||||
let replaceToTs = this.fileNameLine?.replace(".js", ".ts");
|
|
||||||
let matched = replaceToTs?.match(/^(.+\.(js|ts)):\d+:\d+$/);
|
|
||||||
|
|
||||||
return matched ? matched[1] : undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 计算路径名
|
|
||||||
*/
|
|
||||||
public calcPathName():string | undefined {
|
|
||||||
|
|
||||||
let replaceToTs = this.url?.replace(".js", ".ts");
|
|
||||||
let matched = replaceToTs?.match(/^https?:\/\/(\d+\.){3}\d+:\d+\/(.+):\d+:\d+$/);
|
|
||||||
|
|
||||||
return matched ? matched[2] : undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取函数调用栈列表
|
|
||||||
*/
|
|
||||||
public static getCallStack():StackInfo[] {
|
|
||||||
|
|
||||||
// 获取堆栈信息
|
|
||||||
let stack:string | undefined = new Error().stack;
|
|
||||||
|
|
||||||
if (stack === void 0) return [];
|
|
||||||
|
|
||||||
// 去除 Error
|
|
||||||
stack = stack.replace(/^(Error)\s/, "");
|
|
||||||
|
|
||||||
// 获取堆栈信息
|
|
||||||
let stackList:string[] = stack.split(/\n/);
|
|
||||||
|
|
||||||
let callStack:StackInfo[] = [];
|
|
||||||
|
|
||||||
for(let i = 0; i < stackList.length; i++) {
|
|
||||||
|
|
||||||
let matcher = stackList[i].match(/^\s+at\s+(.+)\s(\(.+\))/);
|
|
||||||
if (matcher === null || matcher.length < 3) continue;
|
|
||||||
|
|
||||||
let fileName = matcher[2].match(/.+\/(.+\..+:\d+:\d+)\)/);
|
|
||||||
if (fileName === null || matcher.length < 2) continue;
|
|
||||||
|
|
||||||
callStack.push(new StackInfo().setInfo(
|
|
||||||
matcher[1], fileName[1], matcher[2]?.replace(/(\(|\))/g, "")
|
|
||||||
))
|
|
||||||
}
|
|
||||||
|
|
||||||
// console.log(callStack);
|
|
||||||
|
|
||||||
return callStack;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 排除的
|
|
||||||
*/
|
|
||||||
public static readonly excludeFile:RegExp = /^.*(\\|\/)core(\\|\/)(.*Log.*).js:\d+:\d+/;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取第一个调用栈
|
|
||||||
*/
|
|
||||||
public static getFirstStack():StackInfo | undefined {
|
|
||||||
|
|
||||||
let callStack = this.getCallStack();
|
|
||||||
|
|
||||||
for(let i = 0; i < callStack.length; i++) {
|
|
||||||
|
|
||||||
if(!callStack[i].url) continue;
|
|
||||||
|
|
||||||
if(!StackInfo.excludeFile.test(callStack[i].url ?? "")) {
|
|
||||||
return callStack[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 内部预定义的 LogLabel
|
|
||||||
*/
|
|
||||||
class StackLogLabel {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 堆栈路径样式
|
|
||||||
*/
|
|
||||||
public static readonly normalStyle:LogStyle = new LogStyle()
|
|
||||||
.setColor("#979797").setBorder("4px", "1px solid #979797").setBlank("0 5px");
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 一个回车
|
|
||||||
*/
|
|
||||||
public static readonly blankLabel = new LogLabel("\n\r",
|
|
||||||
new LogStyle(), false, true, true);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 包含文件名和行号的 label
|
|
||||||
*/
|
|
||||||
public static get fileNameLabel():LogLabel {
|
|
||||||
|
|
||||||
// 获得调用堆栈
|
|
||||||
let stack = StackInfo.getFirstStack();
|
|
||||||
|
|
||||||
return new LogLabel(stack?.calcFileName() ?? "Unknown file name",
|
|
||||||
StackLogLabel.normalStyle, false, true, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 包含 URL 链接的 label
|
|
||||||
*/
|
|
||||||
public static get urlLabel():LogLabel {
|
|
||||||
|
|
||||||
// 获得调用堆栈
|
|
||||||
let stack = StackInfo.getFirstStack();
|
|
||||||
|
|
||||||
return new LogLabel(stack?.calcPathName() ?? "Unknown url",
|
|
||||||
StackLogLabel.normalStyle, false, true, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 仅仅用来 filter 的 URL 链接的 label
|
|
||||||
*/
|
|
||||||
public static get filterUrlLabel():LogLabel {
|
|
||||||
|
|
||||||
// 获得调用堆栈
|
|
||||||
let stack = StackInfo.getFirstStack();
|
|
||||||
|
|
||||||
return new LogLabel(stack?.calcPathName() ?? "Unknown url",
|
|
||||||
StackLogLabel.normalStyle, true, false, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成圆角颜色标签样式
|
|
||||||
*/
|
|
||||||
const normalLevelStyleGen = (color:string):LogStyle => {
|
|
||||||
return new LogStyle().setBorder("4px", `1px solid ${color}`)
|
|
||||||
.setColor(color).setBlank("0 5px");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 调试等级标签
|
|
||||||
*/
|
|
||||||
class LevelLogLabel {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 致命
|
|
||||||
*/
|
|
||||||
static readonly FatalLabel = new LogLabel(
|
|
||||||
"FATAL", normalLevelStyleGen("#FF00CC")
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 错误
|
|
||||||
*/
|
|
||||||
static readonly ErrorLabel = new LogLabel(
|
|
||||||
"ERROR", normalLevelStyleGen("#FF0000")
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 警告
|
|
||||||
*/
|
|
||||||
static readonly WarnLabel = new LogLabel(
|
|
||||||
"WARN", normalLevelStyleGen("#FF9900")
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 消息
|
|
||||||
*/
|
|
||||||
static readonly InfoLabel = new LogLabel(
|
|
||||||
"INFO", normalLevelStyleGen("#99FF00")
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 调试
|
|
||||||
*/
|
|
||||||
static readonly DebugLabel = new LogLabel(
|
|
||||||
"DEBUG", normalLevelStyleGen("#00FF99")
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 追踪
|
|
||||||
*/
|
|
||||||
static readonly TraceLabel = new LogLabel(
|
|
||||||
"TRACE", normalLevelStyleGen("#00CCFF")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成圆角颜色标签样式
|
|
||||||
*/
|
|
||||||
const normalLifeStyleGen = (r:number, g:number, b:number):LogStyle => {
|
|
||||||
return new LogStyle().setBorder("4px", `1px solid rgb(${ r }, ${ g }, ${ b })`)
|
|
||||||
.setColor(`rgb(${ r }, ${ g }, ${ b })`, `rgba(${ r }, ${ g }, ${ b }, .1)`)
|
|
||||||
.setBlank("0 5px");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生命周期标签
|
|
||||||
*/
|
|
||||||
class LifeCycleLogLabel {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 小程序加载时
|
|
||||||
*/
|
|
||||||
static readonly OnLaunchLabel = new LogLabel(
|
|
||||||
"onLaunch", normalLifeStyleGen(160, 32, 240)
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生命周期函数--监听页面加载
|
|
||||||
*/
|
|
||||||
static readonly OnLoadLabel = new LogLabel(
|
|
||||||
"onLoad", normalLifeStyleGen(255, 140, 105)
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生命周期函数--监听页面初次渲染完成
|
|
||||||
*/
|
|
||||||
static readonly OnReadyLabel = new LogLabel(
|
|
||||||
"onReady", normalLifeStyleGen(255, 127, 36)
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生命周期函数--监听页面显示
|
|
||||||
*/
|
|
||||||
static readonly OnShowLabel = new LogLabel(
|
|
||||||
"onShow", normalLifeStyleGen(255, 215, 0)
|
|
||||||
)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生命周期函数--监听页面隐藏
|
|
||||||
*/
|
|
||||||
static readonly OnHideLabel = new LogLabel(
|
|
||||||
"onHide", normalLifeStyleGen(173, 255, 47)
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生命周期函数--监听页面卸载
|
|
||||||
*/
|
|
||||||
static readonly OnUnloadLabel = new LogLabel(
|
|
||||||
"onUnload", normalLifeStyleGen(127, 255, 212)
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 页面相关事件处理函数--监听用户下拉动作
|
|
||||||
*/
|
|
||||||
static readonly OnPullDownRefreshLabel = new LogLabel(
|
|
||||||
"onPullDownRefresh", normalLifeStyleGen(0, 191, 255)
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 页面上拉触底事件的处理函数
|
|
||||||
*/
|
|
||||||
static readonly OnReachBottomLabel = new LogLabel(
|
|
||||||
"onReachBottom", normalLifeStyleGen(84, 255, 159)
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 用户点击右上角分享
|
|
||||||
*/
|
|
||||||
static readonly OnShareAppMessageLabel = new LogLabel(
|
|
||||||
"onShareAppMessage", normalLifeStyleGen(147, 112, 219)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const NormalStyle = StackLogLabel.normalStyle;
|
|
||||||
|
|
||||||
export {
|
|
||||||
NormalStyle,
|
|
||||||
StackInfo,
|
|
||||||
StackLogLabel,
|
|
||||||
LevelLogLabel,
|
|
||||||
LifeCycleLogLabel,
|
|
||||||
normalLifeStyleGen as colorRadio,
|
|
||||||
};
|
|
||||||
@@ -1,6 +1,12 @@
|
|||||||
import { LogLabel } from "./LogLabel";
|
import { Logger, LogLabel, LevelLogLabel, colorRadio } from "./Logger";
|
||||||
import { Logger } from "./Logger";
|
|
||||||
import { LevelLogLabel, colorRadio } from "./PresetLogLabel";
|
interface IAppStorageParam {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* storage 缓存
|
||||||
|
*/
|
||||||
|
storage: Map<string, Storage<IStorageData>>
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 状态
|
* 状态
|
||||||
@@ -83,7 +89,8 @@ type IStorageData = {
|
|||||||
* 1. 该类封装了 wxStorage 操作
|
* 1. 该类封装了 wxStorage 操作
|
||||||
* 2. 全异步获取无阻塞
|
* 2. 全异步获取无阻塞
|
||||||
* 3. 使用数据缓缓冲区,优化高频存取
|
* 3. 使用数据缓缓冲区,优化高频存取
|
||||||
* 4.
|
* 4. 如果全局范围内已存在莫键值 storage 的实例
|
||||||
|
* 则此实例将链接到已存在实例
|
||||||
*/
|
*/
|
||||||
class Storage<T extends IStorageData> {
|
class Storage<T extends IStorageData> {
|
||||||
|
|
||||||
@@ -105,18 +112,41 @@ class Storage<T extends IStorageData> {
|
|||||||
/**
|
/**
|
||||||
* 缓存数据
|
* 缓存数据
|
||||||
*/
|
*/
|
||||||
private cache:T;
|
private _cache: T = {} as T;
|
||||||
|
private set cache(data: T) {
|
||||||
|
if (this.cacheStorage) {
|
||||||
|
for (const key in data) {
|
||||||
|
this.cacheStorage.cache[key] = data[key];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (const key in data) {
|
||||||
|
this._cache[key] = data[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private get cache():T {
|
||||||
|
if (this.cacheStorage) return this.cacheStorage.cache;
|
||||||
|
else return this._cache;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* cache 到 storage 等待列表
|
* cache 到 storage 等待列表
|
||||||
*/
|
*/
|
||||||
private saveWaiter:Waiter = new Waiter();
|
private saveWaiter:Waiter = new Waiter();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 缓存对象
|
||||||
|
*/
|
||||||
|
private cacheStorage:Storage<T> | undefined;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将数据从 cache 同步到 storage
|
* 将数据从 cache 同步到 storage
|
||||||
*/
|
*/
|
||||||
public async save():Promise<void> {
|
public async save():Promise<void> {
|
||||||
|
|
||||||
|
// 如果存在链接的实例
|
||||||
|
if (this.cacheStorage) return this.cacheStorage.save();
|
||||||
|
|
||||||
// 如果没有开始存储
|
// 如果没有开始存储
|
||||||
// 发起一次异步读取
|
// 发起一次异步读取
|
||||||
if(this.saveWaiter.state === StorageState.DONE)
|
if(this.saveWaiter.state === StorageState.DONE)
|
||||||
@@ -131,7 +161,7 @@ class Storage<T extends IStorageData> {
|
|||||||
|
|
||||||
success: (data) => {
|
success: (data) => {
|
||||||
Logger.log(`数据保存成功! errMsg: ${ data.errMsg }`,
|
Logger.log(`数据保存成功! errMsg: ${ data.errMsg }`,
|
||||||
LevelLogLabel.TraceLabel, this.StorageLogLabel);
|
LevelLogLabel.InfoLabel, this.StorageLogLabel);
|
||||||
},
|
},
|
||||||
|
|
||||||
fail: (data) => {
|
fail: (data) => {
|
||||||
@@ -152,6 +182,24 @@ class Storage<T extends IStorageData> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在缓存中搜索此键值的实例
|
||||||
|
*/
|
||||||
|
private findStorageCache(): boolean {
|
||||||
|
let { storage: storageMap } = getApp<IAppStorageParam>();
|
||||||
|
|
||||||
|
// 查找缓存
|
||||||
|
let storage = storageMap.get(this.key);
|
||||||
|
if (storage) {
|
||||||
|
this.cacheStorage = storage as Storage<T>;
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 缓存此实例
|
||||||
|
storageMap.set(this.key, this);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 重置全部数据
|
* 重置全部数据
|
||||||
*/
|
*/
|
||||||
@@ -166,13 +214,25 @@ class Storage<T extends IStorageData> {
|
|||||||
/**
|
/**
|
||||||
* @param defaultData 键值默认数据
|
* @param defaultData 键值默认数据
|
||||||
*/
|
*/
|
||||||
public constructor(key:string, defaultData:T) {
|
public constructor(key:string, defaultData?:T) {
|
||||||
this.key = key;
|
this.key = key;
|
||||||
this.defaultData = defaultData;
|
this.defaultData = defaultData ?? {} as T;
|
||||||
this.StorageLogLabel = new LogLabel(
|
this.StorageLogLabel = new LogLabel(
|
||||||
`Storage:${ this.key }`, colorRadio(34, 230, 258)
|
`Storage:${ this.key }`, colorRadio(34, 230, 258)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// 如果已找到其他实力,将此实例链接到目标实例
|
||||||
|
if (this.findStorageCache()) {
|
||||||
|
|
||||||
|
// 设置默认值
|
||||||
|
for (const key in this.defaultData) {
|
||||||
|
if (this.cache[key] === void 0) {
|
||||||
|
this.set(key, this.defaultData[key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
// 读取数据到缓存
|
// 读取数据到缓存
|
||||||
this.cache = wx.getStorageSync<T>(this.key);
|
this.cache = wx.getStorageSync<T>(this.key);
|
||||||
|
|
||||||
@@ -207,4 +267,4 @@ class Storage<T extends IStorageData> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default Storage;
|
export default Storage;
|
||||||
export { Storage, StorageState, Waiter };
|
export { Storage, StorageState, Waiter, IAppStorageParam, IStorageData };
|
||||||
@@ -1,205 +0,0 @@
|
|||||||
// import { Logger } from "../logger/Logger";
|
|
||||||
import { LogStyle, LogLabel } from "./LogLabel";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 测试结果
|
|
||||||
*/
|
|
||||||
class TestResult {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 用例名称
|
|
||||||
*/
|
|
||||||
public caseName:string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 测试结果
|
|
||||||
*/
|
|
||||||
public result:boolean;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 消息
|
|
||||||
*/
|
|
||||||
public message:string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 附加消息
|
|
||||||
*/
|
|
||||||
public attach:string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 初始化
|
|
||||||
* @param caseName 用例名称
|
|
||||||
*/
|
|
||||||
constructor(caseName:string) {
|
|
||||||
this.caseName = caseName;
|
|
||||||
this.result = false;
|
|
||||||
this.message = "";
|
|
||||||
this.attach = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置结果
|
|
||||||
*/
|
|
||||||
public setResult(result:boolean, message?:string, attach?:string) {
|
|
||||||
this.result = result;
|
|
||||||
this.message = message ?? (result ? "success!" : "failed!");
|
|
||||||
this.attach = attach ?? this.attach;
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 测试函数结构
|
|
||||||
*/
|
|
||||||
type TestFunction = () => TestResult | Promise<TestResult>;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 收集测试函数结构
|
|
||||||
*/
|
|
||||||
class CaseCollect {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 用例键名
|
|
||||||
*/
|
|
||||||
public key:string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 用例测试函数
|
|
||||||
*/
|
|
||||||
public caseFunction:TestFunction;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 测试结果
|
|
||||||
*/
|
|
||||||
result: Promise<TestResult> | undefined;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param key 测试用例键名
|
|
||||||
* @param caseFunction 测试函数
|
|
||||||
*/
|
|
||||||
public constructor(key:string, caseFunction:TestFunction) {
|
|
||||||
this.key = key;
|
|
||||||
this.caseFunction = caseFunction;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 运行测试用例
|
|
||||||
*/
|
|
||||||
public async runTestCase():Promise<CaseCollect> {
|
|
||||||
|
|
||||||
let result = this.caseFunction();
|
|
||||||
|
|
||||||
if(result instanceof Promise) {
|
|
||||||
this.result = result;
|
|
||||||
} else {
|
|
||||||
this.result = Promise.resolve(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static readonly baseStyle = new LogStyle().setBlank();
|
|
||||||
|
|
||||||
public static readonly successLabel:LogLabel = new LogLabel("√",
|
|
||||||
new LogStyle().setBlank("0 4px").setBorder("1000px", "1px solid green")
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 打印结果
|
|
||||||
* @param current 当前进度
|
|
||||||
* @param total 总进度
|
|
||||||
*/
|
|
||||||
public printResult(current?:number, total?:number) {
|
|
||||||
|
|
||||||
// 如果测试没有运行,先运行它
|
|
||||||
if(this.result === void 0) this.runTestCase();
|
|
||||||
|
|
||||||
this.result?.then((res) => {
|
|
||||||
|
|
||||||
if(res.result) {
|
|
||||||
console.log(
|
|
||||||
`%c√%c %c1/1%c %c${ this.key }%c ` + res.message,
|
|
||||||
"padding:0 4px; border-radius:1000px; border:1px solid green; color:green",
|
|
||||||
"", "padding:0 4px; border-radius:4px; border:1px solid green; color:green",
|
|
||||||
"", "padding:0 4px; border-radius:4px; border:1px solid #979797; color:#979797",
|
|
||||||
""
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
console.log(
|
|
||||||
`%c√%c %c1/1%c %c${ this.key }%c ` + res.message,
|
|
||||||
"padding:0 4px; border-radius:1000px; border:1px solid red; color:red",
|
|
||||||
"", "padding:0 4px; border-radius:4px; border:1px solid red; color:red",
|
|
||||||
"", "padding:0 4px; border-radius:4px; border:1px solid #979797; color:#979797",
|
|
||||||
""
|
|
||||||
)
|
|
||||||
}
|
|
||||||
console.log(res)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 收集测试用例
|
|
||||||
* @param testCaseClass 测试用例表
|
|
||||||
*/
|
|
||||||
public static collectCase(testCaseClass:ITestCase):CaseCollect[] {
|
|
||||||
|
|
||||||
// 获取静态方法 key
|
|
||||||
let key = Object.getOwnPropertyNames(testCaseClass);
|
|
||||||
|
|
||||||
// 过滤掉通用的方法和属性
|
|
||||||
key = key.filter((key) => !/(length|name|prototype)/.test(key) );
|
|
||||||
|
|
||||||
// 生成 CaseCollect
|
|
||||||
let caseCollect = [];
|
|
||||||
|
|
||||||
for (let i = 0; i < key.length; i++) {
|
|
||||||
caseCollect.push(new CaseCollect(key[i], testCaseClass[key[i]]))
|
|
||||||
}
|
|
||||||
|
|
||||||
return caseCollect;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 运行测试样例
|
|
||||||
*/
|
|
||||||
public static async runCollectCase(cases:CaseCollect[]):Promise<CaseCollect[]> {
|
|
||||||
|
|
||||||
let running:Promise<CaseCollect>[] = [];
|
|
||||||
|
|
||||||
for(let i = 0; i < cases.length; i++) {
|
|
||||||
running.push(cases[i].runTestCase());
|
|
||||||
}
|
|
||||||
|
|
||||||
return Promise.all(running);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 启动单元测试
|
|
||||||
*/
|
|
||||||
public static runUnitTest(testCaseClass:ITestCase) {
|
|
||||||
|
|
||||||
let caseCollect = this.collectCase(testCaseClass);
|
|
||||||
|
|
||||||
CaseCollect.runCollectCase(caseCollect).then((caseCollect:CaseCollect[]) => {
|
|
||||||
|
|
||||||
for(let i = 0; i < caseCollect.length; i++) {
|
|
||||||
caseCollect[i].printResult()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 测试用例接口
|
|
||||||
*/
|
|
||||||
interface ITestCase {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 测试用例函数
|
|
||||||
*/
|
|
||||||
[key:string]:TestFunction;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default ITestCase;
|
|
||||||
export { ITestCase, TestResult, TestFunction, CaseCollect };
|
|
||||||
@@ -1,12 +1,10 @@
|
|||||||
import { Modular, Manager, ILifetime } from "../../core/Module";
|
import { Modular, Manager, ILifetime } from "../../core/Module";
|
||||||
import { LevelLogLabel, LifeCycleLogLabel, NormalStyle } from "../../core/PresetLogLabel";
|
import { Logger, LogLabel, LevelLogLabel, LifeCycleLogLabel, NormalStyle } from "../../core/Logger";
|
||||||
import { LogLabel } from "../../core/LogLabel";
|
|
||||||
import { Logger } from "../../core/Logger";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 在 UI 中显示的数据
|
* 在 UI 中显示的数据
|
||||||
*/
|
*/
|
||||||
type DisplayData = {
|
interface IDisplayData {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 显示内容
|
* 显示内容
|
||||||
@@ -22,8 +20,8 @@ type DisplayData = {
|
|||||||
/**
|
/**
|
||||||
* 模组事件
|
* 模组事件
|
||||||
*/
|
*/
|
||||||
type StatusBarEvent = {
|
interface StatusBarEvent {
|
||||||
m: DisplayData
|
m: IDisplayData
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -101,14 +99,14 @@ implements Partial<ILifetime> {
|
|||||||
|
|
||||||
Logger.log(`计算并设置 StatusBar 的高度为: ${ barHeight + 4 }px, ` +
|
Logger.log(`计算并设置 StatusBar 的高度为: ${ barHeight + 4 }px, ` +
|
||||||
`状态栏高度: ${ statusBarHeight }px, 胶囊占位: ${ capsule }px`,
|
`状态栏高度: ${ statusBarHeight }px, 胶囊占位: ${ capsule }px`,
|
||||||
LevelLogLabel.DebugLabel, StatusBar.StatusBarLabel);
|
LevelLogLabel.InfoLabel, StatusBar.StatusBarLabel);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public onLoad() {
|
public onLoad() {
|
||||||
|
|
||||||
Logger.log("StatusBar 模块加载...",
|
Logger.log("StatusBar 模块加载...",
|
||||||
LevelLogLabel.TraceLabel, LifeCycleLogLabel.OnLoadLabel, StatusBar.StatusBarLabel);
|
LevelLogLabel.InfoLabel, LifeCycleLogLabel.OnLoadLabel, StatusBar.StatusBarLabel);
|
||||||
|
|
||||||
this.setHeight();
|
this.setHeight();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,72 @@
|
|||||||
|
import { Modular, Manager, ILifetime } from "../../core/Module";
|
||||||
|
import { API, IParamSetting } from "../../core/Api";
|
||||||
|
import { Storage } from "../../core/Storage";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 顶部状态栏
|
||||||
|
*/
|
||||||
|
class TestCore<M extends Manager> extends Modular<M>
|
||||||
|
implements Partial<ILifetime> {
|
||||||
|
|
||||||
|
public override onLoad() {
|
||||||
|
|
||||||
|
let s = new Storage("test", {
|
||||||
|
a: new Date(),
|
||||||
|
be: 2
|
||||||
|
});
|
||||||
|
|
||||||
|
let s2 = new Storage("test", {
|
||||||
|
be: 1,
|
||||||
|
aa: "abc"
|
||||||
|
});
|
||||||
|
|
||||||
|
s2.set("be", 4);
|
||||||
|
|
||||||
|
console.log(s, s2);
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
s.set("be", 12);
|
||||||
|
}, 1000)
|
||||||
|
|
||||||
|
interface ITestApiInput {
|
||||||
|
name: string,
|
||||||
|
id: number,
|
||||||
|
info: {
|
||||||
|
data: string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class TestApi extends API<ITestApiInput, {}> {
|
||||||
|
|
||||||
|
public override params: IParamSetting<ITestApiInput> = {
|
||||||
|
name: {
|
||||||
|
tester: "123",
|
||||||
|
isHeader: true
|
||||||
|
},
|
||||||
|
id: {
|
||||||
|
parse: (i) => ++i,
|
||||||
|
},
|
||||||
|
info: {}
|
||||||
|
}
|
||||||
|
|
||||||
|
public constructor() {
|
||||||
|
super();
|
||||||
|
this.initDebugLabel("TestApi");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let api = new TestApi();
|
||||||
|
api.param({
|
||||||
|
name: "123",
|
||||||
|
id: 456,
|
||||||
|
info: {
|
||||||
|
data: "abc"
|
||||||
|
}
|
||||||
|
}).request().wait({
|
||||||
|
success: (d) => console.log(d)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default TestCore;
|
||||||
|
export { TestCore };
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import { Manager} from "../../core/Module";
|
import { Manager} from "../../core/Module";
|
||||||
import { StatusBar } from "./StatusBar";
|
import { StatusBar } from "./StatusBar";
|
||||||
|
import { TestCore } from "./TestCore";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 此页面使用 Manager 进行模块化管理
|
* 此页面使用 Manager 进行模块化管理
|
||||||
@@ -7,4 +8,5 @@ import { StatusBar } from "./StatusBar";
|
|||||||
*/
|
*/
|
||||||
Manager.Page((manager)=>{
|
Manager.Page((manager)=>{
|
||||||
manager.addModule(StatusBar, "statusBar");
|
manager.addModule(StatusBar, "statusBar");
|
||||||
|
manager.addModule(TestCore, "testCore");
|
||||||
})
|
})
|
||||||
+1
-1
@@ -35,7 +35,7 @@
|
|||||||
"outputPath": ""
|
"outputPath": ""
|
||||||
},
|
},
|
||||||
"enableEngineNative": false,
|
"enableEngineNative": false,
|
||||||
"useIsolateContext": true,
|
"useIsolateContext": false,
|
||||||
"userConfirmedBundleSwitch": false,
|
"userConfirmedBundleSwitch": false,
|
||||||
"packNpmManually": false,
|
"packNpmManually": false,
|
||||||
"packNpmRelationList": [],
|
"packNpmRelationList": [],
|
||||||
|
|||||||
Reference in New Issue
Block a user