-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Web: Add support for auto-reloading dev plugins on change (#11545)
- Loading branch information
1 parent
a81af07
commit 98fce34
Showing
20 changed files
with
249 additions
and
64 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
60 changes: 60 additions & 0 deletions
60
packages/app-mobile/components/plugins/utils/useOnDevPluginsUpdated.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,60 @@ | ||
import useAsyncEffect from '@joplin/lib/hooks/useAsyncEffect'; | ||
import shim from '@joplin/lib/shim'; | ||
import time from '@joplin/lib/time'; | ||
import { basename, join } from 'path'; | ||
import { useRef } from 'react'; | ||
|
||
type OnDevPluginChange = (id: string)=> void; | ||
|
||
const useOnDevPluginsUpdated = (onDevPluginChange: OnDevPluginChange, devPluginPath: string, pluginSupportEnabled: boolean) => { | ||
const onDevPluginChangeRef = useRef(onDevPluginChange); | ||
onDevPluginChangeRef.current = onDevPluginChange; | ||
const isFirstUpdateRef = useRef(true); | ||
|
||
useAsyncEffect(async (event) => { | ||
if (!devPluginPath || !pluginSupportEnabled) return; | ||
|
||
const itemToLastModTime = new Map<string, number>(); | ||
|
||
// publishPath should point to the publish/ subfolder of a plugin's development | ||
// directory. | ||
const checkPluginChange = async (pluginPublishPath: string) => { | ||
const dirStats = await shim.fsDriver().readDirStats(pluginPublishPath); | ||
let hasChange = false; | ||
let changedPluginId = ''; | ||
for (const item of dirStats) { | ||
if (item.path.endsWith('.jpl')) { | ||
const lastModTime = itemToLastModTime.get(item.path); | ||
const modTime = item.mtime.getTime(); | ||
if (lastModTime === undefined || lastModTime < modTime) { | ||
itemToLastModTime.set(item.path, modTime); | ||
hasChange = true; | ||
changedPluginId = basename(item.path, '.jpl'); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if (hasChange) { | ||
if (isFirstUpdateRef.current) { | ||
// Avoid sending an event the first time the hook is called. The first iteration | ||
// collects initial timestamp information. In that case, hasChange | ||
// will always be true, even with no plugin reload. | ||
isFirstUpdateRef.current = false; | ||
} else { | ||
onDevPluginChangeRef.current(changedPluginId); | ||
} | ||
} | ||
}; | ||
|
||
while (!event.cancelled) { | ||
const publishFolder = join(devPluginPath, 'publish'); | ||
await checkPluginChange(publishFolder); | ||
|
||
const pollingIntervalSeconds = 5; | ||
await time.sleep(pollingIntervalSeconds); | ||
} | ||
}, [devPluginPath, pluginSupportEnabled]); | ||
}; | ||
|
||
export default useOnDevPluginsUpdated; |
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
Oops, something went wrong.