From 58529853a669379cc0e56887256841e659d27754 Mon Sep 17 00:00:00 2001 From: atanasster Date: Sat, 18 Jul 2020 20:34:27 -0400 Subject: [PATCH] fix: update readdata store for sb --- core/config/package.json | 3 ++ core/loader/src/replaceSource.ts | 63 ++++++++++++++++---------- core/loader/src/runtimeLoader.ts | 5 +- core/store/src/Store/BroadcastStore.ts | 11 +++-- core/store/src/Store/Store.ts | 22 +++++++++ yarn.lock | 2 +- 6 files changed, 77 insertions(+), 29 deletions(-) diff --git a/core/config/package.json b/core/config/package.json index f966db5a1..84c0eb025 100644 --- a/core/config/package.json +++ b/core/config/package.json @@ -41,7 +41,10 @@ "devDependencies": { "@component-controls/ts-markdown-docs": "^1.5.0", "@types/faker": "^4.1.9", + "@types/glob": "^7.1.3", + "@types/glob-base": "^0.3.0", "@types/jest": "^25.1.2", + "@types/micromatch": "^4.0.1", "cross-env": "^5.2.1", "eslint": "^6.5.1", "jest": "^24.9.0" diff --git a/core/loader/src/replaceSource.ts b/core/loader/src/replaceSource.ts index 3c57e68b7..cc9f3511e 100644 --- a/core/loader/src/replaceSource.ts +++ b/core/loader/src/replaceSource.ts @@ -17,13 +17,16 @@ export const replaceSource = ( const configJSON = ${ configFilePath ? `require("${configFilePath}")` : 'undefined' }; - const imports = []; + const contexts = []; ${contexts .map( context => - `imports.push(require.context('${context.directory}', ${ + `contexts.push({ + folder: "${context.directory}", + req: require.context('${context.directory}', ${ context.useSubdirectories ? 'true' : 'false' - }, ${context.regExp}));`, + }, ${context.regExp}) + });`, ) .join('\n')} `; @@ -47,34 +50,46 @@ ${contexts for (let i = 0; i < store.stores.length; i+= 1) { const s = store.stores[i]; const doc = s.doc; - const importedFile = imports.find(i => i.hasOwnProperty(doc.fileName)); - if (doc && importedFile) { - const exports = importedFile[doc.fileName]; - try { - Object.keys(exports).forEach(key => { - const exported = exports[key]; - if (key === 'default') { - const { storySource, ...rest } = exported; - assignProps(doc, rest); - } else { - const story = s.stories[key]; - if (story) { - story.renderFn = exported; - assignProps(story, exported); - if (exported.story) { - assignProps(story, exported.story); + if (doc) { + let exports = null; + for (const context of contexts) { + const key = context.req.keys().find(k => { + const fullPath = path.resolve(context.folder, k); + return doc.fileName === fullPath; + }) + if (key) { + exports = context.req(key); + break; + } + } + if (exports) { + try { + Object.keys(exports).forEach(key => { + const exported = exports[key]; + if (key === 'default') { + const { storySource, ...rest } = exported; + assignProps(doc, rest); + } else { + const story = s.stories[key]; + if (story) { + story.renderFn = exported; + assignProps(story, exported); + if (exported.story) { + assignProps(story, exported.story); + } } } - } - }); - } catch (e) { - console.error(\`unable to load module \${doc.moduleId}\`, e); - } + }); + } catch (e) { + console.error(\`unable to load module \${doc.moduleId}\`, e); + } + } } } `; const exports = `module.exports = store;\n`; const newContent = ` +const path = require('path'); ${imports} ${storeConst} store.config = ${configFilePath ? 'configJSON.default ||' : ''} configJSON; diff --git a/core/loader/src/runtimeLoader.ts b/core/loader/src/runtimeLoader.ts index bd0b6a1cc..79bb26723 100644 --- a/core/loader/src/runtimeLoader.ts +++ b/core/loader/src/runtimeLoader.ts @@ -3,10 +3,11 @@ import { deepMergeArrays, defaultBuildConfig } from '@component-controls/core'; import { loadConfiguration, configRequireContext, + extractDocuments, } from '@component-controls/config'; import { loader } from 'webpack'; import { replaceSource } from './replaceSource'; -import { store } from './store'; +import { store, reserveStories } from './store'; module.exports = function(content: string) { const context = (this as unknown) as loader.LoaderContext; @@ -17,6 +18,8 @@ module.exports = function(content: string) { : defaultBuildConfig; const contexts = config ? configRequireContext(config) || [] : []; + const stories: string[] = config ? extractDocuments(config) || [] : []; + reserveStories(stories); content = replaceSource( contexts, config?.optionsFilePath, diff --git a/core/store/src/Store/BroadcastStore.ts b/core/store/src/Store/BroadcastStore.ts index 9c606bb5f..597f3ec6f 100644 --- a/core/store/src/Store/BroadcastStore.ts +++ b/core/store/src/Store/BroadcastStore.ts @@ -4,7 +4,6 @@ import { LoadingStore } from '@component-controls/loader'; import { MessageType, UPDATE_STORY_MSG } from '../types'; import { HMRStore } from './HMRStore'; import { readStore, updateStory } from '../serialization/StoreStorage'; - export { BroadcastChannel }; export class BroadcastStore extends HMRStore { @@ -21,7 +20,7 @@ export class BroadcastStore extends HMRStore { if (storyId && moduleId) { if (this.moduleId !== moduleId) { this.readData(storyId, propName); - super.notifyObservers(storyId, propName); + this.notifyObservers(storyId, propName); } } }; @@ -33,10 +32,16 @@ export class BroadcastStore extends HMRStore { this.loadedStore = store; this.notifyObservers(); }; + getStore = () => { + if (this.loadedStore) { + return this.loadedStore; + } + this.readData(); + return this.loadedStore; + }; private readData = (storyId?: string, propName?: string) => { this.setStore(readStore(this.loadedStore, storyId, propName)); }; - /** * modify story properties, for example controls values. * will notify all installed store observers of the changed story. diff --git a/core/store/src/Store/Store.ts b/core/store/src/Store/Store.ts index f81790766..5f706f8dc 100644 --- a/core/store/src/Store/Store.ts +++ b/core/store/src/Store/Store.ts @@ -108,6 +108,28 @@ export class Store implements StoryStore { } }; + /** + * modify story properties, for example controls values. + * will notify all installed store observers of the changed story. + */ + updateStoryProp = ( + storyId: string, + propName: string, + newValue: any, + ): void => { + if (this.loadedStore) { + this.loadedStore.stories = { + ...this.loadedStore.stories, + [storyId]: { + ...this.loadedStore.stories[storyId], + [propName]: newValue, + }, + }; + } + if (this.loadedStore) { + this.notifyObservers(storyId, propName); + } + }; /** * returns an instance of the store */ diff --git a/yarn.lock b/yarn.lock index 92ea8cb32..8c69f1221 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4592,7 +4592,7 @@ resolved "https://registry.yarnpkg.com/@types/glob-base/-/glob-base-0.3.0.tgz#a581d688347e10e50dd7c17d6f2880a10354319d" integrity sha1-pYHWiDR+EOUN18F9byiAoQNUMZ0= -"@types/glob@*", "@types/glob@^7.1.1": +"@types/glob@*", "@types/glob@^7.1.1", "@types/glob@^7.1.3": version "7.1.3" resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.1.3.tgz#e6ba80f36b7daad2c685acd9266382e68985c183" integrity sha512-SEYeGAIQIQX8NN6LDKprLjbrd5dARM5EXsd8GI/A5l0apYI1fGMWgPHSe4ZKL4eozlAyI+doUE9XbYS4xCkQ1w==