-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): request animation depending on zonejs; reduce global events (
#855)
- Loading branch information
1 parent
a9d919b
commit 98a615c
Showing
7 changed files
with
151 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2023 Siemens AG | ||
* | ||
* SPDX-License-Identifier: MIT | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import { getElement } from '@stencil/core'; | ||
|
||
export type ListenerOptions = { | ||
target?: 'window'; | ||
defaultEnabled?: boolean; | ||
}; | ||
|
||
const defaultOptions: ListenerOptions = { | ||
target: 'window', | ||
defaultEnabled: true, | ||
}; | ||
|
||
export function createListener(event: string, options: ListenerOptions = {}) { | ||
const opts = { | ||
...defaultOptions, | ||
...options, | ||
}; | ||
|
||
let callback: any; | ||
const onEvent = (event: Event) => { | ||
callback(event); | ||
}; | ||
|
||
const resultObject = { | ||
on: (onEventCallback: any) => { | ||
callback = onEventCallback; | ||
}, | ||
|
||
isEnabled: opts.defaultEnabled, | ||
|
||
enable: (state: boolean) => { | ||
resultObject.isEnabled = state; | ||
|
||
if (state) { | ||
addEventListener(event, onEvent); | ||
} else { | ||
removeEventListener(event, onEvent); | ||
} | ||
}, | ||
|
||
destroy: () => { | ||
resultObject.enable(false); | ||
}, | ||
}; | ||
|
||
resultObject.enable(opts.defaultEnabled); | ||
return resultObject; | ||
} | ||
|
||
export function OnListener<T>(event: string, fnExp?: (self: T) => boolean) { | ||
return (proto: any, methodName: string) => { | ||
const { componentWillLoad, componentWillRender, disconnectedCallback } = | ||
proto; | ||
|
||
proto.componentWillRender = function () { | ||
const host = getElement(this); | ||
host[`__ix__${methodName}`].enable(fnExp(this)); | ||
return componentWillRender && componentWillRender.call(this); | ||
}; | ||
|
||
proto.componentWillLoad = function () { | ||
const listener = createListener(event); | ||
const host = getElement(this); | ||
const method = this[methodName]; | ||
|
||
host[`__ix__${methodName}`] = listener; | ||
|
||
listener.on(method.bind(this)); | ||
return componentWillLoad && componentWillLoad.call(this); | ||
}; | ||
|
||
proto.disconnectedCallback = function () { | ||
const host = getElement(this); | ||
|
||
host[`__ix__${methodName}`].destroy(); | ||
host[`__ix__${methodName}`] = null; | ||
return disconnectedCallback && disconnectedCallback.call(this); | ||
}; | ||
}; | ||
} |
28 changes: 28 additions & 0 deletions
28
packages/core/src/components/utils/requestAnimationFrame.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2023 Siemens AG | ||
* | ||
* SPDX-License-Identifier: MIT | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
declare const __zone_symbol__requestAnimationFrame: any; | ||
|
||
/** | ||
* Prevents angular from change detection when requesting an animation frame | ||
* | ||
* Credits goes to: | ||
* https://github.com/ionic-team/ionic-framework/blob/main/core/src/utils/helpers.ts | ||
*/ | ||
export const requestAnimationFrameNoNgZone = ( | ||
callback: (...args: any[]) => void | ||
) => { | ||
if (typeof __zone_symbol__requestAnimationFrame === 'function') { | ||
return __zone_symbol__requestAnimationFrame(callback); | ||
} | ||
if (typeof requestAnimationFrame === 'function') { | ||
return requestAnimationFrame(callback); | ||
} | ||
return setTimeout(callback); | ||
}; |