Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mall-webview
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
frontend
mall-webview
Commits
c3eac09c
Commit
c3eac09c
authored
Mar 18, 2021
by
hewenhao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: mitt、type define
parent
3c4285a2
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
104 additions
and
9 deletions
+104
-9
shims-vue.d.ts
src/shims-vue.d.ts
+6
-0
mitt.ts
src/util/mitt.ts
+86
-0
nativeBridge.ts
src/util/nativeBridge.ts
+12
-9
No files found.
src/shims-vue.d.ts
View file @
c3eac09c
...
...
@@ -4,3 +4,8 @@ declare module '*.vue' {
const
component
:
DefineComponent
<
{},
{},
any
>
export
default
component
}
declare
interface
Window
{
ReactNativeWebView
:
any
;
$geek_excuteJS
:
any
;
}
\ No newline at end of file
src/util/mitt.ts
0 → 100644
View file @
c3eac09c
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
;
export
type
WildcardHandler
=
(
type
:
EventType
,
event
?:
any
)
=>
void
;
// An array of all currently registered event handlers for a type
export
type
EventHandlerList
=
Array
<
Handler
>
;
export
type
WildCardEventHandlerList
=
Array
<
WildcardHandler
>
;
// A map of event types and their corresponding event handlers.
export
type
EventHandlerMap
=
Map
<
EventType
,
EventHandlerList
|
WildCardEventHandlerList
>
;
export
interface
Emitter
{
all
:
EventHandlerMap
;
on
<
T
=
any
>
(
type
:
EventType
,
handler
:
Handler
<
T
>
):
void
;
on
(
type
:
'*'
,
handler
:
WildcardHandler
):
void
;
off
<
T
=
any
>
(
type
:
EventType
,
handler
:
Handler
<
T
>
):
void
;
off
(
type
:
'*'
,
handler
:
WildcardHandler
):
void
;
emit
<
T
=
any
>
(
type
:
EventType
,
event
?:
T
):
void
;
emit
(
type
:
'*'
,
event
?:
any
):
void
;
}
/**
* Mitt: Tiny (~200b) functional event emitter / pubsub.
* @name mitt
* @returns {Mitt}
*/
export
default
function
mitt
(
_all
?:
EventHandlerMap
):
Emitter
{
const
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
<
T
=
any
>
(
type
:
EventType
,
handler
:
Handler
<
T
>
)
{
const
handlers
=
all
.
get
(
type
);
const
added
=
handlers
&&
handlers
.
push
(
handler
);
if
(
!
added
)
{
all
.
set
(
type
,
[
handler
]);
}
},
/**
* Remove an event handler for the given type.
* @param {string|symbol} type Type of event to unregister `handler` from, or `"*"`
* @param {Function} handler Handler function to remove
* @memberOf mitt
*/
off
<
T
=
any
>
(
type
:
EventType
,
handler
:
Handler
<
T
>
)
{
const
handlers
=
all
.
get
(
type
);
if
(
handlers
)
{
handlers
.
splice
(
handlers
.
indexOf
(
handler
)
>>>
0
,
1
);
}
},
/**
* 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
<
T
=
any
>
(
type
:
EventType
,
evt
:
T
)
{
((
all
.
get
(
type
)
||
[])
as
EventHandlerList
).
slice
().
map
((
handler
)
=>
{
handler
(
evt
);
});
((
all
.
get
(
'*'
)
||
[])
as
WildCardEventHandlerList
).
slice
().
map
((
handler
)
=>
{
handler
(
type
,
evt
);
});
}
};
}
\ No newline at end of file
src/util/nativeBridge.ts
View file @
c3eac09c
import
{
App
}
from
'vue'
import
mitt
from
'./mitt'
const
isiOS
=
()
=>
{
const
u
=
navigator
.
userAgent
;
const
iOs
=
!!
u
.
match
(
/
\(
i
[^
;
]
+;
(
U;
)?
CPU.+Mac OS X/
);
...
...
@@ -33,20 +36,21 @@ if (isiOS() || isAndroid()) {
// }
}
function
install
(
Vue
)
{
function
install
(
Vue
:
App
)
{
const
GEEK_NATVIE_NAME
=
'GEEK_NATVIE_NAME'
;
// eslint-disable-next-line camelcase
const
event_bus
=
new
Vue
();
const
event_bus
=
mitt
();
// eslint-disable-next-line func-names
window
.
$geek_excuteJS
=
function
(
args
)
{
try
{
const
params
=
JSON
.
parse
(
args
);
event_bus
.
$
emit
(
GEEK_NATVIE_NAME
,
params
);
event_bus
.
emit
(
GEEK_NATVIE_NAME
,
params
);
}
catch
(
error
)
{
event_bus
.
$
emit
(
GEEK_NATVIE_NAME
,
args
);
event_bus
.
emit
(
GEEK_NATVIE_NAME
,
args
);
}
};
const
nativeMixin
=
{
Vue
.
mixin
(
{
methods
:
{
listenNativeMessage
(
arg
)
{
const
{
method
,
data
}
=
arg
;
...
...
@@ -119,13 +123,12 @@ function install(Vue) {
}
},
created
()
{
event_bus
.
$
on
(
GEEK_NATVIE_NAME
,
this
.
listenNativeMessage
);
event_bus
.
on
(
GEEK_NATVIE_NAME
,
this
.
listenNativeMessage
);
},
beforeDestroy
()
{
event_bus
.
$off
(
'GEEK_NATVIE_NAME'
,
this
.
listenNativeMessage
);
event_bus
.
off
(
GEEK_NATVIE_NAME
,
this
.
listenNativeMessage
);
},
};
Vue
.
mixin
(
nativeMixin
);
});
}
export
default
install
;
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment