From 41dc17fea94caf87cd962fc40b7dc52613c92b64 Mon Sep 17 00:00:00 2001 From: Jordan Eldredge Date: Mon, 12 Aug 2019 16:23:30 -0700 Subject: [PATCH] Move skin fetching to thunk --- modern/src/Actions.ts | 58 ++++++++++++++++++++++++++++++++++++++++ modern/src/App.js | 61 +++---------------------------------------- modern/src/store.ts | 5 ++-- 3 files changed, 64 insertions(+), 60 deletions(-) diff --git a/modern/src/Actions.ts b/modern/src/Actions.ts index 4c7bdd0e..8564fa0e 100644 --- a/modern/src/Actions.ts +++ b/modern/src/Actions.ts @@ -1,5 +1,63 @@ import { MakiTree, ModernAction } from "./types"; +import JSZip from "jszip"; +import * as Utils from "./utils"; +import initialize from "./initialize"; +import { run } from "./maki-interpreter/virtualMachine"; +import System from "./runtime/System"; +import runtime from "./runtime"; + +async function getMakiTreeFromUrl(skinUrl: string): Promise { + const resp = await fetch(skinUrl); + // const resp = await fetch(simpleSkin); + const blob = await resp.blob(); + const zip = await JSZip.loadAsync(blob); + const skinXml = await Utils.inlineIncludes( + await Utils.readXml(zip, "skin.xml"), + zip + ); + + return await initialize(zip, skinXml); +} export function setMakiTree(makiTree: MakiTree): ModernAction { return { type: "SET_MAKI_TREE", makiTree }; } + +export function gotSkinUrl(skinUrl: string) { + return async dispatch => { + const makiTree = await getMakiTreeFromUrl(skinUrl); + // Execute scripts + await Utils.asyncTreeFlatMap(makiTree, node => { + switch (node.name) { + case "groupdef": { + // removes groupdefs from consideration (only run scripts when actually referenced by group) + return {}; + } + case "script": { + // TODO: stop ignoring standardframe + if (node.attributes.file.endsWith("standardframe.maki")) { + break; + } + const scriptGroup = Utils.findParentNodeOfType(node, [ + "group", + "WinampAbstractionLayer", + "WasabiXML", + ]); + const system = new System(scriptGroup); + run({ + runtime, + data: node.js_annotations.script, + system, + log: false, + }); + return node; + } + default: { + return node; + } + } + }); + + dispatch(setMakiTree(makiTree)); + }; +} diff --git a/modern/src/App.js b/modern/src/App.js index c691721c..59334e41 100644 --- a/modern/src/App.js +++ b/modern/src/App.js @@ -1,30 +1,11 @@ import React, { useEffect, useReducer } from "react"; -import JSZip from "jszip"; import "./App.css"; -import * as Utils from "./utils"; -import initialize from "./initialize"; -import System from "./runtime/System"; -import runtime from "./runtime"; -import { run } from "./maki-interpreter/virtualMachine"; import * as Actions from "./Actions"; import * as Selectors from "./Selectors"; // import simpleSkin from "../skins/simple.wal"; import cornerSkin from "../skins/CornerAmp_Redux.wal"; import { useDispatch, useSelector } from "react-redux"; -async function getSkin() { - const resp = await fetch(cornerSkin); - // const resp = await fetch(simpleSkin); - const blob = await resp.blob(); - const zip = await JSZip.loadAsync(blob); - const skinXml = await Utils.inlineIncludes( - await Utils.readXml(zip, "skin.xml"), - zip - ); - - return await initialize(zip, skinXml); -} - function useJsUpdates(node) { const [ignored, forceUpdate] = useReducer(x => x + 1, 0); useEffect(() => node.js_listen("js_update", forceUpdate)); @@ -341,49 +322,13 @@ function XmlNode({ node }) { function App() { const dispatch = useDispatch(); - const data = useSelector(Selectors.getMakiTree); + const root = useSelector(Selectors.getMakiTree); React.useEffect(() => { - getSkin().then(async root => { - // Execute scripts - await Utils.asyncTreeFlatMap(root, node => { - switch (node.name) { - case "groupdef": { - // removes groupdefs from consideration (only run scripts when actually referenced by group) - return {}; - } - case "script": { - // TODO: stop ignoring standardframe - if (node.attributes.file.endsWith("standardframe.maki")) { - break; - } - const scriptGroup = Utils.findParentNodeOfType(node, [ - "group", - "WinampAbstractionLayer", - "WasabiXML", - ]); - const system = new System(scriptGroup); - run({ - runtime, - data: node.js_annotations.script, - system, - log: false, - }); - return node; - } - default: { - return node; - } - } - }); - - dispatch(Actions.setMakiTree(root)); - }); + dispatch(Actions.gotSkinUrl(cornerSkin)); }, []); - if (data == null) { + if (root == null) { return

Loading...

; } - const root = data; - return ; } diff --git a/modern/src/store.ts b/modern/src/store.ts index 8ca72d4d..959d4329 100644 --- a/modern/src/store.ts +++ b/modern/src/store.ts @@ -1,5 +1,6 @@ import { ModernAppState, ModernAction } from "./types"; -import { createStore } from "redux"; +import { createStore, applyMiddleware } from "redux"; +import thunk from "redux-thunk"; const defaultState = { makiTree: null }; @@ -16,5 +17,5 @@ function reducer( } export function create() { - return createStore(reducer); + return createStore(reducer, applyMiddleware(thunk)); }